MPAndroid multi line chart align 0 line of left and right scale

Datetime:2016-08-23 02:56:25          Topic:          Share

I have a multi line chart with 3 datasets. dataset 1 & 2 use the left side scale, the 3rd one uses the right side scale.Values of first 2 datasets are between -300W and +2500W, the values of the 3rd dataset are between 0 and 7000lux.

How can I align the 0 position of the left and right scale?

That is what I get right now:

The 0 line of the green plot is at -500 of the others. The data changes dynamically, so I cannot set a fixed offset for the green plot.

Anybody knows how to do this? I didn’t find anything in the MPAndroid documentation.

Initialization code of the chart:

/**
 * Initialize chart to show solar power, consumption and light values
 *
 * @param isContinuous
 *		  Flag for display mode
 *		  true = continuous display of data received from spMonitor
 *		  false = display content of a log file
 */
private void initChart(boolean isContinuous) {
	// Pointer to the chart in the layout
	lineChart = (LineChart) findViewById(R.id.graph);
	timeSeries.clear();
	solarSeries.clear();
	consSeries.clear();
	lightSeries.clear();
	zeroSeries.clear();
	if (!isContinuous) {
		for (int i=0; i<timeStamps.size(); i++) {
			timeSeries.add(timeStamps.get(i));
		}
		for (int i=0; i<solarPower.size(); i++) {
			solarSeries.add(new Entry(solarPower.get(i), i));
		}
		for (int i=0; i<consumPower.size(); i++) {
			consSeries.add(new Entry(consumPower.get(i), i));
		}
		for (int i= 0; i<lightValue.size(); i++) {
			lightSeries.add(new Entry(lightValue.get(i), i));
		}
		for (int i= 0; i<timeStamps.size(); i++) {
			zeroSeries.add(new Entry(0, i));
		}
	} else {
		if (timeStampsCont.size() != 0) {
			for (int i=0; i<timeStampsCont.size(); i++) {
				timeSeries.add(timeStampsCont.get(i));
			}
			for (int i=0; i<solarPowerCont.size(); i++) {
				solarSeries.add(new Entry(solarPowerCont.get(i), i));
			}
			for (int i=0; i<consumPowerCont.size(); i++) {
				consSeries.add(new Entry(consumPowerCont.get(i), i));
			}
			for (int i= 0; i<lightValueCont.size(); i++) {
				lightSeries.add(new Entry(lightValueCont.get(i), i));
			}
			for (int i= 0; i<timeStampsCont.size(); i++) {
				zeroSeries.add(new Entry(0, i));
			}
		}
	}
	/** Line data set for solar data */
	solar = new LineDataSet(solarSeries, "Solar");
	/** Line data set for consumption data */
	cons = new LineDataSet(consSeries, "Consumption");
	/** Line data set for light data */
	light = new LineDataSet(lightSeries, "Light");
	/** Line data set for zero line */
	zero = new LineDataSet(zeroSeries, "");
	solar.setLineWidth(1.75f);
	solar.setCircleSize(0f);
	if (showSolar) {
		solar.setColor(0xFFFFBB33);
		solar.setCircleColor(0xFFFFBB33);
		solar.setHighLightColor(0xFFFFBB33);
		solar.setFillColor(0xAAFFBB33);
	} else {
		solar.setColor(Color.TRANSPARENT);
		solar.setCircleColor(Color.TRANSPARENT);
		solar.setHighLightColor(Color.TRANSPARENT);
		solar.setFillColor(Color.TRANSPARENT);
	}
	solar.setDrawValues(false);
	solar.setDrawFilled(true);
	cons.setLineWidth(1.75f);
	cons.setCircleSize(0f);
	if (showCons) {
		cons.setColor(0xFF33B5E5);
		cons.setCircleColor(0xFF33B5E5);
		cons.setHighLightColor(0xFF33B5E5);
		cons.setFillColor(0xAA33B5E5);
	} else {
		cons.setColor(Color.TRANSPARENT);
		cons.setCircleColor(Color.TRANSPARENT);
		cons.setHighLightColor(Color.TRANSPARENT);
		cons.setFillColor(Color.TRANSPARENT);
	}
	cons.setDrawValues(false);
	cons.setDrawValues(false);
	cons.setDrawFilled(true);
	cons.setAxisDependency(YAxis.AxisDependency.LEFT);
	light.setLineWidth(1.75f);
	light.setCircleSize(0f);
	if (showLight) {
		light.setColor(Color.GREEN);
		light.setCircleColor(Color.GREEN);
		light.setHighLightColor(Color.GREEN);
	} else {
		light.setColor(Color.TRANSPARENT);
		light.setCircleColor(Color.TRANSPARENT);
		light.setHighLightColor(Color.TRANSPARENT);
	}
	light.setDrawValues(false);
	light.setAxisDependency(YAxis.AxisDependency.RIGHT);
	zero.setLineWidth(1.75f);
	zero.setCircleSize(0f);
	zero.setColor(Color.RED);
	zero.setCircleColor(Color.RED);
	zero.setHighLightColor(Color.RED);
	zero.setDrawValues(false);
	/** Data set with data for the 4 plots */
	dataSets = new ArrayList<>();
	dataSets.add(zero);
	dataSets.add(solar);
	dataSets.add(cons);
	dataSets.add(light);
	/** Data object with the data set and the y values */
	plotData = new LineData(timeSeries, dataSets);
	lineChart.setBackgroundColor(Color.BLACK);
	lineChart.setDrawGridBackground(false);
	lineChart.setTouchEnabled(true);
	lineChart.setDragEnabled(true);
	//lineChart.setScaleEnabled(false);
	//lineChart.setPinchZoom(true);
	lineChart.setAutoScaleMinMaxEnabled(true);
	lineChart.setData(plotData);
	TextView chartTitle = (TextView) findViewById(R.id.tv_plotTitle);
	chartTitle.setText(dayToShow);
	/** Instance of left y axis */
	YAxis lYAx = lineChart.getAxisLeft();
	lYAx.setEnabled(true);
	lYAx.setTextColor(Color.WHITE);
	lYAx.setStartAtZero(false);
	lYAx.setSpaceTop(1);
	lYAx.setSpaceBottom(1);
	/** Instance of right y axis */
	YAxis rYAx = lineChart.getAxisRight();
	rYAx.setEnabled(true);
	rYAx.setTextColor(Color.WHITE);
	rYAx.setStartAtZero(false);
	rYAx.setSpaceTop(1);
	rYAx.setSpaceBottom(1);
	/** Instance of x axis */
	XAxis xAx = lineChart.getXAxis();
	xAx.setEnabled(true);
	xAx.setTextColor(Color.WHITE);
	xAx.setPosition(XAxis.XAxisPosition.BOTTOM);
	lineChart.getLegend().setEnabled(false);
	// create a custom MarkerView (extend MarkerView) and specify the layout
	// to use for it
	CustomMarkerView mv = new CustomMarkerView(appContext);
	lineChart.setMarkerView(mv);
	// set the marker to the chart
	lineChart.setMarkerView(mv);
	// let the chart know it's data has changed
	lineChart.notifyDataSetChanged();
	lineChart.invalidate();
}