From e30c6ce366b759dfb39b781becb837282efa0d0f Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Sun, 17 Jan 2016 16:41:25 +0100 Subject: BarChart: Added color scale support. * Color scale's extents are not changed: the values are supposed to match the values used in the other plots * Temporarily removed the bar outlines in order to properly display colors when the number of bars is equal to or greater than the number of available pixels to draw --- barchart.cpp | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'barchart.cpp') 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(barNode->firstChild()); + updateRectGeometry(barGeomNode->geometry(), x, y, barWidth, barHeight); + barGeomNode->markDirty(QSGNode::DirtyGeometry); + + //QSGGeometryNode *outlineGeomNode = + // static_cast(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(barNode->firstChild()->nextSibling()); + static_cast(barNode->firstChild()); - float y = height() - barHeight; - updateRectGeometry(outlineGeomNode->geometry(), x, y, barWidth, barHeight); - updateRectGeometry(barGeomNode->geometry(), x, y, barWidth, barHeight); + QSGFlatColorMaterial *material = + static_cast(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; } -- cgit v1.2.3