PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.67k stars 9.02k forks source link

Left and right Y Axis show different scales - should be the same #4436

Open Kaspatoo opened 5 years ago

Kaspatoo commented 5 years ago

I recognized that my linechart has two y-axis (left + right). Unfortunately they do not have exactly the same scale. It differs slightly and due to that there are additional lines.

1

This is the chart: <com.github.mikephil.charting.charts.LineChart android:id="@+id/device_chart" android:layout_width="match_parent" android:layout_height="300dp" android:layout_marginTop="0dp" android:layout_marginLeft="@dimen/margin_card_text" android:layout_marginRight="@dimen/margin_card_text" android:layout_weight="1" />

This is the configuration:

LineChart chart= findViewById(R.id.device_chart);
        chart.getDescription().setEnabled(false);

           XAxisValueFormatter valueFormatter = new XAxisValueFormatter();
                HashMap<Integer, String> valueLableMap = valueFormatter.getLabels();
                List<Entry> entries = new ArrayList<Entry>();
                int xIndex = 0;
                for(BaseSensorData sensorData : sensorDataList) {
                    float yValue = sensorData.getValue().floatValue();
                    yAxisMinimum = checkAndSetAxisMin(yAxisMinimum, yValue);
                    yAxisMaximum = checkAndSetAxisMax(yAxisMaximum, yValue);
                    Long timestamp = ((RawSensorData) sensorData).getTimestamp();
                    label = DeviceChartActivity.LABEL_RAW;
                    valueLableMap.put(xIndex, timestamp + "");
                    entries.add(new Entry(xIndex, yValue));
                    xIndex++;
                }

                LineData lineData = chart.getLineData();

                if(lineData != null) {
                    ILineDataSet dataSet = lineData.getDataSetByLabel(label, false);
                    lineData.removeDataSet(dataSet);
                }
                int lineColor = colorDataLine;
                int textColor = textColorDataLine;

                Collections.sort(entries, new EntryXComparator());
                LineDataSet dataSet = new LineDataSet(entries, label);
                dataSet.setColor(lineColor);
                dataSet.setValueTextColor(textColor);

                lineData = new LineData(dataSet);
                lineData.setDrawValues(false);
                chart.setData(lineData);

                XAxis xAxis = chart.getXAxis();
                xAxis.setValueFormatter(valueFormatter);
                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                xAxis.setLabelRotationAngle(-45);
setChartYAxisBound(chart, yAxisMinimum, yAxisMaximum);
            chart.notifyDataSetChanged();
            chart.invalidate(); // refresh

Used functions:

    private void setChartYAxisBound(LineChart chart, float minY, float maxY, float overhead){
        float range = maxY - minY;
        float offset = range * overhead;

        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setAxisMinimum(minY - offset);
        leftAxis.setAxisMaximum(maxY + offset);
    }

I also am adding three LimitLines this way:

LimitLine llLower = getLimitLineByLabel(leftAxis.getLimitLines(), labelLower);
                if(llLower == null){
                    llLower = new LimitLine(minNorm, labelLower);
                }
                llLower.setLineColor(colorNormLine);
                llLower.setLineWidth(sizeNormLine);
                llLower.setTextColor(textColorNormLine);
                llLower.setTextSize(textSizeNormLine);
                leftAxis.addLimitLine(llLower);

When working with the y-axis I only am using leftAxis of the chart. Also to comment out all addition of limiLines or ValueFormatters (so only the dataline is included) will not change anything to the right axis. Also when only inserting a single point, the right axis goes wrong.

Edit: When having a look onto the data, I can confirm that they relate to the left axis (e.g. the max value of the dataset is 1062.3 while with the right axis it were below 1060).

JunevaYang commented 5 years ago

I have the same problem, have you solved it?

JunevaYang commented 5 years ago

i found if just set one axis, is solved

Kaspatoo commented 5 years ago

well, i currently disabled the right axis, thats a workaround, not a solution.

Bhvk0331 commented 4 years ago

@Kaspatoo and @JunevaYang For this issue I set axisDependency in my linedatasets like this and it worked, give it a try,

set2.axisDependency = YAxis.AxisDependency.RIGHT

NOTE:- here set2 is my LineDataSet() object

hd-agarwal commented 3 years ago

I was facing the same issue and found out that if we set any scaling and/or value limiting method on one of the right or leftAxes, e.g setAxisMinimum, we have to set the same for the other axis as well. I your setChartYAxisBound function, you are setting the minimum and maximum for leftAxis only. Adding the following lines must fix the problem: /* YAxis rightAxis = chart.getAxisRight(); rightAxis.setAxisMinimum(minY - offset); rightAxis.setAxisMaximum(maxY + offset); */

private void setChartYAxisBound(LineChart chart, float minY, float maxY, float overhead){
    float range = maxY - minY;
    float offset = range * overhead;

    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setAxisMinimum(minY - offset);
    leftAxis.setAxisMaximum(maxY + offset);
}