mafulechka
June 24, 2019, 2:48 p.m.

Qt Support - Aligning Chart Views to Each Other

One thing that comes up in support from time to time is that when you have multiple chart types, it's good to align them to each other so that the actual chart size is the same for both charts. If you have the same axle sizes this is easy to achieve as they will take up the same width and align accordingly. However, if it isn't, which is often the case, then we can use margins to force it to take on the desired dimensions.


So let's imagine our graph currently looks like this:

If you are using C++ for your chart code, this can be achieved with a single slot that can be connected to the QChart::plotAreaChanged() signal.
First we need to prevent recursion since we'll be changing fields, but since we want the internals to keep doing their thing, we won't block the signals. To do this, we'll have a static boolean that we'll set to true to indicate that we're in the middle of our calculations:

  1. void updatePlotArea(const QRectF &area)
  2. {
  3. static bool fixing = false;
  4. if (fixing)
  5. return;
  6. fixing = true;

The next thing we need to do is determine which chart is best to use to equalize the others. This means picking the one with the largest left value for the plot area (i.e. the one with the widest axis). We are currently using "modified" as a starting point, and if there are any, we are changing the margins for them, as this will provide the largest size available for the plot area at any given point for the largest axis.

  1. QChart *bestChart = (QChart *)sender();
  2. QRectF bestRect = area;
  3. foreach(QChart *chart, charts) {
  4. if (chart->plotArea().left() > bestRect.left()) {
  5. bestChart = chart;
  6. bestRect = chart->plotArea();
  7. chart->setMargins(QMargins(20, 0, 20, 0));
  8. }
  9. }

Then, with the exception of the one that happens to be the "best" (bestChart), we adjust the margins to make sure they match the "best" and align correctly, setting the margins to be the existing margin plus the difference between the area of the "best" chart and the current one. Finally, we send any published events to be immediately updated for us.

  1. foreach(QChart *chart, charts) {
  2. if (bestChart != chart) {
  3. const int left = chart->margins().left() +
  4. (bestRect.left() - chart->plotArea().left());
  5. const int right = chart->margins().right() +
  6. (chart->plotArea().right() - bestRect.right());
  7. chart->setMargins(QMargins(left, 0, right, 0));
  8. }
  9. }
  10. QApplication::sendPostedEvents();
  11. fixing = false;

This will give us two aligned plots that look like this:

As far as QML is concerned, we can do something similar through a function that is called with onPlotAreaChanged .

  1. property bool fixing: false
  2. property var chartViews: [chartview, chartview_b]
  3. function updatePlotArea(chart, area) {
  4. if (fixing)
  5. return
  6. fixing = true
  7. var tmpChart
  8. var bestRect = chart.plotArea
  9. var bestChart = chart
  10. for (var i = 0; i Math.ceil(bestRect.left) ||
  11. (Math.ceil(tmpChart.plotArea.left) ===
  12. Math.ceil(bestRect.left) &&
  13. Math.floor(tmpChart.plotArea.right) <
  14. Math.floor(bestRect.right))) {
  15. bestChart = tmpChart;
  16. bestRect = tmpChart.plotArea;
  17. }
  18. }
  19. bestRect.left = Math.ceil(bestRect.left)
  20. bestRect.right = Math.floor(bestRect.right)
  21. for (i = 0; i < chartViews.length; i++) {
  22. tmpChart = chartViews[i]
  23. if (tmpChart !== bestChart) {
  24. var newLeft = 20 + bestRect.left -
  25. Math.floor(tmpChart.plotArea.left);
  26. var newRight = 20 +
  27. Math.ceil(tmpChart.plotArea.right) -
  28. bestRect.right;
  29. tmpChart.margins.left = newLeft
  30. tmpChart.margins.right = newRight
  31. }
  32. }
  33. fixing = false;
  34. }

The only difference is that we account for the fact that the plot area uses real values and the fields are still integer based, so we make some extra credentials as a result.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup