summaryrefslogtreecommitdiff
path: root/src/charts/ChartDataLine.cpp
blob: 17919cdded03bf75546769480cb741a1734f89e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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));
}