-
Notifications
You must be signed in to change notification settings - Fork 0
/
barChart.cpp
61 lines (57 loc) · 2.13 KB
/
barChart.cpp
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
#include "barChart.h"
BarChart::BarChart(QString _title, Matrix* dataSet, const QVector<int>& numericIndexes, int textIndex) : title(_title)
{
if(textIndex >= 0){
for(unsigned int y = 0; y < dataSet->getDataMatrixHeigth(); y++)
textData.append(static_cast<TextData*>(dataSet->getDataAt(textIndex, y))->getData());
}
for(unsigned int col = 0; col < dataSet->getDataMatrixWidth(); col++){
if(numericIndexes.contains(col)){
QVector<double>* aux = new QVector<double>;
for(unsigned int y = 0; y < dataSet->getDataMatrixHeigth(); y++)
aux->append(static_cast<NumericData*>(dataSet->getDataAt(col,y))->getData());
numericData.append(aux);
columnsTitles.append(dataSet->getTitle(col));
}
}
}
BarChart::~BarChart()
{
for(int x = 0; x < numericData.size(); x++)
delete numericData.at(x);
}
QChart* BarChart::draw()
{
QBarSeries* barSeries = new QBarSeries();
for(int y = 0; y < numericData.at(0)->size(); y++){
QBarSet* bar;
if(!textData.isEmpty())
bar = new QBarSet(textData.at(y));
else
bar = new QBarSet(QString::number(y+1));
for(int x = 0; x < numericData.size(); x++)
bar->append(numericData.at(x)->at(y));
barSeries->append(bar);
}
QChart* chart = new QChart();
chart->addSeries(barSeries);
if(!columnsTitles.isEmpty()){
QStringList categories;
for(int i = 0; i < columnsTitles.size(); i++){
categories.append(columnsTitles.at(i));
}
QBarCategoryAxis *axisX = new QBarCategoryAxis();
axisX->append(categories);
chart->addAxis(axisX, Qt::AlignBottom);
barSeries->attachAxis(axisX);
axisX->setRange(columnsTitles.at(0), columnsTitles.at(columnsTitles.size()-1));
QValueAxis *axisY = new QValueAxis();
chart->addAxis(axisY, Qt::AlignLeft);
barSeries->attachAxis(axisY);
}
else chart->createDefaultAxes();
chart->setTitle(title);
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
return chart;
}