diff options
Diffstat (limited to 'scatterplot.cpp')
-rw-r--r-- | scatterplot.cpp | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/scatterplot.cpp b/scatterplot.cpp index b5e8261..a3a0dfc 100644 --- a/scatterplot.cpp +++ b/scatterplot.cpp @@ -1,18 +1,22 @@ #include "scatterplot.h" +#include <iostream> + #include <cmath> -#include <cstdio> #include <QSGNode> #include <QSGGeometry> #include <QSGGeometryNode> #include <QSGMaterial> #include <QSGFlatColorMaterial> +#include <QSGSimpleRectNode> -const int GLYPH_SIZE = 4; -const float PADDING = 5; +const int GLYPH_SIZE = 5; +const float PADDING = 10; const float PI = 3.1415f; -Scatterplot::Scatterplot() +Scatterplot::Scatterplot(QQuickItem *parent) + : QQuickItem(parent) + , m_colorScale{QColor("red"), QColor("green"), QColor("blue")} { setFlag(QQuickItem::ItemHasContents); } @@ -23,10 +27,11 @@ Scatterplot::~Scatterplot() void Scatterplot::setData(const arma::mat &data) { - if (data.n_cols != 2) + if (data.n_cols != 3) return; m_data = data; + m_colorScale.setExtents(m_data.col(2).min(), m_data.col(2).max()); update(); } @@ -68,10 +73,10 @@ void updateSquareGeometry(QSGGeometry *geometry, float size, float cx, float cy) QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) { + if (m_data.n_rows < 1) + return 0; + QSGNode *node = 0; - QSGGeometryNode *childNode = 0; - QSGGeometry *geometry = 0; - QSGFlatColorMaterial *material = 0; int vertexCount = calculateCircleVertexCount(GLYPH_SIZE / 2); qreal xmin = m_data.col(0).min(), xmax = m_data.col(0).max(), @@ -82,19 +87,15 @@ QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) if (!oldNode) { node = new QSGNode; for (arma::uword i = 0; i < m_data.n_rows; i++) { - arma::rowvec row = m_data.row(i); - x = (row[0] - xmin) / (xmax - xmin) * width(); - y = (row[1] - ymin) / (ymax - ymin) * height(); - - childNode = new QSGGeometryNode; + QSGGeometryNode *childNode = new QSGGeometryNode; - geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount); + QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount); geometry->setDrawingMode(GL_POLYGON); childNode->setGeometry(geometry); childNode->setFlag(QSGNode::OwnsGeometry); - material = new QSGFlatColorMaterial; - material->setColor(QColor()); + QSGFlatColorMaterial *material = new QSGFlatColorMaterial; + material->setColor(m_colorScale.color(m_data(i, 2))); childNode->setMaterial(material); childNode->setFlag(QSGNode::OwnsMaterial); @@ -104,17 +105,17 @@ QSGNode *Scatterplot::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) node = oldNode; } - childNode = static_cast<QSGGeometryNode *>(node->firstChild()); + QSGNode *childNode = node->firstChild(); for (arma::uword i = 0; i < m_data.n_rows; i++) { arma::rowvec row = m_data.row(i); - x = (row[0] - xmin) / (xmax - xmin) * width(); - y = (row[1] - ymin) / (ymax - ymin) * height(); + x = PADDING + (row[0] - xmin) / (xmax - xmin) * (width() - 2*PADDING); + y = PADDING + (row[1] - ymin) / (ymax - ymin) * (height() - 2*PADDING); - geometry = childNode->geometry(); + QSGGeometry *geometry = static_cast<QSGGeometryNode *>(childNode)->geometry(); updateCircleGeometry(geometry, GLYPH_SIZE, x, y); - childNode = static_cast<QSGGeometryNode *>(childNode->nextSibling()); + childNode->markDirty(QSGNode::DirtyGeometry); + childNode = childNode->nextSibling(); } - node->markDirty(QSGNode::DirtyGeometry); return node; } |