diff options
author | Samuel Fadel <samuelfadel@gmail.com> | 2016-01-13 15:09:20 +0100 |
---|---|---|
committer | Samuel Fadel <samuelfadel@gmail.com> | 2016-01-13 15:09:20 +0100 |
commit | 26c4d3e5974f89fe4c140292d4a9b6c33e42450b (patch) | |
tree | f31d1939faa004fbad908fdf3a319a2fb845ff29 | |
parent | 29d0c06eb3a69cd838e1224f46942389c501f9b8 (diff) |
BarChart: values are now sorted in descreasing order.
-rw-r--r-- | barchart.cpp | 18 | ||||
-rw-r--r-- | barchart.h | 8 |
2 files changed, 21 insertions, 5 deletions
diff --git a/barchart.cpp b/barchart.cpp index a4d9791..2227d97 100644 --- a/barchart.cpp +++ b/barchart.cpp @@ -3,7 +3,6 @@ #include <algorithm> #include "geometry.h" -#include "scale.h" static const QColor OUTLINE_COLOR(0, 0, 0); static const QColor BAR_COLOR(128, 128, 128); @@ -12,6 +11,7 @@ static const float DEFAULT_OPACITY = 0.8f; BarChart::BarChart(QQuickItem *parent) : QQuickItem(parent) , m_shouldUpdateBars(false) + , m_scale(0, 1, 0, 1) { setClip(true); setFlag(QQuickItem::ItemHasContents); @@ -26,6 +26,16 @@ BarChart::~BarChart() void BarChart::setValues(const arma::vec &values) { m_values = values; + m_scale.setDomain(m_values.min(), m_values.max()); + + m_originalIndices.resize(m_values.n_elem); + for (int i = 0; i < m_originalIndices.size(); i++) { + m_originalIndices[i] = i; + } + + std::sort(m_originalIndices.begin(), m_originalIndices.end(), + [this](int i, int j) { return m_values[i] > m_values[j]; }); + m_shouldUpdateBars = true; emit valuesChanged(values); } @@ -82,10 +92,10 @@ void BarChart::updateBars(QSGNode *root) QSGNode *node = root->firstChild(); float x = 0; float barWidth = width() / m_values.n_elem; - LinearScale<float> heightScale(m_values.min(), m_values.max(), 0, height()); - for (auto it = m_values.cbegin(); it != m_values.cend(); it++) { - updateBarNodeGeom(node, x, barWidth, heightScale((float) *it)); + m_scale.setRange(0, height()); + for (auto it = m_originalIndices.cbegin(); it != m_originalIndices.cend(); it++) { + updateBarNodeGeom(node, x, barWidth, m_scale(m_values[*it])); x += barWidth; node = node->nextSibling(); } @@ -1,9 +1,13 @@ #ifndef BARCHART_H #define BARCHART_H +#include <vector> + #include <QtQuick> #include <armadillo> +#include "scale.h" + class BarChart : public QQuickItem { Q_OBJECT @@ -26,9 +30,11 @@ private: QSGNode *newBarNode() const; void updateBarNodeGeom(QSGNode *barNode, float x, float width, float height); void updateBars(QSGNode *root); + bool m_shouldUpdateBars; arma::vec m_values; - bool m_shouldUpdateBars; + std::vector<int> m_originalIndices; + LinearScale<float> m_scale; }; #endif // BARCHART_H |