##// END OF EJS Templates
Sets temp data range...
Alexandre Leroux -
r1038:a7c49d344c7d
parent child
Show More
@@ -1,172 +1,174
1 #include "Visualization/AxisRenderingUtils.h"
1 #include "Visualization/AxisRenderingUtils.h"
2
2
3 #include <Data/ScalarSeries.h>
3 #include <Data/ScalarSeries.h>
4 #include <Data/SpectrogramSeries.h>
4 #include <Data/SpectrogramSeries.h>
5 #include <Data/VectorSeries.h>
5 #include <Data/VectorSeries.h>
6
6
7 #include <Visualization/qcustomplot.h>
7 #include <Visualization/qcustomplot.h>
8
8
9 Q_LOGGING_CATEGORY(LOG_AxisRenderingUtils, "AxisRenderingUtils")
9 Q_LOGGING_CATEGORY(LOG_AxisRenderingUtils, "AxisRenderingUtils")
10
10
11 namespace {
11 namespace {
12
12
13 const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz");
13 const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz");
14
14
15 /// Format for datetimes on a axis
15 /// Format for datetimes on a axis
16 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
16 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
17
17
18 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
18 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
19 /// non-time data
19 /// non-time data
20 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis, QCPAxis::ScaleType scaleType)
20 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis, QCPAxis::ScaleType scaleType)
21 {
21 {
22 if (isTimeAxis) {
22 if (isTimeAxis) {
23 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
23 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
24 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
24 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
25 dateTicker->setDateTimeSpec(Qt::UTC);
25 dateTicker->setDateTimeSpec(Qt::UTC);
26
26
27 return dateTicker;
27 return dateTicker;
28 }
28 }
29 else if (scaleType == QCPAxis::stLogarithmic) {
29 else if (scaleType == QCPAxis::stLogarithmic) {
30 return QSharedPointer<QCPAxisTickerLog>::create();
30 return QSharedPointer<QCPAxisTickerLog>::create();
31 }
31 }
32 else {
32 else {
33 // default ticker
33 // default ticker
34 return QSharedPointer<QCPAxisTicker>::create();
34 return QSharedPointer<QCPAxisTicker>::create();
35 }
35 }
36 }
36 }
37
37
38 /**
38 /**
39 * Sets properties of the axis passed as parameter
39 * Sets properties of the axis passed as parameter
40 * @param axis the axis to set
40 * @param axis the axis to set
41 * @param unit the unit to set for the axis
41 * @param unit the unit to set for the axis
42 * @param scaleType the scale type to set for the axis
42 * @param scaleType the scale type to set for the axis
43 */
43 */
44 void setAxisProperties(QCPAxis &axis, const Unit &unit,
44 void setAxisProperties(QCPAxis &axis, const Unit &unit,
45 QCPAxis::ScaleType scaleType = QCPAxis::stLinear)
45 QCPAxis::ScaleType scaleType = QCPAxis::stLinear)
46 {
46 {
47 // label (unit name)
47 // label (unit name)
48 axis.setLabel(unit.m_Name);
48 axis.setLabel(unit.m_Name);
49
49
50 // scale type
50 // scale type
51 axis.setScaleType(scaleType);
51 axis.setScaleType(scaleType);
52 if (scaleType == QCPAxis::stLogarithmic) {
52 if (scaleType == QCPAxis::stLogarithmic) {
53 // Scientific notation
53 // Scientific notation
54 axis.setNumberPrecision(0);
54 axis.setNumberPrecision(0);
55 axis.setNumberFormat("eb");
55 axis.setNumberFormat("eb");
56 }
56 }
57
57
58 // ticker (depending on the type of unit)
58 // ticker (depending on the type of unit)
59 axis.setTicker(axisTicker(unit.m_TimeUnit, scaleType));
59 axis.setTicker(axisTicker(unit.m_TimeUnit, scaleType));
60 }
60 }
61
61
62 /**
62 /**
63 * Delegate used to set axes properties
63 * Delegate used to set axes properties
64 */
64 */
65 template <typename T, typename Enabled = void>
65 template <typename T, typename Enabled = void>
66 struct AxisSetter {
66 struct AxisSetter {
67 static void setProperties(T &, QCustomPlot &, QCPColorScale &)
67 static void setProperties(T &, QCustomPlot &, QCPColorScale &)
68 {
68 {
69 // Default implementation does nothing
69 // Default implementation does nothing
70 qCCritical(LOG_AxisRenderingUtils()) << "Can't set axis properties: unmanaged type of data";
70 qCCritical(LOG_AxisRenderingUtils()) << "Can't set axis properties: unmanaged type of data";
71 }
71 }
72 };
72 };
73
73
74 /**
74 /**
75 * Specialization of AxisSetter for scalars and vectors
75 * Specialization of AxisSetter for scalars and vectors
76 * @sa ScalarSeries
76 * @sa ScalarSeries
77 * @sa VectorSeries
77 * @sa VectorSeries
78 */
78 */
79 template <typename T>
79 template <typename T>
80 struct AxisSetter<T, typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
80 struct AxisSetter<T, typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
81 or std::is_base_of<VectorSeries, T>::value> > {
81 or std::is_base_of<VectorSeries, T>::value> > {
82 static void setProperties(T &dataSeries, QCustomPlot &plot, QCPColorScale &)
82 static void setProperties(T &dataSeries, QCustomPlot &plot, QCPColorScale &)
83 {
83 {
84 dataSeries.lockRead();
84 dataSeries.lockRead();
85 auto xAxisUnit = dataSeries.xAxisUnit();
85 auto xAxisUnit = dataSeries.xAxisUnit();
86 auto valuesUnit = dataSeries.valuesUnit();
86 auto valuesUnit = dataSeries.valuesUnit();
87 dataSeries.unlock();
87 dataSeries.unlock();
88
88
89 setAxisProperties(*plot.xAxis, xAxisUnit);
89 setAxisProperties(*plot.xAxis, xAxisUnit);
90 setAxisProperties(*plot.yAxis, valuesUnit);
90 setAxisProperties(*plot.yAxis, valuesUnit);
91 }
91 }
92 };
92 };
93
93
94 /**
94 /**
95 * Specialization of AxisSetter for spectrograms
95 * Specialization of AxisSetter for spectrograms
96 * @sa SpectrogramSeries
96 * @sa SpectrogramSeries
97 */
97 */
98 template <typename T>
98 template <typename T>
99 struct AxisSetter<T, typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
99 struct AxisSetter<T, typename std::enable_if_t<std::is_base_of<SpectrogramSeries, T>::value> > {
100 static void setProperties(T &dataSeries, QCustomPlot &plot, QCPColorScale &colorScale)
100 static void setProperties(T &dataSeries, QCustomPlot &plot, QCPColorScale &colorScale)
101 {
101 {
102 dataSeries.lockRead();
102 dataSeries.lockRead();
103 auto xAxisUnit = dataSeries.xAxisUnit();
103 auto xAxisUnit = dataSeries.xAxisUnit();
104 auto yAxisUnit = dataSeries.yAxisUnit();
104 auto yAxisUnit = dataSeries.yAxisUnit();
105 auto valuesUnit = dataSeries.valuesUnit();
105 auto valuesUnit = dataSeries.valuesUnit();
106 dataSeries.unlock();
106 dataSeries.unlock();
107
107
108 setAxisProperties(*plot.xAxis, xAxisUnit);
108 setAxisProperties(*plot.xAxis, xAxisUnit);
109 setAxisProperties(*plot.yAxis, yAxisUnit, QCPAxis::stLogarithmic);
109 setAxisProperties(*plot.yAxis, yAxisUnit, QCPAxis::stLogarithmic);
110
110
111 // Displays color scale in plot
111 // Displays color scale in plot
112 plot.plotLayout()->insertRow(0);
112 plot.plotLayout()->insertRow(0);
113 plot.plotLayout()->addElement(0, 0, &colorScale);
113 plot.plotLayout()->addElement(0, 0, &colorScale);
114 colorScale.setType(QCPAxis::atTop);
114 colorScale.setType(QCPAxis::atTop);
115 colorScale.setMinimumMargins(QMargins{0, 0, 0, 0});
115 colorScale.setMinimumMargins(QMargins{0, 0, 0, 0});
116
116
117 // Aligns color scale with axes
117 // Aligns color scale with axes
118 auto marginGroups = plot.axisRect()->marginGroups();
118 auto marginGroups = plot.axisRect()->marginGroups();
119 for (auto it = marginGroups.begin(), end = marginGroups.end(); it != end; ++it) {
119 for (auto it = marginGroups.begin(), end = marginGroups.end(); it != end; ++it) {
120 colorScale.setMarginGroup(it.key(), it.value());
120 colorScale.setMarginGroup(it.key(), it.value());
121 }
121 }
122
122
123 // Set color scale properties
123 // Set color scale properties
124 setAxisProperties(*colorScale.axis(), valuesUnit, QCPAxis::stLogarithmic);
124 setAxisProperties(*colorScale.axis(), valuesUnit, QCPAxis::stLogarithmic);
125 /// @todo ALX: temp data range, remove it when widget to set data range is implemented
126 colorScale.setDataRange(QCPRange{8.32e2, 1.77e7});
125 }
127 }
126 };
128 };
127
129
128 /**
130 /**
129 * Default implementation of IAxisHelper, which takes data series to set axes properties
131 * Default implementation of IAxisHelper, which takes data series to set axes properties
130 * @tparam T the data series' type
132 * @tparam T the data series' type
131 */
133 */
132 template <typename T>
134 template <typename T>
133 struct AxisHelper : public IAxisHelper {
135 struct AxisHelper : public IAxisHelper {
134 explicit AxisHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
136 explicit AxisHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
135
137
136 void setProperties(QCustomPlot &plot, QCPColorScale &colorScale) override
138 void setProperties(QCustomPlot &plot, QCPColorScale &colorScale) override
137 {
139 {
138 AxisSetter<T>::setProperties(m_DataSeries, plot, colorScale);
140 AxisSetter<T>::setProperties(m_DataSeries, plot, colorScale);
139 }
141 }
140
142
141 T &m_DataSeries;
143 T &m_DataSeries;
142 };
144 };
143
145
144 } // namespace
146 } // namespace
145
147
146 QString formatValue(double value, const QCPAxis &axis)
148 QString formatValue(double value, const QCPAxis &axis)
147 {
149 {
148 // If the axis is a time axis, formats the value as a date
150 // If the axis is a time axis, formats the value as a date
149 if (auto axisTicker = qSharedPointerDynamicCast<QCPAxisTickerDateTime>(axis.ticker())) {
151 if (auto axisTicker = qSharedPointerDynamicCast<QCPAxisTickerDateTime>(axis.ticker())) {
150 return DateUtils::dateTime(value, axisTicker->dateTimeSpec()).toString(DATETIME_FORMAT);
152 return DateUtils::dateTime(value, axisTicker->dateTimeSpec()).toString(DATETIME_FORMAT);
151 }
153 }
152 else {
154 else {
153 return QString::number(value);
155 return QString::number(value);
154 }
156 }
155 }
157 }
156
158
157 std::unique_ptr<IAxisHelper>
159 std::unique_ptr<IAxisHelper>
158 IAxisHelperFactory::create(std::shared_ptr<IDataSeries> dataSeries) noexcept
160 IAxisHelperFactory::create(std::shared_ptr<IDataSeries> dataSeries) noexcept
159 {
161 {
160 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
162 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
161 return std::make_unique<AxisHelper<ScalarSeries> >(*scalarSeries);
163 return std::make_unique<AxisHelper<ScalarSeries> >(*scalarSeries);
162 }
164 }
163 else if (auto spectrogramSeries = std::dynamic_pointer_cast<SpectrogramSeries>(dataSeries)) {
165 else if (auto spectrogramSeries = std::dynamic_pointer_cast<SpectrogramSeries>(dataSeries)) {
164 return std::make_unique<AxisHelper<SpectrogramSeries> >(*spectrogramSeries);
166 return std::make_unique<AxisHelper<SpectrogramSeries> >(*spectrogramSeries);
165 }
167 }
166 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
168 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
167 return std::make_unique<AxisHelper<VectorSeries> >(*vectorSeries);
169 return std::make_unique<AxisHelper<VectorSeries> >(*vectorSeries);
168 }
170 }
169 else {
171 else {
170 return std::make_unique<AxisHelper<IDataSeries> >(*dataSeries);
172 return std::make_unique<AxisHelper<IDataSeries> >(*dataSeries);
171 }
173 }
172 }
174 }
General Comments 0
You need to be logged in to leave comments. Login now