-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineChart.cpp
More file actions
79 lines (67 loc) · 2.01 KB
/
LineChart.cpp
File metadata and controls
79 lines (67 loc) · 2.01 KB
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
#include "LineChart.h"
#include<QPoint>
#include<QTableView>
lineChart::lineChart(chartModel * chart) : chartDrawable(chart)
{
axisX = new QCategoryAxis(this);
axisY = new QValueAxis(this);
axisX->setLabelsAngle(25);
axisX->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);
legend()->setVisible(true);
legend()->setAlignment(Qt::AlignBottom);
}
void lineChart::buildXaxis(){
delete axisX;
axisX = new QCategoryAxis(this);
QStringList cats;
int i = 0;
while(i< getModel()->columnCount()){
axisX->append(getModel()->headerData(i,Qt::Horizontal).toString(),1*i);
i++;
}
axisX->setLabelsPosition(QCategoryAxis::AxisLabelsPositionOnValue);
axisX->setLabelsAngle(25);
axisX->setRange(0,i -1);
axisX->setStartValue(0);
}
lineChart::~lineChart(){
removeAllSeries();
delete axisX;
delete axisY;
}
void lineChart::updateSeries()
{
removeAllSeries();
for(int i = 0; i < getModel()->rowCount();i++)
{
QLineSeries * canvasSeries = new QLineSeries(this);
canvasSeries->setName(getModel()->headerData(i,Qt::Vertical).toString());
for(int j = 0;j < getModel()->columnCount();j++){
*canvasSeries << QPoint(j,getModel()->data(getModel()->index(i,j)).toDouble());
}
addSeries(canvasSeries);
canvasSeries->attachAxis(axisY);
canvasSeries->attachAxis(axisX);
}
}
void lineChart::draw(){
double max = 0;
double min = 0;
if(getModel()->columnCount() != 0 && getModel()->rowCount() != 0){
for(int i = 0; i < getModel()->rowCount();i++)
{for(int j = 0;j < getModel()->columnCount();j++){
double aux = getModel()->data(getModel()->index(i,j)).value<double>();
if(aux > max)
max = aux;
if(aux< min)
min = aux;
}
}
}
buildXaxis();
axisY->setRange(min,max);
updateTitle();
addAxis(axisX,Qt::AlignBottom);
addAxis(axisY,Qt::AlignLeft);
updateSeries();
}