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

Qt Code:
  1. ChartView {
  2. id: linechart;
  3. anchors.fill: parent;
  4.  
  5. property var updateChart: chartContext.updateChart;
  6.  
  7. title: chartContext.chartTitle;
  8. antialiasing: true;
  9.  
  10. ValueAxis {
  11. id: xAxis;
  12. min: chartContext.minXAxis;
  13. max: chartContext.maxXAxis;
  14. }
  15. ValueAxis {
  16. id: yAxis;
  17. min: chartContext.minYAxis;
  18. max: chartContext.maxYAxis;
  19. }
  20. ValueAxis {
  21. id: yAxis2;
  22. min: chartContext.minYAxis2;
  23. max: chartContext.maxYAxis2;
  24. }
  25.  
  26. onUpdateChartChanged: {
  27. linechart.removeAllSeries();
  28.  
  29. for (var i = 0; i < dataModel.numberOfSeries; ++i) {
  30. var lineSeries;
  31. if (dataModel.axisLocation(i) == "Right") {
  32. lineSeries = linechart.createSeries(ChartView.SeriesTypeLine, dataModel.getYLabel(i), xAxis);
  33. lineSeries.axisYRight = yAxis2;
  34. }
  35. else {
  36. lineSeries = linechart.createSeries(ChartView.SeriesTypeLine, dataModel.getYLabel(i), xAxis, yAxis);
  37. }
  38. var point;
  39. for (var j = 0; j < dataModel.getNumberOfDataPoints(i); ++j) {
  40. point = lineSeries.append(dataModel.getXValue(i,j), dataModel.getYValue(i,j));
  41. }
  42. lineSeries.pointsVisible = true;
  43. }
To copy to clipboard, switch view to plain text mode