Adding right axis in QtQuick ChartView changes also left axis



  • Hi,

    I want dynamically change LineSeries in a QML ChartView. I'm using Qt 5.7
    For this purpose, I have a variable 'updateChart', which triggers the replacement of existing LineSeries, using removeAllSeries() and createSeries()

    Sometimes, a LineSeries needs to be displayed using a secondary axis (right axis).
    The ChartView createSeries() doesn't allow to assign a right axis to a LineSeries, and from the documentation I cannot find an alternative.

    Additionally, you need to have already an axis before calling the createSeries method.
    Therefor I have declared 3 axes (X, Y left, Y right) in the ChartView for using them in the createSeries call. The min/max values are modified in another view and kept in the 'chartContext'.
    After creation, I change the axisY to axisYRight.

    The problem is that my ChartView displays the same axis (min/max values) on both sides. When I change one of the minima/maxima, it updates to that last value, even mixing the minimum from the left axis with the maximum from the right axis.

    How can I avoid this ?
    Below is my (shortened) code :
    chartContext and dataModel are C++ contexts

    ChartView {
    	id: linechart;
    	anchors.fill: parent;
     
    	property var updateChart: chartContext.updateChart;
     
    	title: chartContext.chartTitle;
    	antialiasing: true;
     
    	ValueAxis {
    		id: xAxis;
    		min: chartContext.minXAxis;
    		max: chartContext.maxXAxis;			
    	}
    	ValueAxis {
    		id: yAxis;
    		min: chartContext.minYAxis;
    		max: chartContext.maxYAxis;
    	}
    	ValueAxis {
    		id: yAxis2;
    		min: chartContext.minYAxis2;
    		max: chartContext.maxYAxis2;
    	}
     
    	onUpdateChartChanged: {
    		linechart.removeAllSeries();
     
    		for (var i = 0; i < dataModel.numberOfSeries; ++i) {			
    			var lineSeries;
    			if (dataModel.axisLocation(i) == "Right") {
    				lineSeries = linechart.createSeries(ChartView.SeriesTypeLine, dataModel.getYLabel(i), xAxis);
    				lineSeries.axisYRight = yAxis2;
    			}
    			else {
    				lineSeries = linechart.createSeries(ChartView.SeriesTypeLine, dataModel.getYLabel(i), xAxis, yAxis);
    			}
    			var point;
    			for (var j = 0; j < dataModel.getNumberOfDataPoints(i); ++j) { 
    				point = lineSeries.append(dataModel.getXValue(i,j), dataModel.getYValue(i,j));
    			}
    			lineSeries.pointsVisible = true;			
    		}
            }
    }
    


  • I'm working with qt5.9 and I have the same problem. Do you have find any solution?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.