##// END OF EJS Templates
Updates VisualizationGraphHelper to handle vectors
Alexandre Leroux -
r547:04be26486541
parent child
Show More
@@ -218,6 +218,8 public:
218 }
218 }
219 }
219 }
220
220
221 int componentCount() const noexcept { return m_Data.size(); }
222
221 /**
223 /**
222 * @return the data of a component
224 * @return the data of a component
223 * @param componentIndex the index of the component to retrieve the data
225 * @param componentIndex the index of the component to retrieve the data
@@ -2,6 +2,7
2 #include "Visualization/qcustomplot.h"
2 #include "Visualization/qcustomplot.h"
3
3
4 #include <Data/ScalarSeries.h>
4 #include <Data/ScalarSeries.h>
5 #include <Data/VectorSeries.h>
5
6
6 #include <Variable/Variable.h>
7 #include <Variable/Variable.h>
7
8
@@ -35,161 +36,199 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
35 }
36 }
36 }
37 }
37
38
38 void updateScalarData(QCPAbstractPlottable *component, std::shared_ptr<ScalarSeries> scalarSeries,
39 /// Sets axes properties according to the properties of a data series
39 const SqpRange &range)
40 template <int Dim>
41 void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept
40 {
42 {
41 qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData"
43 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
42 << QThread::currentThread()->objectName();
44 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
43 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
45 auto setAxisProperties = [](auto axis, const auto &unit) {
44 scalarSeries->lockRead();
46 // label (unit name)
45 {
47 axis->setLabel(unit.m_Name);
46 auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create();
48
47 qcpGraph->setData(sqpDataContainer);
49 // ticker (depending on the type of unit)
48 auto bounds = scalarSeries->subData(range.m_TStart, range.m_TEnd);
50 axis->setTicker(axisTicker(unit.m_TimeUnit));
49 for (auto it = bounds.first; it != bounds.second; ++it) {
51 };
50 sqpDataContainer->appendGraphData(QCPGraphData(it->x(), it->value()));
52 setAxisProperties(plot.xAxis, dataSeries.xAxisUnit());
51 }
53 setAxisProperties(plot.yAxis, dataSeries.valuesUnit());
52
54 }
53 qCInfo(LOG_VisualizationGraphHelper()) << "TODEBUG: Current points displayed"
54 << sqpDataContainer->size();
55 }
56 scalarSeries->unlock();
57
58
55
59 // Display all data
56 /**
60 component->parentPlot()->replot();
57 * Struct used to create plottables, depending on the type of the data series from which to create them
61 }
58 * @tparam T the data series' type
62 else {
59 * @remarks Default implementation can't create plottables
63 /// @todo DEBUG
60 */
61 template <typename T, typename Enabled = void>
62 struct PlottablesCreator {
63 static PlottablesMap createPlottables(T &, QCustomPlot &)
64 {
65 qCCritical(LOG_DataSeries())
66 << QObject::tr("Can't create plottables: unmanaged data series type");
67 return {};
64 }
68 }
65 }
69 };
66
70
67 QCPAbstractPlottable *createScalarSeriesComponentV2(std::shared_ptr<ScalarSeries> scalarSeries,
71 /**
68 QCustomPlot &plot)
72 * Specialization of PlottablesCreator for scalars and vectors
69 {
73 * @sa ScalarSeries
70 auto component = plot.addGraph();
74 * @sa VectorSeries
75 */
76 template <typename T>
77 struct PlottablesCreator<T,
78 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
79 or std::is_base_of<VectorSeries, T>::value> > {
80 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
81 {
82 PlottablesMap result{};
83
84 // Gets the number of components of the data series
85 auto componentCount = dataSeries.valuesData()->componentCount();
86
87 // For each component of the data series, creates a QCPGraph to add to the plot
88 for (auto i = 0; i < componentCount; ++i) {
89 auto graph = plot.addGraph();
90
91 result.insert({i, graph});
92 }
71
93
72 if (component) {
73 // Axes properties
94 // Axes properties
74 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
95 setAxesProperties(dataSeries, plot);
75 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
96
76
97 plot.replot();
77 auto setAxisProperties = [](auto axis, const auto &unit) {
98
78 // label (unit name)
99 return result;
79 axis->setLabel(unit.m_Name);
80
81 // ticker (depending on the type of unit)
82 axis->setTicker(axisTicker(unit.m_TimeUnit));
83 };
84 setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit());
85 setAxisProperties(plot.yAxis, scalarSeries->valuesUnit());
86 }
100 }
87 return component;
101 };
88 }
89
102
90 QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr<ScalarSeries> scalarSeries,
103 /**
91 QCustomPlot &plot, const SqpRange &dateTime)
104 * Struct used to update plottables, depending on the type of the data series from which to update them
92 {
105 * @tparam T the data series' type
93 auto component = plot.addGraph();
106 * @remarks Default implementation can't update plottables
107 */
108 template <typename T, typename Enabled = void>
109 struct PlottablesUpdater {
110 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
111 {
112 qCCritical(LOG_DataSeries())
113 << QObject::tr("Can't update plottables: unmanaged data series type");
114 }
115 };
94
116
95 if (component) {
117 /**
96 // // Graph data
118 * Specialization of PlottablesUpdater for scalars and vectors
97 component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(),
119 * @sa ScalarSeries
98 true);
120 * @sa VectorSeries
121 */
122 template <typename T>
123 struct PlottablesUpdater<T,
124 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
125 or std::is_base_of<VectorSeries, T>::value> > {
126 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
127 bool rescaleAxes)
128 {
129 dataSeries.lockRead();
130
131 // For each plottable to update, resets its data
132 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
133 for (const auto &plottable : plottables) {
134 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
135 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
136 graph->setData(dataContainer);
137
138 dataContainers.insert({plottable.first, dataContainer});
139 }
140 }
99
141
100 updateScalarData(component, scalarSeries, dateTime);
142 // - Gets the data of the series included in the current range
143 // - Updates each plottable by adding, for each data item, a point that takes x-axis data and value data. The correct value is retrieved according to the index of the component
144 auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd);
145 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
146 for (const auto &dataContainer : dataContainers) {
147 auto componentIndex = dataContainer.first;
148 dataContainer.second->appendGraphData(
149 QCPGraphData(it->x(), it->value(componentIndex)));
150 }
151 }
101
152
102 // Axes properties
153 dataSeries.unlock();
103 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
104 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
105
154
106 auto setAxisProperties = [](auto axis, const auto &unit) {
155 if (!plottables.empty()) {
107 // label (unit name)
156 auto plot = plottables.begin()->second->parentPlot();
108 axis->setLabel(unit.m_Name);
109
157
110 // ticker (depending on the type of unit)
158 if (rescaleAxes) {
111 axis->setTicker(axisTicker(unit.m_TimeUnit));
159 plot->rescaleAxes();
112 };
160 }
113 setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit());
114 setAxisProperties(plot.yAxis, scalarSeries->valuesUnit());
115
161
116 // Display all data
162 plot->replot();
117 component->rescaleAxes();
163 }
118 plot.replot();
119 }
164 }
120 else {
165 };
121 qCDebug(LOG_VisualizationGraphHelper())
166
122 << QObject::tr("Can't create graph for the scalar series");
167 /**
168 * Helper used to create/update plottables
169 */
170 struct IPlottablesHelper {
171 virtual ~IPlottablesHelper() noexcept = default;
172 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
173 virtual void update(PlottablesMap &plottables, const SqpRange &range,
174 bool rescaleAxes = false) const = 0;
175 };
176
177 /**
178 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
179 * @tparam T the data series' type
180 */
181 template <typename T>
182 struct PlottablesHelper : public IPlottablesHelper {
183 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
184
185 PlottablesMap create(QCustomPlot &plot) const override
186 {
187 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
123 }
188 }
124
189
125 return component;
190 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
126 }
191 {
192 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
193 }
127
194
128 } // namespace
195 T &m_DataSeries;
196 };
129
197
130 QVector<QCPAbstractPlottable *>
198 /// Creates IPlottablesHelper according to a data series
131 VisualizationGraphHelper::createV2(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept
199 std::unique_ptr<IPlottablesHelper> createHelper(IDataSeries *dataSeries) noexcept
132 {
200 {
133 auto result = QVector<QCPAbstractPlottable *>{};
201 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
134
202 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
135 if (variable) {
203 }
136 // Gets the data series of the variable to call the creation of the right components
204 else if (auto vectorSeries = dynamic_cast<VectorSeries *>(dataSeries)) {
137 // according to its type
205 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
138 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) {
139 result.append(createScalarSeriesComponentV2(scalarSeries, plot));
140 }
141 else {
142 qCDebug(LOG_VisualizationGraphHelper())
143 << QObject::tr("Can't create graph plottables : unmanaged data series type");
144 }
145 }
206 }
146 else {
207 else {
147 qCDebug(LOG_VisualizationGraphHelper())
208 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
148 << QObject::tr("Can't create graph plottables : the variable is null");
149 }
209 }
150
151 return result;
152 }
210 }
153
211
154 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
212 } // namespace
155 QCustomPlot &plot) noexcept
156 {
157 auto result = QVector<QCPAbstractPlottable *>{};
158
213
214 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
215 QCustomPlot &plot) noexcept
216 {
159 if (variable) {
217 if (variable) {
160 // Gets the data series of the variable to call the creation of the right components
218 auto helper = createHelper(variable->dataSeries().get());
161 // according to its type
219 auto plottables = helper->create(plot);
162 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) {
220 return plottables;
163 result.append(createScalarSeriesComponent(scalarSeries, plot, variable->range()));
164 }
165 else {
166 qCDebug(LOG_VisualizationGraphHelper())
167 << QObject::tr("Can't create graph plottables : unmanaged data series type");
168 }
169 }
221 }
170 else {
222 else {
171 qCDebug(LOG_VisualizationGraphHelper())
223 qCDebug(LOG_VisualizationGraphHelper())
172 << QObject::tr("Can't create graph plottables : the variable is null");
224 << QObject::tr("Can't create graph plottables : the variable is null");
225 return PlottablesMap{};
173 }
226 }
174
175 return result;
176 }
227 }
177
228
178 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
229 void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries,
179 std::shared_ptr<IDataSeries> dataSeries,
180 const SqpRange &dateTime)
230 const SqpRange &dateTime)
181 {
231 {
182 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
232 auto helper = createHelper(dataSeries);
183 if (plotableVect.size() == 1) {
233 helper->update(plottables, dateTime);
184 updateScalarData(plotableVect.at(0), scalarSeries, dateTime);
185 }
186 else {
187 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
188 "Can't update Data of a scalarSeries because there is not only one component "
189 "associated");
190 }
191 }
192 else {
193 /// @todo DEBUG
194 }
195 }
234 }
General Comments 0
You need to be logged in to leave comments. Login now