Results 1 to 4 of 4

Thread: Problem with implementing custom QChartView

  1. #1
    Join Date
    Apr 2021
    Posts
    27
    Thanks
    5

    Default Problem with implementing custom QChartView

    I have implemented a line graph using QchartView. Capture.jpg

    However, I would like to highlight a specific section of only the green line graph.

    Any suggestions?


    Qt Code:
    1. // Actual hit-ratio
    2. QLineSeries *seriesData = new QLineSeries();
    3. for(int i=0; i<timeWindowHR.size(); i++){
    4. int xFirstPoint = timeWinXCoor[i];
    5. int yFirstPoint = timeWindowHR[i];
    6. seriesData->append(xFirstPoint, yFirstPoint);
    7. int xSecondPoint = timeWinXCoor[i+1];
    8. int ySecondPoint = timeWindowHR[i];
    9. seriesData->append(xSecondPoint, ySecondPoint);
    10. }
    11. QPen pen((Qt::darkGreen));
    12. pen.setWidth(2);
    13. seriesData->setName("Actual Hit-Ratio");
    14. seriesData->setPen(pen);
    15.  
    16. // Expected hit-ratio
    17. QLineSeries *expectedSeries = new QLineSeries();
    18. for(int i=0; i<expectedHR.size(); i++){
    19. int xFirstPoint = timeWinXCoor[i];
    20. int yFirstPoint = expectedHR[i];
    21. expectedSeries->append(xFirstPoint, yFirstPoint);
    22. int xSecondPoint = timeWinXCoor[i+1];
    23. int ySecondPoint = expectedHR[i];
    24. expectedSeries->append(xSecondPoint, ySecondPoint);
    25. }
    26. QPen expPen((Qt::darkGray));
    27. expPen.setWidth(2);
    28. expectedSeries->setName("Expected Hit-Ratio");
    29. expectedSeries->setPen(expPen);
    30.  
    31. chartView->chart()->removeAllSeries();
    32. chartView->chart()->addSeries(seriesData);
    33. chartView->chart()->addSeries(expectedSeries);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Problem with implementing custom QChartView

    QCharts is probably the most poorly-designed library in all of Qt. You can't customize it in any way because none of the classes have virtual methods and there is no way to override them through inheritance to change their behavior.

    I think the only way to solve your problem is to create a third QLineSeries that contains the points in the section you want to highlight, and to remove those points from the green series. You probably need to split the green series into three separate series - one before the highlight, the highlight, and one after, because if you don't there will be a line drawn through the highlight region that connects the two parts on the non-highlighted series.

    You might want to check out QCustomPlot instead. I have been able to customize it to do almost everything I need. I gave up on QCharts long ago when I realized there was nothing I could change. Literally everything is locked-in way down into the implementation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2021
    Posts
    27
    Thanks
    5

    Default Re: Problem with implementing custom QChartView

    Quote Originally Posted by d_stranz View Post
    QCharts is probably the most poorly-designed library in all of Qt. You can't customize it in any way because none of the classes have virtual methods and there is no way to override them through inheritance to change their behavior.

    I think the only way to solve your problem is to create a third QLineSeries that contains the points in the section you want to highlight, and to remove those points from the green series. You probably need to split the green series into three separate series - one before the highlight, the highlight, and one after, because if you don't there will be a line drawn through the highlight region that connects the two parts on the non-highlighted series.

    You might want to check out QCustomPlot instead. I have been able to customize it to do almost everything I need. I gave up on QCharts long ago when I realized there was nothing I could change. Literally everything is locked-in way down into the implementation.
    Thanks, I will try to implement it.

    I also need to draw a cursor on the QChartView object. something like this:
    Cursor on the Chartfmrhs.png

    Whenever the user will hover on the QchartView, I need to show this vertical cursor. Can you tell me how can I do that?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,022
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Problem with implementing custom QChartView

    Can you tell me how can I do that?
    Sorry, like I said, I don't use QtCharts. Maybe someone else here can give you some help.

    However, because you have three different colors, you will probably have to create three QScatterSeries, one for each dot and color, and use a QChartView RubberBand for the vertical line. Each time the line moves, you will have to call QScatterSeries::replace() for each dot to update the position. It will be ugly code.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. adding new QChart in a QChartView
    By zemlemer in forum Newbie
    Replies: 3
    Last Post: 7th September 2019, 16:02
  2. Frameless QChart in QChartview not possible?
    By mustermann.klaus@gmx.de in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2019, 16:29
  3. Save QChartView as PNG
    By Carlsberg in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2017, 16:44
  4. Problem getting QChartView to display
    By derrickbj in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2016, 16:55
  5. Replies: 1
    Last Post: 18th July 2011, 15:24

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.