From 2b8825a4f27b385ff7738393bacfe5852b1b0af0 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Mon, 18 Jan 2016 11:26:40 +0100 Subject: BarChart: updated to scale properly when component is resized. --- barchart.cpp | 25 ++++++++++++++++++++----- barchart.h | 3 +++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/barchart.cpp b/barchart.cpp index ad31fb9..941d9e8 100644 --- a/barchart.cpp +++ b/barchart.cpp @@ -53,6 +53,12 @@ void BarChart::setColorScale(const ColorScale *scale) update(); } +QSGNode *BarChart::newSceneGraph() const +{ + QSGTransformNode *root = new QSGTransformNode; + return root; +} + QSGNode *BarChart::newBarNode() const { // A bar node is: @@ -88,9 +94,17 @@ QSGNode *BarChart::newBarNode() const return opacityNode; } +void BarChart::updateViewport(QSGNode *root) const +{ + QSGTransformNode *viewportNode = static_cast(root); + QMatrix4x4 viewport; + viewport.scale(width(), height()); + viewportNode->setMatrix(viewport); +} + void BarChart::updateBarNodeGeom(QSGNode *barNode, float x, float barWidth, float barHeight) { - float y = height() - barHeight; + float y = 1.0f - barHeight; QSGGeometryNode *barGeomNode = static_cast(barNode->firstChild()); @@ -116,11 +130,10 @@ void BarChart::updateBarNodeColor(QSGNode *barNode, const QColor &color) void BarChart::updateBars(QSGNode *root) { + float barWidth = 1.0f / m_values.n_elem; + QSGNode *node = root->firstChild(); float x = 0; - float barWidth = width() / m_values.n_elem; - - 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])); @@ -131,7 +144,9 @@ void BarChart::updateBars(QSGNode *root) QSGNode *BarChart::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) { - QSGNode *root = oldNode ? oldNode : new QSGNode; + QSGNode *root = oldNode ? oldNode : newSceneGraph(); + updateViewport(root); + if (m_shouldUpdateBars) { int numValues = (int) m_values.n_elem; // First, make sure we have the same number of values & bars diff --git a/barchart.h b/barchart.h index 2d37419..e0edc0a 100644 --- a/barchart.h +++ b/barchart.h @@ -32,7 +32,10 @@ protected: void mousePressEvent(QMouseEvent *event); private: + QSGNode *newSceneGraph() const; QSGNode *newBarNode() const; + + void updateViewport(QSGNode *root) const; void updateBarNodeGeom(QSGNode *barNode, float x, float width, float height); void updateBarNodeColor(QSGNode *barNode, const QColor &color); void updateBars(QSGNode *root); -- cgit v1.2.3