##// END OF EJS Templates
Rename QLineChartSeries to QLineSeries
Michal Klocek -
r349:7594bc4927de
parent child
Show More
@@ -1,31 +1,31
1 !include(common.pri) {
1 !include(common.pri) {
2 error('missing common.pri')
2 error('missing common.pri')
3 }
3 }
4
4
5 TEMPLATE = subdirs
5 TEMPLATE = subdirs
6 SUBDIRS += src example test qmlplugin
6 SUBDIRS += src example test qmlplugin
7
7
8 integrated_build:{
8 integrated_build:{
9 message('Configured for integrated build')
9 message('Configured for integrated build')
10 } else {
10 } else {
11 message('Please build example test and qmlplugin after installing library.')
11 message('Please build example test and qmlplugin after installing library.')
12 }
12 }
13
13
14 CONFIG += ordered
14 CONFIG += ordered
15 QMAKE_CXXFLAGS += -g -Wall
15 QMAKE_CXXFLAGS += -g -Wall
16 unix:QMAKE_DISTCLEAN += -r build bin doc/html
16 unix:QMAKE_DISTCLEAN += -r build bin include lib doc/html
17 win32:QMAKE_DISTCLEAN += /Q /s build bin doc\\html
17 win32:QMAKE_DISTCLEAN += /Q /s build bin include lib doc\\html
18
18
19 # install feature file
19 # install feature file
20 feature.path = $$[QT_INSTALL_DATA]/mkspecs/features
20 feature.path = $$[QT_INSTALL_DATA]/mkspecs/features
21 feature.files = $$PWD/features/qtcommercialchart.prf
21 feature.files = $$PWD/features/qtcommercialchart.prf
22 INSTALLS += feature
22 INSTALLS += feature
23
23
24 doc.target = doc
24 doc.target = doc
25 win32:{
25 win32:{
26 doc.commands = qdoc3 $$CHART_BUILD_DOC_DIR\\qcharts.qdocconf
26 doc.commands = qdoc3 $$CHART_BUILD_DOC_DIR\\qcharts.qdocconf
27 }else{
27 }else{
28 doc.commands = qdoc3 $$CHART_BUILD_DOC_DIR/qcharts.qdocconf
28 doc.commands = qdoc3 $$CHART_BUILD_DOC_DIR/qcharts.qdocconf
29 }
29 }
30 doc.depends = FORCE
30 doc.depends = FORCE
31 QMAKE_EXTRA_TARGETS += doc
31 QMAKE_EXTRA_TARGETS += doc
@@ -1,67 +1,67
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qlinechartseries.h>
4 #include <qlineseries.h>
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <qchartaxis.h>
6 #include <qchartaxis.h>
7 #include <cmath>
7 #include <cmath>
8
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
10
11 #define PI 3.14159265358979
11 #define PI 3.14159265358979
12
12
13 int main(int argc, char *argv[])
13 int main(int argc, char *argv[])
14 {
14 {
15 QApplication a(argc, argv);
15 QApplication a(argc, argv);
16
16
17 QMainWindow window;
17 QMainWindow window;
18
18
19 QLineChartSeries* series0 = new QLineChartSeries();
19 QLineSeries* series0 = new QLineSeries();
20 QPen blue(Qt::blue);
20 QPen blue(Qt::blue);
21 blue.setWidth(3);
21 blue.setWidth(3);
22 series0->setPen(blue);
22 series0->setPen(blue);
23 QLineChartSeries* series1 = new QLineChartSeries();
23 QLineSeries* series1 = new QLineSeries();
24 QPen red(Qt::red);
24 QPen red(Qt::red);
25 red.setWidth(3);
25 red.setWidth(3);
26 series1->setPen(red);
26 series1->setPen(red);
27
27
28 int numPoints = 100;
28 int numPoints = 100;
29
29
30 for (int x = 0; x <= numPoints; ++x) {
30 for (int x = 0; x <= numPoints; ++x) {
31 series0->add(x, fabs(sin(PI/50*x)*100));
31 series0->add(x, fabs(sin(PI/50*x)*100));
32 series1->add(x, fabs(cos(PI/50*x)*100));
32 series1->add(x, fabs(cos(PI/50*x)*100));
33 }
33 }
34
34
35 QChartView* chartView = new QChartView(&window);
35 QChartView* chartView = new QChartView(&window);
36
36
37 chartView->setRenderHint(QPainter::Antialiasing);
37 chartView->setRenderHint(QPainter::Antialiasing);
38 chartView->setChartTitle("This is custom axis chart example");
38 chartView->setChartTitle("This is custom axis chart example");
39 chartView->addSeries(series0);
39 chartView->addSeries(series0);
40 chartView->addSeries(series1);
40 chartView->addSeries(series1);
41
41
42 QLinearGradient backgroundGradient;
42 QLinearGradient backgroundGradient;
43 backgroundGradient.setColorAt(0.0, Qt::white);
43 backgroundGradient.setColorAt(0.0, Qt::white);
44 backgroundGradient.setColorAt(1.0, QRgb(0xffff80));
44 backgroundGradient.setColorAt(1.0, QRgb(0xffff80));
45 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
45 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
46 chartView->setChartBackgroundBrush(backgroundGradient);
46 chartView->setChartBackgroundBrush(backgroundGradient);
47
47
48 QChartAxis* axisX = chartView->axisX();
48 QChartAxis* axisX = chartView->axisX();
49 axisX->setLabelsAngle(45);
49 axisX->setLabelsAngle(45);
50 axisX->setGridPen(Qt::DashLine);
50 axisX->setGridPen(Qt::DashLine);
51 axisX->addAxisTickLabel(0,"low");
51 axisX->addAxisTickLabel(0,"low");
52 axisX->addAxisTickLabel(50,"medium");
52 axisX->addAxisTickLabel(50,"medium");
53 axisX->addAxisTickLabel(100,"High");
53 axisX->addAxisTickLabel(100,"High");
54
54
55 QChartAxis* axisY = chartView->axisY();
55 QChartAxis* axisY = chartView->axisY();
56 axisY->setLabelsAngle(45);
56 axisY->setLabelsAngle(45);
57 axisY->setShadesBrush(Qt::yellow);
57 axisY->setShadesBrush(Qt::yellow);
58 axisY->addAxisTickLabel(0,"low");
58 axisY->addAxisTickLabel(0,"low");
59 axisY->addAxisTickLabel(50,"medium");
59 axisY->addAxisTickLabel(50,"medium");
60 axisY->addAxisTickLabel(100,"High");
60 axisY->addAxisTickLabel(100,"High");
61
61
62 window.setCentralWidget(chartView);
62 window.setCentralWidget(chartView);
63 window.resize(400, 300);
63 window.resize(400, 300);
64 window.show();
64 window.show();
65
65
66 return a.exec();
66 return a.exec();
67 }
67 }
@@ -1,83 +1,83
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qlinechartseries.h>
5 #include <qlineseries.h>
6 #include <qscatterseries.h>
6 #include <qscatterseries.h>
7 #include <qbarseries.h>
7 #include <qbarseries.h>
8 #include <qbarset.h>
8 #include <qbarset.h>
9 #include <qbarcategory.h>
9 #include <qbarcategory.h>
10 #include <qpieseries.h>
10 #include <qpieseries.h>
11
11
12 QTCOMMERCIALCHART_USE_NAMESPACE
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
13
14 int main(int argc, char *argv[])
14 int main(int argc, char *argv[])
15 {
15 {
16 QApplication a(argc, argv);
16 QApplication a(argc, argv);
17
17
18 //! [1]
18 //! [1]
19 // Create chart view
19 // Create chart view
20 QChartView *chartView = new QChartView();
20 QChartView *chartView = new QChartView();
21 chartView->setRenderHint(QPainter::Antialiasing);
21 chartView->setRenderHint(QPainter::Antialiasing);
22 chartView->setChartTitle("Simple Line Chart");
22 chartView->setChartTitle("Simple Line Chart");
23 // Add series to the chart
23 // Add series to the chart
24 QLineChartSeries *line = new QLineChartSeries();
24 QLineSeries *line = new QLineSeries();
25 line->add(0.0, 0.8);
25 line->add(0.0, 0.8);
26 line->add(1.1, 1.1);
26 line->add(1.1, 1.1);
27 line->add(2.0, 2.5);
27 line->add(2.0, 2.5);
28 chartView->addSeries(line);
28 chartView->addSeries(line);
29 //! [1]
29 //! [1]
30
30
31 chartView->setChartTitle("\'Scietific\' theme");
31 chartView->setChartTitle("\'Scietific\' theme");
32 //! [2]
32 //! [2]
33 // Change theme
33 // Change theme
34 chartView->setChartTheme(QChart::ChartThemeScientific);
34 chartView->setChartTheme(QChart::ChartThemeScientific);
35 //! [2]
35 //! [2]
36
36
37 chartView->setChartTitle("Simple Pie Chart");
37 chartView->setChartTitle("Simple Pie Chart");
38 //! [3]
38 //! [3]
39 // Add pie series
39 // Add pie series
40 // ...
40 // ...
41 QPieSeries *pie = new QPieSeries();
41 QPieSeries *pie = new QPieSeries();
42 pie->add(3.4, "slice1");
42 pie->add(3.4, "slice1");
43 pie->add(6.7, "slice2");
43 pie->add(6.7, "slice2");
44 chartView->addSeries(pie);
44 chartView->addSeries(pie);
45 //! [3]
45 //! [3]
46
46
47 chartView->setChartTitle("Simple Scatter Chart");
47 chartView->setChartTitle("Simple Scatter Chart");
48 //! [4]
48 //! [4]
49 // Add scatter series
49 // Add scatter series
50 // ...
50 // ...
51 QScatterSeries *scatter = new QScatterSeries();
51 QScatterSeries *scatter = new QScatterSeries();
52 for (qreal x(0); x < 100; x += 0.5) {
52 for (qreal x(0); x < 100; x += 0.5) {
53 qreal y = rand() % 100;
53 qreal y = rand() % 100;
54 *(scatter) << QPointF(x, y);
54 *(scatter) << QPointF(x, y);
55 }
55 }
56 chartView->addSeries(scatter);
56 chartView->addSeries(scatter);
57 //! [4]
57 //! [4]
58
58
59 chartView->setChartTitle("Simple Bar Chart");
59 chartView->setChartTitle("Simple Bar Chart");
60 //! [5]
60 //! [5]
61 // ...
61 // ...
62 // Add bar series
62 // Add bar series
63 QBarCategory *barCategory = new QBarCategory();
63 QBarCategory *barCategory = new QBarCategory();
64 *barCategory << "Jan"
64 *barCategory << "Jan"
65 << "Feb"
65 << "Feb"
66 << "Mar";
66 << "Mar";
67 QBarSeries *bar = new QBarSeries(barCategory);
67 QBarSeries *bar = new QBarSeries(barCategory);
68 QBarSet *barSet = new QBarSet("Sales");
68 QBarSet *barSet = new QBarSet("Sales");
69 *barSet << 123.2
69 *barSet << 123.2
70 << 301.3
70 << 301.3
71 << 285.8;
71 << 285.8;
72 bar->addBarSet(barSet);
72 bar->addBarSet(barSet);
73 chartView->addSeries(bar);
73 chartView->addSeries(bar);
74 //! [5]
74 //! [5]
75
75
76 QMainWindow w;
76 QMainWindow w;
77 w.resize(400, 300);
77 w.resize(400, 300);
78 w.setCentralWidget(chartView);
78 w.setCentralWidget(chartView);
79 w.setWindowFlags(Qt::FramelessWindowHint);
79 w.setWindowFlags(Qt::FramelessWindowHint);
80 w.show();
80 w.show();
81
81
82 return a.exec();
82 return a.exec();
83 }
83 }
@@ -1,55 +1,55
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qlinechartseries.h>
4 #include <qlineseries.h>
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <cmath>
6 #include <cmath>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 #define PI 3.14159265358979
10 #define PI 3.14159265358979
11
11
12 int main(int argc, char *argv[])
12 int main(int argc, char *argv[])
13 {
13 {
14 QApplication a(argc, argv);
14 QApplication a(argc, argv);
15
15
16 QMainWindow window;
16 QMainWindow window;
17
17
18 QLineChartSeries* series0 = new QLineChartSeries();
18 QLineSeries* series0 = new QLineSeries();
19 QPen blue(Qt::blue);
19 QPen blue(Qt::blue);
20 blue.setWidth(3);
20 blue.setWidth(3);
21 series0->setPen(blue);
21 series0->setPen(blue);
22 QLineChartSeries* series1 = new QLineChartSeries();
22 QLineSeries* series1 = new QLineSeries();
23 QPen red(Qt::red);
23 QPen red(Qt::red);
24 red.setWidth(3);
24 red.setWidth(3);
25 series1->setPen(red);
25 series1->setPen(red);
26
26
27 int numPoints = 100;
27 int numPoints = 100;
28
28
29 for (int x = 0; x <= numPoints; ++x) {
29 for (int x = 0; x <= numPoints; ++x) {
30 series0->add(x, fabs(sin(PI/50*x)*100));
30 series0->add(x, fabs(sin(PI/50*x)*100));
31 series1->add(x, fabs(cos(PI/50*x)*100));
31 series1->add(x, fabs(cos(PI/50*x)*100));
32 }
32 }
33
33
34 QChartView* chartView = new QChartView(&window);
34 QChartView* chartView = new QChartView(&window);
35 chartView->setRenderHint(QPainter::Antialiasing);
35 chartView->setRenderHint(QPainter::Antialiasing);
36
36
37 QFont font;
37 QFont font;
38 font.setPixelSize(18);
38 font.setPixelSize(18);
39 chartView->setChartTitleFont(font);
39 chartView->setChartTitleFont(font);
40 chartView->setChartTitle("Custom color line chart example");
40 chartView->setChartTitle("Custom color line chart example");
41 chartView->addSeries(series0);
41 chartView->addSeries(series0);
42 chartView->addSeries(series1);
42 chartView->addSeries(series1);
43
43
44 QLinearGradient backgroundGradient;
44 QLinearGradient backgroundGradient;
45 backgroundGradient.setColorAt(0.0, Qt::blue);
45 backgroundGradient.setColorAt(0.0, Qt::blue);
46 backgroundGradient.setColorAt(1.0, Qt::yellow);
46 backgroundGradient.setColorAt(1.0, Qt::yellow);
47 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
47 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
48 chartView->setChartBackgroundBrush(backgroundGradient);
48 chartView->setChartBackgroundBrush(backgroundGradient);
49
49
50 window.setCentralWidget(chartView);
50 window.setCentralWidget(chartView);
51 window.resize(400, 300);
51 window.resize(400, 300);
52 window.show();
52 window.show();
53
53
54 return a.exec();
54 return a.exec();
55 }
55 }
@@ -1,40 +1,40
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qlinechartseries.h>
4 #include <qlineseries.h>
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <cmath>
6 #include <cmath>
7 #include "wavegenerator.h"
7 #include "wavegenerator.h"
8 #include <QGLWidget>
8 #include <QGLWidget>
9
9
10 int main(int argc, char *argv[])
10 int main(int argc, char *argv[])
11 {
11 {
12 QApplication a(argc, argv);
12 QApplication a(argc, argv);
13
13
14 QMainWindow window;
14 QMainWindow window;
15
15
16 QLineChartSeries* series0 = new QLineChartSeries();
16 QLineSeries* series0 = new QLineSeries();
17 QPen blue(Qt::blue);
17 QPen blue(Qt::blue);
18 blue.setWidth(3);
18 blue.setWidth(3);
19 series0->setPen(blue);
19 series0->setPen(blue);
20 QLineChartSeries* series1 = new QLineChartSeries();
20 QLineSeries* series1 = new QLineSeries();
21 QPen red(Qt::red);
21 QPen red(Qt::red);
22 red.setWidth(3);
22 red.setWidth(3);
23 series1->setPen(red);
23 series1->setPen(red);
24
24
25 WaveGenerator generator(series0,series1);
25 WaveGenerator generator(series0,series1);
26
26
27 QChartView* chartView = new QChartView(&window);
27 QChartView* chartView = new QChartView(&window);
28
28
29 chartView->setViewport( new QGLWidget() );
29 chartView->setViewport( new QGLWidget() );
30 chartView->setRenderHint(QPainter::Antialiasing);
30 chartView->setRenderHint(QPainter::Antialiasing);
31 chartView->setChartTitle("This is wave generator buahha.");
31 chartView->setChartTitle("This is wave generator buahha.");
32 chartView->addSeries(series0);
32 chartView->addSeries(series0);
33 chartView->addSeries(series1);
33 chartView->addSeries(series1);
34
34
35 window.setCentralWidget(chartView);
35 window.setCentralWidget(chartView);
36 window.resize(400, 300);
36 window.resize(400, 300);
37 window.show();
37 window.show();
38
38
39 return a.exec();
39 return a.exec();
40 }
40 }
@@ -1,60 +1,60
1 #include <QTimer>
1 #include <QTimer>
2 #include <QTime>
2 #include <QTime>
3 #include <QObject>
3 #include <QObject>
4 #include <cmath>
4 #include <cmath>
5 #include <qlinechartseries.h>
5 #include <qlineseries.h>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 #define PI 3.14159265358979
9 #define PI 3.14159265358979
10 static const int numPoints =100;
10 static const int numPoints =100;
11
11
12 class WaveGenerator: public QObject
12 class WaveGenerator: public QObject
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15
15
16 public:
16 public:
17 WaveGenerator(QLineChartSeries* series1, QLineChartSeries* series2) :
17 WaveGenerator(QLineSeries* series1, QLineSeries* series2) :
18 m_series1(series1),
18 m_series1(series1),
19 m_series2(series2),
19 m_series2(series2),
20 m_wave(0),
20 m_wave(0),
21 m_step(2*PI/numPoints)
21 m_step(2*PI/numPoints)
22 {
22 {
23
23
24 QTime now = QTime::currentTime();
24 QTime now = QTime::currentTime();
25 qsrand((uint)now.msec());
25 qsrand((uint)now.msec());
26
26
27 int fluctuate = 100;
27 int fluctuate = 100;
28
28
29 for (qreal x = 0; x <= 2*PI; x+=m_step) {
29 for (qreal x = 0; x <= 2*PI; x+=m_step) {
30 series1->add(x, fabs(sin(x)*fluctuate));
30 series1->add(x, fabs(sin(x)*fluctuate));
31 series2->add(x, fabs(cos(x)*fluctuate));
31 series2->add(x, fabs(cos(x)*fluctuate));
32 }
32 }
33
33
34 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(update()));
34 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(update()));
35 m_timer.setInterval(5000);
35 m_timer.setInterval(5000);
36 m_timer.start();
36 m_timer.start();
37
37
38 };
38 };
39
39
40 public slots:
40 public slots:
41 void update()
41 void update()
42 {
42 {
43 int fluctuate;
43 int fluctuate;
44
44
45 for (qreal i = 0, x = 0; x <= 2*PI; x+=m_step, i++) {
45 for (qreal i = 0, x = 0; x <= 2*PI; x+=m_step, i++) {
46 fluctuate = qrand() % 100;
46 fluctuate = qrand() % 100;
47 m_series1->replace(x, fabs(sin(x)*fluctuate));
47 m_series1->replace(x, fabs(sin(x)*fluctuate));
48 fluctuate = qrand() % 100;
48 fluctuate = qrand() % 100;
49 m_series2->replace(x, fabs(cos(x)*fluctuate));
49 m_series2->replace(x, fabs(cos(x)*fluctuate));
50 }
50 }
51
51
52 }
52 }
53
53
54 private:
54 private:
55 QLineChartSeries* m_series1;
55 QLineSeries* m_series1;
56 QLineChartSeries* m_series2;
56 QLineSeries* m_series2;
57 int m_wave;
57 int m_wave;
58 qreal m_step;
58 qreal m_step;
59 QTimer m_timer;
59 QTimer m_timer;
60 };
60 };
@@ -1,56 +1,56
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartview.h>
3 #include <qchartview.h>
4 #include <qlinechartseries.h>
4 #include <qlineseries.h>
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <cmath>
6 #include <cmath>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 int main(int argc, char *argv[])
10 int main(int argc, char *argv[])
11 {
11 {
12 QApplication a(argc, argv);
12 QApplication a(argc, argv);
13
13
14 QMainWindow window;
14 QMainWindow window;
15
15
16 //![1]
16 //![1]
17
17
18 QLineChartSeries* series0 = new QLineChartSeries();
18 QLineSeries* series0 = new QLineSeries();
19 QPen blue(Qt::blue);
19 QPen blue(Qt::blue);
20 blue.setWidth(3);
20 blue.setWidth(3);
21 series0->setPen(blue);
21 series0->setPen(blue);
22
22
23 QLineChartSeries* series1 = new QLineChartSeries();
23 QLineSeries* series1 = new QLineSeries();
24 QPen red(Qt::red);
24 QPen red(Qt::red);
25 red.setWidth(3);
25 red.setWidth(3);
26 series1->setPen(red);
26 series1->setPen(red);
27 //![1]
27 //![1]
28
28
29 //![2]
29 //![2]
30 series0->add(0, 6);
30 series0->add(0, 6);
31 series0->add(2, 4);
31 series0->add(2, 4);
32 series0->add(3, 8);
32 series0->add(3, 8);
33 series0->add(7, 4);
33 series0->add(7, 4);
34 series0->add(10,5);
34 series0->add(10,5);
35
35
36 series1->add(1, 1);
36 series1->add(1, 1);
37 series1->add(3, 3);
37 series1->add(3, 3);
38 series1->add(7, 6);
38 series1->add(7, 6);
39 series1->add(8, 3);
39 series1->add(8, 3);
40 series1->add(10,2);
40 series1->add(10,2);
41 //![2]
41 //![2]
42 //![3]
42 //![3]
43 QChartView* chartView = new QChartView(&window);
43 QChartView* chartView = new QChartView(&window);
44
44
45 chartView->setRenderHint(QPainter::Antialiasing);
45 chartView->setRenderHint(QPainter::Antialiasing);
46 chartView->setChartTitle("Basic line chart example");
46 chartView->setChartTitle("Basic line chart example");
47 chartView->addSeries(series0);
47 chartView->addSeries(series0);
48 chartView->addSeries(series1);
48 chartView->addSeries(series1);
49 //![3]
49 //![3]
50
50
51 window.setCentralWidget(chartView);
51 window.setCentralWidget(chartView);
52 window.resize(400, 300);
52 window.resize(400, 300);
53 window.show();
53 window.show();
54
54
55 return a.exec();
55 return a.exec();
56 }
56 }
@@ -1,59 +1,59
1 #include "chartview.h"
1 #include "chartview.h"
2 #include <qlinechartseries.h>
2 #include <qlineseries.h>
3 #include <QTime>
3 #include <QTime>
4
4
5 ChartView::ChartView(QWidget* parent):QChartView(parent),
5 ChartView::ChartView(QWidget* parent):QChartView(parent),
6 m_index(0)
6 m_index(0)
7 {
7 {
8 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
8 QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
9 m_timer.setInterval(3000);
9 m_timer.setInterval(3000);
10
10
11 QTime now = QTime::currentTime();
11 QTime now = QTime::currentTime();
12 qsrand((uint)now.msec());
12 qsrand((uint)now.msec());
13
13
14 QLineChartSeries* series0 = new QLineChartSeries();
14 QLineSeries* series0 = new QLineSeries();
15 QPen blue(Qt::blue);
15 QPen blue(Qt::blue);
16 blue.setWidth(3);
16 blue.setWidth(3);
17 series0->setPen(blue);
17 series0->setPen(blue);
18 QLineChartSeries* series1 = new QLineChartSeries();
18 QLineSeries* series1 = new QLineSeries();
19 QPen red(Qt::red);
19 QPen red(Qt::red);
20 red.setWidth(3);
20 red.setWidth(3);
21 series1->setPen(red);
21 series1->setPen(red);
22 QLineChartSeries* series2 = new QLineChartSeries();
22 QLineSeries* series2 = new QLineSeries();
23 QPen green(Qt::green);
23 QPen green(Qt::green);
24 green.setWidth(3);
24 green.setWidth(3);
25 series2->setPen(green);
25 series2->setPen(green);
26
26
27 int numPoints = 10;
27 int numPoints = 10;
28
28
29 for (int x = 0; x <= numPoints; ++x) {
29 for (int x = 0; x <= numPoints; ++x) {
30 series0->add(x, qrand() % 100);
30 series0->add(x, qrand() % 100);
31 series1->add(x, qrand() % 100);
31 series1->add(x, qrand() % 100);
32 series2->add(x, qrand() % 100);
32 series2->add(x, qrand() % 100);
33 }
33 }
34
34
35 addSeries(series0);
35 addSeries(series0);
36
36
37 m_series<<series0;
37 m_series<<series0;
38 m_series<<series1;
38 m_series<<series1;
39 m_series<<series2;
39 m_series<<series2;
40
40
41 m_timer.start();
41 m_timer.start();
42 }
42 }
43
43
44 ChartView::~ChartView()
44 ChartView::~ChartView()
45 {
45 {
46 if(m_series.size()==0) return;
46 if(m_series.size()==0) return;
47 removeSeries(m_series.at(m_index));
47 removeSeries(m_series.at(m_index));
48 qDeleteAll(m_series);
48 qDeleteAll(m_series);
49 }
49 }
50
50
51 void ChartView::handleTimeout()
51 void ChartView::handleTimeout()
52 {
52 {
53 if(m_series.size()==0) return;
53 if(m_series.size()==0) return;
54
54
55 removeSeries(m_series.at(m_index));
55 removeSeries(m_series.at(m_index));
56 m_index++;
56 m_index++;
57 m_index=m_index%m_series.size();
57 m_index=m_index%m_series.size();
58 addSeries(m_series.at(m_index));
58 addSeries(m_series.at(m_index));
59 }
59 }
@@ -1,44 +1,44
1 #include "chartwidget.h"
1 #include "chartwidget.h"
2 #include <QApplication>
2 #include <QApplication>
3 #include <QMainWindow>
3 #include <QMainWindow>
4 #include <qlinechartseries.h>
4 #include <qlineseries.h>
5 #include <cmath>
5 #include <cmath>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 #define PI 3.14159265358979
9 #define PI 3.14159265358979
10
10
11 int main(int argc, char *argv[])
11 int main(int argc, char *argv[])
12 {
12 {
13 QApplication a(argc, argv);
13 QApplication a(argc, argv);
14
14
15 QMainWindow window;
15 QMainWindow window;
16
16
17 QLineChartSeries* series0 = new QLineChartSeries();
17 QLineSeries* series0 = new QLineSeries();
18 QPen blue(Qt::blue);
18 QPen blue(Qt::blue);
19 blue.setWidth(3);
19 blue.setWidth(3);
20 series0->setPen(blue);
20 series0->setPen(blue);
21 QLineChartSeries* series1 = new QLineChartSeries();
21 QLineSeries* series1 = new QLineSeries();
22 QPen red(Qt::red);
22 QPen red(Qt::red);
23 red.setWidth(3);
23 red.setWidth(3);
24 series1->setPen(red);
24 series1->setPen(red);
25
25
26 int numPoints = 100;
26 int numPoints = 100;
27
27
28 for (int x = 0; x <= numPoints; ++x) {
28 for (int x = 0; x <= numPoints; ++x) {
29 series0->add(x, fabs(sin(PI/50*x)*100));
29 series0->add(x, fabs(sin(PI/50*x)*100));
30 series1->add(x, fabs(cos(PI/50*x)*100));
30 series1->add(x, fabs(cos(PI/50*x)*100));
31 }
31 }
32
32
33 ChartWidget* chartWidget = new ChartWidget(&window);
33 ChartWidget* chartWidget = new ChartWidget(&window);
34 chartWidget->setRenderHint(QPainter::Antialiasing);
34 chartWidget->setRenderHint(QPainter::Antialiasing);
35 chartWidget->setChartTitle("Zoom in/out line chart example");
35 chartWidget->setChartTitle("Zoom in/out line chart example");
36 chartWidget->addSeries(series0);
36 chartWidget->addSeries(series0);
37 chartWidget->addSeries(series1);
37 chartWidget->addSeries(series1);
38
38
39 window.setCentralWidget(chartWidget);
39 window.setCentralWidget(chartWidget);
40 window.resize(400, 300);
40 window.resize(400, 300);
41 window.show();
41 window.show();
42
42
43 return a.exec();
43 return a.exec();
44 }
44 }
@@ -1,55 +1,55
1 #include "declarativelineseries.h"
1 #include "declarativelineseries.h"
2 #include "declarativechart.h"
2 #include "declarativechart.h"
3 #include "qchart.h"
3 #include "qchart.h"
4 #include "qlinechartseries.h"
4 #include "qlineseries.h"
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 DeclarativeLineSeries::DeclarativeLineSeries(QDeclarativeItem *parent) :
8 DeclarativeLineSeries::DeclarativeLineSeries(QDeclarativeItem *parent) :
9 QDeclarativeItem(parent)
9 QDeclarativeItem(parent)
10 {
10 {
11 setFlag(QGraphicsItem::ItemHasNoContents, false);
11 setFlag(QGraphicsItem::ItemHasNoContents, false);
12 connect(this, SIGNAL(parentChanged()),
12 connect(this, SIGNAL(parentChanged()),
13 this, SLOT(setParentForSeries()));
13 this, SLOT(setParentForSeries()));
14 }
14 }
15
15
16 void DeclarativeLineSeries::setParentForSeries()
16 void DeclarativeLineSeries::setParentForSeries()
17 {
17 {
18 if (!m_series) {
18 if (!m_series) {
19 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
19 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
20
20
21 if (declarativeChart) {
21 if (declarativeChart) {
22 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
22 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
23 qDebug() << "creating line series for chart: " << chart;
23 qDebug() << "creating line series for chart: " << chart;
24 Q_ASSERT(chart);
24 Q_ASSERT(chart);
25
25
26 m_series = new QLineChartSeries();
26 m_series = new QLineSeries();
27 Q_ASSERT(m_series);
27 Q_ASSERT(m_series);
28 for (int i(0); i < m_data.count(); i++) {
28 for (int i(0); i < m_data.count(); i++) {
29 ScatterElement *element = m_data.at(i);
29 ScatterElement *element = m_data.at(i);
30 m_series->add(element->x(), element->y());
30 m_series->add(element->x(), element->y());
31 }
31 }
32 chart->addSeries(m_series);
32 chart->addSeries(m_series);
33 }
33 }
34 }
34 }
35 }
35 }
36
36
37 QDeclarativeListProperty<ScatterElement> DeclarativeLineSeries::data()
37 QDeclarativeListProperty<ScatterElement> DeclarativeLineSeries::data()
38 {
38 {
39 return QDeclarativeListProperty<ScatterElement>(this, 0, &DeclarativeLineSeries::appendData);
39 return QDeclarativeListProperty<ScatterElement>(this, 0, &DeclarativeLineSeries::appendData);
40 }
40 }
41
41
42 void DeclarativeLineSeries::appendData(QDeclarativeListProperty<ScatterElement> *list,
42 void DeclarativeLineSeries::appendData(QDeclarativeListProperty<ScatterElement> *list,
43 ScatterElement *element)
43 ScatterElement *element)
44 {
44 {
45 DeclarativeLineSeries *series = qobject_cast<DeclarativeLineSeries *>(list->object);
45 DeclarativeLineSeries *series = qobject_cast<DeclarativeLineSeries *>(list->object);
46 if (series) {
46 if (series) {
47 series->m_data.append(element);
47 series->m_data.append(element);
48 if (series->m_series)
48 if (series->m_series)
49 series->m_series->add(element->x(), element->y());
49 series->m_series->add(element->x(), element->y());
50 }
50 }
51 }
51 }
52
52
53 #include "moc_declarativelineseries.cpp"
53 #include "moc_declarativelineseries.cpp"
54
54
55 QTCOMMERCIALCHART_END_NAMESPACE
55 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,39 +1,39
1 #ifndef DECLARATIVELINESERIES_H
1 #ifndef DECLARATIVELINESERIES_H
2 #define DECLARATIVELINESERIES_H
2 #define DECLARATIVELINESERIES_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "scatterelement.h" // TODO: rename header
5 #include "scatterelement.h" // TODO: rename header
6 #include <QDeclarativeItem>
6 #include <QDeclarativeItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChart;
10 class QChart;
11 class QLineChartSeries;
11 class QLineSeries;
12
12
13 class DeclarativeLineSeries : public QDeclarativeItem
13 class DeclarativeLineSeries : public QDeclarativeItem
14 {
14 {
15 Q_OBJECT
15 Q_OBJECT
16 Q_PROPERTY(QDeclarativeListProperty<ScatterElement> data READ data)
16 Q_PROPERTY(QDeclarativeListProperty<ScatterElement> data READ data)
17
17
18 public:
18 public:
19 explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0);
19 explicit DeclarativeLineSeries(QDeclarativeItem *parent = 0);
20 QDeclarativeListProperty<ScatterElement> data();
20 QDeclarativeListProperty<ScatterElement> data();
21
21
22 signals:
22 signals:
23
23
24 public slots:
24 public slots:
25 static void appendData(QDeclarativeListProperty<ScatterElement> *list,
25 static void appendData(QDeclarativeListProperty<ScatterElement> *list,
26 ScatterElement *element);
26 ScatterElement *element);
27
27
28 private slots:
28 private slots:
29 void setParentForSeries();
29 void setParentForSeries();
30
30
31 private:
31 private:
32 QChart *m_chart;
32 QChart *m_chart;
33 QLineChartSeries *m_series;
33 QLineSeries *m_series;
34 QList<ScatterElement *> m_data;
34 QList<ScatterElement *> m_data;
35 };
35 };
36
36
37 QTCOMMERCIALCHART_END_NAMESPACE
37 QTCOMMERCIALCHART_END_NAMESPACE
38
38
39 #endif // DECLARATIVELINESERIES_H
39 #endif // DECLARATIVELINESERIES_H
@@ -1,357 +1,357
1 #include "chartdataset_p.h"
1 #include "chartdataset_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 //series
3 //series
4 #include "qlinechartseries.h"
4 #include "qlineseries.h"
5 #include "qbarseries.h"
5 #include "qbarseries.h"
6 #include "qstackedbarseries.h"
6 #include "qstackedbarseries.h"
7 #include "qpercentbarseries.h"
7 #include "qpercentbarseries.h"
8 #include "qpieseries.h"
8 #include "qpieseries.h"
9 #include "qscatterseries.h"
9 #include "qscatterseries.h"
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 m_axisX(new QChartAxis(this)),
14 m_axisX(new QChartAxis(this)),
15 m_axisY(new QChartAxis(this)),
15 m_axisY(new QChartAxis(this)),
16 m_domainIndex(0),
16 m_domainIndex(0),
17 m_axisXInitialized(false)
17 m_axisXInitialized(false)
18 {
18 {
19 }
19 }
20
20
21 ChartDataSet::~ChartDataSet()
21 ChartDataSet::~ChartDataSet()
22 {
22 {
23 // TODO Auto-generated destructor stub
23 // TODO Auto-generated destructor stub
24 }
24 }
25
25
26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 {
27 {
28 int i = m_domainMap.count(axisY);
28 int i = m_domainMap.count(axisY);
29 if(i == 0){
29 if(i == 0){
30 return Domain();
30 return Domain();
31 }
31 }
32 i = i - m_domainIndex -1;
32 i = i - m_domainIndex -1;
33 return m_domainMap.values(axisY).at(i);
33 return m_domainMap.values(axisY).at(i);
34 }
34 }
35
35
36 void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY)
36 void ChartDataSet::addSeries(QChartSeries* series, QChartAxis *axisY)
37 {
37 {
38 // TODO: we should check the series not already added
38 // TODO: we should check the series not already added
39
39
40 series->setParent(this); // take ownership
40 series->setParent(this); // take ownership
41 clearDomains();
41 clearDomains();
42
42
43 if(axisY==0) axisY = m_axisY;
43 if(axisY==0) axisY = m_axisY;
44 axisY->setParent(this); // take ownership
44 axisY->setParent(this); // take ownership
45
45
46 QList<QChartSeries*> seriesList = m_seriesMap.values(axisY);
46 QList<QChartSeries*> seriesList = m_seriesMap.values(axisY);
47
47
48 QList<Domain> domainList = m_domainMap.values(axisY);
48 QList<Domain> domainList = m_domainMap.values(axisY);
49
49
50 Q_ASSERT(domainList.size()<=1);
50 Q_ASSERT(domainList.size()<=1);
51
51
52 Domain domain;
52 Domain domain;
53
53
54 if(domainList.size()>0) domain = domainList.at(0);
54 if(domainList.size()>0) domain = domainList.at(0);
55
55
56 switch(series->type())
56 switch(series->type())
57 {
57 {
58 case QChartSeries::SeriesTypeLine: {
58 case QChartSeries::SeriesTypeLine: {
59
59
60 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
60 QLineSeries* xyseries = static_cast<QLineSeries*>(series);
61
61
62 for (int i = 0; i < xyseries->count(); i++)
62 for (int i = 0; i < xyseries->count(); i++)
63 {
63 {
64 qreal x = xyseries->x(i);
64 qreal x = xyseries->x(i);
65 qreal y = xyseries->y(i);
65 qreal y = xyseries->y(i);
66 domain.m_minX = qMin(domain.m_minX,x);
66 domain.m_minX = qMin(domain.m_minX,x);
67 domain.m_minY = qMin(domain.m_minY,y);
67 domain.m_minY = qMin(domain.m_minY,y);
68 domain.m_maxX = qMax(domain.m_maxX,x);
68 domain.m_maxX = qMax(domain.m_maxX,x);
69 domain.m_maxY = qMax(domain.m_maxY,y);
69 domain.m_maxY = qMax(domain.m_maxY,y);
70 }
70 }
71 break;
71 break;
72 }
72 }
73 case QChartSeries::SeriesTypeBar: {
73 case QChartSeries::SeriesTypeBar: {
74 qDebug() << "QChartSeries::SeriesTypeBar";
74 qDebug() << "QChartSeries::SeriesTypeBar";
75 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
75 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
76 qreal x = barSeries->countCategories();
76 qreal x = barSeries->countCategories();
77 qreal y = barSeries->max();
77 qreal y = barSeries->max();
78 domain.m_minX = qMin(domain.m_minX,x);
78 domain.m_minX = qMin(domain.m_minX,x);
79 domain.m_minY = qMin(domain.m_minY,y);
79 domain.m_minY = qMin(domain.m_minY,y);
80 domain.m_maxX = qMax(domain.m_maxX,x);
80 domain.m_maxX = qMax(domain.m_maxX,x);
81 domain.m_maxY = qMax(domain.m_maxY,y);
81 domain.m_maxY = qMax(domain.m_maxY,y);
82 break;
82 break;
83 }
83 }
84 case QChartSeries::SeriesTypeStackedBar: {
84 case QChartSeries::SeriesTypeStackedBar: {
85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86
86
87 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
87 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
88 qreal x = stackedBarSeries->countCategories();
88 qreal x = stackedBarSeries->countCategories();
89 qreal y = stackedBarSeries->maxCategorySum();
89 qreal y = stackedBarSeries->maxCategorySum();
90 domain.m_minX = qMin(domain.m_minX,x);
90 domain.m_minX = qMin(domain.m_minX,x);
91 domain.m_minY = qMin(domain.m_minY,y);
91 domain.m_minY = qMin(domain.m_minY,y);
92 domain.m_maxX = qMax(domain.m_maxX,x);
92 domain.m_maxX = qMax(domain.m_maxX,x);
93 domain.m_maxY = qMax(domain.m_maxY,y);
93 domain.m_maxY = qMax(domain.m_maxY,y);
94 break;
94 break;
95 }
95 }
96 case QChartSeries::SeriesTypePercentBar: {
96 case QChartSeries::SeriesTypePercentBar: {
97 qDebug() << "QChartSeries::SeriesTypePercentBar";
97 qDebug() << "QChartSeries::SeriesTypePercentBar";
98
98
99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
100 qreal x = percentBarSeries->countCategories();
100 qreal x = percentBarSeries->countCategories();
101 domain.m_minX = qMin(domain.m_minX,x);
101 domain.m_minX = qMin(domain.m_minX,x);
102 domain.m_minY = 0;
102 domain.m_minY = 0;
103 domain.m_maxX = qMax(domain.m_maxX,x);
103 domain.m_maxX = qMax(domain.m_maxX,x);
104 domain.m_maxY = 100;
104 domain.m_maxY = 100;
105 break;
105 break;
106 }
106 }
107
107
108 case QChartSeries::SeriesTypePie: {
108 case QChartSeries::SeriesTypePie: {
109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 // TODO: domain stuff
110 // TODO: domain stuff
111 break;
111 break;
112 }
112 }
113
113
114 case QChartSeries::SeriesTypeScatter: {
114 case QChartSeries::SeriesTypeScatter: {
115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 Q_ASSERT(scatterSeries);
116 Q_ASSERT(scatterSeries);
117 foreach (QPointF point, scatterSeries->data()) {
117 foreach (QPointF point, scatterSeries->data()) {
118 domain.m_minX = qMin(domain.m_minX, point.x());
118 domain.m_minX = qMin(domain.m_minX, point.x());
119 domain.m_maxX = qMax(domain.m_maxX, point.x());
119 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 domain.m_minY = qMin(domain.m_minY, point.y());
120 domain.m_minY = qMin(domain.m_minY, point.y());
121 domain.m_maxY = qMax(domain.m_maxY, point.y());
121 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 }
122 }
123 break;
123 break;
124 }
124 }
125
125
126 default: {
126 default: {
127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
128 return;
128 return;
129 break;
129 break;
130 }
130 }
131
131
132 }
132 }
133
133
134 if(!m_domainMap.contains(axisY))
134 if(!m_domainMap.contains(axisY))
135 {
135 {
136 emit axisAdded(axisY);
136 emit axisAdded(axisY);
137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
139 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
139 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
140 }
140 }
141 m_domainMap.replace(axisY,domain);
141 m_domainMap.replace(axisY,domain);
142 m_seriesMap.insert(axisY,series);
142 m_seriesMap.insert(axisY,series);
143
143
144 if(!m_axisXInitialized)
144 if(!m_axisXInitialized)
145 {
145 {
146 emit axisAdded(axisX());
146 emit axisAdded(axisX());
147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
149 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
149 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
150 m_axisXInitialized=true;
150 m_axisXInitialized=true;
151 }
151 }
152
152
153
153
154 emit seriesAdded(series);
154 emit seriesAdded(series);
155 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
155 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
156 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
156 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
157 emit axisLabelsChanged(axisY,ylabels);
157 emit axisLabelsChanged(axisY,ylabels);
158 emit axisLabelsChanged(axisX(),xlabels);
158 emit axisLabelsChanged(axisX(),xlabels);
159 emit seriesDomainChanged(series,domain);
159 emit seriesDomainChanged(series,domain);
160
160
161 }
161 }
162
162
163 void ChartDataSet::removeSeries(QChartSeries* series)
163 void ChartDataSet::removeSeries(QChartSeries* series)
164 {
164 {
165 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
165 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
166 foreach(QChartAxis* axis , keys) {
166 foreach(QChartAxis* axis , keys) {
167 if(m_seriesMap.contains(axis,series)){
167 if(m_seriesMap.contains(axis,series)){
168 emit seriesRemoved(series);
168 emit seriesRemoved(series);
169 m_seriesMap.remove(axis,series);
169 m_seriesMap.remove(axis,series);
170 //remove axis if no longer there
170 //remove axis if no longer there
171 if(!m_seriesMap.contains(axis)){
171 if(!m_seriesMap.contains(axis)){
172 emit axisRemoved(axis);
172 emit axisRemoved(axis);
173 m_domainMap.remove(axis);
173 m_domainMap.remove(axis);
174 if(axis != m_axisY)
174 if(axis != m_axisY)
175 delete axis;
175 delete axis;
176 }
176 }
177 series->setParent(0);
177 series->setParent(0);
178 break;
178 break;
179 }
179 }
180 }
180 }
181 }
181 }
182
182
183 void ChartDataSet::removeAllSeries()
183 void ChartDataSet::removeAllSeries()
184 {
184 {
185 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
185 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
186 foreach(QChartAxis* axis , keys) {
186 foreach(QChartAxis* axis , keys) {
187 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
187 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
188 for(int i =0 ; i < seriesList.size();i++ )
188 for(int i =0 ; i < seriesList.size();i++ )
189 {
189 {
190 emit seriesRemoved(seriesList.at(i));
190 emit seriesRemoved(seriesList.at(i));
191 delete(seriesList.at(i));
191 delete(seriesList.at(i));
192 }
192 }
193 m_seriesMap.remove(axis);
193 m_seriesMap.remove(axis);
194 m_domainMap.remove(axis);
194 m_domainMap.remove(axis);
195 emit axisRemoved(axis);
195 emit axisRemoved(axis);
196 if(axis != m_axisY) delete axis;
196 if(axis != m_axisY) delete axis;
197 }
197 }
198 m_domainIndex=0;
198 m_domainIndex=0;
199 }
199 }
200
200
201 bool ChartDataSet::nextDomain()
201 bool ChartDataSet::nextDomain()
202 {
202 {
203 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
203 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
204
204
205 if (m_domainIndex < limit) {
205 if (m_domainIndex < limit) {
206 m_domainIndex++;
206 m_domainIndex++;
207 setDomain(m_domainIndex);
207 setDomain(m_domainIndex);
208 return true;
208 return true;
209 }
209 }
210 else {
210 else {
211 return false;
211 return false;
212 }
212 }
213 }
213 }
214
214
215 bool ChartDataSet::previousDomain()
215 bool ChartDataSet::previousDomain()
216 {
216 {
217 if (m_domainIndex > 0) {
217 if (m_domainIndex > 0) {
218 m_domainIndex--;
218 m_domainIndex--;
219 setDomain(m_domainIndex);
219 setDomain(m_domainIndex);
220 return true;
220 return true;
221 }
221 }
222 else {
222 else {
223 return false;
223 return false;
224 }
224 }
225 }
225 }
226
226
227 void ChartDataSet::setDomain(int index)
227 void ChartDataSet::setDomain(int index)
228 {
228 {
229 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
229 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
230
230
231 Domain domain;
231 Domain domain;
232
232
233 foreach (QChartAxis* axis , domainList) {
233 foreach (QChartAxis* axis , domainList) {
234 int i = m_domainMap.count(axis) - index -1;
234 int i = m_domainMap.count(axis) - index -1;
235 Q_ASSERT(i>=0);
235 Q_ASSERT(i>=0);
236 domain = m_domainMap.values(axis).at(i);
236 domain = m_domainMap.values(axis).at(i);
237 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
237 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
238 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
238 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
239 foreach(QChartSeries* series, seriesList) {
239 foreach(QChartSeries* series, seriesList) {
240 emit seriesDomainChanged(series,domain);
240 emit seriesDomainChanged(series,domain);
241 }
241 }
242 emit axisLabelsChanged(axis,labels);
242 emit axisLabelsChanged(axis,labels);
243 }
243 }
244
244
245 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
245 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
246 emit axisLabelsChanged(axisX(),labels);
246 emit axisLabelsChanged(axisX(),labels);
247 }
247 }
248
248
249 void ChartDataSet::clearDomains(int toIndex)
249 void ChartDataSet::clearDomains(int toIndex)
250 {
250 {
251 Q_ASSERT(toIndex>=0);
251 Q_ASSERT(toIndex>=0);
252
252
253 m_domainIndex = toIndex;
253 m_domainIndex = toIndex;
254
254
255 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
255 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
256
256
257 foreach (QChartAxis* key , keys)
257 foreach (QChartAxis* key , keys)
258 {
258 {
259 QList<Domain> domains = m_domainMap.values(key);
259 QList<Domain> domains = m_domainMap.values(key);
260 m_domainMap.remove(key);
260 m_domainMap.remove(key);
261 int i = domains.size() - toIndex - 1;
261 int i = domains.size() - toIndex - 1;
262 while(i--){
262 while(i--){
263 domains.removeFirst();
263 domains.removeFirst();
264 }
264 }
265 for(int j=domains.size()-1; j>=0 ;j--)
265 for(int j=domains.size()-1; j>=0 ;j--)
266 m_domainMap.insert(key,domains.at(j));
266 m_domainMap.insert(key,domains.at(j));
267 }
267 }
268 }
268 }
269
269
270 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
270 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
271 {
271 {
272 Q_ASSERT(rect.isValid());
272 Q_ASSERT(rect.isValid());
273 Q_ASSERT(viewport.isValid());
273 Q_ASSERT(viewport.isValid());
274
274
275 clearDomains(m_domainIndex);
275 clearDomains(m_domainIndex);
276
276
277 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
277 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
278
278
279 Domain domain;
279 Domain domain;
280
280
281 foreach (QChartAxis* axis , domainList){
281 foreach (QChartAxis* axis , domainList){
282 domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height());
282 domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height());
283 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
283 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
284 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
284 QList<QChartSeries*> seriesList = m_seriesMap.values(axis);
285 foreach(QChartSeries* series, seriesList){
285 foreach(QChartSeries* series, seriesList){
286 emit seriesDomainChanged(series,domain);
286 emit seriesDomainChanged(series,domain);
287 }
287 }
288 emit axisLabelsChanged(axis,labels);
288 emit axisLabelsChanged(axis,labels);
289 m_domainMap.insert(axis,domain);
289 m_domainMap.insert(axis,domain);
290 }
290 }
291
291
292 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
292 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
293 emit axisLabelsChanged(axisX(),labels);
293 emit axisLabelsChanged(axisX(),labels);
294
294
295 m_domainIndex++;
295 m_domainIndex++;
296 }
296 }
297
297
298 QChartAxis* ChartDataSet::axisY(QChartSeries* series) const
298 QChartAxis* ChartDataSet::axisY(QChartSeries* series) const
299 {
299 {
300 if(series == 0) return m_axisY;
300 if(series == 0) return m_axisY;
301
301
302 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
302 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
303
303
304 foreach(QChartAxis* axis , keys) {
304 foreach(QChartAxis* axis , keys) {
305 if(m_seriesMap.contains(axis,series)){
305 if(m_seriesMap.contains(axis,series)){
306 return axis;
306 return axis;
307 }
307 }
308 }
308 }
309 return 0;
309 return 0;
310 }
310 }
311
311
312 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
312 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
313 {
313 {
314 Q_ASSERT(max>=min);
314 Q_ASSERT(max>=min);
315
315
316 QStringList labels;
316 QStringList labels;
317
317
318 int ticks = axis->ticksCount()-1;
318 int ticks = axis->ticksCount()-1;
319
319
320 for(int i=0; i<= ticks; i++){
320 for(int i=0; i<= ticks; i++){
321 qreal value = min + (i * (max - min)/ ticks);
321 qreal value = min + (i * (max - min)/ ticks);
322 QString label = axis->axisTickLabel(value);
322 QString label = axis->axisTickLabel(value);
323 if(label.isEmpty()){
323 if(label.isEmpty()){
324 labels << QString::number(value);
324 labels << QString::number(value);
325 }else{
325 }else{
326 labels << label;
326 labels << label;
327 }
327 }
328 }
328 }
329 return labels;
329 return labels;
330 }
330 }
331
331
332
332
333 void ChartDataSet::handleMinChanged(qreal min)
333 void ChartDataSet::handleMinChanged(qreal min)
334 {
334 {
335
335
336 }
336 }
337
337
338 void ChartDataSet::handleMaxChanged(qreal max)
338 void ChartDataSet::handleMaxChanged(qreal max)
339 {
339 {
340
340
341 }
341 }
342
342
343 void ChartDataSet::handleTickChanged(QChartAxis* axis)
343 void ChartDataSet::handleTickChanged(QChartAxis* axis)
344 {
344 {
345 Domain domain = m_domainMap.value(axisY());
345 Domain domain = m_domainMap.value(axisY());
346 if(axis==axisX()){
346 if(axis==axisX()){
347 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
347 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
348 emit axisLabelsChanged(axis,labels);
348 emit axisLabelsChanged(axis,labels);
349 }else{
349 }else{
350 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
350 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
351 emit axisLabelsChanged(axis,labels);
351 emit axisLabelsChanged(axis,labels);
352 }
352 }
353 }
353 }
354
354
355 #include "moc_chartdataset_p.cpp"
355 #include "moc_chartdataset_p.cpp"
356
356
357 QTCOMMERCIALCHART_END_NAMESPACE
357 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,283 +1,283
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include "charttheme_p.h"
5 #include "charttheme_p.h"
6 //series
6 //series
7 #include "qbarseries.h"
7 #include "qbarseries.h"
8 #include "qstackedbarseries.h"
8 #include "qstackedbarseries.h"
9 #include "qpercentbarseries.h"
9 #include "qpercentbarseries.h"
10 #include "qlinechartseries.h"
10 #include "qlineseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 //items
13 //items
14 #include "axisitem_p.h"
14 #include "axisitem_p.h"
15 #include "axisanimationitem_p.h"
15 #include "axisanimationitem_p.h"
16 #include "barpresenter.h"
16 #include "barpresenter.h"
17 #include "stackedbarpresenter.h"
17 #include "stackedbarpresenter.h"
18 #include "linechartitem_p.h"
18 #include "linechartitem_p.h"
19 #include "percentbarpresenter.h"
19 #include "percentbarpresenter.h"
20 #include "linechartanimationitem_p.h"
20 #include "linechartanimationitem_p.h"
21 #include "piepresenter.h"
21 #include "piepresenter.h"
22 #include "scatterpresenter_p.h"
22 #include "scatterpresenter_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
27 m_chart(chart),
27 m_chart(chart),
28 m_dataset(dataset),
28 m_dataset(dataset),
29 m_chartTheme(0),
29 m_chartTheme(0),
30 m_marginSize(0),
30 m_marginSize(0),
31 m_rect(QRectF(QPoint(0,0),m_chart->size())),
31 m_rect(QRectF(QPoint(0,0),m_chart->size())),
32 m_options(0)
32 m_options(0)
33 {
33 {
34 createConnections();
34 createConnections();
35 setChartTheme(QChart::ChartThemeDefault);
35 setChartTheme(QChart::ChartThemeDefault);
36
36
37 }
37 }
38
38
39 ChartPresenter::~ChartPresenter()
39 ChartPresenter::~ChartPresenter()
40 {
40 {
41 }
41 }
42
42
43 void ChartPresenter::createConnections()
43 void ChartPresenter::createConnections()
44 {
44 {
45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
47 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*)));
47 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*)));
48 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
48 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
49 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
49 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
50 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&)));
50 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&)));
51 QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&)));
51 QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&)));
52 }
52 }
53
53
54
54
55 QRectF ChartPresenter::geometry() const
55 QRectF ChartPresenter::geometry() const
56 {
56 {
57 return m_rect;
57 return m_rect;
58 }
58 }
59
59
60 void ChartPresenter::handleGeometryChanged()
60 void ChartPresenter::handleGeometryChanged()
61 {
61 {
62 m_rect = QRectF(QPoint(0,0),m_chart->size());
62 m_rect = QRectF(QPoint(0,0),m_chart->size());
63 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
63 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
64 Q_ASSERT(m_rect.isValid());
64 Q_ASSERT(m_rect.isValid());
65 emit geometryChanged(m_rect);
65 emit geometryChanged(m_rect);
66 }
66 }
67
67
68 int ChartPresenter::margin() const
68 int ChartPresenter::margin() const
69 {
69 {
70 return m_marginSize;
70 return m_marginSize;
71 }
71 }
72
72
73 void ChartPresenter::setMargin(int margin)
73 void ChartPresenter::setMargin(int margin)
74 {
74 {
75 m_marginSize = margin;
75 m_marginSize = margin;
76 }
76 }
77
77
78 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
78 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
79 {
79 {
80
80
81 AxisItem* item ;
81 AxisItem* item ;
82
82
83 if(!m_options.testFlag(QChart::GridAxisAnimations))
83 if(!m_options.testFlag(QChart::GridAxisAnimations))
84 {
84 {
85 item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
85 item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
86 }else{
86 }else{
87 item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
87 item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
88 }
88 }
89
89
90 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
90 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
91 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
91 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
92
92
93 item->handleAxisUpdate(axis);
93 item->handleAxisUpdate(axis);
94 item->handleGeometryChanged(m_rect);
94 item->handleGeometryChanged(m_rect);
95 m_chartTheme->decorate(axis,item);
95 m_chartTheme->decorate(axis,item);
96 m_axisItems.insert(axis,item);
96 m_axisItems.insert(axis,item);
97 }
97 }
98
98
99 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
99 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
100 {
100 {
101 AxisItem* item = m_axisItems.take(axis);
101 AxisItem* item = m_axisItems.take(axis);
102 Q_ASSERT(item);
102 Q_ASSERT(item);
103 delete item;
103 delete item;
104 }
104 }
105
105
106
106
107 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
107 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
108 {
108 {
109 switch(series->type())
109 switch(series->type())
110 {
110 {
111 case QChartSeries::SeriesTypeLine: {
111 case QChartSeries::SeriesTypeLine: {
112 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
112 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
113 LineChartItem* item;
113 LineChartItem* item;
114 if(m_options.testFlag(QChart::SeriesAnimations)){
114 if(m_options.testFlag(QChart::SeriesAnimations)){
115 item = new LineChartAnimationItem(this,lineSeries,m_chart);
115 item = new LineChartAnimationItem(this,lineSeries,m_chart);
116 }else{
116 }else{
117 item = new LineChartItem(this,lineSeries,m_chart);
117 item = new LineChartItem(this,lineSeries,m_chart);
118 }
118 }
119 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
119 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
121 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
122 m_chartItems.insert(series,item);
122 m_chartItems.insert(series,item);
123 break;
123 break;
124 }
124 }
125
125
126 case QChartSeries::SeriesTypeBar: {
126 case QChartSeries::SeriesTypeBar: {
127 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
127 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
128 BarPresenter* item = new BarPresenter(barSeries,m_chart);
128 BarPresenter* item = new BarPresenter(barSeries,m_chart);
129 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
129 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
130 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
130 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
131 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
131 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
132 m_chartItems.insert(series,item);
132 m_chartItems.insert(series,item);
133 // m_axisXItem->setVisible(false);
133 // m_axisXItem->setVisible(false);
134 break;
134 break;
135 }
135 }
136
136
137 case QChartSeries::SeriesTypeStackedBar: {
137 case QChartSeries::SeriesTypeStackedBar: {
138
138
139 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
139 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
140 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
140 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
141 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
141 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
142 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
142 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
143 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
143 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
144 m_chartItems.insert(series,item);
144 m_chartItems.insert(series,item);
145 break;
145 break;
146 }
146 }
147
147
148 case QChartSeries::SeriesTypePercentBar: {
148 case QChartSeries::SeriesTypePercentBar: {
149
149
150 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
150 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
151 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
151 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
152 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
152 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
153 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
153 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
154 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
154 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
155 m_chartItems.insert(series,item);
155 m_chartItems.insert(series,item);
156 break;
156 break;
157 }
157 }
158 case QChartSeries::SeriesTypeScatter: {
158 case QChartSeries::SeriesTypeScatter: {
159 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
159 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
160 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
160 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
161 QObject::connect(scatterPresenter, SIGNAL(clicked()), scatterSeries, SIGNAL(clicked()));
161 QObject::connect(scatterPresenter, SIGNAL(clicked()), scatterSeries, SIGNAL(clicked()));
162 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
162 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
163 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
163 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
164 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
164 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
165 m_chartItems.insert(scatterSeries, scatterPresenter);
165 m_chartItems.insert(scatterSeries, scatterPresenter);
166 break;
166 break;
167 }
167 }
168 case QChartSeries::SeriesTypePie: {
168 case QChartSeries::SeriesTypePie: {
169 QPieSeries *s = qobject_cast<QPieSeries *>(series);
169 QPieSeries *s = qobject_cast<QPieSeries *>(series);
170 PiePresenter* pie = new PiePresenter(m_chart, s);
170 PiePresenter* pie = new PiePresenter(m_chart, s);
171 m_chartTheme->decorate(pie, s, m_chartItems.count());
171 m_chartTheme->decorate(pie, s, m_chartItems.count());
172 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
172 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
173
173
174 // Hide all from background when there is only piechart
174 // Hide all from background when there is only piechart
175 // TODO: refactor this ugly code... should be one setting for this
175 // TODO: refactor this ugly code... should be one setting for this
176 if (m_chartItems.count() == 0) {
176 if (m_chartItems.count() == 0) {
177 m_chart->axisX()->setAxisVisible(false);
177 m_chart->axisX()->setAxisVisible(false);
178 m_chart->axisY()->setAxisVisible(false);
178 m_chart->axisY()->setAxisVisible(false);
179 m_chart->axisX()->setGridVisible(false);
179 m_chart->axisX()->setGridVisible(false);
180 m_chart->axisY()->setGridVisible(false);
180 m_chart->axisY()->setGridVisible(false);
181 m_chart->axisX()->setLabelsVisible(false);
181 m_chart->axisX()->setLabelsVisible(false);
182 m_chart->axisY()->setLabelsVisible(false);
182 m_chart->axisY()->setLabelsVisible(false);
183 m_chart->axisX()->setShadesVisible(false);
183 m_chart->axisX()->setShadesVisible(false);
184 m_chart->axisY()->setShadesVisible(false);
184 m_chart->axisY()->setShadesVisible(false);
185 m_chart->setChartBackgroundBrush(Qt::transparent);
185 m_chart->setChartBackgroundBrush(Qt::transparent);
186 }
186 }
187
187
188 m_chartItems.insert(series, pie);
188 m_chartItems.insert(series, pie);
189 break;
189 break;
190 }
190 }
191 default: {
191 default: {
192 qDebug()<< "Series type" << series->type() << "not implemented.";
192 qDebug()<< "Series type" << series->type() << "not implemented.";
193 break;
193 break;
194 }
194 }
195 }
195 }
196
196
197 if(m_rect.isValid()) emit geometryChanged(m_rect);
197 if(m_rect.isValid()) emit geometryChanged(m_rect);
198 }
198 }
199
199
200 void ChartPresenter::handleSeriesRemoved(QChartSeries* series)
200 void ChartPresenter::handleSeriesRemoved(QChartSeries* series)
201 {
201 {
202 ChartItem* item = m_chartItems.take(series);
202 ChartItem* item = m_chartItems.take(series);
203 delete item;
203 delete item;
204 }
204 }
205
205
206 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
206 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
207 {
207 {
208 //TODO:
208 //TODO:
209 }
209 }
210
210
211 void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain)
211 void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain)
212 {
212 {
213 m_chartItems.value(series)->handleDomainChanged(domain);
213 m_chartItems.value(series)->handleDomainChanged(domain);
214 }
214 }
215
215
216 void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels)
216 void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels)
217 {
217 {
218 m_axisItems.value(axis)->handleLabelsChanged(axis,labels);
218 m_axisItems.value(axis)->handleLabelsChanged(axis,labels);
219 }
219 }
220
220
221 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
221 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
222 {
222 {
223 delete m_chartTheme;
223 delete m_chartTheme;
224
224
225 m_chartTheme = ChartTheme::createTheme(theme);
225 m_chartTheme = ChartTheme::createTheme(theme);
226
226
227 m_chartTheme->decorate(m_chart);
227 m_chartTheme->decorate(m_chart);
228 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
228 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
229
229
230 int index=0;
230 int index=0;
231 while (i.hasNext()) {
231 while (i.hasNext()) {
232 i.next();
232 i.next();
233 m_chartTheme->decorate(i.value(),i.key(),index);
233 m_chartTheme->decorate(i.value(),i.key(),index);
234 index++;
234 index++;
235 }
235 }
236
236
237 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
237 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
238 while (j.hasNext()) {
238 while (j.hasNext()) {
239 j.next();
239 j.next();
240 m_chartTheme->decorate(j.key(),j.value());
240 m_chartTheme->decorate(j.key(),j.value());
241 }
241 }
242 }
242 }
243
243
244 QChart::ChartTheme ChartPresenter::chartTheme()
244 QChart::ChartTheme ChartPresenter::chartTheme()
245 {
245 {
246 return m_chartTheme->id();
246 return m_chartTheme->id();
247 }
247 }
248
248
249 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
249 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
250 {
250 {
251 if(m_options!=options) {
251 if(m_options!=options) {
252
252
253 m_options=options;
253 m_options=options;
254
254
255 //recreate elements
255 //recreate elements
256
256
257 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
257 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
258 QList<QChartSeries*> seriesList = m_chartItems.uniqueKeys();
258 QList<QChartSeries*> seriesList = m_chartItems.uniqueKeys();
259
259
260 foreach(QChartAxis* axis, axisList) {
260 foreach(QChartAxis* axis, axisList) {
261 handleAxisRemoved(axis);
261 handleAxisRemoved(axis);
262 handleAxisAdded(axis);
262 handleAxisAdded(axis);
263 }
263 }
264 foreach(QChartSeries* series, seriesList) {
264 foreach(QChartSeries* series, seriesList) {
265 handleSeriesRemoved(series);
265 handleSeriesRemoved(series);
266 handleSeriesAdded(series);
266 handleSeriesAdded(series);
267 }
267 }
268
268
269 //now reintialize view data
269 //now reintialize view data
270 //TODO: make it more nice
270 //TODO: make it more nice
271 m_dataset->setDomain(m_dataset->domainIndex());
271 m_dataset->setDomain(m_dataset->domainIndex());
272 }
272 }
273 }
273 }
274
274
275 QChart::AnimationOptions ChartPresenter::animationOptions() const
275 QChart::AnimationOptions ChartPresenter::animationOptions() const
276 {
276 {
277 return m_options;
277 return m_options;
278 }
278 }
279
279
280
280
281 #include "moc_chartpresenter_p.cpp"
281 #include "moc_chartpresenter_p.cpp"
282
282
283 QTCOMMERCIALCHART_END_NAMESPACE
283 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,267 +1,267
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4 #include <QTime>
4 #include <QTime>
5
5
6 //series
6 //series
7 #include "qbarset.h"
7 #include "qbarset.h"
8 #include "qbarseries.h"
8 #include "qbarseries.h"
9 #include "qstackedbarseries.h"
9 #include "qstackedbarseries.h"
10 #include "qpercentbarseries.h"
10 #include "qpercentbarseries.h"
11 #include "qlinechartseries.h"
11 #include "qlineseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 #include "qpieseries.h"
13 #include "qpieseries.h"
14 #include "qpieslice.h"
14 #include "qpieslice.h"
15
15
16 //items
16 //items
17 #include "axisitem_p.h"
17 #include "axisitem_p.h"
18 #include "barpresenter.h"
18 #include "barpresenter.h"
19 #include "stackedbarpresenter.h"
19 #include "stackedbarpresenter.h"
20 #include "linechartitem_p.h"
20 #include "linechartitem_p.h"
21 #include "percentbarpresenter.h"
21 #include "percentbarpresenter.h"
22 #include "scatterpresenter_p.h"
22 #include "scatterpresenter_p.h"
23 #include "piepresenter.h"
23 #include "piepresenter.h"
24
24
25 //themes
25 //themes
26 #include "chartthemevanilla_p.h"
26 #include "chartthemevanilla_p.h"
27 #include "chartthemeicy_p.h"
27 #include "chartthemeicy_p.h"
28 #include "chartthemegrayscale_p.h"
28 #include "chartthemegrayscale_p.h"
29 #include "chartthemescientific_p.h"
29 #include "chartthemescientific_p.h"
30
30
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 /* TODO
34 /* TODO
35 case QChart::ChartThemeUnnamed1:
35 case QChart::ChartThemeUnnamed1:
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
36 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
37 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
38 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
39 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
41
41
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
42 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
43 m_gradientEndColor = QColor(QRgb(0xffafafaf));
44 */
44 */
45
45
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
46 ChartTheme::ChartTheme(QChart::ChartTheme id)
47 {
47 {
48 m_id = id;
48 m_id = id;
49 m_seriesColor.append(QRgb(0xff000000));
49 m_seriesColor.append(QRgb(0xff000000));
50 m_seriesColor.append(QRgb(0xff707070));
50 m_seriesColor.append(QRgb(0xff707070));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
51 m_gradientStartColor = QColor(QRgb(0xffffffff));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
52 m_gradientEndColor = QColor(QRgb(0xffafafaf));
53
53
54 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
54 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
55 }
55 }
56
56
57
57
58 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
58 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
59 {
59 {
60 switch(theme) {
60 switch(theme) {
61 case QChart::ChartThemeDefault:
61 case QChart::ChartThemeDefault:
62 return new ChartThemeIcy();
62 return new ChartThemeIcy();
63 case QChart::ChartThemeVanilla:
63 case QChart::ChartThemeVanilla:
64 return new ChartThemeVanilla();
64 return new ChartThemeVanilla();
65 case QChart::ChartThemeIcy:
65 case QChart::ChartThemeIcy:
66 return new ChartThemeIcy();
66 return new ChartThemeIcy();
67 case QChart::ChartThemeGrayscale:
67 case QChart::ChartThemeGrayscale:
68 return new ChartThemeGrayscale();
68 return new ChartThemeGrayscale();
69 case QChart::ChartThemeScientific:
69 case QChart::ChartThemeScientific:
70 return new ChartThemeScientific();
70 return new ChartThemeScientific();
71 }
71 }
72 }
72 }
73
73
74 void ChartTheme::decorate(QChart* chart)
74 void ChartTheme::decorate(QChart* chart)
75 {
75 {
76 QLinearGradient backgroundGradient;
76 QLinearGradient backgroundGradient;
77 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
77 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
78 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
78 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
79 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
79 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
80 chart->setChartBackgroundBrush(backgroundGradient);
80 chart->setChartBackgroundBrush(backgroundGradient);
81 }
81 }
82 //TODO helper to by removed later
82 //TODO helper to by removed later
83 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
83 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
84 {
84 {
85 switch(series->type())
85 switch(series->type())
86 {
86 {
87 case QChartSeries::SeriesTypeLine: {
87 case QChartSeries::SeriesTypeLine: {
88 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
88 QLineSeries* s = static_cast<QLineSeries*>(series);
89 LineChartItem* i = static_cast<LineChartItem*>(item);
89 LineChartItem* i = static_cast<LineChartItem*>(item);
90 decorate(i,s,count);
90 decorate(i,s,count);
91 break;
91 break;
92 }
92 }
93 case QChartSeries::SeriesTypeBar: {
93 case QChartSeries::SeriesTypeBar: {
94 QBarSeries* b = static_cast<QBarSeries*>(series);
94 QBarSeries* b = static_cast<QBarSeries*>(series);
95 BarPresenter* i = static_cast<BarPresenter*>(item);
95 BarPresenter* i = static_cast<BarPresenter*>(item);
96 decorate(i,b,count);
96 decorate(i,b,count);
97 break;
97 break;
98 }
98 }
99 case QChartSeries::SeriesTypeStackedBar: {
99 case QChartSeries::SeriesTypeStackedBar: {
100 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
100 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
101 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
101 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
102 decorate(i,s,count);
102 decorate(i,s,count);
103 break;
103 break;
104 }
104 }
105 case QChartSeries::SeriesTypePercentBar: {
105 case QChartSeries::SeriesTypePercentBar: {
106 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
106 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
107 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
107 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
108 decorate(i,s,count);
108 decorate(i,s,count);
109 break;
109 break;
110 }
110 }
111 case QChartSeries::SeriesTypeScatter: {
111 case QChartSeries::SeriesTypeScatter: {
112 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
112 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
113 Q_ASSERT(s);
113 Q_ASSERT(s);
114 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
114 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
115 Q_ASSERT(i);
115 Q_ASSERT(i);
116 decorate(i, s, count);
116 decorate(i, s, count);
117 break;
117 break;
118 }
118 }
119 case QChartSeries::SeriesTypePie: {
119 case QChartSeries::SeriesTypePie: {
120 QPieSeries* s = static_cast<QPieSeries*>(series);
120 QPieSeries* s = static_cast<QPieSeries*>(series);
121 PiePresenter* i = static_cast<PiePresenter*>(item);
121 PiePresenter* i = static_cast<PiePresenter*>(item);
122 decorate(i,s,count);
122 decorate(i,s,count);
123 break;
123 break;
124 }
124 }
125 default:
125 default:
126 qDebug()<<"Wrong item to be decorated by theme";
126 qDebug()<<"Wrong item to be decorated by theme";
127 break;
127 break;
128 }
128 }
129
129
130 }
130 }
131
131
132 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
132 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
133 {
133 {
134 QPen pen;
134 QPen pen;
135 if(pen != series->pen()){
135 if(pen != series->pen()){
136 item->setPen(series->pen());
136 item->setPen(series->pen());
137 return;
137 return;
138 }
138 }
139 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
139 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
140 pen.setWidthF(2);
140 pen.setWidthF(2);
141 item->setPen(pen);
141 item->setPen(pen);
142 }
142 }
143
143
144 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
144 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
145 {
145 {
146 for (int i=0; i<series->countSets(); i++) {
146 for (int i=0; i<series->countSets(); i++) {
147 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
147 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
148 }
148 }
149 }
149 }
150
150
151 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
151 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
152 {
152 {
153 for (int i=0; i<series->countSets(); i++) {
153 for (int i=0; i<series->countSets(); i++) {
154 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
154 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
155 }
155 }
156 }
156 }
157
157
158 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
158 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
159 {
159 {
160 for (int i=0; i<series->countSets(); i++) {
160 for (int i=0; i<series->countSets(); i++) {
161 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
161 series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
162 }
162 }
163 }
163 }
164
164
165 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
165 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
166 {
166 {
167 Q_ASSERT(presenter);
167 Q_ASSERT(presenter);
168 Q_ASSERT(series);
168 Q_ASSERT(series);
169
169
170 QColor color = m_seriesColor.at(count % m_seriesColor.size());
170 QColor color = m_seriesColor.at(count % m_seriesColor.size());
171 // TODO: define alpha in the theme? or in the series?
171 // TODO: define alpha in the theme? or in the series?
172 //color.setAlpha(120);
172 //color.setAlpha(120);
173
173
174 QBrush brush(color, Qt::SolidPattern);
174 QBrush brush(color, Qt::SolidPattern);
175 presenter->m_markerBrush = brush;
175 presenter->m_markerBrush = brush;
176
176
177 QPen pen(brush, 3);
177 QPen pen(brush, 3);
178 pen.setColor(color);
178 pen.setColor(color);
179 presenter->m_markerPen = pen;
179 presenter->m_markerPen = pen;
180 }
180 }
181
181
182 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
182 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
183 {
183 {
184 // create a list of slice colors based on current theme
184 // create a list of slice colors based on current theme
185 int i = 0;
185 int i = 0;
186 QList<QColor> colors;
186 QList<QColor> colors;
187 while (colors.count() < series->count()) {
187 while (colors.count() < series->count()) {
188
188
189 // get base color
189 // get base color
190 QColor c = m_seriesColor[i++];
190 QColor c = m_seriesColor[i++];
191 i = i % m_seriesColor.count();
191 i = i % m_seriesColor.count();
192
192
193 // dont use black colors... looks bad
193 // dont use black colors... looks bad
194 if (c == Qt::black)
194 if (c == Qt::black)
195 continue;
195 continue;
196
196
197 // by default use the "raw" theme color
197 // by default use the "raw" theme color
198 if (!colors.contains(c)) {
198 if (!colors.contains(c)) {
199 colors << c;
199 colors << c;
200 continue;
200 continue;
201 }
201 }
202 // ...ok we need to generate something that looks like the same color
202 // ...ok we need to generate something that looks like the same color
203 // but different lightness
203 // but different lightness
204
204
205 int tryCount = 0;
205 int tryCount = 0;
206 while (tryCount++ < 100) {
206 while (tryCount++ < 100) {
207
207
208 // find maximum value we can raise the lightness
208 // find maximum value we can raise the lightness
209 int lMax = 255;
209 int lMax = 255;
210 if (lMax > 255 - c.red())
210 if (lMax > 255 - c.red())
211 lMax = 255 - c.red();
211 lMax = 255 - c.red();
212 if (lMax > 255 - c.green())
212 if (lMax > 255 - c.green())
213 lMax = 255 - c.green();
213 lMax = 255 - c.green();
214 if (lMax > 255 - c.blue())
214 if (lMax > 255 - c.blue())
215 lMax = 255 - c.blue();
215 lMax = 255 - c.blue();
216
216
217 // find maximum value we can make it darker
217 // find maximum value we can make it darker
218 int dMax = 255;
218 int dMax = 255;
219 if (dMax > c.red())
219 if (dMax > c.red())
220 dMax = c.red();
220 dMax = c.red();
221 if (dMax > c.green())
221 if (dMax > c.green())
222 dMax = c.green();
222 dMax = c.green();
223 if (dMax > c.blue())
223 if (dMax > c.blue())
224 dMax = c.blue();
224 dMax = c.blue();
225
225
226 int max = dMax + lMax;
226 int max = dMax + lMax;
227 if (max == 0) {
227 if (max == 0) {
228 // no room to make color lighter or darker...
228 // no room to make color lighter or darker...
229 qDebug() << "cannot generate a color for pie!";
229 qDebug() << "cannot generate a color for pie!";
230 break;
230 break;
231 }
231 }
232
232
233 // generate random color
233 // generate random color
234 int r = c.red() - dMax;
234 int r = c.red() - dMax;
235 int g = c.green() - dMax;
235 int g = c.green() - dMax;
236 int b = c.blue() - dMax;
236 int b = c.blue() - dMax;
237 int d = qrand() % max;
237 int d = qrand() % max;
238 c.setRgb(r+d, g+d, b+d);
238 c.setRgb(r+d, g+d, b+d);
239
239
240 // found a unique color?
240 // found a unique color?
241 if (!colors.contains(c))
241 if (!colors.contains(c))
242 break;
242 break;
243 }
243 }
244
244
245 qDebug() << "generated a color for pie" << c;
245 qDebug() << "generated a color for pie" << c;
246 colors << c;
246 colors << c;
247 }
247 }
248
248
249 // finally update colors
249 // finally update colors
250 foreach (QPieSlice* s, series->slices()) {
250 foreach (QPieSlice* s, series->slices()) {
251 QColor c = colors.takeFirst();
251 QColor c = colors.takeFirst();
252 s->setPen(c);
252 s->setPen(c);
253 s->setBrush(c);
253 s->setBrush(c);
254 }
254 }
255 }
255 }
256
256
257
257
258 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
258 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
259 {
259 {
260 //TODO: dummy defults for now
260 //TODO: dummy defults for now
261 axis->setLabelsBrush(Qt::black);
261 axis->setLabelsBrush(Qt::black);
262 axis->setLabelsPen(Qt::NoPen);
262 axis->setLabelsPen(Qt::NoPen);
263 axis->setShadesPen(Qt::NoPen);
263 axis->setShadesPen(Qt::NoPen);
264 axis->setShadesOpacity(0.5);
264 axis->setShadesOpacity(0.5);
265 }
265 }
266
266
267 QTCOMMERCIALCHART_END_NAMESPACE
267 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,52
1 #ifndef CHARTTHEME_H
1 #ifndef CHARTTHEME_H
2 #define CHARTTHEME_H
2 #define CHARTTHEME_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include <QColor>
6 #include <QColor>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartItem;
10 class ChartItem;
11 class QChartSeries;
11 class QChartSeries;
12 class LineChartItem;
12 class LineChartItem;
13 class QLineChartSeries;
13 class QLineSeries;
14 class BarPresenter;
14 class BarPresenter;
15 class QBarSeries;
15 class QBarSeries;
16 class StackedBarPresenter;
16 class StackedBarPresenter;
17 class QStackedBarSeries;
17 class QStackedBarSeries;
18 class QPercentBarSeries;
18 class QPercentBarSeries;
19 class PercentBarPresenter;
19 class PercentBarPresenter;
20 class QScatterSeries;
20 class QScatterSeries;
21 class ScatterPresenter;
21 class ScatterPresenter;
22 class PiePresenter;
22 class PiePresenter;
23 class QPieSeries;
23 class QPieSeries;
24
24
25 class ChartTheme
25 class ChartTheme
26 {
26 {
27 protected:
27 protected:
28 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
28 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
29 public:
29 public:
30 static ChartTheme* createTheme(QChart::ChartTheme theme);
30 static ChartTheme* createTheme(QChart::ChartTheme theme);
31 QChart::ChartTheme id() const {return m_id;}
31 QChart::ChartTheme id() const {return m_id;}
32 void decorate(QChart* chart);
32 void decorate(QChart* chart);
33 void decorate(ChartItem* item, QChartSeries* series,int count);
33 void decorate(ChartItem* item, QChartSeries* series,int count);
34 void decorate(LineChartItem* item, QLineChartSeries*, int count);
35 void decorate(BarPresenter* item, QBarSeries* series,int count);
34 void decorate(BarPresenter* item, QBarSeries* series,int count);
36 void decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count);
35 void decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count);
37 void decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count);
36 void decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count);
37 void decorate(LineChartItem* item, QLineSeries*, int count);
38 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
38 void decorate(ScatterPresenter* presenter, QScatterSeries* series, int count);
39 void decorate(PiePresenter* item, QPieSeries* series, int count);
39 void decorate(PiePresenter* item, QPieSeries* series, int count);
40 void decorate(QChartAxis* axis,AxisItem* item);
40 void decorate(QChartAxis* axis,AxisItem* item);
41
41
42 protected:
42 protected:
43 QChart::ChartTheme m_id;
43 QChart::ChartTheme m_id;
44 QColor m_gradientStartColor;
44 QColor m_gradientStartColor;
45 QColor m_gradientEndColor;
45 QColor m_gradientEndColor;
46 QList<QColor> m_seriesColor;
46 QList<QColor> m_seriesColor;
47
47
48 };
48 };
49
49
50 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
51
51
52 #endif // CHARTTHEME_H
52 #endif // CHARTTHEME_H
@@ -1,14 +1,14
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/linechartanimationitem.cpp \
5 $$PWD/linechartanimationitem.cpp \
6 $$PWD/linechartitem.cpp \
6 $$PWD/linechartitem.cpp \
7 $$PWD/qlinechartseries.cpp
7 $$PWD/qlineseries.cpp
8
8
9 PRIVATE_HEADERS += \
9 PRIVATE_HEADERS += \
10 $$PWD/linechartitem_p.h \
10 $$PWD/linechartitem_p.h \
11 $$PWD/linechartanimationitem_p.h
11 $$PWD/linechartanimationitem_p.h
12
12
13 PUBLIC_HEADERS += \
13 PUBLIC_HEADERS += \
14 $$PWD/qlinechartseries.h No newline at end of file
14 $$PWD/qlineseries.h No newline at end of file
@@ -1,63 +1,63
1 #include "linechartanimationitem_p.h"
1 #include "linechartanimationitem_p.h"
2 #include "linechartitem_p.h"
2 #include "linechartitem_p.h"
3 #include <QPropertyAnimation>
3 #include <QPropertyAnimation>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 const static int duration = 500;
7 const static int duration = 500;
8
8
9
9
10 LineChartAnimationItem::LineChartAnimationItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):
10 LineChartAnimationItem::LineChartAnimationItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):
11 LineChartItem(presenter,series,parent)
11 LineChartItem(presenter,series,parent)
12 {
12 {
13
13
14 }
14 }
15
15
16 LineChartAnimationItem::~LineChartAnimationItem()
16 LineChartAnimationItem::~LineChartAnimationItem()
17 {
17 {
18 }
18 }
19
19
20 void LineChartAnimationItem::addPoints(const QVector<QPointF>& points)
20 void LineChartAnimationItem::addPoints(const QVector<QPointF>& points)
21 {
21 {
22 m_data=points;
22 m_data=points;
23 clearView();
23 clearView();
24 QPropertyAnimation *animation = new QPropertyAnimation(this, "a_addPoints", parent());
24 QPropertyAnimation *animation = new QPropertyAnimation(this, "a_addPoints", parent());
25 animation->setDuration(duration);
25 animation->setDuration(duration);
26 //animation->setEasingCurve(QEasingCurve::InOutBack);
26 //animation->setEasingCurve(QEasingCurve::InOutBack);
27 animation->setKeyValueAt(0.0, 0);
27 animation->setKeyValueAt(0.0, 0);
28 animation->setKeyValueAt(1.0, m_data.size());
28 animation->setKeyValueAt(1.0, m_data.size());
29 animation->start(QAbstractAnimation::DeleteWhenStopped);
29 animation->start(QAbstractAnimation::DeleteWhenStopped);
30 }
30 }
31
31
32 void LineChartAnimationItem::setPoint(int index,const QPointF& point)
32 void LineChartAnimationItem::setPoint(int index,const QPointF& point)
33 {
33 {
34 AnimationHelper* helper = new AnimationHelper(this,index);
34 AnimationHelper* helper = new AnimationHelper(this,index);
35 QPropertyAnimation *animation = new QPropertyAnimation(helper, "point", parent());
35 QPropertyAnimation *animation = new QPropertyAnimation(helper, "point", parent());
36 animation->setDuration(duration);
36 animation->setDuration(duration);
37 //animation->setEasingCurve(QEasingCurve::InOutBack);
37 //animation->setEasingCurve(QEasingCurve::InOutBack);
38 animation->setKeyValueAt(0.0, points().value(index));
38 animation->setKeyValueAt(0.0, points().value(index));
39 animation->setKeyValueAt(1.0, point);
39 animation->setKeyValueAt(1.0, point);
40 animation->start(QAbstractAnimation::DeleteWhenStopped);
40 animation->start(QAbstractAnimation::DeleteWhenStopped);
41 }
41 }
42
42
43 void LineChartAnimationItem::aw_addPoints(int points)
43 void LineChartAnimationItem::aw_addPoints(int points)
44 {
44 {
45 int index = count();
45 int index = count();
46 for(int i = index;i< points ;i++){
46 for(int i = index;i< points ;i++){
47 LineChartItem::addPoint(m_data.at(i));
47 LineChartItem::addPoint(m_data.at(i));
48 }
48 }
49 updateGeometry();
49 updateGeometry();
50 update();
50 update();
51 }
51 }
52
52
53 void LineChartAnimationItem::aw_setPoint(int index,const QPointF& point)
53 void LineChartAnimationItem::aw_setPoint(int index,const QPointF& point)
54 {
54 {
55 LineChartItem::setPoint(index,point);
55 LineChartItem::setPoint(index,point);
56 updateGeometry();
56 updateGeometry();
57 update();
57 update();
58 }
58 }
59
59
60
60
61 #include "moc_linechartanimationitem_p.cpp"
61 #include "moc_linechartanimationitem_p.cpp"
62
62
63 QTCOMMERCIALCHART_END_NAMESPACE
63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,55
1 #ifndef LINECHARTANIMATIONITEM_P_H_
1 #ifndef LINECHARTANIMATIONITEM_P_H_
2 #define LINECHARTANIMATIONITEM_P_H_
2 #define LINECHARTANIMATIONITEM_P_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "linechartitem_p.h"
5 #include "linechartitem_p.h"
6 #include "domain_p.h"
6 #include "domain_p.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class LineChartItem;
10 class LineChartItem;
11
11
12 class LineChartAnimationItem : public LineChartItem {
12 class LineChartAnimationItem : public LineChartItem {
13 Q_OBJECT
13 Q_OBJECT
14 Q_PROPERTY(int a_addPoints READ ar_addPoints WRITE aw_addPoints);
14 Q_PROPERTY(int a_addPoints READ ar_addPoints WRITE aw_addPoints);
15 // Q_PROPERTY(QPointF a_setPoint READ ar_setPoint WRITE aw_setPoint);
15 // Q_PROPERTY(QPointF a_setPoint READ ar_setPoint WRITE aw_setPoint);
16 public:
16 public:
17 LineChartAnimationItem(ChartPresenter* presenter, QLineChartSeries *series, QGraphicsItem *parent = 0);
17 LineChartAnimationItem(ChartPresenter* presenter, QLineSeries *series, QGraphicsItem *parent = 0);
18 virtual ~LineChartAnimationItem();
18 virtual ~LineChartAnimationItem();
19
19
20 void addPoints(const QVector<QPointF>& points);
20 void addPoints(const QVector<QPointF>& points);
21 void setPoint(int index,const QPointF& point);
21 void setPoint(int index,const QPointF& point);
22 //void removePoint(const QPointF& point){};
22 //void removePoint(const QPointF& point){};
23 //void setPoint(const QPointF& oldPoint, const QPointF& newPoint){};
23 //void setPoint(const QPointF& oldPoint, const QPointF& newPoint){};
24
24
25 int ar_addPoints() const { return m_addPoints;}
25 int ar_addPoints() const { return m_addPoints;}
26 void aw_addPoints(int points);
26 void aw_addPoints(int points);
27 const QPointF& ar_setPoint() const { return m_setPoint;}
27 const QPointF& ar_setPoint() const { return m_setPoint;}
28 void aw_setPoint(int index,const QPointF& point);
28 void aw_setPoint(int index,const QPointF& point);
29
29
30 private:
30 private:
31 QVector<QPointF> m_data;
31 QVector<QPointF> m_data;
32 Domain m_domain;
32 Domain m_domain;
33 int m_addPoints;
33 int m_addPoints;
34 QPointF m_setPoint;
34 QPointF m_setPoint;
35 int m_setPoint_index;
35 int m_setPoint_index;
36 };
36 };
37
37
38 class AnimationHelper: public QObject
38 class AnimationHelper: public QObject
39 {
39 {
40 Q_OBJECT
40 Q_OBJECT
41 Q_PROPERTY(QPointF point READ point WRITE setPoint);
41 Q_PROPERTY(QPointF point READ point WRITE setPoint);
42 public:
42 public:
43 AnimationHelper(LineChartAnimationItem* item,int index):m_item(item),m_index(index){};
43 AnimationHelper(LineChartAnimationItem* item,int index):m_item(item),m_index(index){};
44 void setPoint(const QPointF& point){
44 void setPoint(const QPointF& point){
45 m_item->aw_setPoint(m_index,point);
45 m_item->aw_setPoint(m_index,point);
46 }
46 }
47 QPointF point(){return m_point;}
47 QPointF point(){return m_point;}
48 QPointF m_point;
48 QPointF m_point;
49 LineChartAnimationItem* m_item;
49 LineChartAnimationItem* m_item;
50 int m_index;
50 int m_index;
51 };
51 };
52
52
53 QTCOMMERCIALCHART_END_NAMESPACE
53 QTCOMMERCIALCHART_END_NAMESPACE
54
54
55 #endif
55 #endif
@@ -1,210 +1,210
1 #include "linechartitem_p.h"
1 #include "linechartitem_p.h"
2 #include "qlinechartseries.h"
2 #include "qlineseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5
5
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 //TODO: optimazie : remove points which are not visible
9 //TODO: optimazie : remove points which are not visible
10
10
11 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
11 LineChartItem::LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent):ChartItem(parent),
12 m_presenter(presenter),
12 m_presenter(presenter),
13 m_series(series),
13 m_series(series),
14 m_dirtyData(false),
14 m_dirtyData(false),
15 m_dirtyGeometry(false),
15 m_dirtyGeometry(false),
16 m_dirtyDomain(false)
16 m_dirtyDomain(false)
17 {
17 {
18 setZValue(ChartPresenter::LineChartZValue);
18 setZValue(ChartPresenter::LineChartZValue);
19 }
19 }
20
20
21 QRectF LineChartItem::boundingRect() const
21 QRectF LineChartItem::boundingRect() const
22 {
22 {
23 return m_rect;
23 return m_rect;
24 }
24 }
25
25
26 QPainterPath LineChartItem::shape() const
26 QPainterPath LineChartItem::shape() const
27 {
27 {
28 return m_path;
28 return m_path;
29 }
29 }
30
30
31
31
32 void LineChartItem::addPoints(const QVector<QPointF>& points)
32 void LineChartItem::addPoints(const QVector<QPointF>& points)
33 {
33 {
34 m_data = points;
34 m_data = points;
35 for(int i=0; i<m_data.size();i++){
35 for(int i=0; i<m_data.size();i++){
36 const QPointF& point =m_data[i];
36 const QPointF& point =m_data[i];
37 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
37 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
38 item->setPos(point.x()-1,point.y()-1);;
38 item->setPos(point.x()-1,point.y()-1);;
39 if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false);
39 if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false);
40 m_points << item;
40 m_points << item;
41 }
41 }
42 }
42 }
43
43
44 void LineChartItem::addPoint(const QPointF& point)
44 void LineChartItem::addPoint(const QPointF& point)
45 {
45 {
46 m_data << point;
46 m_data << point;
47 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
47 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
48 m_clipRect.contains(point);
48 m_clipRect.contains(point);
49 item->setPos(point.x()-1,point.y()-1);
49 item->setPos(point.x()-1,point.y()-1);
50 if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false);
50 if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false);
51 m_points << item;
51 m_points << item;
52 }
52 }
53
53
54 void LineChartItem::removePoint(const QPointF& point)
54 void LineChartItem::removePoint(const QPointF& point)
55 {
55 {
56 Q_ASSERT(m_data.count() == m_points.count());
56 Q_ASSERT(m_data.count() == m_points.count());
57 int index = m_data.lastIndexOf(point,0);
57 int index = m_data.lastIndexOf(point,0);
58 m_data.remove(index);
58 m_data.remove(index);
59 delete(m_points.takeAt(index));
59 delete(m_points.takeAt(index));
60 }
60 }
61
61
62 void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint)
62 void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint)
63 {
63 {
64 Q_ASSERT(m_data.count() == m_points.count());
64 Q_ASSERT(m_data.count() == m_points.count());
65 int index = m_data.lastIndexOf(oldPoint,0);
65 int index = m_data.lastIndexOf(oldPoint,0);
66
66
67 if(index > -1){
67 if(index > -1){
68 m_data.replace(index,newPoint);
68 m_data.replace(index,newPoint);
69 QGraphicsItem* item = m_points.at(index);
69 QGraphicsItem* item = m_points.at(index);
70 item->setPos(newPoint.x()-1,newPoint.y()-1);
70 item->setPos(newPoint.x()-1,newPoint.y()-1);
71 }
71 }
72 }
72 }
73
73
74 void LineChartItem::setPoint(int index,const QPointF& point)
74 void LineChartItem::setPoint(int index,const QPointF& point)
75 {
75 {
76 Q_ASSERT(m_data.count() == m_points.count());
76 Q_ASSERT(m_data.count() == m_points.count());
77 Q_ASSERT(index>=0);
77 Q_ASSERT(index>=0);
78
78
79 m_data.replace(index,point);
79 m_data.replace(index,point);
80 QGraphicsItem* item = m_points.at(index);
80 QGraphicsItem* item = m_points.at(index);
81 item->setPos(point.x()-1,point.y()-1);
81 item->setPos(point.x()-1,point.y()-1);
82 }
82 }
83
83
84 void LineChartItem::clear()
84 void LineChartItem::clear()
85 {
85 {
86 qDeleteAll(m_points);
86 qDeleteAll(m_points);
87 m_points.clear();
87 m_points.clear();
88 m_hash.clear();
88 m_hash.clear();
89 m_path = QPainterPath();
89 m_path = QPainterPath();
90 m_rect = QRect();
90 m_rect = QRect();
91 m_data.clear();
91 m_data.clear();
92 }
92 }
93
93
94 void LineChartItem::clearView()
94 void LineChartItem::clearView()
95 {
95 {
96 qDeleteAll(m_points);
96 qDeleteAll(m_points);
97 m_points.clear();
97 m_points.clear();
98 m_path = QPainterPath();
98 m_path = QPainterPath();
99 m_rect = QRect();
99 m_rect = QRect();
100 m_data.clear();
100 m_data.clear();
101 }
101 }
102
102
103 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
104 {
104 {
105 Q_UNUSED(widget);
105 Q_UNUSED(widget);
106 Q_UNUSED(option);
106 Q_UNUSED(option);
107 painter->save();
107 painter->save();
108 painter->setPen(m_pen);
108 painter->setPen(m_pen);
109 painter->setClipRect(m_clipRect);
109 painter->setClipRect(m_clipRect);
110 painter->drawPath(m_path);
110 painter->drawPath(m_path);
111 painter->restore();
111 painter->restore();
112 }
112 }
113
113
114 void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const
114 void LineChartItem::calculatePoint(QPointF& point, int index, const QLineSeries* series,const QSizeF& size, const Domain& domain) const
115 {
115 {
116 const qreal deltaX = size.width()/domain.spanX();
116 const qreal deltaX = size.width()/domain.spanX();
117 const qreal deltaY = size.height()/domain.spanY();
117 const qreal deltaY = size.height()/domain.spanY();
118 qreal x = (series->x(index) - domain.m_minX)* deltaX;
118 qreal x = (series->x(index) - domain.m_minX)* deltaX;
119 qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height();
119 qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height();
120 point.setX(x);
120 point.setX(x);
121 point.setY(y);
121 point.setY(y);
122 }
122 }
123
123
124
124
125 void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const
125 void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineSeries* series,const QSizeF& size, const Domain& domain) const
126 {
126 {
127 const qreal deltaX = size.width()/domain.spanX();
127 const qreal deltaX = size.width()/domain.spanX();
128 const qreal deltaY = size.height()/domain.spanY();
128 const qreal deltaY = size.height()/domain.spanY();
129
129
130 for (int i = 0; i < series->count(); ++i) {
130 for (int i = 0; i < series->count(); ++i) {
131 qreal x = (series->x(i) - domain.m_minX)* deltaX;
131 qreal x = (series->x(i) - domain.m_minX)* deltaX;
132 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
132 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
133 hash[i] = points.size();
133 hash[i] = points.size();
134 points << QPointF(x,y);
134 points << QPointF(x,y);
135 }
135 }
136 }
136 }
137
137
138 void LineChartItem::updateDomain()
138 void LineChartItem::updateDomain()
139 {
139 {
140 clear();
140 clear();
141 prepareGeometryChange();
141 prepareGeometryChange();
142 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
142 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
143 addPoints(m_data);
143 addPoints(m_data);
144 }
144 }
145
145
146 void LineChartItem::updateData()
146 void LineChartItem::updateData()
147 {
147 {
148 //for now the same
148 //for now the same
149 updateDomain();
149 updateDomain();
150 }
150 }
151
151
152 void LineChartItem::updateGeometry()
152 void LineChartItem::updateGeometry()
153 {
153 {
154
154
155 if(m_data.size()==0) return;
155 if(m_data.size()==0) return;
156
156
157 prepareGeometryChange();
157 prepareGeometryChange();
158 QPainterPath path;
158 QPainterPath path;
159 const QPointF& point = m_data.at(0);
159 const QPointF& point = m_data.at(0);
160 path.moveTo(point);
160 path.moveTo(point);
161
161
162 foreach( const QPointF& point , m_data) {
162 foreach( const QPointF& point , m_data) {
163 path.lineTo(point);
163 path.lineTo(point);
164 }
164 }
165
165
166 m_path = path;
166 m_path = path;
167 m_rect = path.boundingRect();
167 m_rect = path.boundingRect();
168 }
168 }
169
169
170 void LineChartItem::setPen(const QPen& pen)
170 void LineChartItem::setPen(const QPen& pen)
171 {
171 {
172 m_pen = pen;
172 m_pen = pen;
173 }
173 }
174
174
175 //handlers
175 //handlers
176
176
177 void LineChartItem::handleModelChanged(int index)
177 void LineChartItem::handleModelChanged(int index)
178 {
178 {
179 Q_ASSERT(index<m_series->count());
179 Q_ASSERT(index<m_series->count());
180 if(m_hash.contains(index)){
180 if(m_hash.contains(index)){
181 int i = m_hash.value(index);
181 int i = m_hash.value(index);
182 QPointF point;
182 QPointF point;
183 calculatePoint(point,index,m_series,m_size,m_domain);
183 calculatePoint(point,index,m_series,m_size,m_domain);
184 setPoint(i,point);
184 setPoint(i,point);
185 }
185 }
186 update();
186 update();
187 }
187 }
188
188
189 void LineChartItem::handleDomainChanged(const Domain& domain)
189 void LineChartItem::handleDomainChanged(const Domain& domain)
190 {
190 {
191 m_domain = domain;
191 m_domain = domain;
192 updateDomain();
192 updateDomain();
193 update();
193 update();
194 }
194 }
195
195
196 void LineChartItem::handleGeometryChanged(const QRectF& rect)
196 void LineChartItem::handleGeometryChanged(const QRectF& rect)
197 {
197 {
198 Q_ASSERT(rect.isValid());
198 Q_ASSERT(rect.isValid());
199 m_size=rect.size();
199 m_size=rect.size();
200 m_clipRect=rect.translated(-rect.topLeft());
200 m_clipRect=rect.translated(-rect.topLeft());
201 updateDomain();
201 updateDomain();
202 updateGeometry();
202 updateGeometry();
203 setPos(rect.topLeft());
203 setPos(rect.topLeft());
204 update();
204 update();
205 }
205 }
206
206
207
207
208 #include "moc_linechartitem_p.cpp"
208 #include "moc_linechartitem_p.cpp"
209
209
210 QTCOMMERCIALCHART_END_NAMESPACE
210 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,74 +1,74
1 #ifndef LINECHARTITEM_H
1 #ifndef LINECHARTITEM_H
2 #define LINECHARTITEM_H
2 #define LINECHARTITEM_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class ChartPresenter;
10 class ChartPresenter;
11 class QLineChartSeries;
11 class QLineSeries;
12 class LineChartAnimationItem;
12 class LineChartAnimationItem;
13
13
14 class LineChartItem : public QObject , public ChartItem
14 class LineChartItem : public QObject , public ChartItem
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent = 0);
18 LineChartItem(ChartPresenter* presenter, QLineSeries* series,QGraphicsItem *parent = 0);
19 ~ LineChartItem(){};
19 ~ LineChartItem(){};
20
20
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 QPainterPath shape() const;
24 QPainterPath shape() const;
25
25
26 void setPen(const QPen& pen);
26 void setPen(const QPen& pen);
27 const Domain& domain() const { return m_domain;}
27 const Domain& domain() const { return m_domain;}
28
28
29 virtual void addPoint(const QPointF& );
29 virtual void addPoint(const QPointF& );
30 virtual void addPoints(const QVector<QPointF>& points);
30 virtual void addPoints(const QVector<QPointF>& points);
31 virtual void removePoint(const QPointF& point);
31 virtual void removePoint(const QPointF& point);
32 virtual void setPoint(const QPointF& oldPoint, const QPointF& newPoint);
32 virtual void setPoint(const QPointF& oldPoint, const QPointF& newPoint);
33 virtual void setPoint(int index,const QPointF& point);
33 virtual void setPoint(int index,const QPointF& point);
34 void setPointsVisible(bool visible);
34 void setPointsVisible(bool visible);
35 void clear();
35 void clear();
36 void clearView();
36 void clearView();
37 int count() const { return m_data.size();}
37 int count() const { return m_data.size();}
38
38
39 const QVector<QPointF>& points(){ return m_data;}
39 const QVector<QPointF>& points(){ return m_data;}
40
40
41 protected:
41 protected:
42 virtual void updateGeometry();
42 virtual void updateGeometry();
43 virtual void updateData();
43 virtual void updateData();
44 virtual void updateDomain();
44 virtual void updateDomain();
45 //refactor
45 //refactor
46 void calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const;
46 void calculatePoint(QPointF& point, int index, const QLineSeries* series,const QSizeF& size, const Domain& domain) const;
47 void calculatePoints(QVector<QPointF>& points,QHash<int,int>& hash,const QLineChartSeries* series, const QSizeF& size, const Domain& domain) const;
47 void calculatePoints(QVector<QPointF>& points,QHash<int,int>& hash,const QLineSeries* series, const QSizeF& size, const Domain& domain) const;
48
48
49 protected slots:
49 protected slots:
50 void handleModelChanged(int index);
50 void handleModelChanged(int index);
51 void handleDomainChanged(const Domain& domain);
51 void handleDomainChanged(const Domain& domain);
52 void handleGeometryChanged(const QRectF& size);
52 void handleGeometryChanged(const QRectF& size);
53
53
54 private:
54 private:
55 ChartPresenter* m_presenter;
55 ChartPresenter* m_presenter;
56 QPainterPath m_path;
56 QPainterPath m_path;
57 QSizeF m_size;
57 QSizeF m_size;
58 QRectF m_rect;
58 QRectF m_rect;
59 QRectF m_clipRect;
59 QRectF m_clipRect;
60 Domain m_domain;
60 Domain m_domain;
61 QList<QGraphicsItem*> m_points;
61 QList<QGraphicsItem*> m_points;
62 QVector<QPointF> m_data;
62 QVector<QPointF> m_data;
63 QHash<int,int> m_hash;
63 QHash<int,int> m_hash;
64 QLineChartSeries* m_series;
64 QLineSeries* m_series;
65 QPen m_pen;
65 QPen m_pen;
66 bool m_dirtyData;
66 bool m_dirtyData;
67 bool m_dirtyGeometry;
67 bool m_dirtyGeometry;
68 bool m_dirtyDomain;
68 bool m_dirtyDomain;
69
69
70 };
70 };
71
71
72 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
73
73
74 #endif
74 #endif
@@ -1,195 +1,195
1 #include "qlinechartseries.h"
1 #include "qlineseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QLineChartSeries
6 \class QLineChartSeries
7 \brief The QLineChartSeries class is used for making line charts.
7 \brief The QLineChartSeries class is used for making line charts.
8
8
9 \mainclass
9 \mainclass
10
10
11 A line chart is used to show information as a series of data points
11 A line chart is used to show information as a series of data points
12 connected by straight lines.
12 connected by straight lines.
13
13
14 \image linechart.png
14 \image linechart.png
15
15
16 To create line charts, users need to first QLineChartSeries object.
16 To create line charts, users need to first QLineChartSeries object.
17
17
18 \snippet ../example/linechart/main.cpp 1
18 \snippet ../example/linechart/main.cpp 1
19
19
20 Populate with the data
20 Populate with the data
21
21
22 \snippet ../example/linechart/main.cpp 2
22 \snippet ../example/linechart/main.cpp 2
23
23
24 Add created series objects to QChartView or QChart instance.
24 Add created series objects to QChartView or QChart instance.
25
25
26 \snippet ../example/linechart/main.cpp 3
26 \snippet ../example/linechart/main.cpp 3
27
27
28 */
28 */
29
29
30 /*!
30 /*!
31 \fn virtual QChartSeriesType QLineChartSeries::type() const
31 \fn virtual QChartSeriesType QLineChartSeries::type() const
32 \brief Returns type of series.
32 \brief Returns type of series.
33 \sa QChartSeries, QChartSeriesType
33 \sa QChartSeries, QChartSeriesType
34 */
34 */
35
35
36 /*!
36 /*!
37 \fn QPen QLineChartSeries::pen() const
37 \fn QPen QLineChartSeries::pen() const
38 \brief Returns the pen used to draw line for this series.
38 \brief Returns the pen used to draw line for this series.
39 \sa setPen()
39 \sa setPen()
40 */
40 */
41
41
42 /*!
42 /*!
43 \fn bool QLineChartSeries::isPointsVisible() const
43 \fn bool QLineChartSeries::isPointsVisible() const
44 \brief Returns if the points are drawn for this series.
44 \brief Returns if the points are drawn for this series.
45 \sa setPointsVisible()
45 \sa setPointsVisible()
46 */
46 */
47
47
48
48
49 /*!
49 /*!
50 \fn void QLineChartSeries::changed(int index)
50 \fn void QLineChartSeries::changed(int index)
51 \brief \internal \a index
51 \brief \internal \a index
52 */
52 */
53
53
54 /*!
54 /*!
55 Constructs empty series object which is a child of \a parent.
55 Constructs empty series object which is a child of \a parent.
56 When series object is added to QChartView or QChart instance ownerships is transfered.
56 When series object is added to QChartView or QChart instance ownerships is transfered.
57 */
57 */
58 QLineChartSeries::QLineChartSeries(QObject* parent):QChartSeries(parent),
58 QLineSeries::QLineSeries(QObject* parent):QChartSeries(parent),
59 m_pointsVisible(false)
59 m_pointsVisible(false)
60 {
60 {
61 }
61 }
62 /*!
62 /*!
63 Destroys the object. Series added to QChartView or QChart instances are owned by those,
63 Destroys the object. Series added to QChartView or QChart instances are owned by those,
64 and are deleted when mentioned object are destroyed.
64 and are deleted when mentioned object are destroyed.
65 */
65 */
66 QLineChartSeries::~QLineChartSeries()
66 QLineSeries::~QLineSeries()
67 {
67 {
68 }
68 }
69
69
70 /*!
70 /*!
71 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
71 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
72 */
72 */
73 void QLineChartSeries::add(qreal x,qreal y)
73 void QLineSeries::add(qreal x,qreal y)
74 {
74 {
75 m_x<<x;
75 m_x<<x;
76 m_y<<y;
76 m_y<<y;
77 }
77 }
78
78
79 /*!
79 /*!
80 This is an overloaded function.
80 This is an overloaded function.
81 Adds data \a point to the series. Points are connected with lines on the chart.
81 Adds data \a point to the series. Points are connected with lines on the chart.
82 */
82 */
83 void QLineChartSeries::add(const QPointF& point)
83 void QLineSeries::add(const QPointF& point)
84 {
84 {
85 m_x<<point.x();
85 m_x<<point.x();
86 m_y<<point.y();
86 m_y<<point.y();
87 }
87 }
88
88
89 /*!
89 /*!
90 Modifies \a y value for given \a x a value.
90 Modifies \a y value for given \a x a value.
91 */
91 */
92 void QLineChartSeries::replace(qreal x,qreal y)
92 void QLineSeries::replace(qreal x,qreal y)
93 {
93 {
94 int index = m_x.indexOf(x);
94 int index = m_x.indexOf(x);
95 m_x[index]=x;
95 m_x[index]=x;
96 m_y[index]=y;
96 m_y[index]=y;
97 emit changed(index);
97 emit changed(index);
98 }
98 }
99
99
100 /*!
100 /*!
101 This is an overloaded function.
101 This is an overloaded function.
102 Replaces current y value of for given \a point x value with \a point y value.
102 Replaces current y value of for given \a point x value with \a point y value.
103 */
103 */
104 void QLineChartSeries::replace(const QPointF& point)
104 void QLineSeries::replace(const QPointF& point)
105 {
105 {
106 int index = m_x.indexOf(point.x());
106 int index = m_x.indexOf(point.x());
107 m_x[index]=point.x();
107 m_x[index]=point.x();
108 m_y[index]=point.y();
108 m_y[index]=point.y();
109 emit changed(index);
109 emit changed(index);
110 }
110 }
111
111
112 /*!
112 /*!
113 Removes current \a x and y value.
113 Removes current \a x and y value.
114 */
114 */
115 void QLineChartSeries::remove(qreal x)
115 void QLineSeries::remove(qreal x)
116 {
116 {
117
117
118 }
118 }
119
119
120 /*!
120 /*!
121 Removes current \a point x value. Note \point y value is ignored.
121 Removes current \a point x value. Note \point y value is ignored.
122 */
122 */
123 void QLineChartSeries::remove(const QPointF& point)
123 void QLineSeries::remove(const QPointF& point)
124 {
124 {
125
125
126 }
126 }
127
127
128 /*!
128 /*!
129 Clears all the data.
129 Clears all the data.
130 */
130 */
131 void QLineChartSeries::clear()
131 void QLineSeries::clear()
132 {
132 {
133 m_x.clear();
133 m_x.clear();
134 m_y.clear();
134 m_y.clear();
135 }
135 }
136
136
137 /*!
137 /*!
138 \internal \a pos
138 \internal \a pos
139 */
139 */
140 qreal QLineChartSeries::x(int pos) const
140 qreal QLineSeries::x(int pos) const
141 {
141 {
142 return m_x.at(pos);
142 return m_x.at(pos);
143 }
143 }
144
144
145 /*!
145 /*!
146 \internal \a pos
146 \internal \a pos
147 */
147 */
148 qreal QLineChartSeries::y(int pos) const
148 qreal QLineSeries::y(int pos) const
149 {
149 {
150 return m_y.at(pos);
150 return m_y.at(pos);
151 }
151 }
152
152
153 /*!
153 /*!
154 Returns number of data points within series.
154 Returns number of data points within series.
155 */
155 */
156 int QLineChartSeries::count() const
156 int QLineSeries::count() const
157 {
157 {
158 Q_ASSERT(m_x.size() == m_y.size());
158 Q_ASSERT(m_x.size() == m_y.size());
159
159
160 return m_x.size();
160 return m_x.size();
161
161
162 }
162 }
163
163
164 /*!
164 /*!
165 Sets \a pen used for drawing given series..
165 Sets \a pen used for drawing given series..
166 */
166 */
167 void QLineChartSeries::setPen(const QPen& pen)
167 void QLineSeries::setPen(const QPen& pen)
168 {
168 {
169 m_pen=pen;
169 m_pen=pen;
170 }
170 }
171
171
172 /*!
172 /*!
173 Sets if data points are \a visible and should be drawn on line.
173 Sets if data points are \a visible and should be drawn on line.
174 */
174 */
175 void QLineChartSeries::setPointsVisible(bool visible)
175 void QLineSeries::setPointsVisible(bool visible)
176 {
176 {
177 m_pointsVisible=visible;
177 m_pointsVisible=visible;
178 }
178 }
179
179
180 QDebug operator<< (QDebug debug, const QLineChartSeries series)
180 QDebug operator<< (QDebug debug, const QLineSeries series)
181 {
181 {
182 Q_ASSERT(series.m_x.size() == series.m_y.size());
182 Q_ASSERT(series.m_x.size() == series.m_y.size());
183
183
184 int size = series.m_x.size();
184 int size = series.m_x.size();
185
185
186 for (int i=0;i<size;i++) {
186 for (int i=0;i<size;i++) {
187 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
187 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
188 }
188 }
189 return debug.space();
189 return debug.space();
190 }
190 }
191
191
192
192
193 #include "moc_qlinechartseries.cpp"
193 #include "moc_qlineseries.cpp"
194
194
195 QTCOMMERCIALCHART_END_NAMESPACE
195 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,52
1 #ifndef QLINECHARTSERIES_H_
1 #ifndef QLINECHARTSERIES_H_
2 #define QLINECHARTSERIES_H_
2 #define QLINECHARTSERIES_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchartseries.h"
5 #include "qchartseries.h"
6 #include <QDebug>
6 #include <QDebug>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QLineChartSeries : public QChartSeries
12 class QTCOMMERCIALCHART_EXPORT QLineSeries : public QChartSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 QLineChartSeries(QObject* parent=0);
16 QLineSeries(QObject* parent=0);
17 virtual ~QLineChartSeries();
17 virtual ~QLineSeries();
18
18
19 public: // from QChartSeries
19 public: // from QChartSeries
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeLine;}
21 void add(qreal x, qreal y);
21 void add(qreal x, qreal y);
22 void add(const QPointF& point);
22 void add(const QPointF& point);
23 void replace(qreal x,qreal y);
23 void replace(qreal x,qreal y);
24 void replace(const QPointF& point);
24 void replace(const QPointF& point);
25 void remove(qreal x);
25 void remove(qreal x);
26 void remove(const QPointF& point);
26 void remove(const QPointF& point);
27 void clear();
27 void clear();
28
28
29 void setPen(const QPen& pen);
29 void setPen(const QPen& pen);
30 QPen pen() const { return m_pen;}
30 QPen pen() const { return m_pen;}
31
31
32 void setPointsVisible(bool visible);
32 void setPointsVisible(bool visible);
33 bool isPointsVisible() const {return m_pointsVisible;}
33 bool isPointsVisible() const {return m_pointsVisible;}
34
34
35 int count() const;
35 int count() const;
36 qreal x(int pos) const;
36 qreal x(int pos) const;
37 qreal y(int pos) const;
37 qreal y(int pos) const;
38 friend QDebug operator<< (QDebug d, const QLineChartSeries series);
38 friend QDebug operator<< (QDebug d, const QLineSeries series);
39
39
40 signals:
40 signals:
41 void changed(int index);
41 void changed(int index);
42
42
43 private:
43 private:
44 QVector<qreal> m_x;
44 QVector<qreal> m_x;
45 QVector<qreal> m_y;
45 QVector<qreal> m_y;
46 QPen m_pen;
46 QPen m_pen;
47 bool m_pointsVisible;
47 bool m_pointsVisible;
48 };
48 };
49
49
50 QTCOMMERCIALCHART_END_NAMESPACE
50 QTCOMMERCIALCHART_END_NAMESPACE
51
51
52 #endif
52 #endif
@@ -1,599 +1,599
1 #include <QtTest/QtTest>
1 #include <QtTest/QtTest>
2 #include <qchartaxis.h>
2 #include <qchartaxis.h>
3 #include <qchartseries.h>
3 #include <qchartseries.h>
4 #include <qlinechartseries.h>
4 #include <qlineseries.h>
5 #include <private/chartdataset_p.h>
5 #include <private/chartdataset_p.h>
6 #include <private/domain_p.h>
6 #include <private/domain_p.h>
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 Q_DECLARE_METATYPE(Domain)
10 Q_DECLARE_METATYPE(Domain)
11 Q_DECLARE_METATYPE(QChartAxis*)
11 Q_DECLARE_METATYPE(QChartAxis*)
12 Q_DECLARE_METATYPE(QChartSeries*)
12 Q_DECLARE_METATYPE(QChartSeries*)
13 Q_DECLARE_METATYPE(QLineChartSeries*)
13 Q_DECLARE_METATYPE(QLineSeries*)
14
14
15 class tst_ChartDataSet: public QObject {
15 class tst_ChartDataSet: public QObject {
16 Q_OBJECT
16 Q_OBJECT
17
17
18 public slots:
18 public slots:
19 void initTestCase();
19 void initTestCase();
20 void cleanupTestCase();
20 void cleanupTestCase();
21 void init();
21 void init();
22 void cleanup();
22 void cleanup();
23
23
24 private slots:
24 private slots:
25 void chartdataset_data();
25 void chartdataset_data();
26 void chartdataset();
26 void chartdataset();
27
27
28 void addDomain_data();
28 void addDomain_data();
29 void addDomain();
29 void addDomain();
30 void addSeries_data();
30 void addSeries_data();
31 void addSeries();
31 void addSeries();
32 void axisY_data();
32 void axisY_data();
33 void axisY();
33 void axisY();
34 void clearDomains_data();
34 void clearDomains_data();
35 void clearDomains();
35 void clearDomains();
36 void domain_data();
36 void domain_data();
37 void domain();
37 void domain();
38 void nextpreviousDomain_data();
38 void nextpreviousDomain_data();
39 void nextpreviousDomain();
39 void nextpreviousDomain();
40 void removeSeries_data();
40 void removeSeries_data();
41 void removeSeries();
41 void removeSeries();
42 void removeAllSeries_data();
42 void removeAllSeries_data();
43 void removeAllSeries();
43 void removeAllSeries();
44 };
44 };
45
45
46 void tst_ChartDataSet::initTestCase()
46 void tst_ChartDataSet::initTestCase()
47 {
47 {
48 qRegisterMetaType<Domain>("Domain");
48 qRegisterMetaType<Domain>("Domain");
49 qRegisterMetaType<QChartAxis*>();
49 qRegisterMetaType<QChartAxis*>();
50 qRegisterMetaType<QChartSeries*>();
50 qRegisterMetaType<QChartSeries*>();
51 }
51 }
52
52
53 void tst_ChartDataSet::cleanupTestCase()
53 void tst_ChartDataSet::cleanupTestCase()
54 {
54 {
55 }
55 }
56
56
57 void tst_ChartDataSet::init()
57 void tst_ChartDataSet::init()
58 {
58 {
59 }
59 }
60
60
61 void tst_ChartDataSet::cleanup()
61 void tst_ChartDataSet::cleanup()
62 {
62 {
63 }
63 }
64
64
65 void tst_ChartDataSet::chartdataset_data()
65 void tst_ChartDataSet::chartdataset_data()
66 {
66 {
67 }
67 }
68
68
69 void tst_ChartDataSet::chartdataset()
69 void tst_ChartDataSet::chartdataset()
70 {
70 {
71 ChartDataSet dataSet;
71 ChartDataSet dataSet;
72 QVERIFY2(dataSet.axisX(), "Missing axisX.");
72 QVERIFY2(dataSet.axisX(), "Missing axisX.");
73 QVERIFY2(dataSet.axisY(), "Missing axisY.");
73 QVERIFY2(dataSet.axisY(), "Missing axisY.");
74 //check if not dangling pointer
74 //check if not dangling pointer
75 dataSet.axisX()->objectName();
75 dataSet.axisX()->objectName();
76 dataSet.axisY()->objectName();
76 dataSet.axisY()->objectName();
77 QVERIFY(dataSet.domain(dataSet.axisX())==Domain());
77 QVERIFY(dataSet.domain(dataSet.axisX())==Domain());
78 QVERIFY(dataSet.domain(dataSet.axisY())==Domain());
78 QVERIFY(dataSet.domain(dataSet.axisY())==Domain());
79 QCOMPARE(dataSet.domainIndex(), 0);
79 QCOMPARE(dataSet.domainIndex(), 0);
80 }
80 }
81
81
82 void tst_ChartDataSet::addDomain_data()
82 void tst_ChartDataSet::addDomain_data()
83 {
83 {
84 QTest::addColumn<QRectF>("rect");
84 QTest::addColumn<QRectF>("rect");
85 QTest::addColumn<QRectF>("viewport");
85 QTest::addColumn<QRectF>("viewport");
86 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
86 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
87 << QRectF(0, 0, 1000, 1000);
87 << QRectF(0, 0, 1000, 1000);
88 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
88 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
89 << QRectF(0, 0, 1000, 1000);
89 << QRectF(0, 0, 1000, 1000);
90 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
90 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
91 << QRectF(0, 0, 1000, 1000);
91 << QRectF(0, 0, 1000, 1000);
92 }
92 }
93
93
94 void tst_ChartDataSet::addDomain()
94 void tst_ChartDataSet::addDomain()
95 {
95 {
96 QFETCH(QRectF, rect);
96 QFETCH(QRectF, rect);
97 QFETCH(QRectF, viewport);
97 QFETCH(QRectF, viewport);
98
98
99 ChartDataSet dataSet;
99 ChartDataSet dataSet;
100
100
101 Domain domain1(0, 1000, 0, 1000);
101 Domain domain1(0, 1000, 0, 1000);
102 QLineChartSeries series;
102 QLineSeries series;
103 series.add(0, 0);
103 series.add(0, 0);
104 series.add(1000, 1000);
104 series.add(1000, 1000);
105
105
106 dataSet.addSeries(&series);
106 dataSet.addSeries(&series);
107
107
108 QCOMPARE(dataSet.domainIndex(), 0);
108 QCOMPARE(dataSet.domainIndex(), 0);
109
109
110 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
110 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
111 QSignalSpy spy1(&dataSet,
111 QSignalSpy spy1(&dataSet,
112 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
112 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
113 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
113 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
114 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
114 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
115 QSignalSpy spy4(&dataSet,
115 QSignalSpy spy4(&dataSet,
116 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
116 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
117 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
117 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
118
118
119 Domain domain2 = dataSet.domain(dataSet.axisY());
119 Domain domain2 = dataSet.domain(dataSet.axisY());
120 QVERIFY(domain1 == domain2);
120 QVERIFY(domain1 == domain2);
121
121
122 dataSet.addDomain(rect, viewport);
122 dataSet.addDomain(rect, viewport);
123 QCOMPARE(dataSet.domainIndex(), 1);
123 QCOMPARE(dataSet.domainIndex(), 1);
124 Domain domain3 = dataSet.domain(dataSet.axisY());
124 Domain domain3 = dataSet.domain(dataSet.axisY());
125 Domain domain4 = domain1.subDomain(rect, viewport.width(),
125 Domain domain4 = domain1.subDomain(rect, viewport.width(),
126 viewport.height());
126 viewport.height());
127 QVERIFY(domain3 == domain4);
127 QVERIFY(domain3 == domain4);
128
128
129 QCOMPARE(spy0.count(), 0);
129 QCOMPARE(spy0.count(), 0);
130 QCOMPARE(spy1.count(), 2);
130 QCOMPARE(spy1.count(), 2);
131 QCOMPARE(spy2.count(), 0);
131 QCOMPARE(spy2.count(), 0);
132 QCOMPARE(spy3.count(), 0);
132 QCOMPARE(spy3.count(), 0);
133 QCOMPARE(spy4.count(), 1);
133 QCOMPARE(spy4.count(), 1);
134 QCOMPARE(spy5.count(), 0);
134 QCOMPARE(spy5.count(), 0);
135 }
135 }
136
136
137 void tst_ChartDataSet::addSeries_data()
137 void tst_ChartDataSet::addSeries_data()
138 {
138 {
139 QTest::addColumn<int>("seriesCount");
139 QTest::addColumn<int>("seriesCount");
140 QTest::addColumn<int>("axisYCount");
140 QTest::addColumn<int>("axisYCount");
141 QTest::newRow("2 series, default axis") << 2 << 0;
141 QTest::newRow("2 series, default axis") << 2 << 0;
142 QTest::newRow("2 series, 2 new axis") << 2 << 2;
142 QTest::newRow("2 series, 2 new axis") << 2 << 2;
143 QTest::newRow("2 series, 1 new axis") << 2 << 2;
143 QTest::newRow("2 series, 1 new axis") << 2 << 2;
144 QTest::newRow("3 series, 3 new axis") << 3 << 3;
144 QTest::newRow("3 series, 3 new axis") << 3 << 3;
145 QTest::newRow("3 series, 2 new axis") << 3 << 2;
145 QTest::newRow("3 series, 2 new axis") << 3 << 2;
146 QTest::newRow("3 series, 1 new axis") << 3 << 1;
146 QTest::newRow("3 series, 1 new axis") << 3 << 1;
147 }
147 }
148
148
149 void tst_ChartDataSet::addSeries()
149 void tst_ChartDataSet::addSeries()
150 {
150 {
151 QFETCH(int, seriesCount);
151 QFETCH(int, seriesCount);
152 QFETCH(int, axisYCount);
152 QFETCH(int, axisYCount);
153
153
154 ChartDataSet dataSet;
154 ChartDataSet dataSet;
155
155
156 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
156 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
157 QSignalSpy spy1(&dataSet,
157 QSignalSpy spy1(&dataSet,
158 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
158 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
159 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
159 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
160 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
160 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
161 QSignalSpy spy4(&dataSet,
161 QSignalSpy spy4(&dataSet,
162 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
162 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
163 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
163 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
164
164
165 QList<QChartAxis*> axisList;
165 QList<QChartAxis*> axisList;
166
166
167 for (int i = 0; i < axisYCount; i++) {
167 for (int i = 0; i < axisYCount; i++) {
168 QChartAxis* axis = new QChartAxis();
168 QChartAxis* axis = new QChartAxis();
169 axisList << axis;
169 axisList << axis;
170 }
170 }
171
171
172 QList<QChartAxis*>::iterator iterator = axisList.begin();
172 QList<QChartAxis*>::iterator iterator = axisList.begin();
173
173
174 for (int i = 0; i < seriesCount; i++) {
174 for (int i = 0; i < seriesCount; i++) {
175 QChartAxis* axisY = 0;
175 QChartAxis* axisY = 0;
176 QLineChartSeries* series = new QLineChartSeries();
176 QLineSeries* series = new QLineSeries();
177 if (iterator != axisList.end()) {
177 if (iterator != axisList.end()) {
178 axisY = *iterator;
178 axisY = *iterator;
179 iterator++;
179 iterator++;
180 } else if (axisList.count() > 0) {
180 } else if (axisList.count() > 0) {
181 iterator--;
181 iterator--;
182 axisY = *iterator;
182 axisY = *iterator;
183 iterator++;
183 iterator++;
184 }
184 }
185 dataSet.addSeries(series, axisY);
185 dataSet.addSeries(series, axisY);
186 }
186 }
187
187
188 //default axis
188 //default axis
189 if (axisYCount == 0)
189 if (axisYCount == 0)
190 axisYCount+=2;
190 axisYCount+=2;
191 else
191 else
192 axisYCount++;
192 axisYCount++;
193
193
194 QCOMPARE(spy0.count(), axisYCount);
194 QCOMPARE(spy0.count(), axisYCount);
195 QCOMPARE(spy1.count(), seriesCount*2);
195 QCOMPARE(spy1.count(), seriesCount*2);
196 QCOMPARE(spy2.count(), 0);
196 QCOMPARE(spy2.count(), 0);
197 QCOMPARE(spy3.count(), seriesCount);
197 QCOMPARE(spy3.count(), seriesCount);
198 QCOMPARE(spy4.count(), seriesCount);
198 QCOMPARE(spy4.count(), seriesCount);
199 QCOMPARE(spy5.count(), 0);
199 QCOMPARE(spy5.count(), 0);
200
200
201 QCOMPARE(dataSet.domainIndex(), 0);
201 QCOMPARE(dataSet.domainIndex(), 0);
202 }
202 }
203
203
204 void tst_ChartDataSet::axisY_data()
204 void tst_ChartDataSet::axisY_data()
205 {
205 {
206 QTest::addColumn<QChartAxis*>("axisY");
206 QTest::addColumn<QChartAxis*>("axisY");
207 QTest::newRow("axisY1") << new QChartAxis();
207 QTest::newRow("axisY1") << new QChartAxis();
208 QTest::newRow("axisY2") << new QChartAxis();
208 QTest::newRow("axisY2") << new QChartAxis();
209 }
209 }
210
210
211 void tst_ChartDataSet::axisY()
211 void tst_ChartDataSet::axisY()
212 {
212 {
213 QFETCH(QChartAxis*, axisY);
213 QFETCH(QChartAxis*, axisY);
214
214
215 ChartDataSet dataSet;
215 ChartDataSet dataSet;
216
216
217 QChartAxis* defaultAxisY = dataSet.axisY();
217 QChartAxis* defaultAxisY = dataSet.axisY();
218
218
219 QVERIFY2(defaultAxisY, "Missing axisY.");
219 QVERIFY2(defaultAxisY, "Missing axisY.");
220
220
221 QLineChartSeries* series1 = new QLineChartSeries();
221 QLineSeries* series1 = new QLineSeries();
222 dataSet.addSeries(series1);
222 dataSet.addSeries(series1);
223
223
224 QLineChartSeries* series2 = new QLineChartSeries();
224 QLineSeries* series2 = new QLineSeries();
225 dataSet.addSeries(series2, axisY);
225 dataSet.addSeries(series2, axisY);
226
226
227 QVERIFY(dataSet.axisY(series1) == defaultAxisY);
227 QVERIFY(dataSet.axisY(series1) == defaultAxisY);
228 QVERIFY(dataSet.axisY(series2) == axisY);
228 QVERIFY(dataSet.axisY(series2) == axisY);
229
229
230
230
231 }
231 }
232
232
233 void tst_ChartDataSet::clearDomains_data()
233 void tst_ChartDataSet::clearDomains_data()
234 {
234 {
235 QTest::addColumn<int>("indexCount");
235 QTest::addColumn<int>("indexCount");
236 QTest::newRow("0") << 0;
236 QTest::newRow("0") << 0;
237 QTest::newRow("1") << 1;
237 QTest::newRow("1") << 1;
238 QTest::newRow("5") << 2;
238 QTest::newRow("5") << 2;
239 QTest::newRow("8") << 3;
239 QTest::newRow("8") << 3;
240 }
240 }
241
241
242 void tst_ChartDataSet::clearDomains()
242 void tst_ChartDataSet::clearDomains()
243 {
243 {
244 QFETCH(int, indexCount);
244 QFETCH(int, indexCount);
245
245
246 Domain domain1(0, 100, 0, 100);
246 Domain domain1(0, 100, 0, 100);
247 QLineChartSeries* series = new QLineChartSeries();
247 QLineSeries* series = new QLineSeries();
248 series->add(0, 0);
248 series->add(0, 0);
249 series->add(100, 100);
249 series->add(100, 100);
250
250
251 ChartDataSet dataSet;
251 ChartDataSet dataSet;
252
252
253 QCOMPARE(dataSet.domainIndex(), 0);
253 QCOMPARE(dataSet.domainIndex(), 0);
254
254
255 dataSet.addSeries(series);
255 dataSet.addSeries(series);
256
256
257 Domain domain2 = dataSet.domain(dataSet.axisY());
257 Domain domain2 = dataSet.domain(dataSet.axisY());
258
258
259 QVERIFY(domain2 == domain1);
259 QVERIFY(domain2 == domain1);
260
260
261 QList<Domain> domains;
261 QList<Domain> domains;
262
262
263 domains << domain1;
263 domains << domain1;
264
264
265 for (int i = 0; i < indexCount; i++) {
265 for (int i = 0; i < indexCount; i++) {
266 dataSet.addDomain(QRect(0, 0, 10, 10), QRect(0, 0, 100, 100));
266 dataSet.addDomain(QRect(0, 0, 10, 10), QRect(0, 0, 100, 100));
267 domains << dataSet.domain(dataSet.axisY());
267 domains << dataSet.domain(dataSet.axisY());
268 }
268 }
269
269
270 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
270 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
271 QSignalSpy spy1(&dataSet,
271 QSignalSpy spy1(&dataSet,
272 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
272 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
273 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
273 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
274 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
274 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
275 QSignalSpy spy4(&dataSet,
275 QSignalSpy spy4(&dataSet,
276 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
276 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
277 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
277 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
278
278
279 dataSet.clearDomains(indexCount);
279 dataSet.clearDomains(indexCount);
280
280
281 QCOMPARE(dataSet.domainIndex(), indexCount);
281 QCOMPARE(dataSet.domainIndex(), indexCount);
282
282
283 domain2 = dataSet.domain(dataSet.axisY());
283 domain2 = dataSet.domain(dataSet.axisY());
284
284
285 QVERIFY(domain2 == domains.at(indexCount));
285 QVERIFY(domain2 == domains.at(indexCount));
286
286
287 QCOMPARE(spy0.count(), 0);
287 QCOMPARE(spy0.count(), 0);
288 QCOMPARE(spy1.count(), 0);
288 QCOMPARE(spy1.count(), 0);
289 QCOMPARE(spy2.count(), 0);
289 QCOMPARE(spy2.count(), 0);
290 QCOMPARE(spy3.count(), 0);
290 QCOMPARE(spy3.count(), 0);
291 QCOMPARE(spy4.count(), 0);
291 QCOMPARE(spy4.count(), 0);
292 QCOMPARE(spy5.count(), 0);
292 QCOMPARE(spy5.count(), 0);
293 }
293 }
294
294
295 void tst_ChartDataSet::domain_data()
295 void tst_ChartDataSet::domain_data()
296 {
296 {
297 QTest::addColumn<Domain>("domain1");
297 QTest::addColumn<Domain>("domain1");
298 QTest::addColumn<Domain>("domain2");
298 QTest::addColumn<Domain>("domain2");
299 QTest::addColumn<Domain>("domain3");
299 QTest::addColumn<Domain>("domain3");
300 QTest::addColumn<Domain>("domain");
300 QTest::addColumn<Domain>("domain");
301 QTest::newRow("Domain(0,10,0,10)") << Domain(0, 10, 0, 10)
301 QTest::newRow("Domain(0,10,0,10)") << Domain(0, 10, 0, 10)
302 << Domain(0, 5, 0, 5) << Domain(0, 3, 0, 3) << Domain(0, 10, 0, 10);
302 << Domain(0, 5, 0, 5) << Domain(0, 3, 0, 3) << Domain(0, 10, 0, 10);
303 QTest::newRow("Domain(-1,11,0,11)") << Domain(-1, 10, 0, 10)
303 QTest::newRow("Domain(-1,11,0,11)") << Domain(-1, 10, 0, 10)
304 << Domain(0, 11, 0, 11) << Domain(0, 3, 0, 3)
304 << Domain(0, 11, 0, 11) << Domain(0, 3, 0, 3)
305 << Domain(-1, 11, 0, 11);
305 << Domain(-1, 11, 0, 11);
306 QTest::newRow("Domain(-5,5,1,8)") << Domain(-5, 0, 1, 1)
306 QTest::newRow("Domain(-5,5,1,8)") << Domain(-5, 0, 1, 1)
307 << Domain(0, 5, 0, 8) << Domain(1, 2, 1, 2) << Domain(-5, 5, 0, 8);
307 << Domain(0, 5, 0, 8) << Domain(1, 2, 1, 2) << Domain(-5, 5, 0, 8);
308 }
308 }
309
309
310 void tst_ChartDataSet::domain()
310 void tst_ChartDataSet::domain()
311 {
311 {
312 QFETCH(Domain, domain1);
312 QFETCH(Domain, domain1);
313 QFETCH(Domain, domain2);
313 QFETCH(Domain, domain2);
314 QFETCH(Domain, domain3);
314 QFETCH(Domain, domain3);
315 QFETCH(Domain, domain);
315 QFETCH(Domain, domain);
316
316
317 ChartDataSet dataSet;
317 ChartDataSet dataSet;
318 QLineChartSeries* series1 = new QLineChartSeries();
318 QLineSeries* series1 = new QLineSeries();
319 series1->add(domain1.m_minX, domain1.m_minY);
319 series1->add(domain1.m_minX, domain1.m_minY);
320 series1->add(domain1.m_maxX, domain1.m_maxY);
320 series1->add(domain1.m_maxX, domain1.m_maxY);
321 QLineChartSeries* series2 = new QLineChartSeries();
321 QLineSeries* series2 = new QLineSeries();
322 series2->add(domain2.m_minX, domain2.m_minY);
322 series2->add(domain2.m_minX, domain2.m_minY);
323 series2->add(domain2.m_maxX, domain2.m_maxY);
323 series2->add(domain2.m_maxX, domain2.m_maxY);
324 QLineChartSeries* series3 = new QLineChartSeries();
324 QLineSeries* series3 = new QLineSeries();
325 series3->add(domain3.m_minX, domain3.m_minY);
325 series3->add(domain3.m_minX, domain3.m_minY);
326 series3->add(domain3.m_maxX, domain3.m_maxY);
326 series3->add(domain3.m_maxX, domain3.m_maxY);
327
327
328 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
328 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
329 QSignalSpy spy1(&dataSet,
329 QSignalSpy spy1(&dataSet,
330 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
330 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
331 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
331 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
332 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
332 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
333 QSignalSpy spy4(&dataSet,
333 QSignalSpy spy4(&dataSet,
334 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
334 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
335 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
335 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
336
336
337 dataSet.addSeries(series1);
337 dataSet.addSeries(series1);
338 dataSet.addSeries(series2);
338 dataSet.addSeries(series2);
339 dataSet.addSeries(series3);
339 dataSet.addSeries(series3);
340
340
341 QCOMPARE(dataSet.domainIndex(), 0);
341 QCOMPARE(dataSet.domainIndex(), 0);
342 QVERIFY2(dataSet.domain(dataSet.axisY()) == domain, "Domain not equal");
342 QVERIFY2(dataSet.domain(dataSet.axisY()) == domain, "Domain not equal");
343
343
344 QCOMPARE(spy0.count(), 2);
344 QCOMPARE(spy0.count(), 2);
345 QCOMPARE(spy1.count(), 6);
345 QCOMPARE(spy1.count(), 6);
346 QCOMPARE(spy2.count(), 0);
346 QCOMPARE(spy2.count(), 0);
347 QCOMPARE(spy3.count(), 3);
347 QCOMPARE(spy3.count(), 3);
348 QCOMPARE(spy4.count(), 3);
348 QCOMPARE(spy4.count(), 3);
349 QCOMPARE(spy5.count(), 0);
349 QCOMPARE(spy5.count(), 0);
350 }
350 }
351
351
352 void tst_ChartDataSet::nextpreviousDomain_data()
352 void tst_ChartDataSet::nextpreviousDomain_data()
353 {
353 {
354 QTest::addColumn<QRectF>("rect");
354 QTest::addColumn<QRectF>("rect");
355 QTest::addColumn<QRectF>("viewport");
355 QTest::addColumn<QRectF>("viewport");
356 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
356 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
357 << QRectF(0, 0, 1000, 1000);
357 << QRectF(0, 0, 1000, 1000);
358 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
358 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
359 << QRectF(0, 0, 1000, 1000);
359 << QRectF(0, 0, 1000, 1000);
360 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
360 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
361 << QRectF(0, 0, 1000, 1000);
361 << QRectF(0, 0, 1000, 1000);
362 }
362 }
363
363
364 void tst_ChartDataSet::nextpreviousDomain()
364 void tst_ChartDataSet::nextpreviousDomain()
365 {
365 {
366
366
367 QFETCH(QRectF, rect);
367 QFETCH(QRectF, rect);
368 QFETCH(QRectF, viewport);
368 QFETCH(QRectF, viewport);
369
369
370 ChartDataSet dataSet;
370 ChartDataSet dataSet;
371
371
372 Domain domain1(0, 1000, 0, 1000);
372 Domain domain1(0, 1000, 0, 1000);
373 QLineChartSeries* series = new QLineChartSeries();
373 QLineSeries* series = new QLineSeries();
374 series->add(0, 0);
374 series->add(0, 0);
375 series->add(1000, 1000);
375 series->add(1000, 1000);
376
376
377 dataSet.addSeries(series);
377 dataSet.addSeries(series);
378
378
379 QCOMPARE(dataSet.domainIndex(), 0);
379 QCOMPARE(dataSet.domainIndex(), 0);
380
380
381 Domain domain2 = dataSet.domain(dataSet.axisY());
381 Domain domain2 = dataSet.domain(dataSet.axisY());
382 QVERIFY(domain1 == domain2);
382 QVERIFY(domain1 == domain2);
383
383
384 dataSet.addDomain(rect, viewport);
384 dataSet.addDomain(rect, viewport);
385 QCOMPARE(dataSet.domainIndex(), 1);
385 QCOMPARE(dataSet.domainIndex(), 1);
386 Domain domain3 = dataSet.domain(dataSet.axisY());
386 Domain domain3 = dataSet.domain(dataSet.axisY());
387 Domain domain4 = domain1.subDomain(rect, viewport.width(),
387 Domain domain4 = domain1.subDomain(rect, viewport.width(),
388 viewport.height());
388 viewport.height());
389 QVERIFY(domain3 == domain4);
389 QVERIFY(domain3 == domain4);
390
390
391 dataSet.addDomain(rect, viewport);
391 dataSet.addDomain(rect, viewport);
392 QCOMPARE(dataSet.domainIndex(), 2);
392 QCOMPARE(dataSet.domainIndex(), 2);
393 Domain domain5 = dataSet.domain(dataSet.axisY());
393 Domain domain5 = dataSet.domain(dataSet.axisY());
394 Domain domain6 = domain3.subDomain(rect, viewport.width(),
394 Domain domain6 = domain3.subDomain(rect, viewport.width(),
395 viewport.height());
395 viewport.height());
396 QVERIFY(domain5 == domain6);
396 QVERIFY(domain5 == domain6);
397
397
398 dataSet.addDomain(rect, viewport);
398 dataSet.addDomain(rect, viewport);
399 QCOMPARE(dataSet.domainIndex(), 3);
399 QCOMPARE(dataSet.domainIndex(), 3);
400 Domain domain7 = dataSet.domain(dataSet.axisY());
400 Domain domain7 = dataSet.domain(dataSet.axisY());
401 Domain domain8 = domain5.subDomain(rect, viewport.width(),
401 Domain domain8 = domain5.subDomain(rect, viewport.width(),
402 viewport.height());
402 viewport.height());
403 QVERIFY(domain7 == domain8);
403 QVERIFY(domain7 == domain8);
404
404
405 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
405 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
406 QSignalSpy spy1(&dataSet,
406 QSignalSpy spy1(&dataSet,
407 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
407 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
408 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
408 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
409 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
409 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
410 QSignalSpy spy4(&dataSet,
410 QSignalSpy spy4(&dataSet,
411 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
411 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
412 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
412 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
413
413
414 Domain domain;
414 Domain domain;
415
415
416 bool previous = dataSet.previousDomain();
416 bool previous = dataSet.previousDomain();
417 QCOMPARE(previous, true);
417 QCOMPARE(previous, true);
418 QCOMPARE(dataSet.domainIndex(), 2);
418 QCOMPARE(dataSet.domainIndex(), 2);
419 domain = dataSet.domain(dataSet.axisY());
419 domain = dataSet.domain(dataSet.axisY());
420 QVERIFY(domain == domain5);
420 QVERIFY(domain == domain5);
421 previous = dataSet.previousDomain();
421 previous = dataSet.previousDomain();
422 QCOMPARE(previous, true);
422 QCOMPARE(previous, true);
423 QCOMPARE(dataSet.domainIndex(), 1);
423 QCOMPARE(dataSet.domainIndex(), 1);
424 domain = dataSet.domain(dataSet.axisY());
424 domain = dataSet.domain(dataSet.axisY());
425 QVERIFY(domain == domain3);
425 QVERIFY(domain == domain3);
426 previous = dataSet.previousDomain();
426 previous = dataSet.previousDomain();
427 QCOMPARE(previous, true);
427 QCOMPARE(previous, true);
428 QCOMPARE(dataSet.domainIndex(), 0);
428 QCOMPARE(dataSet.domainIndex(), 0);
429 domain = dataSet.domain(dataSet.axisY());
429 domain = dataSet.domain(dataSet.axisY());
430 QVERIFY(domain == domain1);
430 QVERIFY(domain == domain1);
431 previous = dataSet.previousDomain();
431 previous = dataSet.previousDomain();
432 QCOMPARE(previous, false);
432 QCOMPARE(previous, false);
433 QCOMPARE(dataSet.domainIndex(), 0);
433 QCOMPARE(dataSet.domainIndex(), 0);
434 domain = dataSet.domain(dataSet.axisY());
434 domain = dataSet.domain(dataSet.axisY());
435 QVERIFY(domain == domain1);
435 QVERIFY(domain == domain1);
436
436
437 bool next = dataSet.nextDomain();
437 bool next = dataSet.nextDomain();
438 QCOMPARE(next, true);
438 QCOMPARE(next, true);
439 QCOMPARE(dataSet.domainIndex(),1);
439 QCOMPARE(dataSet.domainIndex(),1);
440 next = dataSet.nextDomain();
440 next = dataSet.nextDomain();
441 QCOMPARE(next, true);
441 QCOMPARE(next, true);
442 QCOMPARE(dataSet.domainIndex(),2);
442 QCOMPARE(dataSet.domainIndex(),2);
443 next = dataSet.nextDomain();
443 next = dataSet.nextDomain();
444 QCOMPARE(next, true);
444 QCOMPARE(next, true);
445 QCOMPARE(dataSet.domainIndex(),3);
445 QCOMPARE(dataSet.domainIndex(),3);
446 next = dataSet.nextDomain();
446 next = dataSet.nextDomain();
447 QCOMPARE(next, false);
447 QCOMPARE(next, false);
448 QCOMPARE(dataSet.domainIndex(),3);
448 QCOMPARE(dataSet.domainIndex(),3);
449
449
450
450
451 QCOMPARE(spy0.count(), 0);
451 QCOMPARE(spy0.count(), 0);
452 QCOMPARE(spy1.count(), 12);
452 QCOMPARE(spy1.count(), 12);
453 QCOMPARE(spy2.count(), 0);
453 QCOMPARE(spy2.count(), 0);
454 QCOMPARE(spy3.count(), 0);
454 QCOMPARE(spy3.count(), 0);
455 QCOMPARE(spy4.count(), 6);
455 QCOMPARE(spy4.count(), 6);
456 QCOMPARE(spy5.count(), 0);
456 QCOMPARE(spy5.count(), 0);
457 }
457 }
458
458
459 void tst_ChartDataSet::removeSeries_data()
459 void tst_ChartDataSet::removeSeries_data()
460 {
460 {
461 QTest::addColumn<int>("seriesCount");
461 QTest::addColumn<int>("seriesCount");
462 QTest::addColumn<int>("axisYCount");
462 QTest::addColumn<int>("axisYCount");
463 QTest::newRow("2 series, default axis") << 2 << 0;
463 QTest::newRow("2 series, default axis") << 2 << 0;
464 QTest::newRow("2 series, 2 new axis") << 2 << 2;
464 QTest::newRow("2 series, 2 new axis") << 2 << 2;
465 QTest::newRow("2 series, 1 new axis") << 2 << 2;
465 QTest::newRow("2 series, 1 new axis") << 2 << 2;
466 QTest::newRow("3 series, 3 new axis") << 3 << 3;
466 QTest::newRow("3 series, 3 new axis") << 3 << 3;
467 QTest::newRow("3 series, 2 new axis") << 3 << 2;
467 QTest::newRow("3 series, 2 new axis") << 3 << 2;
468 QTest::newRow("3 series, 1 new axis") << 3 << 1;
468 QTest::newRow("3 series, 1 new axis") << 3 << 1;
469 }
469 }
470
470
471 void tst_ChartDataSet::removeSeries()
471 void tst_ChartDataSet::removeSeries()
472 {
472 {
473 QFETCH(int, seriesCount);
473 QFETCH(int, seriesCount);
474 QFETCH(int, axisYCount);
474 QFETCH(int, axisYCount);
475
475
476 ChartDataSet dataSet;
476 ChartDataSet dataSet;
477
477
478 QList<QChartAxis*> axisList;
478 QList<QChartAxis*> axisList;
479 QList<QChartSeries*> seriesList;
479 QList<QChartSeries*> seriesList;
480
480
481 for (int i = 0; i < axisYCount; i++) {
481 for (int i = 0; i < axisYCount; i++) {
482 QChartAxis* axis = new QChartAxis();
482 QChartAxis* axis = new QChartAxis();
483 axisList << axis;
483 axisList << axis;
484 }
484 }
485
485
486 QList<QChartAxis*>::iterator iterator = axisList.begin();
486 QList<QChartAxis*>::iterator iterator = axisList.begin();
487
487
488 for (int i = 0; i < seriesCount; i++) {
488 for (int i = 0; i < seriesCount; i++) {
489 QChartAxis* axisY = 0;
489 QChartAxis* axisY = 0;
490 QLineChartSeries* series = new QLineChartSeries();
490 QLineSeries* series = new QLineSeries();
491 if (iterator != axisList.end()) {
491 if (iterator != axisList.end()) {
492 axisY = *iterator;
492 axisY = *iterator;
493 iterator++;
493 iterator++;
494 } else if (axisList.count() > 0) {
494 } else if (axisList.count() > 0) {
495 iterator--;
495 iterator--;
496 axisY = *iterator;
496 axisY = *iterator;
497 iterator++;
497 iterator++;
498 }
498 }
499 dataSet.addSeries(series, axisY);
499 dataSet.addSeries(series, axisY);
500 seriesList << series;
500 seriesList << series;
501 }
501 }
502
502
503 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
503 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
504 QSignalSpy spy1(&dataSet,
504 QSignalSpy spy1(&dataSet,
505 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
505 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
506 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
506 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
507 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
507 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
508 QSignalSpy spy4(&dataSet,
508 QSignalSpy spy4(&dataSet,
509 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
509 SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
510 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
510 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
511
511
512 for (int i = 0; i < seriesCount; i++) {
512 for (int i = 0; i < seriesCount; i++) {
513 dataSet.removeSeries(seriesList.at(i));
513 dataSet.removeSeries(seriesList.at(i));
514 }
514 }
515
515
516 //default axis
516 //default axis
517 if (axisYCount == 0)
517 if (axisYCount == 0)
518 axisYCount++;
518 axisYCount++;
519
519
520 QCOMPARE(spy0.count(), 0);
520 QCOMPARE(spy0.count(), 0);
521 QCOMPARE(spy1.count(), 0);
521 QCOMPARE(spy1.count(), 0);
522 QCOMPARE(spy2.count(), axisYCount);
522 QCOMPARE(spy2.count(), axisYCount);
523 QCOMPARE(spy3.count(), 0);
523 QCOMPARE(spy3.count(), 0);
524 QCOMPARE(spy4.count(), 0);
524 QCOMPARE(spy4.count(), 0);
525 QCOMPARE(spy5.count(), seriesCount);
525 QCOMPARE(spy5.count(), seriesCount);
526
526
527 QCOMPARE(dataSet.domainIndex(), 0);
527 QCOMPARE(dataSet.domainIndex(), 0);
528
528
529 qDeleteAll(seriesList);
529 qDeleteAll(seriesList);
530 }
530 }
531
531
532 void tst_ChartDataSet::removeAllSeries_data()
532 void tst_ChartDataSet::removeAllSeries_data()
533 {
533 {
534 QTest::addColumn<int>("seriesCount");
534 QTest::addColumn<int>("seriesCount");
535 QTest::addColumn<int>("axisYCount");
535 QTest::addColumn<int>("axisYCount");
536 QTest::newRow("2 series, default axis") << 2 << 0;
536 QTest::newRow("2 series, default axis") << 2 << 0;
537 QTest::newRow("2 series, 2 new axis") << 2 << 2;
537 QTest::newRow("2 series, 2 new axis") << 2 << 2;
538 QTest::newRow("2 series, 1 new axis") << 2 << 2;
538 QTest::newRow("2 series, 1 new axis") << 2 << 2;
539 QTest::newRow("3 series, 3 new axis") << 3 << 3;
539 QTest::newRow("3 series, 3 new axis") << 3 << 3;
540 QTest::newRow("3 series, 2 new axis") << 3 << 2;
540 QTest::newRow("3 series, 2 new axis") << 3 << 2;
541 QTest::newRow("3 series, 1 new axis") << 3 << 1;
541 QTest::newRow("3 series, 1 new axis") << 3 << 1;
542 }
542 }
543
543
544 void tst_ChartDataSet::removeAllSeries()
544 void tst_ChartDataSet::removeAllSeries()
545 {
545 {
546 QFETCH(int, seriesCount);
546 QFETCH(int, seriesCount);
547 QFETCH(int, axisYCount);
547 QFETCH(int, axisYCount);
548
548
549 ChartDataSet dataSet;
549 ChartDataSet dataSet;
550
550
551 QList<QChartAxis*> axisList;
551 QList<QChartAxis*> axisList;
552
552
553 for (int i = 0; i < axisYCount; i++) {
553 for (int i = 0; i < axisYCount; i++) {
554 QChartAxis* axis = new QChartAxis();
554 QChartAxis* axis = new QChartAxis();
555 axisList << axis;
555 axisList << axis;
556 }
556 }
557
557
558 QList<QChartAxis*>::iterator iterator = axisList.begin();
558 QList<QChartAxis*>::iterator iterator = axisList.begin();
559
559
560 for (int i = 0; i < seriesCount; i++) {
560 for (int i = 0; i < seriesCount; i++) {
561 QChartAxis* axisY = 0;
561 QChartAxis* axisY = 0;
562 QLineChartSeries* series = new QLineChartSeries();
562 QLineSeries* series = new QLineSeries();
563 if (iterator != axisList.end()) {
563 if (iterator != axisList.end()) {
564 axisY = *iterator;
564 axisY = *iterator;
565 iterator++;
565 iterator++;
566 } else if (axisList.count() > 0) {
566 } else if (axisList.count() > 0) {
567 iterator--;
567 iterator--;
568 axisY = *iterator;
568 axisY = *iterator;
569 iterator++;
569 iterator++;
570 }
570 }
571 dataSet.addSeries(series, axisY);
571 dataSet.addSeries(series, axisY);
572 }
572 }
573
573
574 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
574 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
575 QSignalSpy spy1(&dataSet, SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
575 QSignalSpy spy1(&dataSet, SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
576 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
576 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
577 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
577 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*)));
578 QSignalSpy spy4(&dataSet, SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
578 QSignalSpy spy4(&dataSet, SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&)));
579 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
579 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*)));
580
580
581 dataSet.removeAllSeries();
581 dataSet.removeAllSeries();
582 //default axis
582 //default axis
583 if (axisYCount == 0)
583 if (axisYCount == 0)
584 axisYCount++;
584 axisYCount++;
585
585
586 QCOMPARE(spy0.count(), 0);
586 QCOMPARE(spy0.count(), 0);
587 QCOMPARE(spy1.count(), 0);
587 QCOMPARE(spy1.count(), 0);
588 QCOMPARE(spy2.count(), axisYCount);
588 QCOMPARE(spy2.count(), axisYCount);
589 QCOMPARE(spy3.count(), 0);
589 QCOMPARE(spy3.count(), 0);
590 QCOMPARE(spy4.count(), 0);
590 QCOMPARE(spy4.count(), 0);
591 QCOMPARE(spy5.count(), seriesCount);
591 QCOMPARE(spy5.count(), seriesCount);
592
592
593 QCOMPARE(dataSet.domainIndex(), 0);
593 QCOMPARE(dataSet.domainIndex(), 0);
594 }
594 }
595
595
596
596
597 QTEST_MAIN(tst_ChartDataSet)
597 QTEST_MAIN(tst_ChartDataSet)
598 #include "tst_chartdataset.moc"
598 #include "tst_chartdataset.moc"
599
599
@@ -1,372 +1,372
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include "dataseriedialog.h"
2 #include "dataseriedialog.h"
3 #include "qchartseries.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include "qscatterseries.h"
5 #include "qscatterseries.h"
6 #include <qlinechartseries.h>
6 #include <qlineseries.h>
7 #include <qbarset.h>
7 #include <qbarset.h>
8 #include <qbarcategory.h>
8 #include <qbarcategory.h>
9 #include <qbarseries.h>
9 #include <qbarseries.h>
10 #include <qstackedbarseries.h>
10 #include <qstackedbarseries.h>
11 #include <qpercentbarseries.h>
11 #include <qpercentbarseries.h>
12 #include <QPushButton>
12 #include <QPushButton>
13 #include <QComboBox>
13 #include <QComboBox>
14 #include <QSpinBox>
14 #include <QSpinBox>
15 #include <QCheckBox>
15 #include <QCheckBox>
16 #include <QGridLayout>
16 #include <QGridLayout>
17 #include <QHBoxLayout>
17 #include <QHBoxLayout>
18 #include <QLabel>
18 #include <QLabel>
19 #include <QSpacerItem>
19 #include <QSpacerItem>
20 #include <QMessageBox>
20 #include <QMessageBox>
21 #include <cmath>
21 #include <cmath>
22 #include <QDebug>
22 #include <QDebug>
23 #include <QStandardItemModel>
23 #include <QStandardItemModel>
24
24
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 MainWidget::MainWidget(QWidget *parent) :
28 MainWidget::MainWidget(QWidget *parent) :
29 QWidget(parent)
29 QWidget(parent)
30 {
30 {
31 m_chartWidget = new QChartView(this);
31 m_chartWidget = new QChartView(this);
32 m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand);
32 m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand);
33
33
34 // Grid layout for the controls for configuring the chart widget
34 // Grid layout for the controls for configuring the chart widget
35 QGridLayout *grid = new QGridLayout();
35 QGridLayout *grid = new QGridLayout();
36 QPushButton *addSeriesButton = new QPushButton("Add series");
36 QPushButton *addSeriesButton = new QPushButton("Add series");
37 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
37 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
38 grid->addWidget(addSeriesButton, 0, 1);
38 grid->addWidget(addSeriesButton, 0, 1);
39 initBackroundCombo(grid);
39 initBackroundCombo(grid);
40 initScaleControls(grid);
40 initScaleControls(grid);
41 initThemeCombo(grid);
41 initThemeCombo(grid);
42 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
42 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
43 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
43 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
44 zoomCheckBox->setChecked(true);
44 zoomCheckBox->setChecked(true);
45 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
45 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
46 // add row with empty label to make all the other rows static
46 // add row with empty label to make all the other rows static
47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
47 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
48 grid->setRowStretch(grid->rowCount() - 1, 1);
48 grid->setRowStretch(grid->rowCount() - 1, 1);
49
49
50 // Another grid layout as a main layout
50 // Another grid layout as a main layout
51 QGridLayout *mainLayout = new QGridLayout();
51 QGridLayout *mainLayout = new QGridLayout();
52 mainLayout->addLayout(grid, 0, 0);
52 mainLayout->addLayout(grid, 0, 0);
53
53
54 // Init series type specific controls
54 // Init series type specific controls
55 initPieControls();
55 initPieControls();
56 mainLayout->addLayout(m_pieLayout, 2, 0);
56 mainLayout->addLayout(m_pieLayout, 2, 0);
57 // Scatter series specific settings
57 // Scatter series specific settings
58 // m_scatterLayout = new QGridLayout();
58 // m_scatterLayout = new QGridLayout();
59 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
59 // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
60 // m_scatterLayout->setEnabled(false);
60 // m_scatterLayout->setEnabled(false);
61 // mainLayout->addLayout(m_scatterLayout, 1, 0);
61 // mainLayout->addLayout(m_scatterLayout, 1, 0);
62
62
63 // Add layouts and the chart widget to the main layout
63 // Add layouts and the chart widget to the main layout
64 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
64 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
65 setLayout(mainLayout);
65 setLayout(mainLayout);
66 }
66 }
67
67
68 // Combo box for selecting the chart's background
68 // Combo box for selecting the chart's background
69 void MainWidget::initBackroundCombo(QGridLayout *grid)
69 void MainWidget::initBackroundCombo(QGridLayout *grid)
70 {
70 {
71 QComboBox *backgroundCombo = new QComboBox(this);
71 QComboBox *backgroundCombo = new QComboBox(this);
72 backgroundCombo->addItem("Color");
72 backgroundCombo->addItem("Color");
73 backgroundCombo->addItem("Gradient");
73 backgroundCombo->addItem("Gradient");
74 backgroundCombo->addItem("Image");
74 backgroundCombo->addItem("Image");
75 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
75 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
76 this, SLOT(backgroundChanged(int)));
76 this, SLOT(backgroundChanged(int)));
77
77
78 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
78 grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0);
79 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
79 grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1);
80 }
80 }
81
81
82 // Scale related controls (auto-scale vs. manual min-max values)
82 // Scale related controls (auto-scale vs. manual min-max values)
83 void MainWidget::initScaleControls(QGridLayout *grid)
83 void MainWidget::initScaleControls(QGridLayout *grid)
84 {
84 {
85 m_autoScaleCheck = new QCheckBox("Automatic scaling");
85 m_autoScaleCheck = new QCheckBox("Automatic scaling");
86 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
86 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
87 // Allow setting also non-sense values (like -2147483648 and 2147483647)
87 // Allow setting also non-sense values (like -2147483648 and 2147483647)
88 m_xMinSpin = new QSpinBox();
88 m_xMinSpin = new QSpinBox();
89 m_xMinSpin->setMinimum(INT_MIN);
89 m_xMinSpin->setMinimum(INT_MIN);
90 m_xMinSpin->setMaximum(INT_MAX);
90 m_xMinSpin->setMaximum(INT_MAX);
91 m_xMinSpin->setValue(0);
91 m_xMinSpin->setValue(0);
92 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
92 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
93 m_xMaxSpin = new QSpinBox();
93 m_xMaxSpin = new QSpinBox();
94 m_xMaxSpin->setMinimum(INT_MIN);
94 m_xMaxSpin->setMinimum(INT_MIN);
95 m_xMaxSpin->setMaximum(INT_MAX);
95 m_xMaxSpin->setMaximum(INT_MAX);
96 m_xMaxSpin->setValue(10);
96 m_xMaxSpin->setValue(10);
97 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
97 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
98 m_yMinSpin = new QSpinBox();
98 m_yMinSpin = new QSpinBox();
99 m_yMinSpin->setMinimum(INT_MIN);
99 m_yMinSpin->setMinimum(INT_MIN);
100 m_yMinSpin->setMaximum(INT_MAX);
100 m_yMinSpin->setMaximum(INT_MAX);
101 m_yMinSpin->setValue(0);
101 m_yMinSpin->setValue(0);
102 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
102 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
103 m_yMaxSpin = new QSpinBox();
103 m_yMaxSpin = new QSpinBox();
104 m_yMaxSpin->setMinimum(INT_MIN);
104 m_yMaxSpin->setMinimum(INT_MIN);
105 m_yMaxSpin->setMaximum(INT_MAX);
105 m_yMaxSpin->setMaximum(INT_MAX);
106 m_yMaxSpin->setValue(10);
106 m_yMaxSpin->setValue(10);
107 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
107 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
108
108
109 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
109 grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0);
110 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
110 grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0);
111 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
111 grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1);
112 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
112 grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0);
113 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
113 grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1);
114 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
114 grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0);
115 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
115 grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1);
116 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
116 grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0);
117 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
117 grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1);
118
118
119 m_autoScaleCheck->setChecked(true);
119 m_autoScaleCheck->setChecked(true);
120 }
120 }
121
121
122 // Combo box for selecting theme
122 // Combo box for selecting theme
123 void MainWidget::initThemeCombo(QGridLayout *grid)
123 void MainWidget::initThemeCombo(QGridLayout *grid)
124 {
124 {
125 QComboBox *chartTheme = new QComboBox();
125 QComboBox *chartTheme = new QComboBox();
126 chartTheme->addItem("Default");
126 chartTheme->addItem("Default");
127 chartTheme->addItem("Vanilla");
127 chartTheme->addItem("Vanilla");
128 chartTheme->addItem("Icy");
128 chartTheme->addItem("Icy");
129 chartTheme->addItem("Grayscale");
129 chartTheme->addItem("Grayscale");
130 chartTheme->addItem("Scientific");
130 chartTheme->addItem("Scientific");
131 chartTheme->addItem("Unnamed1");
131 chartTheme->addItem("Unnamed1");
132 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
132 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
133 this, SLOT(changeChartTheme(int)));
133 this, SLOT(changeChartTheme(int)));
134 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
134 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
135 grid->addWidget(chartTheme, 8, 1);
135 grid->addWidget(chartTheme, 8, 1);
136 }
136 }
137
137
138 void MainWidget::initPieControls()
138 void MainWidget::initPieControls()
139 {
139 {
140 // Pie series specific settings
140 // Pie series specific settings
141 // Pie size factory
141 // Pie size factory
142 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
142 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
143 pieSizeSpin->setMinimum(LONG_MIN);
143 pieSizeSpin->setMinimum(LONG_MIN);
144 pieSizeSpin->setMaximum(LONG_MAX);
144 pieSizeSpin->setMaximum(LONG_MAX);
145 pieSizeSpin->setValue(1.0);
145 pieSizeSpin->setValue(1.0);
146 pieSizeSpin->setSingleStep(0.1);
146 pieSizeSpin->setSingleStep(0.1);
147 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
147 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
148 // Pie position
148 // Pie position
149 QComboBox *piePosCombo = new QComboBox(this);
149 QComboBox *piePosCombo = new QComboBox(this);
150 piePosCombo->addItem("Maximized");
150 piePosCombo->addItem("Maximized");
151 piePosCombo->addItem("Top left");
151 piePosCombo->addItem("Top left");
152 piePosCombo->addItem("Top right");
152 piePosCombo->addItem("Top right");
153 piePosCombo->addItem("Bottom left");
153 piePosCombo->addItem("Bottom left");
154 piePosCombo->addItem("Bottom right");
154 piePosCombo->addItem("Bottom right");
155 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
155 connect(piePosCombo, SIGNAL(currentIndexChanged(int)),
156 this, SLOT(setPiePosition(int)));
156 this, SLOT(setPiePosition(int)));
157 m_pieLayout = new QGridLayout();
157 m_pieLayout = new QGridLayout();
158 m_pieLayout->setEnabled(false);
158 m_pieLayout->setEnabled(false);
159 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
159 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
160 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
160 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
161 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
161 m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0);
162 m_pieLayout->addWidget(piePosCombo, 1, 1);
162 m_pieLayout->addWidget(piePosCombo, 1, 1);
163 }
163 }
164
164
165 void MainWidget::addSeries()
165 void MainWidget::addSeries()
166 {
166 {
167 DataSerieDialog dialog(m_defaultSeriesName, this);
167 DataSerieDialog dialog(m_defaultSeriesName, this);
168 connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)),
168 connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)),
169 this, SLOT(addSeries(QString, int, int, QString, bool)));
169 this, SLOT(addSeries(QString, int, int, QString, bool)));
170 dialog.exec();
170 dialog.exec();
171 }
171 }
172
172
173 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
173 QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics)
174 {
174 {
175 // TODO: dataCharacteristics
175 // TODO: dataCharacteristics
176 QList<RealList> testData;
176 QList<RealList> testData;
177 for (int j(0); j < columnCount; j++) {
177 for (int j(0); j < columnCount; j++) {
178 QList <qreal> newColumn;
178 QList <qreal> newColumn;
179 for (int i(0); i < rowCount; i++) {
179 for (int i(0); i < rowCount; i++) {
180 if (dataCharacteristics == "Sin") {
180 if (dataCharacteristics == "Sin") {
181 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
181 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100));
182 } else if (dataCharacteristics == "Sin + random") {
182 } else if (dataCharacteristics == "Sin + random") {
183 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
183 newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
184 } else if (dataCharacteristics == "Random") {
184 } else if (dataCharacteristics == "Random") {
185 newColumn.append(rand() % 5);
185 newColumn.append(rand() % 5);
186 } else if (dataCharacteristics == "Linear") {
186 } else if (dataCharacteristics == "Linear") {
187 //newColumn.append(i * (j + 1.0));
187 //newColumn.append(i * (j + 1.0));
188 // TODO: temporary hack to make pie work; prevent zero values:
188 // TODO: temporary hack to make pie work; prevent zero values:
189 newColumn.append(i * (j + 1.0) + 0.1);
189 newColumn.append(i * (j + 1.0) + 0.1);
190 } else { // "constant"
190 } else { // "constant"
191 newColumn.append((j + 1.0));
191 newColumn.append((j + 1.0));
192 }
192 }
193 }
193 }
194 testData.append(newColumn);
194 testData.append(newColumn);
195 }
195 }
196 return testData;
196 return testData;
197 }
197 }
198
198
199 QStringList MainWidget::generateLabels(int count)
199 QStringList MainWidget::generateLabels(int count)
200 {
200 {
201 QStringList result;
201 QStringList result;
202 for (int i(0); i < count; i++)
202 for (int i(0); i < count; i++)
203 result.append("label" + QString::number(i));
203 result.append("label" + QString::number(i));
204 return result;
204 return result;
205 }
205 }
206
206
207 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
207 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled)
208 {
208 {
209 qDebug() << "addSeries: " << seriesName
209 qDebug() << "addSeries: " << seriesName
210 << " columnCount: " << columnCount
210 << " columnCount: " << columnCount
211 << " rowCount: " << rowCount
211 << " rowCount: " << rowCount
212 << " dataCharacteristics: " << dataCharacteristics
212 << " dataCharacteristics: " << dataCharacteristics
213 << " labels enabled: " << labelsEnabled;
213 << " labels enabled: " << labelsEnabled;
214 m_defaultSeriesName = seriesName;
214 m_defaultSeriesName = seriesName;
215
215
216 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
216 QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics);
217
217
218 // Line series and scatter series use similar data
218 // Line series and scatter series use similar data
219 if (seriesName.contains("line", Qt::CaseInsensitive)) {
219 if (seriesName.contains("line", Qt::CaseInsensitive)) {
220 for (int j(0); j < data.count(); j ++) {
220 for (int j(0); j < data.count(); j ++) {
221 QList<qreal> column = data.at(j);
221 QList<qreal> column = data.at(j);
222 QLineChartSeries *series = new QLineChartSeries();
222 QLineSeries *series = new QLineSeries();
223 for (int i(0); i < column.count(); i++) {
223 for (int i(0); i < column.count(); i++) {
224 series->add(i, column.at(i));
224 series->add(i, column.at(i));
225 }
225 }
226 m_chartWidget->addSeries(series);
226 m_chartWidget->addSeries(series);
227 setCurrentSeries(series);
227 setCurrentSeries(series);
228 }
228 }
229 } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) {
229 } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) {
230 for (int j(0); j < data.count(); j++) {
230 for (int j(0); j < data.count(); j++) {
231 QList<qreal> column = data.at(j);
231 QList<qreal> column = data.at(j);
232 QScatterSeries *series = new QScatterSeries();
232 QScatterSeries *series = new QScatterSeries();
233 for (int i(0); i < column.count(); i++) {
233 for (int i(0); i < column.count(); i++) {
234 (*series) << QPointF(i, column.at(i));
234 (*series) << QPointF(i, column.at(i));
235 }
235 }
236 m_chartWidget->addSeries(series);
236 m_chartWidget->addSeries(series);
237 setCurrentSeries(series);
237 setCurrentSeries(series);
238 }
238 }
239 } else if (seriesName.contains("pie", Qt::CaseInsensitive)) {
239 } else if (seriesName.contains("pie", Qt::CaseInsensitive)) {
240 QStringList labels = generateLabels(rowCount);
240 QStringList labels = generateLabels(rowCount);
241 for (int j(0); j < data.count(); j++) {
241 for (int j(0); j < data.count(); j++) {
242 QPieSeries *series = new QPieSeries();
242 QPieSeries *series = new QPieSeries();
243 QList<qreal> column = data.at(j);
243 QList<qreal> column = data.at(j);
244 for (int i(0); i < column.count(); i++) {
244 for (int i(0); i < column.count(); i++) {
245 series->add(column.at(i), labels.at(i));
245 series->add(column.at(i), labels.at(i));
246 }
246 }
247 m_chartWidget->addSeries(series);
247 m_chartWidget->addSeries(series);
248 setCurrentSeries(series);
248 setCurrentSeries(series);
249 }
249 }
250 } else if (seriesName == "Bar"
250 } else if (seriesName == "Bar"
251 || seriesName == "Stacked bar"
251 || seriesName == "Stacked bar"
252 || seriesName == "Percent bar") {
252 || seriesName == "Percent bar") {
253 // TODO: replace QBarCategory with QStringList?
253 // TODO: replace QBarCategory with QStringList?
254 QBarCategory *category = new QBarCategory;
254 QBarCategory *category = new QBarCategory;
255 QStringList labels = generateLabels(rowCount);
255 QStringList labels = generateLabels(rowCount);
256 foreach(QString label, labels)
256 foreach(QString label, labels)
257 *category << label;
257 *category << label;
258 QBarSeries* series = 0;
258 QBarSeries* series = 0;
259 if (seriesName == "Bar")
259 if (seriesName == "Bar")
260 series = new QBarSeries(category, this);
260 series = new QBarSeries(category, this);
261 else if (seriesName == "Stacked bar")
261 else if (seriesName == "Stacked bar")
262 series = new QStackedBarSeries(category, this);
262 series = new QStackedBarSeries(category, this);
263 else
263 else
264 series = new QPercentBarSeries(category, this);
264 series = new QPercentBarSeries(category, this);
265
265
266 for (int j(0); j < data.count(); j++) {
266 for (int j(0); j < data.count(); j++) {
267 QList<qreal> column = data.at(j);
267 QList<qreal> column = data.at(j);
268 QBarSet *set = new QBarSet("set" + QString::number(j));
268 QBarSet *set = new QBarSet("set" + QString::number(j));
269 for (int i(0); i < column.count(); i++) {
269 for (int i(0); i < column.count(); i++) {
270 *set << column.at(i);
270 *set << column.at(i);
271 }
271 }
272 series->addBarSet(set);
272 series->addBarSet(set);
273 }
273 }
274 series->enableFloatingValues();
274 series->enableFloatingValues();
275 series->enableToolTip();
275 series->enableToolTip();
276 series->enableSeparators(false);
276 series->enableSeparators(false);
277 m_chartWidget->addSeries(series);
277 m_chartWidget->addSeries(series);
278 setCurrentSeries(series);
278 setCurrentSeries(series);
279 }
279 }
280
280
281 // TODO: spline and area
281 // TODO: spline and area
282 }
282 }
283
283
284 void MainWidget::setCurrentSeries(QChartSeries *series)
284 void MainWidget::setCurrentSeries(QChartSeries *series)
285 {
285 {
286 if (series) {
286 if (series) {
287 m_currentSeries = series;
287 m_currentSeries = series;
288 switch (m_currentSeries->type()) {
288 switch (m_currentSeries->type()) {
289 case QChartSeries::SeriesTypeLine:
289 case QChartSeries::SeriesTypeLine:
290 break;
290 break;
291 case QChartSeries::SeriesTypeScatter:
291 case QChartSeries::SeriesTypeScatter:
292 break;
292 break;
293 case QChartSeries::SeriesTypePie:
293 case QChartSeries::SeriesTypePie:
294 break;
294 break;
295 case QChartSeries::SeriesTypeBar:
295 case QChartSeries::SeriesTypeBar:
296 qDebug() << "setCurrentSeries (bar)";
296 qDebug() << "setCurrentSeries (bar)";
297 break;
297 break;
298 case QChartSeries::SeriesTypeStackedBar:
298 case QChartSeries::SeriesTypeStackedBar:
299 qDebug() << "setCurrentSeries (Stackedbar)";
299 qDebug() << "setCurrentSeries (Stackedbar)";
300 break;
300 break;
301 case QChartSeries::SeriesTypePercentBar:
301 case QChartSeries::SeriesTypePercentBar:
302 qDebug() << "setCurrentSeries (Percentbar)";
302 qDebug() << "setCurrentSeries (Percentbar)";
303 break;
303 break;
304 default:
304 default:
305 Q_ASSERT(false);
305 Q_ASSERT(false);
306 break;
306 break;
307 }
307 }
308 }
308 }
309 }
309 }
310
310
311 void MainWidget::backgroundChanged(int itemIndex)
311 void MainWidget::backgroundChanged(int itemIndex)
312 {
312 {
313 qDebug() << "backgroundChanged: " << itemIndex;
313 qDebug() << "backgroundChanged: " << itemIndex;
314 }
314 }
315
315
316 void MainWidget::autoScaleChanged(int value)
316 void MainWidget::autoScaleChanged(int value)
317 {
317 {
318 if (value) {
318 if (value) {
319 // TODO: enable auto scaling
319 // TODO: enable auto scaling
320 } else {
320 } else {
321 // TODO: set scaling manually (and disable auto scaling)
321 // TODO: set scaling manually (and disable auto scaling)
322 }
322 }
323
323
324 m_xMinSpin->setEnabled(!value);
324 m_xMinSpin->setEnabled(!value);
325 m_xMaxSpin->setEnabled(!value);
325 m_xMaxSpin->setEnabled(!value);
326 m_yMinSpin->setEnabled(!value);
326 m_yMinSpin->setEnabled(!value);
327 m_yMaxSpin->setEnabled(!value);
327 m_yMaxSpin->setEnabled(!value);
328 }
328 }
329
329
330 void MainWidget::xMinChanged(int value)
330 void MainWidget::xMinChanged(int value)
331 {
331 {
332 qDebug() << "xMinChanged: " << value;
332 qDebug() << "xMinChanged: " << value;
333 }
333 }
334
334
335 void MainWidget::xMaxChanged(int value)
335 void MainWidget::xMaxChanged(int value)
336 {
336 {
337 qDebug() << "xMaxChanged: " << value;
337 qDebug() << "xMaxChanged: " << value;
338 }
338 }
339
339
340 void MainWidget::yMinChanged(int value)
340 void MainWidget::yMinChanged(int value)
341 {
341 {
342 qDebug() << "yMinChanged: " << value;
342 qDebug() << "yMinChanged: " << value;
343 }
343 }
344
344
345 void MainWidget::yMaxChanged(int value)
345 void MainWidget::yMaxChanged(int value)
346 {
346 {
347 qDebug() << "yMaxChanged: " << value;
347 qDebug() << "yMaxChanged: " << value;
348 }
348 }
349
349
350 void MainWidget::changeChartTheme(int themeIndex)
350 void MainWidget::changeChartTheme(int themeIndex)
351 {
351 {
352 qDebug() << "changeChartTheme: " << themeIndex;
352 qDebug() << "changeChartTheme: " << themeIndex;
353 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
353 m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex);
354 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
354 //TODO: remove this hack. This is just to make it so that theme change is seen immediately.
355 QSize s = size();
355 QSize s = size();
356 s.setWidth(s.width()+1);
356 s.setWidth(s.width()+1);
357 resize(s);
357 resize(s);
358 }
358 }
359
359
360 void MainWidget::setPieSizeFactor(double size)
360 void MainWidget::setPieSizeFactor(double size)
361 {
361 {
362 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
362 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
363 if (pie)
363 if (pie)
364 pie->setSizeFactor(qreal(size));
364 pie->setSizeFactor(qreal(size));
365 }
365 }
366
366
367 void MainWidget::setPiePosition(int position)
367 void MainWidget::setPiePosition(int position)
368 {
368 {
369 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
369 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
370 if (pie)
370 if (pie)
371 pie->setPosition((QPieSeries::PiePosition) position);
371 pie->setPosition((QPieSeries::PiePosition) position);
372 }
372 }
General Comments 0
You need to be logged in to leave comments. Login now