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. Using data in a model for plotting to chart
Qt 6.11 is out! See what's new in the release blog

Using data in a model for plotting to chart

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 1.9k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Bert59
    wrote on last edited by
    #1

    Hi,

    I have a list of data comming from a text file.
    I load this file on the C++ side and have made a model to display these data in a QML ListView.

    Now I would like to plot these data on a ChartView.
    How can I access the data in the model from outside the ListView in order to add them to a LineSeries which can be displayed on a ChartView?

    Thnaks

    B 1 Reply Last reply
    0
    • B Bert59

      Hi,

      I have a list of data comming from a text file.
      I load this file on the C++ side and have made a model to display these data in a QML ListView.

      Now I would like to plot these data on a ChartView.
      How can I access the data in the model from outside the ListView in order to add them to a LineSeries which can be displayed on a ChartView?

      Thnaks

      B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      @Bert59 since your data is provided by a C++ backend it might make sense to populate your line series in C++. You should be able to instantiate a LineSeries object in your QML and pass that into a C++ API function that you expose to QML (probably via a context property). The C++ function can receive the line series argument as a QAbstractSeries and use the C++ API of that class to populate the series data.

      If I recall, the Oscilloscope example demonstrate how to do this. I think that was my starting point when I was figuring out QML charts. I'd recommend you take a look at it.

      1 Reply Last reply
      0
      • oria66O Offline
        oria66O Offline
        oria66
        wrote on last edited by
        #3

        Hi, @Bert59.

        You have a class or a function in the C++ side that receives a QAbstractSeries:

        void YourClassExportedToQML::update(QAbstractSeries *series)
        {
            if (series)
            {
                QXYSeries *xySeries = static_cast<QXYSeries *>(series);
        
               // Here you replace the data in the xySeries with your data
               // use replace, it's better for performance
        
            }
        }
        

        Now in the QML side, I supposed you have the class YourClassExportedToQML and it has a signal when the data came:

         Connections{
                target: yourClassExportedToQML
                onMySignalWhenDataIsReady: {
        
                        var serie = chart.createSeries(ChartView.SeriesTypeLine,
                                                       "MySerie",
                                                       axisX,
                                                       axisY);
        
                        yourClassExportedToQML.update(serie);
                    
                }
            }
        
        

        If you have doubts, do not hesitate to ask!

        The truth is out there

        1 Reply Last reply
        1
        • B Offline
          B Offline
          Bert59
          wrote on last edited by
          #4

          Thank you Bob64 and oria66 for your replies.

          I must say that I'm a beginner with Qt and I find the oscilloscope example quite complicated to understand.
          Would it be possible to send me a minimum project with a few hard coded values using your hints oria66?

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bert59
            wrote on last edited by
            #5

            Hi oria66,
            Just wanted to let you know that finally I succeeded to implement your code examples
            Thank you

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bert59
              wrote on last edited by
              #6

              Hi, just an additional question.
              Using your suggestion I have added below
              QXYSeries *xySeries = static_cast<QXYSeries *>(series);
              the following:

              QList<QAbstractAxis *> axis;
              axis = xyseries->attachedAxes();
              axis.at(0)->setMax(120);
              This allow setting the max of the X axis.

              Unfortunately I could not find how retrieve the actual setting. There is no getMax or something similar available.
              Do you have an idea how to do that ?

              oria66O 1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                What I would use instead to answer the original question is a VXYModelMapper

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bert59
                  wrote on last edited by
                  #8

                  Thank you for the suggestion.
                  Could you provide an small example on how this class has to be implemeted?
                  From the documentation it's not so obvious when you are a beginner with Qt.
                  Thanks

                  1 Reply Last reply
                  0
                  • B Bert59

                    Hi, just an additional question.
                    Using your suggestion I have added below
                    QXYSeries *xySeries = static_cast<QXYSeries *>(series);
                    the following:

                    QList<QAbstractAxis *> axis;
                    axis = xyseries->attachedAxes();
                    axis.at(0)->setMax(120);
                    This allow setting the max of the X axis.

                    Unfortunately I could not find how retrieve the actual setting. There is no getMax or something similar available.
                    Do you have an idea how to do that ?

                    oria66O Offline
                    oria66O Offline
                    oria66
                    wrote on last edited by oria66
                    #9

                    Hi @Bert59. When I worked with charts I remember that I manage the axis value from the QML side.
                    For example:

                            DateTimeAxis {
                                id: axisX
                                min: new Date(2020,3,30,0,0,0)
                                max: new Date(2020,3,30,24,0,0)
                                format: "hh:mm"; labelsColor: "white"
                            }
                    

                    You can manipulate the min and max value from axisX.

                    However, it would be interesting in implementing the idea proposed by @GrecKo.

                    The truth is out 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