Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Adding series to qml Chartview

Adding series to qml Chartview

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    milan
    wrote on last edited by
    #1

    Hello, I came across a problem. How can I add new series and remove series from existing qml Chartview from C++?
    I have data for series in C++, also I need qml Chartview as I am developing qml application. Please help. How could this be achieved

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mammamia
      wrote on last edited by
      #2

      This is an abstract version of what you should do to achieve what you need.

      void PlotData::updateData(QAbstractSeries *p_series)
      {
              QVector<QPointF> pointList;
              // fill data into pointlist
              QXYSeries *xySeries = static_cast<QXYSeries *>(p_series);
              xySeries->replace(pointList);
      }
          
      void PlotData::clearSeries(QAbstractSeries *p_series)
      {
              if (p_series) {
                  QXYSeries *xySeries = static_cast<QXYSeries *>(p_series);
                  xySeries->clear();
              }
      }
      

      and inside QML

      //QML
      ChartView {
      	id: chartView
      	width: 400
      	height: 300
      	antialiasing: true
      	// Plot line
      	LineSeries {
      		id: lineSeries
                      axisX: axisX
                      axisY: axisY
                      useOpenGL: true
          }
      }
      
      Button {
      	id: reload
      	onClicked: {
      		PlotData.clearSeries(chartView.series(0))  
                      PlotData.updateData(chartView.series(0))       
      	}
      }
      

      Hope it helps.

      M 1 Reply Last reply
      0
      • M Mammamia

        This is an abstract version of what you should do to achieve what you need.

        void PlotData::updateData(QAbstractSeries *p_series)
        {
                QVector<QPointF> pointList;
                // fill data into pointlist
                QXYSeries *xySeries = static_cast<QXYSeries *>(p_series);
                xySeries->replace(pointList);
        }
            
        void PlotData::clearSeries(QAbstractSeries *p_series)
        {
                if (p_series) {
                    QXYSeries *xySeries = static_cast<QXYSeries *>(p_series);
                    xySeries->clear();
                }
        }
        

        and inside QML

        //QML
        ChartView {
        	id: chartView
        	width: 400
        	height: 300
        	antialiasing: true
        	// Plot line
        	LineSeries {
        		id: lineSeries
                        axisX: axisX
                        axisY: axisY
                        useOpenGL: true
            }
        }
        
        Button {
        	id: reload
        	onClicked: {
        		PlotData.clearSeries(chartView.series(0))  
                        PlotData.updateData(chartView.series(0))       
        	}
        }
        

        Hope it helps.

        M Offline
        M Offline
        milan
        wrote on last edited by milan
        #3

        @Mammamia . Thanks for your answer. I do know how to update the existing series. What I do not know is to add "new series", and to remove "existing series" from the chartview from "C++".

        DiracsbracketD 1 Reply Last reply
        0
        • M milan

          @Mammamia . Thanks for your answer. I do know how to update the existing series. What I do not know is to add "new series", and to remove "existing series" from the chartview from "C++".

          DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by
          #4

          @milan
          What about the createSeries method?
          https://doc.qt.io/qt-5.11/qml-qtcharts-chartview.html#createSeries-method

          M 1 Reply Last reply
          0
          • DiracsbracketD Diracsbracket

            @milan
            What about the createSeries method?
            https://doc.qt.io/qt-5.11/qml-qtcharts-chartview.html#createSeries-method

            M Offline
            M Offline
            milan
            wrote on last edited by
            #5

            @Diracsbracket. I know how to add series from QML, I dont know how to add from C++.

            DiracsbracketD 1 Reply Last reply
            0
            • M milan

              @Diracsbracket. I know how to add series from QML, I dont know how to add from C++.

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #6

              @milan
              My bad... it seems I am turning blind and obtuse (again)...

              What about this:
              https://stackoverflow.com/questions/38467769/add-c-qabstractseries-to-qml-chartview

              Instead of instantiating the interfacing object as a QML type as in the example, you probably want to instantiate it in C++ and then pass it to the QML context. This object would emit a signal when the data is ready and the graph needs to be updated.

              In your QML you could then use a Connections {} component to connect to that signal and invoke the series-adding JS code from there.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved