aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--barchart.cpp18
-rw-r--r--barchart.h8
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();
}
diff --git a/barchart.h b/barchart.h
index 164e0eb..ba96470 100644
--- a/barchart.h
+++ b/barchart.h
@@ -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