aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--barchart.cpp41
-rw-r--r--barchart.h5
-rw-r--r--main.cpp1
3 files changed, 39 insertions, 8 deletions
diff --git a/barchart.cpp b/barchart.cpp
index 3c8e45d..ad20c63 100644
--- a/barchart.cpp
+++ b/barchart.cpp
@@ -11,6 +11,7 @@ static const float DEFAULT_OPACITY = 0.8f;
BarChart::BarChart(QQuickItem *parent)
: QQuickItem(parent)
, m_shouldUpdateBars(false)
+ , m_colorScale(0)
, m_scale(0, 1, 0, 1)
{
setClip(true);
@@ -38,9 +39,18 @@ void BarChart::setValues(const arma::vec &values)
std::sort(m_originalIndices.begin(), m_originalIndices.end(),
[this](int i, int j) { return m_values[i] > m_values[j]; });
}
+ emit valuesChanged(values);
m_shouldUpdateBars = true;
- emit valuesChanged(values);
+}
+
+void BarChart::setColorScale(const ColorScale *scale)
+{
+ m_colorScale = scale;
+ emit colorScaleChanged(m_colorScale);
+
+ m_shouldUpdateBars = true;
+ update();
}
QSGNode *BarChart::newBarNode() const
@@ -73,21 +83,35 @@ QSGNode *BarChart::newBarNode() const
QSGOpacityNode *opacityNode = new QSGOpacityNode;
opacityNode->setOpacity(DEFAULT_OPACITY);
opacityNode->appendChildNode(barGeomNode);
- opacityNode->appendChildNode(outlineGeomNode);
+ //opacityNode->appendChildNode(outlineGeomNode);
return opacityNode;
}
void BarChart::updateBarNodeGeom(QSGNode *barNode, float x, float barWidth, float barHeight)
{
- QSGGeometryNode *outlineGeomNode =
+ float y = height() - barHeight;
+
+ QSGGeometryNode *barGeomNode =
static_cast<QSGGeometryNode *>(barNode->firstChild());
+ updateRectGeometry(barGeomNode->geometry(), x, y, barWidth, barHeight);
+ barGeomNode->markDirty(QSGNode::DirtyGeometry);
+
+ //QSGGeometryNode *outlineGeomNode =
+ // static_cast<QSGGeometryNode *>(barGeomNode->nextSibling());
+ //updateRectGeometry(outlineGeomNode->geometry(), x, y, barWidth, barHeight);
+ //outlineGeomNode->markDirty(QSGNode::DirtyGeometry);
+}
+
+void BarChart::updateBarNodeColor(QSGNode *barNode, const QColor &color)
+{
QSGGeometryNode *barGeomNode =
- static_cast<QSGGeometryNode *>(barNode->firstChild()->nextSibling());
+ static_cast<QSGGeometryNode *>(barNode->firstChild());
- float y = height() - barHeight;
- updateRectGeometry(outlineGeomNode->geometry(), x, y, barWidth, barHeight);
- updateRectGeometry(barGeomNode->geometry(), x, y, barWidth, barHeight);
+ QSGFlatColorMaterial *material =
+ static_cast<QSGFlatColorMaterial *>(barGeomNode->material());
+ material->setColor(color);
+ barGeomNode->markDirty(QSGNode::DirtyMaterial);
}
void BarChart::updateBars(QSGNode *root)
@@ -99,6 +123,7 @@ void BarChart::updateBars(QSGNode *root)
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]));
+ updateBarNodeColor(node, m_colorScale->color(m_values[*it]));
x += barWidth;
node = node->nextSibling();
}
@@ -120,7 +145,7 @@ QSGNode *BarChart::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
root->removeChildNode(root->firstChild());
}
- // Then, update the geometry of bars to reflect the values
+ // Then, update the bars to reflect the values
updateBars(root);
m_shouldUpdateBars = false;
}
diff --git a/barchart.h b/barchart.h
index 933c004..2d37419 100644
--- a/barchart.h
+++ b/barchart.h
@@ -7,6 +7,7 @@
#include <armadillo>
+#include "colorscale.h"
#include "scale.h"
class BarChart
@@ -19,9 +20,11 @@ public:
signals:
void valuesChanged(const arma::vec &values) const;
+ void colorScaleChanged(const ColorScale *scale) const;
public slots:
void setValues(const arma::vec &values);
+ void setColorScale(const ColorScale *scale);
protected:
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *);
@@ -31,10 +34,12 @@ protected:
private:
QSGNode *newBarNode() const;
void updateBarNodeGeom(QSGNode *barNode, float x, float width, float height);
+ void updateBarNodeColor(QSGNode *barNode, const QColor &color);
void updateBars(QSGNode *root);
bool m_shouldUpdateBars;
arma::vec m_values;
+ const ColorScale *m_colorScale;
std::vector<int> m_originalIndices;
LinearScale<float> m_scale;
};
diff --git a/main.cpp b/main.cpp
index 402ecef..d2eb5f6 100644
--- a/main.cpp
+++ b/main.cpp
@@ -179,6 +179,7 @@ int main(int argc, char **argv)
cpPlot, SLOT(setScale(const LinearScale<float> &, const LinearScale<float> &)));
BarChart *barChart = engine.rootObjects()[0]->findChild<BarChart *>("barChart");
+ barChart->setColorScale(&colorScale);
barChart->setValues(labels);
//history->addHistoryItem(Ys);