diff options
author | Samuel Fadel <samuelfadel@gmail.com> | 2016-01-17 16:41:25 +0100 |
---|---|---|
committer | Samuel Fadel <samuelfadel@gmail.com> | 2016-01-17 16:41:25 +0100 |
commit | e30c6ce366b759dfb39b781becb837282efa0d0f (patch) | |
tree | d95750f662835bf2c628ccd2da5f4251b41a78b4 /barchart.cpp | |
parent | a9236429e5691159f1ddc017b28ee0c060e0092d (diff) |
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
Diffstat (limited to 'barchart.cpp')
-rw-r--r-- | barchart.cpp | 41 |
1 files changed, 33 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; } |