##// END OF EJS Templates
modify dateTime method to range method
perrinel -
r535:624470ecacf4
parent child
Show More
@@ -1,162 +1,162
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 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 dateTicker->setDateTimeSpec(Qt::UTC);
29 29
30 30 return dateTicker;
31 31 }
32 32 else {
33 33 // default ticker
34 34 return QSharedPointer<QCPAxisTicker>::create();
35 35 }
36 36 }
37 37
38 38 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries,
39 39 const SqpRange &dateTime)
40 40 {
41 41 qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData"
42 42 << QThread::currentThread()->objectName();
43 43 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
44 44 scalarSeries.lockRead();
45 45 {
46 46 const auto &xData = scalarSeries.xAxisData()->cdata();
47 47 const auto &valuesData = scalarSeries.valuesData()->cdata();
48 48
49 49 auto xDataBegin = xData.cbegin();
50 50 auto xDataEnd = xData.cend();
51 51
52 52 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache"
53 53 << xData.count();
54 54
55 55 auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create();
56 56 qcpGraph->setData(sqpDataContainer);
57 57
58 58 auto lowerIt = std::lower_bound(xDataBegin, xDataEnd, dateTime.m_TStart);
59 59 auto upperIt = std::upper_bound(xDataBegin, xDataEnd, dateTime.m_TEnd);
60 60 auto distance = std::distance(xDataBegin, lowerIt);
61 61
62 62 auto valuesDataIt = valuesData.cbegin() + distance;
63 63 for (auto xAxisDataIt = lowerIt; xAxisDataIt != upperIt;
64 64 ++xAxisDataIt, ++valuesDataIt) {
65 65 sqpDataContainer->appendGraphData(QCPGraphData(*xAxisDataIt, *valuesDataIt));
66 66 }
67 67
68 68 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed"
69 69 << sqpDataContainer->size();
70 70 }
71 71 scalarSeries.unlock();
72 72
73 73
74 74 // Display all data
75 75 component->parentPlot()->replot();
76 76 }
77 77 else {
78 78 /// @todo DEBUG
79 79 }
80 80 }
81 81
82 82 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot,
83 83 const SqpRange &dateTime)
84 84 {
85 85 auto component = plot.addGraph();
86 86
87 87 if (component) {
88 88 // // Graph data
89 89 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
90 90 true);
91 91
92 92 updateScalarData(component, scalarSeries, dateTime);
93 93
94 94 // Axes properties
95 95 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
96 96 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
97 97
98 98 auto setAxisProperties = [](auto axis, const auto &unit) {
99 99 // label (unit name)
100 100 axis->setLabel(unit.m_Name);
101 101
102 102 // ticker (depending on the type of unit)
103 103 axis->setTicker(axisTicker(unit.m_TimeUnit));
104 104 };
105 105 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
106 106 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
107 107
108 108 // Display all data
109 109 component->rescaleAxes();
110 110 plot.replot();
111 111 }
112 112 else {
113 113 qCDebug(LOG_VisualizationGraphHelper())
114 114 << QObject::tr("Can't create graph for the scalar series");
115 115 }
116 116
117 117 return component;
118 118 }
119 119
120 120 } // namespace
121 121
122 122 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
123 123 QCustomPlot &plot) noexcept
124 124 {
125 125 auto result = QVector<QCPAbstractPlottable *>{};
126 126
127 127 if (variable) {
128 128 // Gets the data series of the variable to call the creation of the right components
129 129 // according to its type
130 130 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
131 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime()));
131 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->range()));
132 132 }
133 133 else {
134 134 qCDebug(LOG_VisualizationGraphHelper())
135 135 << QObject::tr("Can't create graph plottables : unmanaged data series type");
136 136 }
137 137 }
138 138 else {
139 139 qCDebug(LOG_VisualizationGraphHelper())
140 140 << QObject::tr("Can't create graph plottables : the variable is null");
141 141 }
142 142
143 143 return result;
144 144 }
145 145
146 146 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
147 147 IDataSeries *dataSeries, const SqpRange &dateTime)
148 148 {
149 149 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
150 150 if (plotableVect.size() == 1) {
151 151 updateScalarData(plotableVect.at(0), *scalarSeries, dateTime);
152 152 }
153 153 else {
154 154 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
155 155 "Can't update Data of a scalarSeries because there is not only one component "
156 156 "associated");
157 157 }
158 158 }
159 159 else {
160 160 /// @todo DEBUG
161 161 }
162 162 }
General Comments 0
You need to be logged in to leave comments. Login now