Results 1 to 6 of 6

Thread: QChart + QDateTiemAxis, mapTo and mapFrom

  1. #1
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default QChart + QDateTiemAxis, mapTo and mapFrom

    Hello Everybody,

    I'd like to add some describing text to a given QPointF on a chart. I really spent a lot of time trying all of the mapToX's and mapFromX's to map coordinates.
    Here's a short wrap up about what I'm doing:

    Qt Code:
    1. // create Chart
    2. chart = new QChart();
    3. chart->createDefaultAxes();
    4.  
    5. // costumize chart
    6. dateTimeAxis = new QDateTimeAxis;
    7.  
    8. categoryAxis = new QBarCategoryAxis();
    9. categoryAxis->append(categoriesList);
    10.  
    11. chart->addAxis(dateTimeAxis,Qt::AlignBottom);
    12. chart->addAxis(categoryAxis, Qt::AlignLeft);
    13.  
    14. // hide casual axes
    15. chart->axisX()->hide();
    16. chart->axisY()->hide();
    17.  
    18. // use QChartView
    19. myChartView = new QChartView(chart);
    20.  
    21. // promote to QMainWindow
    22. layout->addWidget(myChartView);
    23.  
    24. myWidget = new QWidget();
    25. myWidget->setLayout(layout);
    26. setCentralWidget(myWidget);
    27.  
    28. ///////////////////
    29.  
    30. // generate some life
    31. lowerSeries = new QLineSeries();
    32. upperSeries = new QLineSeries();
    33. area = new QAreaSeries(upperSeries, lowerSeries);
    34.  
    35. area->attachAxis(dateTimeAxis);
    36. area->attachAxis(chart->axisY());
    37.  
    38. p1 = QPointF(QDateTime::currentTime.toMSecsSinceEpoch(), 1);
    39. p3 = QPointF(QDateTime::currentTime.toMSecsSinceEpoch(), 5);
    40. p2 = QPointF(QDateTime::currentTime.addSecs(rand_N_ofSec).toMSecsSinceEpoch(), 1);
    41. p4 = QPointF(QDateTime::currentTime.addSecs(rand_N_ofSec).toMSecsSinceEpoch(), 5);
    42.  
    43. lowerSeries << p1 << p2;
    44. upperSeries << p3 << p4;
    45.  
    46.  
    47. // now the problem:
    48. tag = new QGraphicsSimpleTextItem(chart);
    49. tag->setText("any text to describe the point");
    50.  
    51. // how toset the location of the tag properly (near the point)?
    To copy to clipboard, switch view to plain text mode 

    i.e. the point is at (30,60) I'd like to set the tag on (31,61)
    a typical QPointF on the chart would look like

    Qt Code:
    1. p1 = QPointF(QDateTime::currentTime.toMSecsSinceEpoch(), 3);
    2. qDebug() << "p:" << p;
    3.  
    4. p: QPointF(1.51138e+12, 3) // strange numbers due to toMSecSinceEpoche()
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance, Lars

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QChart + QDateTiemAxis, mapTo and mapFrom

    What's wrong with "tag->setPos(1.51138e+12, 3);" ? Those are the world coordinates of your axes. And if that doesn't work, then use the QPointF returned by mapToValue(), using the world coordinates and the series as arguments to the method.
    <=== 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
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QChart + QDateTiemAxis, mapTo and mapFrom

    Hey d_stranz,
    very glad to hearing from you. One of your replies brought me here.

    That is exactely my question. No, unfortunately it doesn't work like this. To me it seems that p=(1.51138e+12, 3) are the "inside-chart-coordinates". I can obtain them using mapFromScene(). I tried a little bit around and I approach the point doing tag->setPos(400, 300); Now: how to map from (1.51138e+12, 3) to (400, 300). I didn't find a way after all.

    The "why" is more than clear due to http://www.qtcentre.org/threads/6145...light=d_stranz

    cheers, Lars
    Last edited by mustermann.klaus@gmx.de; 23rd November 2017 at 11:38.

  4. #4
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QChart + QDateTiemAxis, mapTo and mapFrom

    Hey d_stranz,
    very glad to hearing from you. One of your replies brought me here.

    That is exactely my question. No, unfortunately it doesn't work like this. To me it seems that p=(1.51138e+12, 3) are the "inside-chart-coordinates". I can obtain them using mapFromScene(). I tried a little bit around and I approach the point doing tag->setPos(400, 300); Now: how to map from (1.51138e+12, 3) to (400, 300). I didn't find a way after all.

    The "why" is more than clear due to http://www.qtcentre.org/threads/6145...light=d_stranz

    cheers, Lars


    Added after 1 55 minutes:


    Hey d_stranz,

    I fixed the code a littel so you perhaps can try out.

    Qt Code:
    1. // PRO-File
    2. QT += core gui \
    3. widgets \
    4. charts
    5. TEMPLATE = app
    6. SOURCES += main.cpp\
    7.  
    8.  
    9. // cpp-file
    10. #include <QApplication>
    11. #include <QMainWindow>
    12. #include <QtCharts/QChartView>
    13. #include <QtCharts/QLegend>
    14. #include <QtCharts/QBarCategoryAxis>
    15. #include <QtCharts/QDateTimeAxis>
    16. #include <QtCharts/QLineSeries>
    17. #include <QtCharts/QValueAxis>
    18. #include <QtCharts/QAreaSeries>
    19. #include <QtWidgets/QGraphicsView>
    20. #include <QtCharts/QChartGlobal>
    21. #include <QTime>
    22. #include <QDebug>
    23. #include <QHBoxLayout>
    24.  
    25. QT_CHARTS_USE_NAMESPACE
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29.  
    30. QApplication a(argc, argv);
    31.  
    32.  
    33. // obtain time first
    34. QDateTime currentTime = QDateTime::currentDateTime();
    35.  
    36. // create Chart
    37. QChart *chart = new QChart();
    38. chart->legend()->hide();
    39. chart->setMinimumSize(640, 480);
    40. chart->createDefaultAxes();
    41.  
    42.  
    43. // costumize chart a little
    44. QDateTimeAxis *dateTimeAxis = new QDateTimeAxis;
    45. dateTimeAxis->setTickCount(7);
    46. dateTimeAxis->setFormat("hh:mm");
    47. dateTimeAxis->setTitleText("Time");
    48. dateTimeAxis->setMin(currentTime.addSecs(-3600));
    49. dateTimeAxis->setMax(currentTime.addSecs( 3600));
    50.  
    51.  
    52. // config axes
    53. QStringList categories;
    54. categories << "Paris" << "London" << "Berlin" << "Madrid";
    55.  
    56. QBarCategoryAxis *categoryAxis = new QBarCategoryAxis();
    57. categoryAxis->append(categories);
    58.  
    59. chart->addAxis(dateTimeAxis,Qt::AlignBottom);
    60. chart->addAxis(categoryAxis, Qt::AlignLeft);
    61.  
    62. chart->axisY()->setRange(0, categories.length());
    63.  
    64.  
    65. // use QChartView
    66. QChartView *myChartView = new QChartView(chart);
    67.  
    68. // promote to QMainWindow
    69. QHBoxLayout *layout = new QHBoxLayout;
    70. layout->addWidget(myChartView);
    71.  
    72. QWidget *window = new QWidget;
    73. window->setLayout(layout);
    74.  
    75. QMainWindow *mainwindow = new QMainWindow();
    76. mainwindow->setCentralWidget(window);
    77.  
    78. ///////////////////
    79.  
    80. // add some life
    81. QLineSeries *lowerSeries = new QLineSeries();
    82. QLineSeries *upperSeries = new QLineSeries();
    83. QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries);
    84. area->setBrush(QColor(100,100,100,100));
    85. chart->addSeries(area);
    86.  
    87. area->attachAxis(dateTimeAxis);
    88. area->attachAxis(chart->axisY());
    89.  
    90.  
    91. QPointF p1 = QPointF(currentTime.toMSecsSinceEpoch(), 1);
    92. QPointF p3 = QPointF(currentTime.toMSecsSinceEpoch(), 2);
    93. QPointF p2 = QPointF(currentTime.addSecs(2400).toMSecsSinceEpoch(), 1);
    94. QPointF p4 = QPointF(currentTime.addSecs(2400).toMSecsSinceEpoch(), 2);
    95.  
    96. lowerSeries->append(p1);
    97. lowerSeries->append(p2);
    98. upperSeries->append(p3);
    99. upperSeries->append(p4);
    100.  
    101.  
    102. // now the problem:
    103. QString x, y;
    104. QPointF position;
    105.  
    106.  
    107. // first try
    108. position = chart->mapToPosition(p1, lowerSeries);
    109.  
    110. x = x.setNum(position.x());
    111. y = y.setNum(position.y());
    112.  
    113. tag1->setText(x + " " + y);
    114. tag1->setPos(position);
    115.  
    116.  
    117. // approach randomly
    118. position = QPointF(300,200);
    119. x = x.setNum(position.x());
    120. y = y.setNum(position.y());
    121.  
    122. tag2->setText(x + " " + y);
    123. tag2->setPos(position);
    124.  
    125.  
    126.  
    127.  
    128. mainwindow->show();
    129. return a.exec();
    130. }
    To copy to clipboard, switch view to plain text mode 


    Should work out of the box.

    As you, or of course anybody else, might figure out this is supposed to be something like a timeline. I'd like to label the shown elements. If you (or anybody else) know a better way, the input would be highly appreciatet. QAreaSeries unfortunately has no setText(). I really struggled a lot.

    Regards, Lars
    Last edited by mustermann.klaus@gmx.de; 23rd November 2017 at 16:23.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QChart + QDateTiemAxis, mapTo and mapFrom

    OK, here it is:

    Qt Code:
    1. QPointF position = tag1->mapFromParent( chart->mapToPosition( p1 ) );
    2. tag1->setPos( position );
    To copy to clipboard, switch view to plain text mode 

    Note that when you resize the window, the label doesn't move because it has been put into a fixed pixel position on the chart. You will need to implement resizeEvent() for the view and in that method you must recalculate the positions and move all the labels.

    Also note that the origin (0,0) for the text is the upper left corner of the text's bounding rect. If you want the text to be closer to the actual series point, you'll need to adjust this. For example, this roughly centers the text on the point:

    Qt Code:
    1. QPointF position = tag1->mapFromParent( chart->mapToPosition( p1 ) );
    2. QRectF tagRect = tag1->boundingRect();
    3. position.setX( position.x() - tagRect.width() / 2 );
    4. position.setY( position.y() - tagRect.height() / 2 );
    5. tag1->setPos( position );
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 23rd November 2017 at 21:39.
    <=== 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.

  6. #6
    Join Date
    May 2015
    Posts
    42
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QChart + QDateTiemAxis, mapTo and mapFrom

    Oh Yeahhhhh,

    that is pretty much what I needed. Thanks a million d_stranz. I am aware of the resize problem. Now I am at that stage to take care about that. Also thank you for the 2nd snippet. Very useful.
    Most likely I'll use qlabel anyway since this can carry a pixmap.

    cheers, Lars

Similar Threads

  1. qchart hide legend for part of series
    By marrierius in forum Newbie
    Replies: 5
    Last Post: 9th September 2021, 15:11
  2. adding new QChart in a QChartView
    By zemlemer in forum Newbie
    Replies: 3
    Last Post: 7th September 2019, 17:02
  3. QChart Questions
    By rhb327 in forum Qt Programming
    Replies: 0
    Last Post: 17th May 2017, 12:04
  4. Dynamically updating QChart
    By nbkhwjm in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2017, 18:06
  5. contextMenuEvent works partially in QChart
    By marrierius in forum Newbie
    Replies: 0
    Last Post: 16th December 2016, 19:48

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.