##// END OF EJS Templates
Spline chart example added
Marek Rosa -
r434:52aa7c3abf86
parent child
Show More
@@ -1,28 +1,29
1 /*!
1 /*!
2 \page examples.html
2 \page examples.html
3 \title Examples
3 \title Examples
4 \keyword Examples
4 \keyword Examples
5
5
6 \raw HTML
6 \raw HTML
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 <tr>
8 <tr>
9 <th class="titleheader" width="33%">
9 <th class="titleheader" width="33%">
10 List of examples
10 List of examples
11 </th>
11 </th>
12 </tr>
12 </tr>
13 <tr>
13 <tr>
14 <td valign="top">
14 <td valign="top">
15 <ul>
15 <ul>
16 <li><a href="example-areachart.html">Area Chart example</a></li>
16 <li><a href="example-areachart.html">Area Chart example</a></li>
17 <li><a href="example-barchart.html">Bar Chart example</a></li>
17 <li><a href="example-barchart.html">Bar Chart example</a></li>
18 <li><a href="example-stackedbarchart.html">Stacked Bar Chart example</a></li>
18 <li><a href="example-stackedbarchart.html">Stacked Bar Chart example</a></li>
19 <li><a href="example-percentbarchart.html">Percent Bar Chart example</a></li>
19 <li><a href="example-percentbarchart.html">Percent Bar Chart example</a></li>
20 <li><a href="example-linechart.html">Line Chart example</a></li>
20 <li><a href="example-linechart.html">Line Chart example</a></li>
21 <li><a href="example-piechart.html">Pie Chart example</a></li>
21 <li><a href="example-piechart.html">Pie Chart example</a></li>
22 <li><a href="example-gdpbarchart.html">GDP Chart example</a></li>
22 <li><a href="example-gdpbarchart.html">GDP Chart example</a></li>
23 <li><a href="example-splinechart.html">Spline Chart example</a></li>
23 </ul>
24 </ul>
24 </td>
25 </td>
25 </tr>
26 </tr>
26 </table>
27 </table>
27 \endraw
28 \endraw
28 */
29 */
@@ -1,229 +1,230
1 /*!
1 /*!
2 \class Widget
2 \class Widget
3 \brief Ui for the application.
3 \brief Ui for the application.
4 \internal
4 \internal
5 */
5 */
6
6
7 #include "widget.h"
7 #include "widget.h"
8 #include <QGridLayout>
8 #include <QGridLayout>
9 #include <QPushButton>
9 #include <QPushButton>
10 #include <QLabel>
10 #include <QLabel>
11
11
12 #include <QSqlQuery>
12 #include <QSqlQuery>
13 #include <qscatterseries.h>
13 #include <qscatterseries.h>
14 #include <qchartview.h>
14 #include <qchartview.h>
15 #include <qchartaxis.h>
15 #include <qchartaxis.h>
16 #include <qbarset.h>
16 #include <qbarset.h>
17 #include <QListWidget>
17 #include <QListWidget>
18 #include <QPrinter>
18 #include <QPrinter>
19 #include <QPrintDialog>
19 #include <QPrintDialog>
20 #include <QRadioButton>
20 #include <QRadioButton>
21 #include <QStringList>
21 #include <QStringList>
22 #include <qbarseries.h>
22 #include <qbarseries.h>
23
23
24 QTCOMMERCIALCHART_USE_NAMESPACE
24 QTCOMMERCIALCHART_USE_NAMESPACE
25
25
26 Widget::Widget(QWidget *parent)
26 Widget::Widget(QWidget *parent)
27 : QWidget(parent)
27 : QWidget(parent)
28 {
28 {
29 setGeometry(100, 100, 1000, 600);
29 setGeometry(100, 100, 1000, 600);
30
30
31 // right panel layout
31 // right panel layout
32 barChartRadioButton = new QRadioButton(tr("Bar chart"));
32 barChartRadioButton = new QRadioButton(tr("Bar chart"));
33 barChartRadioButton->setChecked(true);
33 barChartRadioButton->setChecked(true);
34 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
34 scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
35 scatterChartRadioButton->setChecked(false);
35 scatterChartRadioButton->setChecked(false);
36 countrieslist = new QListWidget;
36 countrieslist = new QListWidget;
37 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
37 countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
38
38
39 //list of years widget
39 //list of years widget
40 yearslist = new QListWidget;
40 yearslist = new QListWidget;
41 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
41 yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
42 for (int i = 1990; i < 2011; i++)
42 for (int i = 1990; i < 2011; i++)
43 yearslist->addItem(QString("%1").arg(i));
43 yearslist->addItem(QString("%1").arg(i));
44
44
45 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
45 QPushButton* refreshButton = new QPushButton(tr("Refresh"));
46 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
46 connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
47
47
48 QPushButton* printButton = new QPushButton(tr("Print chart"));
48 QPushButton* printButton = new QPushButton(tr("Print chart"));
49 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
49 connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
50
50
51 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
51 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
52 rightPanelLayout->addWidget(barChartRadioButton);
52 rightPanelLayout->addWidget(barChartRadioButton);
53 rightPanelLayout->addWidget(scatterChartRadioButton);
53 rightPanelLayout->addWidget(scatterChartRadioButton);
54 rightPanelLayout->addWidget(countrieslist);
54 rightPanelLayout->addWidget(countrieslist);
55 rightPanelLayout->addWidget(yearslist);
55 rightPanelLayout->addWidget(yearslist);
56 rightPanelLayout->addWidget(refreshButton);
56 rightPanelLayout->addWidget(refreshButton);
57 rightPanelLayout->addWidget(printButton);
57 rightPanelLayout->addWidget(printButton);
58 rightPanelLayout->setStretch(0, 1);
58 rightPanelLayout->setStretch(0, 1);
59 rightPanelLayout->setStretch(1, 0);
59 rightPanelLayout->setStretch(1, 0);
60
60
61 // main layout
61 // main layout
62 chartArea = new QChartView(this);
62 chartArea = new QChartView(this);
63 chartArea->setChartTitle("GDP by country");
63 chartArea->setChartTitle("GDP by country");
64 QGridLayout* mainLayout = new QGridLayout;
64 QGridLayout* mainLayout = new QGridLayout;
65 mainLayout->addWidget(chartArea, 0, 0);
65 mainLayout->addWidget(chartArea, 0, 0);
66 mainLayout->addLayout(rightPanelLayout, 0, 1);
66 mainLayout->addLayout(rightPanelLayout, 0, 1);
67 mainLayout->setColumnStretch(0,1);
67 mainLayout->setColumnStretch(0,1);
68 setLayout(mainLayout);
68 setLayout(mainLayout);
69
69
70 // connect to the database
70 // connect to the database
71 db = QSqlDatabase::addDatabase("QSQLITE");
71 db = QSqlDatabase::addDatabase("QSQLITE");
72 db.setDatabaseName("gdpData");
72 db.setDatabaseName("gdpData");
73 if(!db.open())
73 if(!db.open())
74 {
74 {
75 qDebug() << "could not open database. SQLite db file missing (?)";
75 qDebug() << "could not open database. SQLite db file missing (?)";
76 return;
76 return;
77 }
77 }
78
78
79 // get the list of all countires and regions.
79 // get the list of all countires and regions.
80 QSqlQuery query;
80 QSqlQuery query;
81 query.exec("SELECT DISTINCT country FROM gdp2");
81 query.exec("SELECT DISTINCT country FROM gdp2");
82
82
83 // add the countries to the country filter
83 // add the countries to the country filter
84 while (query.next()) {
84 while (query.next()) {
85 countrieslist->addItem(query.value(0).toString());
85 countrieslist->addItem(query.value(0).toString());
86 }
86 }
87
87
88 // hide axis X labels
88 // hide axis X labels
89 QChartAxis* axis = chartArea->axisX();
89 QChartAxis* axis = chartArea->axisX();
90 // axis->
90 // axis->setLabelsVisible(false);
91 // axis->setLabelsVisible(false);
91 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
92 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
92
93
93 }
94 }
94
95
95 Widget::~Widget()
96 Widget::~Widget()
96 {
97 {
97 //
98 //
98 db.close();
99 db.close();
99 }
100 }
100
101
101 /*!
102 /*!
102 refreshes the chart
103 refreshes the chart
103 */
104 */
104 void Widget::refreshChart()
105 void Widget::refreshChart()
105 {
106 {
106 chartArea->removeAllSeries();
107 chartArea->removeAllSeries();
107
108
108 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
109 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
109 QStringList selectedCountriesStrings;
110 QStringList selectedCountriesStrings;
110 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
111 QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
111 for (int i = 0; i < selectedCountriesItems.size(); i++)
112 for (int i = 0; i < selectedCountriesItems.size(); i++)
112 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
113 selectedCountriesStrings.append(selectedCountriesItems[i]->text());
113 selectedCountriesStrings.sort();
114 selectedCountriesStrings.sort();
114
115
115 QSqlQuery query;
116 QSqlQuery query;
116 // selected years items list is not sorted. copy the values to QList<int> and sort them.
117 // selected years items list is not sorted. copy the values to QList<int> and sort them.
117 QList<int> selectedYearsInts;
118 QList<int> selectedYearsInts;
118 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
119 QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
119 for (int i = 0; i < selectedYearsItems.size(); i++)
120 for (int i = 0; i < selectedYearsItems.size(); i++)
120 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
121 selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
121 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
122 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
122
123
123 if (barChartRadioButton->isChecked())
124 if (barChartRadioButton->isChecked())
124 {
125 {
125 // use the sorted selected coutries list to initialize BarCategory
126 // use the sorted selected coutries list to initialize BarCategory
126 QStringList category;
127 QStringList category;
127 for (int i = 0; i < selectedCountriesStrings.size(); i++)
128 for (int i = 0; i < selectedCountriesStrings.size(); i++)
128 category << selectedCountriesStrings[i];
129 category << selectedCountriesStrings[i];
129 QBarSeries* series0 = new QBarSeries(category);
130 QBarSeries* series0 = new QBarSeries(category);
130 series0 = new QBarSeries(category);
131 series0 = new QBarSeries(category);
131
132
132 // prepare the selected counries SQL query
133 // prepare the selected counries SQL query
133 QString countriesQuery = "country IN (";
134 QString countriesQuery = "country IN (";
134 for (int i = 0; i < selectedCountriesStrings.size(); i++)
135 for (int i = 0; i < selectedCountriesStrings.size(); i++)
135 {
136 {
136 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
137 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
137 if ( i < selectedCountriesStrings.size() - 1)
138 if ( i < selectedCountriesStrings.size() - 1)
138 countriesQuery.append(",");
139 countriesQuery.append(",");
139 else
140 else
140 countriesQuery.append(")");
141 countriesQuery.append(")");
141 }
142 }
142
143
143 // perform a query for each selected year
144 // perform a query for each selected year
144 for (int i = 0; i < selectedYearsInts.size(); i++)
145 for (int i = 0; i < selectedYearsInts.size(); i++)
145 {
146 {
146 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
147 query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
147 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
148 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
148
149
149 // while (query.next()) {
150 // while (query.next()) {
150 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
151 // qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
151 // }
152 // }
152 query.first();
153 query.first();
153
154
154 // the data for some of the coutries for some years might be missing.
155 // the data for some of the coutries for some years might be missing.
155 // QBarChart needs bars to have same size
156 // QBarChart needs bars to have same size
156 for (int k = 0; k < selectedCountriesStrings.size(); k++)
157 for (int k = 0; k < selectedCountriesStrings.size(); k++)
157 {
158 {
158 if (selectedCountriesStrings[k] == query.value(0).toString())
159 if (selectedCountriesStrings[k] == query.value(0).toString())
159 {
160 {
160 *barSet << query.value(1).toReal();
161 *barSet << query.value(1).toReal();
161 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
162 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
162 query.next();
163 query.next();
163 }
164 }
164 else
165 else
165 {
166 {
166 // data missing, put 0
167 // data missing, put 0
167 *barSet << 0.0f;
168 *barSet << 0.0f;
168 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
169 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
169 }
170 }
170 }
171 }
171 series0->addBarSet(barSet);
172 series0->addBarSet(barSet);
172 }
173 }
173 // add the serie to the chart
174 // add the serie to the chart
174 chartArea->addSeries(series0);
175 chartArea->addSeries(series0);
175 }
176 }
176 else if (scatterChartRadioButton->isChecked())
177 else if (scatterChartRadioButton->isChecked())
177 {
178 {
178 QString yearsQuery = "year IN (";
179 QString yearsQuery = "year IN (";
179 for (int i = 0; i < selectedYearsInts.size(); i++)
180 for (int i = 0; i < selectedYearsInts.size(); i++)
180 {
181 {
181 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
182 yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
182 if ( i < selectedYearsInts.size() - 1)
183 if ( i < selectedYearsInts.size() - 1)
183 yearsQuery.append(",");
184 yearsQuery.append(",");
184 else
185 else
185 yearsQuery.append(")");
186 yearsQuery.append(")");
186 }
187 }
187
188
188 // perform a query for each selected country
189 // perform a query for each selected country
189 for (int i = 0; i < selectedCountriesStrings.size(); i++)
190 for (int i = 0; i < selectedCountriesStrings.size(); i++)
190 {
191 {
191 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
192 query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
192 query.first();
193 query.first();
193
194
194 QScatterSeries* series = new QScatterSeries;
195 QScatterSeries* series = new QScatterSeries;
195 // the data for some of the coutries for some years might be missing.
196 // the data for some of the coutries for some years might be missing.
196 for (int k = 0; k < selectedYearsInts.size(); k++)
197 for (int k = 0; k < selectedYearsInts.size(); k++)
197 {
198 {
198 if (selectedYearsInts[k] == query.value(0).toInt())
199 if (selectedYearsInts[k] == query.value(0).toInt())
199 {
200 {
200 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
201 *series << QPointF(query.value(0).toInt() , query.value(1).toReal());
201 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
202 qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
202 query.next();
203 query.next();
203 }
204 }
204 else
205 else
205 {
206 {
206 // data missing, put 0
207 // data missing, put 0
207 *series << QPointF(selectedYearsInts[k] , 0.0f);
208 *series << QPointF(selectedYearsInts[k] , 0.0f);
208 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
209 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
209 }
210 }
210 }
211 }
211 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
212 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
212 chartArea->addSeries(series);
213 chartArea->addSeries(series);
213 }
214 }
214 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
215 chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
215 }
216 }
216 }
217 }
217
218
218 void Widget::printChart()
219 void Widget::printChart()
219 {
220 {
220 QPrinter printer;
221 QPrinter printer;
221 // QPrinter printer(QPrinter::HighResolution);
222 // QPrinter printer(QPrinter::HighResolution);
222 printer.setOutputFormat(QPrinter::PdfFormat);
223 printer.setOutputFormat(QPrinter::PdfFormat);
223 printer.setOrientation(QPrinter::Landscape);
224 printer.setOrientation(QPrinter::Landscape);
224 printer.setOutputFileName("print.pdf");
225 printer.setOutputFileName("print.pdf");
225
226
226 QPainter painter;
227 QPainter painter;
227 painter.begin(&printer);
228 painter.begin(&printer);
228 chartArea->render(&painter);
229 chartArea->render(&painter);
229 }
230 }
@@ -1,109 +1,101
1 #include "splinewidget.h"
1 #include "splinewidget.h"
2 #include "qchartview.h"
2 #include "qchartview.h"
3 #include "qlineseries.h"
3 #include "qlineseries.h"
4 #include <QGridLayout>
4 #include <QGridLayout>
5 #include <QPushButton>
5 #include <QPushButton>
6 #include "qchartaxis.h"
7 #include <qmath.h>
6
8
7 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
8
10
9 SplineWidget::SplineWidget(QWidget *parent)
11 SplineWidget::SplineWidget(QWidget *parent)
10 : QWidget(parent)
12 : QWidget(parent)
11 {
13 {
14 qsrand(time(NULL));
15 //! [1]
12 //create QSplineSeries
16 //create QSplineSeries
13 series = new QSplineSeries(this);
17 series = new QSplineSeries(this);
18 //! [1]
19
20 //! [2]
21 // customize the series presentation settings
22 QPen seriesPen(Qt::blue);
23 seriesPen.setWidth(3);
24 series->setPen(seriesPen);
25 //! [2]
26
27 //! [add points to series]
28 //add data points to the series
14 series->add(QPointF(150, 100));
29 series->add(QPointF(150, 100));
15 series->add(QPointF(200, 130));
30 series->add(QPointF(200, 130));
16 series->add(QPointF(250, 120));
31 series->add(QPointF(250, 120));
17 series->add(QPointF(300, 140));
32 series->add(QPointF(300, 140));
18 series->add(QPointF(350, 100));
33 series->add(QPointF(350, 160));
19 series->add(QPointF(400, 120));
34 //! [add points to series]
20 series->add(QPointF(450, 150));
35 // series->add(QPointF(400, 120));
21 // series->add(QPointF(600, 150));
36 // series->add(QPointF(450, 150));
22 series->add(QPointF(500, 145));
37 // series->add(QPointF(500, 145));
23 series->add(QPointF(550, 170));
38 // series->add(QPointF(550, 170));
24 series->add(QPointF(600, 190));
39 // series->add(QPointF(600, 190));
25 series->add(QPointF(650, 210));
40 // series->add(QPointF(650, 210));
26 series->add(QPointF(700, 190));
41 // series->add(QPointF(700, 190));
27 series->add(QPointF(750, 180));
42 // series->add(QPointF(750, 180));
28 series->add(QPointF(800, 170));
43 // series->add(QPointF(800, 170));
29
30 // series->calculateControlPoints();
31
32 QSplineSeries* series2 = new QSplineSeries(this);
33 qsrand(time(NULL));
34 // for (int i = 0; i < 100; i++)
35 // {
36 // series2->add(QPointF(i*7, qrand()%600));
37 // }
38 int k = 10;
39 for (int i = 0; i < 25; i++)
40 {
41 if (k > 60)
42 {
43 k = 10;
44 }
45 series2->add(QPointF(i*50, k));
46 k +=10;
47 }
48
44
49 // series2->calculateControlPoints();
45 //! [3]
50
46 // create chart view
51 // QLineSeries* lineSeries = new QLineSeries;
47 QChartView* chart = new QChartView;
52 // for (int i = 0; i < series->count() - 1; i++)
48 chart->addSeries(series);
53 // {
54 // lineSeries->add(series->at(i).x(), series->at(i).y());
55 // lineSeries->add(series->controlPoint(2*i).x(), series->controlPoint(2*i).y());
56 // lineSeries->add(series->controlPoint(2*i + 1).x(), series->controlPoint(2*i + 1).y());
57 // }
58
49
59 // QLineChartSeries* lineSeries2 = new QLineChartSeries;
50 chart->axisX()->setRange(0, 1500);
60 // lineSeries2->add(10, 50);
51 chart->axisY()->setRange(0, 400);
61 // lineSeries2->add(30, 15);
62 // lineSeries2->add(60, 40);
63 // lineSeries2->add(90, 70);
64 // lineSeries2->add(100, 20);
65
52
66 //create chart view
67 QChartView* chart = new QChartView;
68 chart->setMinimumSize(800,600);
53 chart->setMinimumSize(800,600);
69 // chart->setGeometry(50, 50, 400, 300);
54 //! [3]
70 chart->addSeries(series);
71 chart->addSeries(series2);
72
55
73 //add new item
56 //! [4]
57 //add new data point button
74 QPushButton* addButton = new QPushButton("Add new point");
58 QPushButton* addButton = new QPushButton("Add new point");
75 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
59 connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint()));
76
60
77 QPushButton* removeButton = new QPushButton("Remove point from the end");
61 // remove the last data point in the series
62 QPushButton* removeButton = new QPushButton("Remove point");
78 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
63 connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint()));
64 //! [4]
79
65
66 //! [5]
80 //butttons layout
67 //butttons layout
81 QVBoxLayout* buttonsLayout = new QVBoxLayout;
68 QVBoxLayout* buttonsLayout = new QVBoxLayout;
82 buttonsLayout->addWidget(addButton);
69 buttonsLayout->addWidget(addButton);
83 buttonsLayout->addWidget(removeButton);
70 buttonsLayout->addWidget(removeButton);
84 buttonsLayout->addStretch();
71 buttonsLayout->addStretch();
85
72
86 QGridLayout* mainLayout = new QGridLayout;
73 QGridLayout* mainLayout = new QGridLayout;
87 mainLayout->addWidget(chart, 1, 0);
74 mainLayout->addWidget(chart, 1, 0);
88 mainLayout->addLayout(buttonsLayout, 1, 1);
75 mainLayout->addLayout(buttonsLayout, 1, 1);
89 setLayout(mainLayout);
76 setLayout(mainLayout);
77 //! [5]
90 }
78 }
91
79
80 //! [add point]
92 void SplineWidget::addNewPoint()
81 void SplineWidget::addNewPoint()
93 {
82 {
94 if (series->count() > 0)
83 if (series->count() > 0)
95 series->add(QPointF(series->x(series->count() - 1) + 50, series->y(series->count() - 1) - 50 + qrand()%100));
84 series->add(QPointF(series->x(series->count() - 1) + 20 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100)));
96 else
85 else
97 series->add(QPointF(50, 50 + qrand()%50));
86 series->add(QPointF(50, 50 + qrand()%50));
98 }
87 }
88 //! [add point]
99
89
90 //! [remove point]
100 void SplineWidget::removePoint()
91 void SplineWidget::removePoint()
101 {
92 {
102 if (series->count() > 0)
93 if (series->count() > 0)
103 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
94 series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1)));
104 }
95 }
96 //! [remove point]
105
97
106 SplineWidget::~SplineWidget()
98 SplineWidget::~SplineWidget()
107 {
99 {
108
100
109 }
101 }
@@ -1,319 +1,328
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 "qlineseries.h"
11 #include "qlineseries.h"
12 #include "qareaseries.h"
12 #include "qareaseries.h"
13 #include "qscatterseries.h"
13 #include "qscatterseries.h"
14 #include "qpieseries.h"
14 #include "qpieseries.h"
15 #include "qpieslice.h"
15 #include "qpieslice.h"
16 #include "qsplineseries.h"
16 #include "qsplineseries.h"
17
17
18 //items
18 //items
19 #include "axisitem_p.h"
19 #include "axisitem_p.h"
20 #include "barpresenter_p.h"
20 #include "barpresenter_p.h"
21 #include "stackedbarpresenter_p.h"
21 #include "stackedbarpresenter_p.h"
22 #include "percentbarpresenter_p.h"
22 #include "percentbarpresenter_p.h"
23 #include "linechartitem_p.h"
23 #include "linechartitem_p.h"
24 #include "areachartitem_p.h"
24 #include "areachartitem_p.h"
25 #include "scatterpresenter_p.h"
25 #include "scatterpresenter_p.h"
26 #include "piepresenter_p.h"
26 #include "piepresenter_p.h"
27 #include "splinepresenter_p.h"
27 #include "splinepresenter_p.h"
28
28
29 //themes
29 //themes
30 #include "chartthemevanilla_p.h"
30 #include "chartthemevanilla_p.h"
31 #include "chartthemeicy_p.h"
31 #include "chartthemeicy_p.h"
32 #include "chartthemegrayscale_p.h"
32 #include "chartthemegrayscale_p.h"
33 #include "chartthemescientific_p.h"
33 #include "chartthemescientific_p.h"
34
34
35
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
37
38 /* TODO
38 /* TODO
39 case QChart::ChartThemeUnnamed1:
39 case QChart::ChartThemeUnnamed1:
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
41 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
41 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
42 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
42 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
43 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
43 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
44 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
44 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
45
45
46 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
46 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
47 m_gradientEndColor = QColor(QRgb(0xffafafaf));
47 m_gradientEndColor = QColor(QRgb(0xffafafaf));
48 */
48 */
49
49
50 ChartTheme::ChartTheme(QChart::ChartTheme id)
50 ChartTheme::ChartTheme(QChart::ChartTheme id)
51 {
51 {
52 m_id = id;
52 m_id = id;
53 m_seriesColor.append(QRgb(0xff000000));
53 m_seriesColor.append(QRgb(0xff000000));
54 m_seriesColor.append(QRgb(0xff707070));
54 m_seriesColor.append(QRgb(0xff707070));
55 m_gradientStartColor = QColor(QRgb(0xffffffff));
55 m_gradientStartColor = QColor(QRgb(0xffffffff));
56 m_gradientEndColor = QColor(QRgb(0xffafafaf));
56 m_gradientEndColor = QColor(QRgb(0xffafafaf));
57
57
58 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
58 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
59 }
59 }
60
60
61
61
62 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
62 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
63 {
63 {
64 switch(theme) {
64 switch(theme) {
65 case QChart::ChartThemeDefault:
65 case QChart::ChartThemeDefault:
66 return new ChartTheme();
66 return new ChartTheme();
67 case QChart::ChartThemeVanilla:
67 case QChart::ChartThemeVanilla:
68 return new ChartThemeVanilla();
68 return new ChartThemeVanilla();
69 case QChart::ChartThemeIcy:
69 case QChart::ChartThemeIcy:
70 return new ChartThemeIcy();
70 return new ChartThemeIcy();
71 case QChart::ChartThemeGrayscale:
71 case QChart::ChartThemeGrayscale:
72 return new ChartThemeGrayscale();
72 return new ChartThemeGrayscale();
73 case QChart::ChartThemeScientific:
73 case QChart::ChartThemeScientific:
74 return new ChartThemeScientific();
74 return new ChartThemeScientific();
75 }
75 }
76 }
76 }
77
77
78 void ChartTheme::decorate(QChart* chart)
78 void ChartTheme::decorate(QChart* chart)
79 {
79 {
80 QLinearGradient backgroundGradient;
80 QLinearGradient backgroundGradient;
81 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
81 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
82 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
82 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
83 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
83 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
84 chart->setChartBackgroundBrush(backgroundGradient);
84 chart->setChartBackgroundBrush(backgroundGradient);
85 }
85 }
86 //TODO helper to by removed later
86 //TODO helper to by removed later
87 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
87 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
88 {
88 {
89 switch(series->type())
89 switch(series->type())
90 {
90 {
91 case QSeries::SeriesTypeLine: {
91 case QSeries::SeriesTypeLine: {
92 QLineSeries* s = static_cast<QLineSeries*>(series);
92 QLineSeries* s = static_cast<QLineSeries*>(series);
93 LineChartItem* i = static_cast<LineChartItem*>(item);
93 LineChartItem* i = static_cast<LineChartItem*>(item);
94 decorate(i,s,count);
94 decorate(i,s,count);
95 break;
95 break;
96 }
96 }
97 case QSeries::SeriesTypeArea: {
97 case QSeries::SeriesTypeArea: {
98 QAreaSeries* s = static_cast<QAreaSeries*>(series);
98 QAreaSeries* s = static_cast<QAreaSeries*>(series);
99 AreaChartItem* i = static_cast<AreaChartItem*>(item);
99 AreaChartItem* i = static_cast<AreaChartItem*>(item);
100 decorate(i,s,count);
100 decorate(i,s,count);
101 break;
101 break;
102 }
102 }
103 case QSeries::SeriesTypeBar: {
103 case QSeries::SeriesTypeBar: {
104 QBarSeries* b = static_cast<QBarSeries*>(series);
104 QBarSeries* b = static_cast<QBarSeries*>(series);
105 BarPresenter* i = static_cast<BarPresenter*>(item);
105 BarPresenter* i = static_cast<BarPresenter*>(item);
106 decorate(i,b,count);
106 decorate(i,b,count);
107 break;
107 break;
108 }
108 }
109 case QSeries::SeriesTypeStackedBar: {
109 case QSeries::SeriesTypeStackedBar: {
110 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
110 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
111 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
111 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
112 decorate(i,s,count);
112 decorate(i,s,count);
113 break;
113 break;
114 }
114 }
115 case QSeries::SeriesTypePercentBar: {
115 case QSeries::SeriesTypePercentBar: {
116 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
116 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
117 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
117 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
118 decorate(i,s,count);
118 decorate(i,s,count);
119 break;
119 break;
120 }
120 }
121 case QSeries::SeriesTypeScatter: {
121 case QSeries::SeriesTypeScatter: {
122 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
122 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
123 Q_ASSERT(s);
123 Q_ASSERT(s);
124 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
124 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
125 Q_ASSERT(i);
125 Q_ASSERT(i);
126 decorate(i, s, count);
126 decorate(i, s, count);
127 break;
127 break;
128 }
128 }
129 case QSeries::SeriesTypePie: {
129 case QSeries::SeriesTypePie: {
130 QPieSeries* s = static_cast<QPieSeries*>(series);
130 QPieSeries* s = static_cast<QPieSeries*>(series);
131 PiePresenter* i = static_cast<PiePresenter*>(item);
131 PiePresenter* i = static_cast<PiePresenter*>(item);
132 decorate(i,s,count);
132 decorate(i,s,count);
133 break;
133 break;
134 }
134 }
135 default:
135 default:
136 qDebug()<<"Wrong item to be decorated by theme";
136 qDebug()<<"Wrong item to be decorated by theme";
137 break;
137 break;
138 }
138 }
139
139
140 }
140 }
141
141
142 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count)
142 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count)
143 {
143 {
144 QPen pen;
144 QPen pen;
145 QBrush brush;
145 QBrush brush;
146
146
147 if(pen != series->pen()){
147 if(pen != series->pen()){
148 item->setPen(series->pen());
148 item->setPen(series->pen());
149 }else{
149 }else{
150 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
150 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
151 pen.setWidthF(2);
151 pen.setWidthF(2);
152 item->setPen(pen);
152 item->setPen(pen);
153 }
153 }
154
154
155 if(brush != series->brush()){
155 if(brush != series->brush()){
156 item->setBrush(series->brush());
156 item->setBrush(series->brush());
157 }else{
157 }else{
158 QBrush brush(m_seriesColor.at(count%m_seriesColor.size()));
158 QBrush brush(m_seriesColor.at(count%m_seriesColor.size()));
159 item->setBrush(brush);
159 item->setBrush(brush);
160 }
160 }
161 }
161 }
162
162
163
163
164 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
164 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
165 {
165 {
166 QPen pen;
166 QPen pen;
167 if(pen != series->pen()){
167 if(pen != series->pen()){
168 item->setPen(series->pen());
168 item->setPen(series->pen());
169 return;
169 return;
170 }
170 }
171 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
171 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
172 pen.setWidthF(2);
172 pen.setWidthF(2);
173 item->setPen(pen);
173 item->setPen(pen);
174 }
174 }
175
175
176 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
176 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
177 {
177 {
178 QList<QBarSet*> sets = series->barSets();
178 QList<QBarSet*> sets = series->barSets();
179 for (int i=0; i<series->barsetCount(); i++) {
179 for (int i=0; i<series->barsetCount(); i++) {
180 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
180 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
181 }
181 }
182 }
182 }
183
183
184 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
184 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
185 {
185 {
186 QList<QBarSet*> sets = series->barSets();
186 QList<QBarSet*> sets = series->barSets();
187 for (int i=0; i<series->barsetCount(); i++) {
187 for (int i=0; i<series->barsetCount(); i++) {
188 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
188 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
189 }
189 }
190 }
190 }
191
191
192 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
192 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
193 {
193 {
194 QList<QBarSet*> sets = series->barSets();
194 QList<QBarSet*> sets = series->barSets();
195 for (int i=0; i<series->barsetCount(); i++) {
195 for (int i=0; i<series->barsetCount(); i++) {
196 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
196 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
197 }
197 }
198 }
198 }
199
199
200 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
200 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
201 {
201 {
202 Q_ASSERT(presenter);
202 Q_ASSERT(presenter);
203 Q_ASSERT(series);
203 Q_ASSERT(series);
204
204
205 QColor color = m_seriesColor.at(count % m_seriesColor.size());
205 QColor color = m_seriesColor.at(count % m_seriesColor.size());
206 // TODO: define alpha in the theme? or in the series?
206 // TODO: define alpha in the theme? or in the series?
207 //color.setAlpha(120);
207 //color.setAlpha(120);
208
208
209 QBrush brush(color, Qt::SolidPattern);
209 QBrush brush(color, Qt::SolidPattern);
210 presenter->m_markerBrush = brush;
210 presenter->m_markerBrush = brush;
211
211
212 QPen pen(brush, 3);
212 QPen pen(brush, 3);
213 pen.setColor(color);
213 pen.setColor(color);
214 presenter->m_markerPen = pen;
214 presenter->m_markerPen = pen;
215 }
215 }
216
216
217 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
217 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
218 {
218 {
219 // create a list of slice colors based on current theme
219 // create a list of slice colors based on current theme
220 int i = 0;
220 int i = 0;
221 QList<QColor> colors;
221 QList<QColor> colors;
222 while (colors.count() < series->count()) {
222 while (colors.count() < series->count()) {
223
223
224 // get base color
224 // get base color
225 QColor c = m_seriesColor[i++];
225 QColor c = m_seriesColor[i++];
226 i = i % m_seriesColor.count();
226 i = i % m_seriesColor.count();
227
227
228 // dont use black colors... looks bad
228 // dont use black colors... looks bad
229 if (c == Qt::black)
229 if (c == Qt::black)
230 continue;
230 continue;
231
231
232 // by default use the "raw" theme color
232 // by default use the "raw" theme color
233 if (!colors.contains(c)) {
233 if (!colors.contains(c)) {
234 colors << c;
234 colors << c;
235 continue;
235 continue;
236 }
236 }
237 // ...ok we need to generate something that looks like the same color
237 // ...ok we need to generate something that looks like the same color
238 // but different lightness
238 // but different lightness
239
239
240 int tryCount = 0;
240 int tryCount = 0;
241 while (tryCount++ < 100) {
241 while (tryCount++ < 100) {
242
242
243 // find maximum value we can raise the lightness
243 // find maximum value we can raise the lightness
244 int lMax = 255;
244 int lMax = 255;
245 if (lMax > 255 - c.red())
245 if (lMax > 255 - c.red())
246 lMax = 255 - c.red();
246 lMax = 255 - c.red();
247 if (lMax > 255 - c.green())
247 if (lMax > 255 - c.green())
248 lMax = 255 - c.green();
248 lMax = 255 - c.green();
249 if (lMax > 255 - c.blue())
249 if (lMax > 255 - c.blue())
250 lMax = 255 - c.blue();
250 lMax = 255 - c.blue();
251
251
252 // find maximum value we can make it darker
252 // find maximum value we can make it darker
253 int dMax = 255;
253 int dMax = 255;
254 if (dMax > c.red())
254 if (dMax > c.red())
255 dMax = c.red();
255 dMax = c.red();
256 if (dMax > c.green())
256 if (dMax > c.green())
257 dMax = c.green();
257 dMax = c.green();
258 if (dMax > c.blue())
258 if (dMax > c.blue())
259 dMax = c.blue();
259 dMax = c.blue();
260
260
261 int max = dMax + lMax;
261 int max = dMax + lMax;
262 if (max == 0) {
262 if (max == 0) {
263 // no room to make color lighter or darker...
263 // no room to make color lighter or darker...
264 qDebug() << "cannot generate a color for pie!";
264 qDebug() << "cannot generate a color for pie!";
265 break;
265 break;
266 }
266 }
267
267
268 // generate random color
268 // generate random color
269 int r = c.red() - dMax;
269 int r = c.red() - dMax;
270 int g = c.green() - dMax;
270 int g = c.green() - dMax;
271 int b = c.blue() - dMax;
271 int b = c.blue() - dMax;
272 int d = qrand() % max;
272 int d = qrand() % max;
273 c.setRgb(r+d, g+d, b+d);
273 c.setRgb(r+d, g+d, b+d);
274
274
275 // found a unique color?
275 // found a unique color?
276 if (!colors.contains(c))
276 if (!colors.contains(c))
277 break;
277 break;
278 }
278 }
279
279
280 qDebug() << "generated a color for pie" << c;
280 qDebug() << "generated a color for pie" << c;
281 colors << c;
281 colors << c;
282 }
282 }
283
283
284 // finally update colors
284 // finally update colors
285 foreach (QPieSlice* s, series->slices()) {
285 foreach (QPieSlice* s, series->slices()) {
286 QColor c = colors.takeFirst();
286 QColor c = colors.takeFirst();
287 s->setPen(c);
287 s->setPen(c);
288 s->setBrush(c);
288 s->setBrush(c);
289 }
289 }
290 }
290 }
291
291
292
292
293 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
293 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
294 {
294 {
295 //TODO: dummy defults for now
295 //TODO: dummy defults for now
296 axis->setLabelsBrush(Qt::black);
296 axis->setLabelsBrush(Qt::black);
297 axis->setLabelsPen(Qt::NoPen);
297 axis->setLabelsPen(Qt::NoPen);
298 axis->setShadesPen(Qt::NoPen);
298 axis->setShadesPen(Qt::NoPen);
299 axis->setShadesOpacity(0.5);
299 axis->setShadesOpacity(0.5);
300 }
300 }
301
301
302 void ChartTheme::decorate(SplinePresenter* presenter, QSplineSeries* series, int count)
302 void ChartTheme::decorate(SplinePresenter* presenter, QSplineSeries* series, int count)
303 {
303 {
304 Q_ASSERT(presenter);
304 Q_ASSERT(presenter);
305 Q_ASSERT(series);
305 Q_ASSERT(series);
306
306
307 QPen pen;
308 if(pen != series->pen()){
309 presenter->setPen(series->pen());
310 return;
311 }
312 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
313 pen.setWidthF(series->pen().widthF());
314 presenter->setPen(pen);
315
307 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
316 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
308 // TODO: define alpha in the theme? or in the series?
317 // TODO: define alpha in the theme? or in the series?
309 //color.setAlpha(120);
318 //color.setAlpha(120);
310
319
311 // QBrush brush(color, Qt::SolidPattern);
320 // QBrush brush(color, Qt::SolidPattern);
312 // presenter->m_markerBrush = brush;
321 // presenter->m_markerBrush = brush;
313
322
314 // QPen pen(brush, 3);
323 // QPen pen(brush, 3);
315 // pen.setColor(color);
324 // pen.setColor(color);
316 // presenter->m_markerPen = pen;
325 // presenter->m_markerPen = pen;
317 }
326 }
318
327
319 QTCOMMERCIALCHART_END_NAMESPACE
328 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,224 +1,224
1 #include "qlineseries.h"
1 #include "qlineseries.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 /*!
5 /*!
6 \class QLineSeries
6 \class QLineSeries
7 \brief The QLineSeries class is used for making line charts.
7 \brief The QLineSeries 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 Creating basic line chart is simple:
16 Creating basic line chart is simple:
17 \code
17 \code
18 QLineSeries* series = new QLineSeries();
18 QLineSeries* series = new QLineSeries();
19 series->add(0, 6);
19 series->add(0, 6);
20 series->add(2, 4);
20 series->add(2, 4);
21 ...
21 ...
22 chartView->addSeries(series);
22 chartView->addSeries(series);
23 \endcode
23 \endcode
24 */
24 */
25
25
26 /*!
26 /*!
27 \fn virtual QSeriesType QLineSeries::type() const
27 \fn virtual QSeriesType QLineSeries::type() const
28 \brief Returns type of series.
28 \brief Returns type of series.
29 \sa QSeries, QSeriesType
29 \sa QSeries, QSeriesType
30 */
30 */
31
31
32 /*!
32 /*!
33 \fn QPen QLineSeries::pen() const
33 \fn QPen QLineSeries::pen() const
34 \brief Returns the pen used to draw line for this series.
34 \brief Returns the pen used to draw line for this series.
35 \sa setPen()
35 \sa setPen()
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn bool QLineSeries::pointsVisible() const
39 \fn bool QLineSeries::pointsVisible() const
40 \brief Returns if the points are drawn for this series.
40 \brief Returns if the points are drawn for this series.
41 \sa setPointsVisible()
41 \sa setPointsVisible()
42 */
42 */
43
43
44
44
45 /*!
45 /*!
46 \fn void QLineSeries::pointReplaced(int index)
46 \fn void QLineSeries::pointReplaced(int index)
47 \brief \internal \a index
47 \brief \internal \a index
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn void QLineSeries::pointAdded(int index)
51 \fn void QLineSeries::pointAdded(int index)
52 \brief \internal \a index
52 \brief \internal \a index
53 */
53 */
54
54
55 /*!
55 /*!
56 \fn void QLineSeries::pointRemoved(int index)
56 \fn void QLineSeries::pointRemoved(int index)
57 \brief \internal \a index
57 \brief \internal \a index
58 */
58 */
59
59
60 /*!
60 /*!
61 \fn void QLineSeries::updated()
61 \fn void QLineSeries::updated()
62 \brief \internal
62 \brief \internal
63 */
63 */
64
64
65 /*!
65 /*!
66 Constructs empty series object which is a child of \a parent.
66 Constructs empty series object which is a child of \a parent.
67 When series object is added to QChartView or QChart instance ownerships is transfered.
67 When series object is added to QChartView or QChart instance ownerships is transfered.
68 */
68 */
69 QLineSeries::QLineSeries(QObject* parent):QSeries(parent),
69 QLineSeries::QLineSeries(QObject* parent):QSeries(parent),
70 m_pointsVisible(false)
70 m_pointsVisible(false)
71 {
71 {
72 }
72 }
73 /*!
73 /*!
74 Destroys the object. Series added to QChartView or QChart instances are owned by those,
74 Destroys the object. Series added to QChartView or QChart instances are owned by those,
75 and are deleted when mentioned object are destroyed.
75 and are deleted when mentioned object are destroyed.
76 */
76 */
77 QLineSeries::~QLineSeries()
77 QLineSeries::~QLineSeries()
78 {
78 {
79 }
79 }
80
80
81 /*!
81 /*!
82 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
82 Adds data point \a x \a y to the series. Points are connected with lines on the chart.
83 */
83 */
84 void QLineSeries::add(qreal x,qreal y)
84 void QLineSeries::add(qreal x,qreal y)
85 {
85 {
86 Q_ASSERT(m_x.size() == m_y.size());
86 Q_ASSERT(m_x.size() == m_y.size());
87 m_x<<x;
87 m_x<<x;
88 m_y<<y;
88 m_y<<y;
89 emit pointAdded(m_x.size()-1);
89 emit pointAdded(m_x.size()-1);
90 }
90 }
91
91
92 /*!
92 /*!
93 This is an overloaded function.
93 This is an overloaded function.
94 Adds data \a point to the series. Points are connected with lines on the chart.
94 Adds data \a point to the series. Points are connected with lines on the chart.
95 */
95 */
96 void QLineSeries::add(const QPointF& point)
96 void QLineSeries::add(const QPointF& point)
97 {
97 {
98 add(point.x(),point.y());
98 add(point.x(),point.y());
99 }
99 }
100
100
101 /*!
101 /*!
102 Modifies \a y value for given \a x a value.
102 Modifies \a y value for given \a x a value.
103 */
103 */
104 void QLineSeries::replace(qreal x,qreal y)
104 void QLineSeries::replace(qreal x,qreal y)
105 {
105 {
106 int index = m_x.indexOf(x);
106 int index = m_x.indexOf(x);
107 m_x[index]=x;
107 m_x[index]=x;
108 m_y[index]=y;
108 m_y[index]=y;
109 emit pointReplaced(index);
109 emit pointReplaced(index);
110 }
110 }
111
111
112 /*!
112 /*!
113 This is an overloaded function.
113 This is an overloaded function.
114 Replaces current y value of for given \a point x value with \a point y value.
114 Replaces current y value of for given \a point x value with \a point y value.
115 */
115 */
116 void QLineSeries::replace(const QPointF& point)
116 void QLineSeries::replace(const QPointF& point)
117 {
117 {
118 replace(point.x(),point.y());
118 replace(point.x(),point.y());
119 }
119 }
120
120
121 /*!
121 /*!
122 Removes current \a x and y value.
122 Removes current \a x and y value.
123 */
123 */
124 void QLineSeries::remove(qreal x)
124 void QLineSeries::remove(qreal x)
125 {
125 {
126 int index = m_x.indexOf(x);
126 int index = m_x.indexOf(x);
127 m_x.remove(index);
128 m_y.remove(index);
129 emit pointRemoved(index);
127 emit pointRemoved(index);
128 m_x.remove(index);
129 m_y.remove(index);
130 }
130 }
131
131
132 /*!
132 /*!
133 Removes current \a point x value. Note \a point y value is ignored.
133 Removes current \a point x value. Note \a point y value is ignored.
134 */
134 */
135 void QLineSeries::remove(const QPointF& point)
135 void QLineSeries::remove(const QPointF& point)
136 {
136 {
137 remove(point.x());
137 remove(point.x());
138 }
138 }
139
139
140 /*!
140 /*!
141 Clears all the data.
141 Clears all the data.
142 */
142 */
143 void QLineSeries::clear()
143 void QLineSeries::clear()
144 {
144 {
145 m_x.clear();
145 m_x.clear();
146 m_y.clear();
146 m_y.clear();
147 }
147 }
148
148
149 /*!
149 /*!
150 \internal \a pos
150 \internal \a pos
151 */
151 */
152 qreal QLineSeries::x(int pos) const
152 qreal QLineSeries::x(int pos) const
153 {
153 {
154 return m_x.at(pos);
154 return m_x.at(pos);
155 }
155 }
156
156
157 /*!
157 /*!
158 \internal \a pos
158 \internal \a pos
159 */
159 */
160 qreal QLineSeries::y(int pos) const
160 qreal QLineSeries::y(int pos) const
161 {
161 {
162 return m_y.at(pos);
162 return m_y.at(pos);
163 }
163 }
164
164
165 /*!
165 /*!
166 Returns number of data points within series.
166 Returns number of data points within series.
167 */
167 */
168 int QLineSeries::count() const
168 int QLineSeries::count() const
169 {
169 {
170 Q_ASSERT(m_x.size() == m_y.size());
170 Q_ASSERT(m_x.size() == m_y.size());
171
171
172 return m_x.size();
172 return m_x.size();
173
173
174 }
174 }
175
175
176 /*!
176 /*!
177 Sets \a pen used for drawing given series..
177 Sets \a pen used for drawing given series..
178 */
178 */
179 void QLineSeries::setPen(const QPen& pen)
179 void QLineSeries::setPen(const QPen& pen)
180 {
180 {
181 if(pen!=m_pen){
181 if(pen!=m_pen){
182 m_pen=pen;
182 m_pen=pen;
183 emit updated();
183 emit updated();
184 }
184 }
185 }
185 }
186
186
187 /*!
187 /*!
188 Sets if data points are \a visible and should be drawn on line.
188 Sets if data points are \a visible and should be drawn on line.
189 */
189 */
190 void QLineSeries::setPointsVisible(bool visible)
190 void QLineSeries::setPointsVisible(bool visible)
191 {
191 {
192 if(m_pointsVisible!=visible){
192 if(m_pointsVisible!=visible){
193 m_pointsVisible=visible;
193 m_pointsVisible=visible;
194 emit updated();
194 emit updated();
195 }
195 }
196 }
196 }
197
197
198 QDebug operator<< (QDebug debug, const QLineSeries series)
198 QDebug operator<< (QDebug debug, const QLineSeries series)
199 {
199 {
200 Q_ASSERT(series.m_x.size() == series.m_y.size());
200 Q_ASSERT(series.m_x.size() == series.m_y.size());
201
201
202 int size = series.m_x.size();
202 int size = series.m_x.size();
203
203
204 for (int i=0;i<size;i++) {
204 for (int i=0;i<size;i++) {
205 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
205 debug.nospace() << "(" << series.m_x.at(i) << ','<< series.m_y.at(i) << ") ";
206 }
206 }
207 return debug.space();
207 return debug.space();
208 }
208 }
209
209
210 /*!
210 /*!
211 Stream operator for adding a data \a point to the series.
211 Stream operator for adding a data \a point to the series.
212 \sa add()
212 \sa add()
213 */
213 */
214
214
215 QLineSeries& QLineSeries::operator<< (const QPointF &point)
215 QLineSeries& QLineSeries::operator<< (const QPointF &point)
216 {
216 {
217 add(point);
217 add(point);
218 return *this;
218 return *this;
219 }
219 }
220
220
221
221
222 #include "moc_qlineseries.cpp"
222 #include "moc_qlineseries.cpp"
223
223
224 QTCOMMERCIALCHART_END_NAMESPACE
224 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,37 +1,42
1 #ifndef QSPLINESERIES_H
1 #ifndef QSPLINESERIES_H
2 #define QSPLINESERIES_H
2 #define QSPLINESERIES_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QtGlobal>
5 #include <QtGlobal>
6 #include "qlineseries.h"
6 #include "qlineseries.h"
7 #include <QList>
7 #include <QList>
8 #include <QPointF>
8 #include <QPointF>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QSplineSeries : public QLineSeries
12 class QSplineSeries : public QLineSeries
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16
16
17 QSplineSeries(QObject *parent = 0);
17 QSplineSeries(QObject *parent = 0);
18 QSeriesType type() const { return QSeries::SeriesTypeSpline; }
18 QSeriesType type() const { return QSeries::SeriesTypeSpline; }
19
19
20 // int count() const { return m_x.size(); }
20 // int count() const { return m_x.size(); }
21 QPointF controlPoint(int index) const { return m_controlPoints[index]; }
21 QPointF controlPoint(int index) const { return m_controlPoints[index]; }
22
22
23 // TODO: allow the user to set custom control points
24 // void setCustomControlPoints(QList<QPointsF> controlPoints);
25 // bool calculateControlPointsAutomatically();
26 // void setCalculateControlPointsAutomatically();
27
23 private:
28 private:
24 void calculateControlPoints();
29 void calculateControlPoints();
25 QList<qreal> getFirstControlPoints(QList<qreal> rhs);
30 QList<qreal> getFirstControlPoints(QList<qreal> rhs);
26
31
27 private slots:
32 private slots:
28 void updateControlPoints();
33 void updateControlPoints();
29
34
30 private:
35 private:
31 QList<QPointF> m_controlPoints;
36 QList<QPointF> m_controlPoints;
32
37
33 };
38 };
34
39
35 QTCOMMERCIALCHART_END_NAMESPACE
40 QTCOMMERCIALCHART_END_NAMESPACE
36
41
37 #endif // QSPLINESERIES_H
42 #endif // QSPLINESERIES_H
@@ -1,77 +1,77
1 #include "splinepresenter_p.h"
1 #include "splinepresenter_p.h"
2 #include <QPainter>
2 #include <QPainter>
3
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
5
6 SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) :
6 SplinePresenter::SplinePresenter(QSplineSeries* series, QGraphicsObject *parent) :
7 LineChartItem(0, series, parent)//,m_boundingRect()
7 LineChartItem(0, series, parent)//,m_boundingRect()
8 {
8 {
9 //
9 //
10 }
10 }
11
11
12
12
13
13
14 QPointF SplinePresenter::calculateGeometryControlPoint(int index) const
14 QPointF SplinePresenter::calculateGeometryControlPoint(int index) const
15 {
15 {
16 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
16 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
17 const qreal deltaX = m_size.width()/m_domain.spanX();
17 const qreal deltaX = m_size.width()/m_domain.spanX();
18 const qreal deltaY = m_size.height()/m_domain.spanY();
18 const qreal deltaY = m_size.height()/m_domain.spanY();
19 qreal x = (splineSeries->controlPoint(index).x() - m_domain.m_minX)* deltaX;
19 qreal x = (splineSeries->controlPoint(index).x() - m_domain.m_minX)* deltaX;
20 qreal y = (splineSeries->controlPoint(index).y() - m_domain.m_minY)*-deltaY + m_size.height();
20 qreal y = (splineSeries->controlPoint(index).y() - m_domain.m_minY)*-deltaY + m_size.height();
21 return QPointF(x,y);
21 return QPointF(x,y);
22 }
22 }
23
23
24 void SplinePresenter::applyGeometry(QVector<QPointF>& points)
24 void SplinePresenter::applyGeometry(QVector<QPointF>& points)
25 {
25 {
26 if(points.size()==0) return;
26 if(points.size()==0) return;
27
27
28 QPainterPath splinePath;
28 QPainterPath splinePath;
29 const QPointF& point = points.at(0);
29 const QPointF& point = points.at(0);
30 splinePath.moveTo(point);
30 splinePath.moveTo(point);
31
31
32 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
32 // QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
33 for (int i = 0; i < splineSeries->count() - 1; i++)
33 for (int i = 0; i < points.size() - 1; i++)
34 {
34 {
35 const QPointF& point = points.at(i + 1);
35 const QPointF& point = points.at(i + 1);
36 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
36 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
37 }
37 }
38
38
39
39
40
40
41 prepareGeometryChange();
41 prepareGeometryChange();
42 m_path = splinePath;
42 m_path = splinePath;
43 m_rect = splinePath.boundingRect();
43 m_rect = splinePath.boundingRect();
44 }
44 }
45
45
46 void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
46 void SplinePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 {
47 {
48 Q_UNUSED(widget);
48 Q_UNUSED(widget);
49 Q_UNUSED(option);
49 Q_UNUSED(option);
50 painter->save();
50 painter->save();
51 painter->setPen(m_pen);
51 painter->setPen(m_pen);
52 painter->setClipRect(m_clipRect);
52 painter->setClipRect(m_clipRect);
53 painter->drawPath(m_path);
53 painter->drawPath(m_path);
54
54
55 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
55 QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(m_series);
56 for (int i = 0; i < splineSeries->count() - 1; i++)
56 for (int i = 0; i < splineSeries->count() - 1; i++)
57 {
57 {
58 painter->setPen(Qt::red);
58 painter->setPen(Qt::red);
59 painter->drawEllipse(m_points[i], 4, 4);
59 painter->drawEllipse(m_points[i], 2, 2);
60
60
61 painter->setPen(Qt::blue);
61 painter->setPen(Qt::blue);
62 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
62 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
63 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
63 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
64 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
64 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
65 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
65 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
66 }
66 }
67 if (m_points.count() > 0)
67 if (m_points.count() > 0)
68 {
68 {
69 painter->setPen(Qt::red);
69 painter->setPen(Qt::red);
70 painter->drawEllipse(m_points[m_points.count() - 1], 4, 4);
70 painter->drawEllipse(m_points[m_points.count() - 1], 2, 2);
71 }
71 }
72 painter->restore();
72 painter->restore();
73 }
73 }
74
74
75 #include "moc_splinepresenter_p.cpp"
75 #include "moc_splinepresenter_p.cpp"
76
76
77 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now