##// END OF EJS Templates
Updates VisualizationGraphHelper to handle vectors
Alexandre Leroux -
r583: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 224 * @return the data of a component
223 225 * @param componentIndex the index of the component to retrieve the data
@@ -2,6 +2,7
2 2 #include "Visualization/qcustomplot.h"
3 3
4 4 #include <Data/ScalarSeries.h>
5 #include <Data/VectorSeries.h>
5 6
6 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 const SqpRange &range)
39 /// Sets axes properties according to the properties of a data series
40 template <int Dim>
41 void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept
40 42 {
41 qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData"
42 << QThread::currentThread()->objectName();
43 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
44 scalarSeries->lockRead();
45 {
46 auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create();
47 qcpGraph->setData(sqpDataContainer);
48 auto bounds = scalarSeries->subData(range.m_TStart, range.m_TEnd);
49 for (auto it = bounds.first; it != bounds.second; ++it) {
50 sqpDataContainer->appendGraphData(QCPGraphData(it->x(), it->value()));
51 }
52
53 qCInfo(LOG_VisualizationGraphHelper()) << "TODEBUG: Current points displayed"
54 << sqpDataContainer->size();
55 }
56 scalarSeries->unlock();
57
43 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
44 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
45 auto setAxisProperties = [](auto axis, const auto &unit) {
46 // label (unit name)
47 axis->setLabel(unit.m_Name);
48
49 // ticker (depending on the type of unit)
50 axis->setTicker(axisTicker(unit.m_TimeUnit));
51 };
52 setAxisProperties(plot.xAxis, dataSeries.xAxisUnit());
53 setAxisProperties(plot.yAxis, dataSeries.valuesUnit());
54 }
58 55
59 // Display all data
60 component->parentPlot()->replot();
61 }
62 else {
63 /// @todo DEBUG
56 /**
57 * Struct used to create plottables, depending on the type of the data series from which to create them
58 * @tparam T the data series' type
59 * @remarks Default implementation can't create plottables
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,
68 QCustomPlot &plot)
69 {
70 auto component = plot.addGraph();
71 /**
72 * Specialization of PlottablesCreator for scalars and vectors
73 * @sa ScalarSeries
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 94 // Axes properties
74 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
75 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
76
77 auto setAxisProperties = [](auto axis, const auto &unit) {
78 // label (unit name)
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());
95 setAxesProperties(dataSeries, plot);
96
97 plot.replot();
98
99 return result;
86 100 }
87 return component;
88 }
101 };
89 102
90 QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr<ScalarSeries> scalarSeries,
91 QCustomPlot &plot, const SqpRange &dateTime)
92 {
93 auto component = plot.addGraph();
103 /**
104 * Struct used to update plottables, depending on the type of the data series from which to update them
105 * @tparam T the data series' type
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) {
96 // // Graph data
97 component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(),
98 true);
117 /**
118 * Specialization of PlottablesUpdater for scalars and vectors
119 * @sa ScalarSeries
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
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
153 dataSeries.unlock();
105 154
106 auto setAxisProperties = [](auto axis, const auto &unit) {
107 // label (unit name)
108 axis->setLabel(unit.m_Name);
155 if (!plottables.empty()) {
156 auto plot = plottables.begin()->second->parentPlot();
109 157
110 // ticker (depending on the type of unit)
111 axis->setTicker(axisTicker(unit.m_TimeUnit));
112 };
113 setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit());
114 setAxisProperties(plot.yAxis, scalarSeries->valuesUnit());
158 if (rescaleAxes) {
159 plot->rescaleAxes();
160 }
115 161
116 // Display all data
117 component->rescaleAxes();
118 plot.replot();
162 plot->replot();
163 }
119 164 }
120 else {
121 qCDebug(LOG_VisualizationGraphHelper())
122 << QObject::tr("Can't create graph for the scalar series");
165 };
166
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;
126 }
190 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
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 *>
131 VisualizationGraphHelper::createV2(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept
198 /// Creates IPlottablesHelper according to a data series
199 std::unique_ptr<IPlottablesHelper> createHelper(IDataSeries *dataSeries) noexcept
132 200 {
133 auto result = QVector<QCPAbstractPlottable *>{};
134
135 if (variable) {
136 // Gets the data series of the variable to call the creation of the right components
137 // according to its type
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 }
201 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
202 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
203 }
204 else if (auto vectorSeries = dynamic_cast<VectorSeries *>(dataSeries)) {
205 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
145 206 }
146 207 else {
147 qCDebug(LOG_VisualizationGraphHelper())
148 << QObject::tr("Can't create graph plottables : the variable is null");
208 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
149 209 }
150
151 return result;
152 210 }
153 211
154 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
155 QCustomPlot &plot) noexcept
156 {
157 auto result = QVector<QCPAbstractPlottable *>{};
212 } // namespace
158 213
214 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
215 QCustomPlot &plot) noexcept
216 {
159 217 if (variable) {
160 // Gets the data series of the variable to call the creation of the right components
161 // according to its type
162 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) {
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 }
218 auto helper = createHelper(variable->dataSeries().get());
219 auto plottables = helper->create(plot);
220 return plottables;
169 221 }
170 222 else {
171 223 qCDebug(LOG_VisualizationGraphHelper())
172 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,
179 std::shared_ptr<IDataSeries> dataSeries,
229 void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries,
180 230 const SqpRange &dateTime)
181 231 {
182 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
183 if (plotableVect.size() == 1) {
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 }
232 auto helper = createHelper(dataSeries);
233 helper->update(plottables, dateTime);
195 234 }
General Comments 0
You need to be logged in to leave comments. Login now