Results 1 to 5 of 5

Thread: Charts in QtQuick: creating a line series with bar sets

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Charts in QtQuick: creating a line series with bar sets

    This question is admittedly a little odd, and convoluted.

    Let me start by acknowledging that it does not actually make sense to use a scatter/line/spline chart when there is no inherent relationship between y and x (i.e., y cannot reasonably be construed to be a function of x).

    The non-scientific reality is that many people simply want to see a trend line. Consider the case wherein a measurement is collected every day, and we are interested in charting monthly averages. The x axis is the month, and the y axis is the average value. Clearly, this is best modelled with a BarSet because of the nature of the data (and this becomes clear when we consider that we prefer "Sept" over 9 on the x axis). But the Powers That Be are insisting on a trend line. The problem is that a LineSeries is composed of one or more instances of XYSeries, and XYSeries consists of two real numbers (x and y).

    Can this be done using the QtChart module? If so, how might I go about it? Any insights are appreciated.

    (I'll update (and hopefully answer!) this question as I go along, but figured I'd get things rolling on this front.)

    Update:

    Before:

    Qt Code:
    1. BarSeries {
    2. id: barSeries
    3.  
    4. axisX: BarCategoryAxis { categories: dummyData.months }
    5.  
    6. BarSet {
    7. id: appetiteBars
    8.  
    9. label: "Appetite"
    10. values: dummyData.appetite
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    After:

    Qt Code:
    1. LineSeries {
    2. id: lineSeries
    3.  
    4. axisX: BarCategoryAxis { categories: dummyData.months }
    5.  
    6. XYPoint { x: 0; y: dummyData.appetite[0] }
    7. XYPoint { x: 1; y: dummyData.appetite[1] }
    8. XYPoint { x: 2; y: dummyData.appetite[2] }
    9. XYPoint { x: 3; y: dummyData.appetite[3] }
    10. XYPoint { x: 4; y: dummyData.appetite[4] }
    11. XYPoint { x: 5; y: dummyData.appetite[5] }
    12. }
    To copy to clipboard, switch view to plain text mode 

    My brain still rebels against doing this, and it's much more cumbersome to implement as a line series (i.e., per value, rather than dumping the whole array into a BarSet) but it works...
    Last edited by Urthas; 5th July 2016 at 00:04.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Charts in QtQuick: creating a line series with bar sets

    You could try if you could use an Instantiator element to create the XYPoint instances.

    Alternatively that looks like something that could easily be done with a custom item.

    Cheers,
    _

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Charts in QtQuick: creating a line series with bar sets

    The "solution" I came up with above works until I add another line series to the chart. Then I get duplicate x axes. So I will take a look at Instantiator, thanks.

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Charts in QtQuick: creating a line series with bar sets

    Updated solution, to take care of multiple x-axes:

    Qt Code:
    1. LineSeries {
    2. id: appetiteSeries
    3.  
    4. axisX: BarCategoryAxis { categories: dummyData.months }
    5. name: qsTr("Appetite")
    6.  
    7. XYPoint { x: 0; y: dummyData.appetite[0] }
    8. XYPoint { x: 1; y: dummyData.appetite[1] }
    9. XYPoint { x: 2; y: dummyData.appetite[2] }
    10. XYPoint { x: 3; y: dummyData.appetite[3] }
    11. XYPoint { x: 4; y: dummyData.appetite[4] }
    12. XYPoint { x: 5; y: dummyData.appetite[5] }
    13. }
    14.  
    15. LineSeries {
    16. id: energySeries
    17.  
    18. axisX: BarCategoryAxis { visible: false }
    19. name: qsTr("Energy")
    20.  
    21. XYPoint { x: 0; y: dummyData.energy[0] }
    22. XYPoint { x: 1; y: dummyData.energy[1] }
    23. XYPoint { x: 2; y: dummyData.energy[2] }
    24. XYPoint { x: 3; y: dummyData.energy[3] }
    25. XYPoint { x: 4; y: dummyData.energy[4] }
    26. XYPoint { x: 5; y: dummyData.energy[5] }
    27. }
    To copy to clipboard, switch view to plain text mode 

    The key point here is to make the x-axis of subsequent series invisible.

    However, this only works as-is with hard-coded data. Putting the correct number of series with the correct names and XYPoint values, dynamically (as in the case of fetching from a data layer), is a whole other kettle of fish. Perhaps Instantiator will be helpful there, i.e., using a LineSeries as a delegate. We'll see.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Charts in QtQuick: creating a line series with bar sets

    LineSeries is an Item I think, so it should even work with a Repeater.
    I was thinking of using Instantiator to create XYPoint instances.

    Not sure though if that is supported by LineSeries.

    Cheers,
    _

Similar Threads

  1. Qwt charts wtih large data set and multiple series
    By deepal_de in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2015, 15:31
  2. Replies: 0
    Last Post: 3rd March 2015, 09:00
  3. Replies: 3
    Last Post: 6th March 2014, 10:09
  4. Replies: 21
    Last Post: 8th December 2011, 02:18
  5. Creating images from sets of data.
    By maverick_pol in forum Qt Programming
    Replies: 5
    Last Post: 26th February 2008, 09:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.