diff options
author | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
---|---|---|
committer | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
commit | d24f813f3f2a05c112e803e4256b53535895fc98 (patch) | |
tree | 601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/charts/ChartDataLine.cpp |
Diffstat (limited to 'src/charts/ChartDataLine.cpp')
-rw-r--r-- | src/charts/ChartDataLine.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/charts/ChartDataLine.cpp b/src/charts/ChartDataLine.cpp new file mode 100644 index 0000000..17919cd --- /dev/null +++ b/src/charts/ChartDataLine.cpp @@ -0,0 +1,80 @@ +#include "ChartDataLine.h" +#include "ChartScene.h" +#include "ChartMarker.h" + +const QColor ChartDataLine::MarkerColor(Qt::blue); +const QColor ChartDataLine::FillColor(0, 0, 255, 128); + +ChartDataLine::ChartDataLine(ChartScene* scene): + scene(scene), + origin(getDataSetOrigin()), + xSpacing(scene->getXTickSpacing()) +{ +} + +QPointF ChartDataLine::getDataSetOrigin() const +{ + QSizeF padding = scene->DataSetPadding; + QPointF res = scene->getChartOrigin(); + res.ry() -= padding.height(); + if(scene->getDataDirection() == 1) + res.rx() += padding.width(); + else + res.rx() += scene->getChartRect().width(); + return res; +} + +void ChartDataLine::paint() +{ + QPainterPath linePath = createLinePath(); + addLinePath(linePath); + addAreaPath(linePath); +} + +QPainterPath ChartDataLine::createLinePath() +{ + QList<DataPoint> dataSet = scene->getDataSet(); + QPointF firstPoint = getDataPos(0, dataSet.first().value); + QPainterPath linePath(firstPoint); + for (int i = 0; i < dataSet.size(); i++) + addLineSegment(linePath, i); + return linePath; +} + +void ChartDataLine::addLineSegment(QPainterPath& path, int i) +{ + DataPoint dataPoint = scene->getDataSet().at(i); + QPointF pos = getDataPos(i, dataPoint.value); + QString toolTipText = createToolTipText(dataPoint); + scene->addItem(new ChartMarker(pos, toolTipText)); + path.lineTo(pos); +} + +QPointF ChartDataLine::getDataPos(int index, int yValue) +{ + qreal xPos = scene->getDataDirection() * index * xSpacing; + qreal yPos = scene->getYPosFromValue(yValue - scene->getMinY()); + return origin + QPointF(xPos, -yPos); +} + +QString ChartDataLine::createToolTipText(const DataPoint& dataPoint) const +{ + return scene->getXLabel() + ": <b>" + dataPoint.toolTipLabel + "</b><br/>" + + scene->getYLabel() + ": <b>" + QString::number(dataPoint.value) + "</b>"; +} + +void ChartDataLine::addLinePath(const QPainterPath& path) +{ + QPen pen(MarkerColor); + pen.setWidth(2); + scene->addPath(path, pen); +} + +void ChartDataLine::addAreaPath(const QPainterPath& linePath) +{ + QPainterPath areaPath(origin); + areaPath.lineTo(QPointF(linePath.elementAt(0))); + areaPath.connectPath(linePath); + areaPath.lineTo(QPointF(linePath.currentPosition().x(), origin.y())); + scene->addPath(areaPath, QPen(FillColor), QBrush(FillColor)); +} |