##// END OF EJS Templates
Updates UI to not call the sort method when update graphs, as data series are already sorted
Alexandre Leroux -
r453:a8d8791b7e7a
parent child
Show More
@@ -1,159 +1,161
1 1 #include "Visualization/VisualizationGraphHelper.h"
2 2 #include "Visualization/qcustomplot.h"
3 3
4 4 #include <Data/ScalarSeries.h>
5 5
6 6 #include <Variable/Variable.h>
7 7
8 8 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
9 9
10 10 namespace {
11 11
12 12 class SqpDataContainer : public QCPGraphDataContainer {
13 13 public:
14 void appendGraphDataUnsorted(const QCPGraphData &data) { mData.append(data); }
14 void appendGraphData(const QCPGraphData &data) { mData.append(data); }
15 15 };
16 16
17 17
18 18 /// Format for datetimes on a axis
19 19 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
20 20
21 21 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
22 22 /// non-time data
23 23 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
24 24 {
25 25 if (isTimeAxis) {
26 26 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
27 27 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
28 28
29 29 return dateTicker;
30 30 }
31 31 else {
32 32 // default ticker
33 33 return QSharedPointer<QCPAxisTicker>::create();
34 34 }
35 35 }
36 36
37 37 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries,
38 38 const SqpDateTime &dateTime)
39 39 {
40 40 qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData"
41 41 << QThread::currentThread()->objectName();
42 42 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
43 // Clean the graph
44 // NAIVE approch
45 43 scalarSeries.lockRead();
46 44 {
47 const auto xData = scalarSeries.xAxisData()->data();
48 const auto valuesData = scalarSeries.valuesData()->data();
49 const auto count = xData.count();
50 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache"
51 << xData.count();
52
53 auto dataContainer = qcpGraph->data();
54 dataContainer->clear();
45 const auto &xData = scalarSeries.xAxisData()->cdata();
46 const auto &valuesData = scalarSeries.valuesData()->cdata();
47
48 auto xDataBegin = xData.cbegin();
49 auto xDataEnd = xData.cend();
50
51 qCInfo(LOG_VisualizationGraphHelper())
52 << "TORM: Current points in cache" << xData.count();
53
55 54 auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create();
56 55 qcpGraph->setData(sqpDataContainer);
57 56
58 for (auto i = 0; i < count; ++i) {
59 const auto x = xData[i];
60 if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) {
61 sqpDataContainer->appendGraphDataUnsorted(QCPGraphData(x, valuesData[i]));
62 }
57 auto lowerIt = std::lower_bound(xDataBegin, xDataEnd, dateTime.m_TStart);
58 auto upperIt = std::upper_bound(xDataBegin, xDataEnd, dateTime.m_TEnd);
59 auto distance = std::distance(xDataBegin, lowerIt);
60
61 auto valuesDataIt = valuesData.cbegin() + distance;
62 for (auto xAxisDataIt = lowerIt; xAxisDataIt != upperIt;
63 ++xAxisDataIt, ++valuesDataIt) {
64 sqpDataContainer->appendGraphData(QCPGraphData(*xAxisDataIt, *valuesDataIt));
63 65 }
64 sqpDataContainer->sort();
65 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed"
66 << sqpDataContainer->size();
66
67 qCInfo(LOG_VisualizationGraphHelper())
68 << "TORM: Current points displayed" << sqpDataContainer->size();
67 69 }
68 70 scalarSeries.unlock();
69 71
70 72
71 73 // Display all data
72 74 component->parentPlot()->replot();
73 75 }
74 76 else {
75 77 /// @todo DEBUG
76 78 }
77 79 }
78 80
79 81 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot,
80 82 const SqpDateTime &dateTime)
81 83 {
82 84 auto component = plot.addGraph();
83 85
84 86 if (component) {
85 87 // // Graph data
86 88 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
87 89 true);
88 90
89 91 updateScalarData(component, scalarSeries, dateTime);
90 92
91 93 // Axes properties
92 94 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
93 95 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
94 96
95 97 auto setAxisProperties = [](auto axis, const auto &unit) {
96 98 // label (unit name)
97 99 axis->setLabel(unit.m_Name);
98 100
99 101 // ticker (depending on the type of unit)
100 102 axis->setTicker(axisTicker(unit.m_TimeUnit));
101 103 };
102 104 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
103 105 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
104 106
105 107 // Display all data
106 108 component->rescaleAxes();
107 109 plot.replot();
108 110 }
109 111 else {
110 112 qCDebug(LOG_VisualizationGraphHelper())
111 113 << QObject::tr("Can't create graph for the scalar series");
112 114 }
113 115
114 116 return component;
115 117 }
116 118
117 119 } // namespace
118 120
119 121 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
120 122 QCustomPlot &plot) noexcept
121 123 {
122 124 auto result = QVector<QCPAbstractPlottable *>{};
123 125
124 126 if (variable) {
125 127 // Gets the data series of the variable to call the creation of the right components
126 128 // according to its type
127 129 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
128 130 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime()));
129 131 }
130 132 else {
131 133 qCDebug(LOG_VisualizationGraphHelper())
132 134 << QObject::tr("Can't create graph plottables : unmanaged data series type");
133 135 }
134 136 }
135 137 else {
136 138 qCDebug(LOG_VisualizationGraphHelper())
137 139 << QObject::tr("Can't create graph plottables : the variable is null");
138 140 }
139 141
140 142 return result;
141 143 }
142 144
143 145 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
144 146 IDataSeries *dataSeries, const SqpDateTime &dateTime)
145 147 {
146 148 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
147 149 if (plotableVect.size() == 1) {
148 150 updateScalarData(plotableVect.at(0), *scalarSeries, dateTime);
149 151 }
150 152 else {
151 153 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
152 154 "Can't update Data of a scalarSeries because there is not only one component "
153 155 "associated");
154 156 }
155 157 }
156 158 else {
157 159 /// @todo DEBUG
158 160 }
159 161 }
General Comments 0
You need to be logged in to leave comments. Login now