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