@@ -1,180 +1,196 | |||||
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 <qpieseries.h> |
|
5 | #include <qpieseries.h> | |
6 | #include <qpieslice.h> |
|
6 | #include <qpieslice.h> | |
|
7 | #include <qbarseries.h> | |||
|
8 | #include <qbarset.h> | |||
7 | #include <QGridLayout> |
|
9 | #include <QGridLayout> | |
8 | #include <QFormLayout> |
|
10 | #include <QFormLayout> | |
9 | #include <QComboBox> |
|
11 | #include <QComboBox> | |
10 | #include <QSpinBox> |
|
12 | #include <QSpinBox> | |
11 | #include <QCheckBox> |
|
13 | #include <QCheckBox> | |
12 | #include <QGroupBox> |
|
14 | #include <QGroupBox> | |
13 | #include <QLabel> |
|
15 | #include <QLabel> | |
14 | #include <QTime> |
|
16 | #include <QTime> | |
15 | #include <qlineseries.h> |
|
17 | #include <qlineseries.h> | |
16 | #include <qsplineseries.h> |
|
18 | #include <qsplineseries.h> | |
17 | #include <qscatterseries.h> |
|
19 | #include <qscatterseries.h> | |
18 | #include <qareaseries.h> |
|
20 | #include <qareaseries.h> | |
19 |
|
21 | |||
20 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
22 | QTCOMMERCIALCHART_USE_NAMESPACE | |
21 |
|
23 | |||
22 | typedef QPair<QPointF, QString> Data; |
|
24 | typedef QPair<QPointF, QString> Data; | |
23 | typedef QList<Data> DataList; |
|
25 | typedef QList<Data> DataList; | |
24 | typedef QList<DataList> DataTable; |
|
26 | typedef QList<DataList> DataTable; | |
25 |
|
27 | |||
26 |
|
28 | |||
27 | class MainWidget : public QWidget |
|
29 | class MainWidget : public QWidget | |
28 | { |
|
30 | { | |
29 | Q_OBJECT |
|
31 | Q_OBJECT | |
30 |
|
32 | |||
31 | public: |
|
33 | public: | |
32 | explicit MainWidget(QWidget* parent = 0) |
|
34 | explicit MainWidget(QWidget* parent = 0) | |
33 | :QWidget(parent) |
|
35 | :QWidget(parent) | |
34 | { |
|
36 | { | |
35 | // set seed for random stuff |
|
37 | // set seed for random stuff | |
36 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
38 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
37 |
|
39 | |||
38 | // generate random data |
|
40 | // generate random data | |
39 | int listCount = 3; |
|
41 | int listCount = 3; | |
40 | int valueMax = 100; |
|
42 | int valueMax = 100; | |
41 |
int valueCount = |
|
43 | int valueCount = 21; | |
42 | for (int i(0); i < listCount; i++) { |
|
44 | for (int i(0); i < listCount; i++) { | |
43 | DataList dataList; |
|
45 | DataList dataList; | |
44 | for (int j(0); j < valueCount; j++) { |
|
46 | for (int j(0); j < valueCount; j++) { | |
45 | QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax); |
|
47 | QPointF value(j + (qreal) rand() / (qreal) RAND_MAX, qrand() % valueMax); | |
46 | QString label = QString::number(i) + ":" + QString::number(j); |
|
48 | QString label = QString::number(i) + ":" + QString::number(j); | |
47 | dataList << Data(value, label); |
|
49 | dataList << Data(value, label); | |
48 | } |
|
50 | } | |
49 | m_dataTable << dataList; |
|
51 | m_dataTable << dataList; | |
50 | } |
|
52 | } | |
51 |
|
53 | |||
52 | // create layout |
|
54 | // create layout | |
53 | QGridLayout* baseLayout = new QGridLayout(); |
|
55 | QGridLayout* baseLayout = new QGridLayout(); | |
54 |
|
56 | |||
55 | // theme combo |
|
57 | // theme combo | |
56 | m_themeComboBox = new QComboBox(); |
|
58 | m_themeComboBox = new QComboBox(); | |
57 | m_themeComboBox->addItem("Default", QChart::ChartThemeDefault); |
|
59 | m_themeComboBox->addItem("Default", QChart::ChartThemeDefault); | |
58 | m_themeComboBox->addItem("Vanilla", QChart::ChartThemeVanilla); |
|
60 | m_themeComboBox->addItem("Vanilla", QChart::ChartThemeVanilla); | |
59 | m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy); |
|
61 | m_themeComboBox->addItem("Icy", QChart::ChartThemeIcy); | |
60 | m_themeComboBox->addItem("Grayscale", QChart::ChartThemeGrayscale); |
|
62 | m_themeComboBox->addItem("Grayscale", QChart::ChartThemeGrayscale); | |
61 | m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific); |
|
63 | m_themeComboBox->addItem("Scientific", QChart::ChartThemeScientific); | |
62 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme())); |
|
64 | connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateTheme())); | |
63 | baseLayout->addWidget(new QLabel("Theme:"), 0, 0); |
|
65 | baseLayout->addWidget(new QLabel("Theme:"), 0, 0); | |
64 | baseLayout->addWidget(m_themeComboBox, 0, 1); |
|
66 | baseLayout->addWidget(m_themeComboBox, 0, 1); | |
65 |
|
67 | |||
66 | // area chart |
|
68 | // area chart | |
67 | QChartView *chart = new QChartView(); |
|
69 | QChartView *chart = new QChartView(); | |
68 | chart->setChartTitle("Area chart"); |
|
70 | chart->setChartTitle("Area chart"); | |
69 | chart->setRenderHint(QPainter::Antialiasing); |
|
71 | chart->setRenderHint(QPainter::Antialiasing); | |
70 | baseLayout->addWidget(chart, 1, 0); |
|
72 | baseLayout->addWidget(chart, 1, 0); | |
71 | { |
|
73 | { | |
72 | QLineSeries *series1 = new QLineSeries(chart); |
|
74 | QLineSeries *series1 = new QLineSeries(chart); | |
73 | foreach (Data data, m_dataTable.first()) |
|
75 | foreach (Data data, m_dataTable.first()) | |
74 | series1->add(data.first); |
|
76 | series1->add(data.first); | |
75 | QLineSeries *series2 = new QLineSeries(chart); |
|
77 | QLineSeries *series2 = new QLineSeries(chart); | |
76 | foreach (Data data, m_dataTable.last()) |
|
78 | foreach (Data data, m_dataTable.last()) | |
77 | series2->add(data.first); |
|
79 | series2->add(data.first); | |
78 | QAreaSeries *series = new QAreaSeries(series1, series2); |
|
80 | QAreaSeries *series = new QAreaSeries(series1, series2); | |
79 | chart->addSeries(series); |
|
81 | chart->addSeries(series); | |
80 | } |
|
82 | } | |
81 | m_charts << chart; |
|
83 | m_charts << chart; | |
82 |
|
84 | |||
83 | // bar chart |
|
85 | // bar chart | |
84 | chart = new QChartView(); |
|
86 | chart = new QChartView(); | |
85 | chart->setChartTitle("bar chart"); |
|
87 | chart->setChartTitle("bar chart"); | |
86 | chart->setRenderHint(QPainter::Antialiasing); |
|
88 | chart->setRenderHint(QPainter::Antialiasing); | |
87 | baseLayout->addWidget(chart, 1, 1); |
|
89 | baseLayout->addWidget(chart, 1, 1); | |
|
90 | { | |||
|
91 | QStringList categories; | |||
|
92 | // TODO: categories | |||
|
93 | for (int i(0); i < valueCount; i++) | |||
|
94 | categories << QString::number(i); | |||
|
95 | QBarSeries* series = new QBarSeries(categories, chart); | |||
|
96 | for (int i(0); i < m_dataTable.count(); i++) { | |||
|
97 | QBarSet *set = new QBarSet("Set" + QString::number(i)); | |||
|
98 | foreach (Data data, m_dataTable[i]) | |||
|
99 | *set << data.first.y(); | |||
|
100 | series->addBarSet(set); | |||
|
101 | } | |||
|
102 | chart->addSeries(series); | |||
|
103 | } | |||
88 | m_charts << chart; |
|
104 | m_charts << chart; | |
89 |
|
105 | |||
90 | // line chart |
|
106 | // line chart | |
91 | chart = new QChartView(); |
|
107 | chart = new QChartView(); | |
92 | chart->setChartTitle("line chart"); |
|
108 | chart->setChartTitle("line chart"); | |
93 | chart->setRenderHint(QPainter::Antialiasing); |
|
109 | chart->setRenderHint(QPainter::Antialiasing); | |
94 | baseLayout->addWidget(chart, 1, 2); |
|
110 | baseLayout->addWidget(chart, 1, 2); | |
95 | foreach (DataList list, m_dataTable) { |
|
111 | foreach (DataList list, m_dataTable) { | |
96 | QLineSeries *series = new QLineSeries(chart); |
|
112 | QLineSeries *series = new QLineSeries(chart); | |
97 | foreach (Data data, list) |
|
113 | foreach (Data data, list) | |
98 | series->add(data.first); |
|
114 | series->add(data.first); | |
99 | chart->addSeries(series); |
|
115 | chart->addSeries(series); | |
100 | } |
|
116 | } | |
101 | m_charts << chart; |
|
117 | m_charts << chart; | |
102 |
|
118 | |||
103 | // pie chart |
|
119 | // pie chart | |
104 | chart = new QChartView(); |
|
120 | chart = new QChartView(); | |
105 | chart->setChartTitle("pie chart"); |
|
121 | chart->setChartTitle("pie chart"); | |
106 | chart->setRenderHint(QPainter::Antialiasing); |
|
122 | chart->setRenderHint(QPainter::Antialiasing); | |
107 | baseLayout->addWidget(chart, 2, 0); |
|
123 | baseLayout->addWidget(chart, 2, 0); | |
108 |
|
124 | |||
109 | qreal pieSize = 1.0 / m_dataTable.count(); |
|
125 | qreal pieSize = 1.0 / m_dataTable.count(); | |
110 | for (int i=0; i<m_dataTable.count(); i++) { |
|
126 | for (int i=0; i<m_dataTable.count(); i++) { | |
111 | QPieSeries *series = new QPieSeries(chart); |
|
127 | QPieSeries *series = new QPieSeries(chart); | |
112 | foreach (Data data, m_dataTable[i]) |
|
128 | foreach (Data data, m_dataTable[i]) | |
113 | series->add(data.first.y(), data.second); |
|
129 | series->add(data.first.y(), data.second); | |
114 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); |
|
130 | qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); | |
115 | series->setPieSize(pieSize); |
|
131 | series->setPieSize(pieSize); | |
116 | series->setPiePosition(hPos, 0.5); |
|
132 | series->setPiePosition(hPos, 0.5); | |
117 | chart->addSeries(series); |
|
133 | chart->addSeries(series); | |
118 | } |
|
134 | } | |
119 | m_charts << chart; |
|
135 | m_charts << chart; | |
120 |
|
136 | |||
121 | // spine chart |
|
137 | // spine chart | |
122 | chart = new QChartView(); |
|
138 | chart = new QChartView(); | |
123 | chart->setChartTitle("spline chart"); |
|
139 | chart->setChartTitle("spline chart"); | |
124 | chart->setRenderHint(QPainter::Antialiasing); |
|
140 | chart->setRenderHint(QPainter::Antialiasing); | |
125 | baseLayout->addWidget(chart, 2, 1); |
|
141 | baseLayout->addWidget(chart, 2, 1); | |
126 | foreach (DataList list, m_dataTable) { |
|
142 | foreach (DataList list, m_dataTable) { | |
127 | QSplineSeries *series = new QSplineSeries(chart); |
|
143 | QSplineSeries *series = new QSplineSeries(chart); | |
128 | foreach (Data data, list) |
|
144 | foreach (Data data, list) | |
129 | series->add(data.first); |
|
145 | series->add(data.first); | |
130 | chart->addSeries(series); |
|
146 | chart->addSeries(series); | |
131 | } |
|
147 | } | |
132 | m_charts << chart; |
|
148 | m_charts << chart; | |
133 |
|
149 | |||
134 | // scatter chart |
|
150 | // scatter chart | |
135 | chart = new QChartView(); |
|
151 | chart = new QChartView(); | |
136 | chart->setChartTitle("scatter chart"); |
|
152 | chart->setChartTitle("scatter chart"); | |
137 | chart->setRenderHint(QPainter::Antialiasing); |
|
153 | chart->setRenderHint(QPainter::Antialiasing); | |
138 | baseLayout->addWidget(chart, 2, 2); |
|
154 | baseLayout->addWidget(chart, 2, 2); | |
139 | foreach (DataList list, m_dataTable) { |
|
155 | foreach (DataList list, m_dataTable) { | |
140 | QScatterSeries *series = new QScatterSeries(chart); |
|
156 | QScatterSeries *series = new QScatterSeries(chart); | |
141 | foreach (Data data, list) |
|
157 | foreach (Data data, list) | |
142 | series->add(data.first); |
|
158 | series->add(data.first); | |
143 | chart->addSeries(series); |
|
159 | chart->addSeries(series); | |
144 | } |
|
160 | } | |
145 | m_charts << chart; |
|
161 | m_charts << chart; | |
146 |
|
162 | |||
147 | setLayout(baseLayout); |
|
163 | setLayout(baseLayout); | |
148 | } |
|
164 | } | |
149 |
|
165 | |||
150 | public Q_SLOTS: |
|
166 | public Q_SLOTS: | |
151 |
|
167 | |||
152 | void updateTheme() |
|
168 | void updateTheme() | |
153 | { |
|
169 | { | |
154 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); |
|
170 | QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt(); | |
155 | foreach (QChartView *chart, m_charts) |
|
171 | foreach (QChartView *chart, m_charts) | |
156 | chart->setChartTheme(theme); |
|
172 | chart->setChartTheme(theme); | |
157 | } |
|
173 | } | |
158 |
|
174 | |||
159 | private: |
|
175 | private: | |
160 | QList<QChartView*> m_charts; |
|
176 | QList<QChartView*> m_charts; | |
161 | QComboBox *m_themeComboBox; |
|
177 | QComboBox *m_themeComboBox; | |
162 | DataTable m_dataTable; |
|
178 | DataTable m_dataTable; | |
163 | }; |
|
179 | }; | |
164 |
|
180 | |||
165 | int main(int argc, char *argv[]) |
|
181 | int main(int argc, char *argv[]) | |
166 | { |
|
182 | { | |
167 | QApplication a(argc, argv); |
|
183 | QApplication a(argc, argv); | |
168 |
|
184 | |||
169 | QMainWindow window; |
|
185 | QMainWindow window; | |
170 |
|
186 | |||
171 | MainWidget* widget = new MainWidget(); |
|
187 | MainWidget* widget = new MainWidget(); | |
172 |
|
188 | |||
173 | window.setCentralWidget(widget); |
|
189 | window.setCentralWidget(widget); | |
174 | window.resize(900, 600); |
|
190 | window.resize(900, 600); | |
175 | window.show(); |
|
191 | window.show(); | |
176 |
|
192 | |||
177 | return a.exec(); |
|
193 | return a.exec(); | |
178 | } |
|
194 | } | |
179 |
|
195 | |||
180 | #include "main.moc" |
|
196 | #include "main.moc" |
@@ -1,89 +1,93 | |||||
1 | #include "bar_p.h" |
|
1 | #include "bar_p.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QPainter> |
|
3 | #include <QPainter> | |
4 | #include <QGraphicsSceneEvent> |
|
4 | #include <QGraphicsSceneEvent> | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | Bar::Bar(QString category, QGraphicsItem *parent) |
|
8 | Bar::Bar(QString category, QGraphicsItem *parent) | |
9 | : QGraphicsObject(parent) |
|
9 | : QGraphicsObject(parent), | |
10 |
|
|
10 | mCategory(category), | |
|
11 | mXpos(0), | |||
|
12 | mYpos(0), | |||
|
13 | mWidth(0), | |||
|
14 | mHeight(0) | |||
11 | { |
|
15 | { | |
12 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
16 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | |
13 | setAcceptHoverEvents(true); |
|
17 | setAcceptHoverEvents(true); | |
14 | } |
|
18 | } | |
15 |
|
19 | |||
16 | void Bar::setSize(const QSizeF& size) |
|
20 | void Bar::setSize(const QSizeF& size) | |
17 | { |
|
21 | { | |
18 | mWidth = size.width(); |
|
22 | mWidth = size.width(); | |
19 | mHeight = size.height(); |
|
23 | mHeight = size.height(); | |
20 | } |
|
24 | } | |
21 |
|
25 | |||
22 |
|
26 | |||
23 | void Bar::resize( qreal w, qreal h ) |
|
27 | void Bar::resize( qreal w, qreal h ) | |
24 | { |
|
28 | { | |
25 | mWidth = w; |
|
29 | mWidth = w; | |
26 | mHeight = h; |
|
30 | mHeight = h; | |
27 | } |
|
31 | } | |
28 |
|
32 | |||
29 | void Bar::setPos(qreal x, qreal y) |
|
33 | void Bar::setPos(qreal x, qreal y) | |
30 | { |
|
34 | { | |
31 | mXpos = x; |
|
35 | mXpos = x; | |
32 | mYpos = y; |
|
36 | mYpos = y; | |
33 | } |
|
37 | } | |
34 |
|
38 | |||
35 | void Bar::setPen(QPen pen) |
|
39 | void Bar::setPen(QPen pen) | |
36 | { |
|
40 | { | |
37 | mPen = pen; |
|
41 | mPen = pen; | |
38 | } |
|
42 | } | |
39 |
|
43 | |||
40 | void Bar::setBrush(QBrush brush) |
|
44 | void Bar::setBrush(QBrush brush) | |
41 | { |
|
45 | { | |
42 | mBrush = brush; |
|
46 | mBrush = brush; | |
43 | } |
|
47 | } | |
44 |
|
48 | |||
45 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
49 | void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
46 | { |
|
50 | { | |
47 | if (0 == mHeight) { |
|
51 | if (0 == mHeight) { | |
48 | return; |
|
52 | return; | |
49 | } |
|
53 | } | |
50 | painter->setBrush(mBrush); |
|
54 | painter->setBrush(mBrush); | |
51 |
|
55 | |||
52 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. |
|
56 | // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. | |
53 | int x0 = mXpos; |
|
57 | int x0 = mXpos; | |
54 | int x1 = (mXpos + mWidth); |
|
58 | int x1 = (mXpos + mWidth); | |
55 | int w = x1-x0; |
|
59 | int w = x1-x0; | |
56 | int y0 = mYpos; |
|
60 | int y0 = mYpos; | |
57 | int y1 = (mYpos + mHeight); |
|
61 | int y1 = (mYpos + mHeight); | |
58 | int h = y1-y0; |
|
62 | int h = y1-y0; | |
59 | painter->drawRect(x0, y0 ,w ,h); |
|
63 | painter->drawRect(x0, y0 ,w ,h); | |
60 | } |
|
64 | } | |
61 |
|
65 | |||
62 | QRectF Bar::boundingRect() const |
|
66 | QRectF Bar::boundingRect() const | |
63 | { |
|
67 | { | |
64 | QRectF r(mXpos, mYpos, mWidth, mHeight); |
|
68 | QRectF r(mXpos, mYpos, mWidth, mHeight); | |
65 | return r; |
|
69 | return r; | |
66 | } |
|
70 | } | |
67 |
|
71 | |||
68 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) |
|
72 | void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) | |
69 | { |
|
73 | { | |
70 | if (event->button() == Qt::LeftButton) { |
|
74 | if (event->button() == Qt::LeftButton) { | |
71 | emit clicked(mCategory); |
|
75 | emit clicked(mCategory); | |
72 | } else if (event->button() == Qt::RightButton) { |
|
76 | } else if (event->button() == Qt::RightButton) { | |
73 | emit rightClicked(mCategory); |
|
77 | emit rightClicked(mCategory); | |
74 | } |
|
78 | } | |
75 | } |
|
79 | } | |
76 |
|
80 | |||
77 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) |
|
81 | void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* event) | |
78 | { |
|
82 | { | |
79 | emit hoverEntered(event->lastScreenPos()); |
|
83 | emit hoverEntered(event->lastScreenPos()); | |
80 | } |
|
84 | } | |
81 |
|
85 | |||
82 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
86 | void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) | |
83 | { |
|
87 | { | |
84 | emit hoverLeaved(); |
|
88 | emit hoverLeaved(); | |
85 | } |
|
89 | } | |
86 |
|
90 | |||
87 | #include "moc_bar_p.cpp" |
|
91 | #include "moc_bar_p.cpp" | |
88 |
|
92 | |||
89 | QTCOMMERCIALCHART_END_NAMESPACE |
|
93 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,178 +1,180 | |||||
1 | #include "barpresenterbase_p.h" |
|
1 | #include "barpresenterbase_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include "qbarset.h" |
|
5 | #include "qbarset.h" | |
6 | #include "qbarseries.h" |
|
6 | #include "qbarseries.h" | |
7 | #include "qchart.h" |
|
7 | #include "qchart.h" | |
8 | #include "qchartaxis.h" |
|
8 | #include "qchartaxis.h" | |
9 | #include "qchartaxiscategories.h" |
|
9 | #include "qchartaxiscategories.h" | |
10 | #include <QDebug> |
|
10 | #include <QDebug> | |
11 | #include <QToolTip> |
|
11 | #include <QToolTip> | |
12 |
|
12 | |||
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
14 |
|
14 | |||
15 | BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) |
|
15 | BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : | |
16 |
|
|
16 | ChartItem(parent), | |
17 |
|
|
17 | mLayoutSet(false), | |
18 |
|
|
18 | mSeries(series), | |
19 |
|
|
19 | mChart(parent), | |
|
20 | mWidth(0), | |||
|
21 | mHeight(0) | |||
20 | { |
|
22 | { | |
21 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); |
|
23 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | |
22 | connect(series,SIGNAL(enableSeparators(bool)),this,SLOT(enableSeparators(bool))); |
|
24 | connect(series,SIGNAL(enableSeparators(bool)),this,SLOT(enableSeparators(bool))); | |
23 | enableSeparators(series->separatorsVisible()); |
|
25 | enableSeparators(series->separatorsVisible()); | |
24 | initAxisLabels(); |
|
26 | initAxisLabels(); | |
25 | dataChanged(); |
|
27 | dataChanged(); | |
26 | } |
|
28 | } | |
27 |
|
29 | |||
28 | BarPresenterBase::~BarPresenterBase() |
|
30 | BarPresenterBase::~BarPresenterBase() | |
29 | { |
|
31 | { | |
30 | disconnect(this,SLOT(showToolTip(QPoint,QString))); |
|
32 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | |
31 | disconnect(this,SLOT(enableSeparators(bool))); |
|
33 | disconnect(this,SLOT(enableSeparators(bool))); | |
32 | } |
|
34 | } | |
33 |
|
35 | |||
34 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
36 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
35 | { |
|
37 | { | |
36 | if (!mLayoutSet) { |
|
38 | if (!mLayoutSet) { | |
37 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; |
|
39 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; | |
38 | return; |
|
40 | return; | |
39 | } |
|
41 | } | |
40 | foreach(QGraphicsItem* i, childItems()) { |
|
42 | foreach(QGraphicsItem* i, childItems()) { | |
41 | i->paint(painter,option,widget); |
|
43 | i->paint(painter,option,widget); | |
42 | } |
|
44 | } | |
43 | } |
|
45 | } | |
44 |
|
46 | |||
45 | QRectF BarPresenterBase::boundingRect() const |
|
47 | QRectF BarPresenterBase::boundingRect() const | |
46 | { |
|
48 | { | |
47 | return QRectF(0,0,mWidth,mHeight); |
|
49 | return QRectF(0, 0, mWidth, mHeight); | |
48 | } |
|
50 | } | |
49 |
|
51 | |||
50 | void BarPresenterBase::dataChanged() |
|
52 | void BarPresenterBase::dataChanged() | |
51 | { |
|
53 | { | |
52 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
54 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | |
53 | // Delete old bars |
|
55 | // Delete old bars | |
54 | foreach (QGraphicsItem* item, childItems()) { |
|
56 | foreach (QGraphicsItem* item, childItems()) { | |
55 | delete item; |
|
57 | delete item; | |
56 | } |
|
58 | } | |
57 |
|
59 | |||
58 | mBars.clear(); |
|
60 | mBars.clear(); | |
59 | mSeparators.clear(); |
|
61 | mSeparators.clear(); | |
60 | mFloatingValues.clear(); |
|
62 | mFloatingValues.clear(); | |
61 |
|
63 | |||
62 | // Create new graphic items for bars |
|
64 | // Create new graphic items for bars | |
63 | for (int c=0; c<mSeries->categoryCount(); c++) { |
|
65 | for (int c=0; c<mSeries->categoryCount(); c++) { | |
64 | QString category = mSeries->categoryName(c); |
|
66 | QString category = mSeries->categoryName(c); | |
65 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
67 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
66 | QBarSet *set = mSeries->barsetAt(s); |
|
68 | QBarSet *set = mSeries->barsetAt(s); | |
67 | Bar *bar = new Bar(category,this); |
|
69 | Bar *bar = new Bar(category,this); | |
68 | childItems().append(bar); |
|
70 | childItems().append(bar); | |
69 | mBars.append(bar); |
|
71 | mBars.append(bar); | |
70 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); |
|
72 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); | |
71 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); |
|
73 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); | |
72 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); |
|
74 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); | |
73 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); |
|
75 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); | |
74 | } |
|
76 | } | |
75 | } |
|
77 | } | |
76 |
|
78 | |||
77 | // Create separators |
|
79 | // Create separators | |
78 | int count = mSeries->categoryCount() - 1; // There is one less separator than columns |
|
80 | int count = mSeries->categoryCount() - 1; // There is one less separator than columns | |
79 | for (int i=0; i<count; i++) { |
|
81 | for (int i=0; i<count; i++) { | |
80 | Separator* sep = new Separator(this); |
|
82 | Separator* sep = new Separator(this); | |
81 | sep->setVisible(mSeries->separatorsVisible()); |
|
83 | sep->setVisible(mSeries->separatorsVisible()); | |
82 | childItems().append(sep); |
|
84 | childItems().append(sep); | |
83 | mSeparators.append(sep); |
|
85 | mSeparators.append(sep); | |
84 | } |
|
86 | } | |
85 |
|
87 | |||
86 | // Create floating values |
|
88 | // Create floating values | |
87 | for (int category=0; category<mSeries->categoryCount(); category++) { |
|
89 | for (int category=0; category<mSeries->categoryCount(); category++) { | |
88 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
90 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
89 | QBarSet *set = mSeries->barsetAt(s); |
|
91 | QBarSet *set = mSeries->barsetAt(s); | |
90 | BarValue *value = new BarValue(*set, this); |
|
92 | BarValue *value = new BarValue(*set, this); | |
91 | childItems().append(value); |
|
93 | childItems().append(value); | |
92 | mFloatingValues.append(value); |
|
94 | mFloatingValues.append(value); | |
93 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); |
|
95 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | |
94 | } |
|
96 | } | |
95 | } |
|
97 | } | |
96 | } |
|
98 | } | |
97 |
|
99 | |||
98 | void BarPresenterBase::initAxisLabels() |
|
100 | void BarPresenterBase::initAxisLabels() | |
99 | { |
|
101 | { | |
100 | int count = mSeries->categoryCount(); |
|
102 | int count = mSeries->categoryCount(); | |
101 | if (0 == count) { |
|
103 | if (0 == count) { | |
102 | return; |
|
104 | return; | |
103 | } |
|
105 | } | |
104 |
|
106 | |||
105 | mChart->axisX()->setTicksCount(count+2); |
|
107 | mChart->axisX()->setTicksCount(count+2); | |
106 |
|
108 | |||
107 | qreal min = 0; |
|
109 | qreal min = 0; | |
108 | qreal max = count+1; |
|
110 | qreal max = count+1; | |
109 |
|
111 | |||
110 | mChart->axisX()->setMin(min); |
|
112 | mChart->axisX()->setMin(min); | |
111 | mChart->axisX()->setMax(max); |
|
113 | mChart->axisX()->setMax(max); | |
112 |
|
114 | |||
113 | QChartAxisCategories* categories = mChart->axisX()->categories(); |
|
115 | QChartAxisCategories* categories = mChart->axisX()->categories(); | |
114 | categories->clear(); |
|
116 | categories->clear(); | |
115 | for (int i=0; i<count; i++) { |
|
117 | for (int i=0; i<count; i++) { | |
116 | categories->insert(i+1,mSeries->categoryName(i)); |
|
118 | categories->insert(i+1,mSeries->categoryName(i)); | |
117 | } |
|
119 | } | |
118 |
|
120 | |||
119 |
|
121 | |||
120 |
|
122 | |||
121 | mChart->axisX()->setLabelsVisible(true); |
|
123 | mChart->axisX()->setLabelsVisible(true); | |
122 | } |
|
124 | } | |
123 |
|
125 | |||
124 | //handlers |
|
126 | //handlers | |
125 |
|
127 | |||
126 | void BarPresenterBase::handleModelChanged(int index) |
|
128 | void BarPresenterBase::handleModelChanged(int index) | |
127 | { |
|
129 | { | |
128 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; |
|
130 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; | |
129 | dataChanged(); |
|
131 | dataChanged(); | |
130 | } |
|
132 | } | |
131 |
|
133 | |||
132 | void BarPresenterBase::handleDomainChanged(const Domain& domain) |
|
134 | void BarPresenterBase::handleDomainChanged(const Domain& domain) | |
133 | { |
|
135 | { | |
134 | qDebug() << "BarPresenterBase::handleDomainChanged"; |
|
136 | qDebug() << "BarPresenterBase::handleDomainChanged"; | |
135 | /* |
|
137 | /* | |
136 | int count = mSeries->categoryCount(); |
|
138 | int count = mSeries->categoryCount(); | |
137 | if (0 == count) { |
|
139 | if (0 == count) { | |
138 | return; |
|
140 | return; | |
139 | } |
|
141 | } | |
140 |
|
142 | |||
141 | // Position labels to domain |
|
143 | // Position labels to domain | |
142 | qreal min = domain.minX(); |
|
144 | qreal min = domain.minX(); | |
143 | qreal max = domain.maxX(); |
|
145 | qreal max = domain.maxX(); | |
144 | qreal step = (max-min)/count; |
|
146 | qreal step = (max-min)/count; | |
145 | QChartAxisCategories& categories = mChart->axisX()->categories(); |
|
147 | QChartAxisCategories& categories = mChart->axisX()->categories(); | |
146 | categories.clear(); |
|
148 | categories.clear(); | |
147 | for (int i=0; i<count; i++) { |
|
149 | for (int i=0; i<count; i++) { | |
148 | categories.insert(min,mSeries->categoryName(i)); |
|
150 | categories.insert(min,mSeries->categoryName(i)); | |
149 | min += step; |
|
151 | min += step; | |
150 | } |
|
152 | } | |
151 | */ |
|
153 | */ | |
152 | } |
|
154 | } | |
153 |
|
155 | |||
154 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) |
|
156 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) | |
155 | { |
|
157 | { | |
156 | mWidth = rect.width(); |
|
158 | mWidth = rect.width(); | |
157 | mHeight = rect.height(); |
|
159 | mHeight = rect.height(); | |
158 | layoutChanged(); |
|
160 | layoutChanged(); | |
159 | mLayoutSet = true; |
|
161 | mLayoutSet = true; | |
160 | setPos(rect.topLeft()); |
|
162 | setPos(rect.topLeft()); | |
161 | } |
|
163 | } | |
162 |
|
164 | |||
163 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) |
|
165 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) | |
164 | { |
|
166 | { | |
165 | // TODO: cool tooltip instead of default |
|
167 | // TODO: cool tooltip instead of default | |
166 | QToolTip::showText(pos,tip); |
|
168 | QToolTip::showText(pos,tip); | |
167 | } |
|
169 | } | |
168 |
|
170 | |||
169 | void BarPresenterBase::enableSeparators(bool enabled) |
|
171 | void BarPresenterBase::enableSeparators(bool enabled) | |
170 | { |
|
172 | { | |
171 | for (int i=0; i<mSeparators.count(); i++) { |
|
173 | for (int i=0; i<mSeparators.count(); i++) { | |
172 | mSeparators.at(i)->setVisible(enabled); |
|
174 | mSeparators.at(i)->setVisible(enabled); | |
173 | } |
|
175 | } | |
174 | } |
|
176 | } | |
175 |
|
177 | |||
176 | #include "moc_barpresenterbase_p.cpp" |
|
178 | #include "moc_barpresenterbase_p.cpp" | |
177 |
|
179 | |||
178 | QTCOMMERCIALCHART_END_NAMESPACE |
|
180 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,66 +1,70 | |||||
1 | #include "barvalue_p.h" |
|
1 | #include "barvalue_p.h" | |
2 | #include <QPainter> |
|
2 | #include <QPainter> | |
3 | #include <QPen> |
|
3 | #include <QPen> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | BarValue::BarValue(QBarSet &set, QGraphicsItem *parent) |
|
7 | BarValue::BarValue(QBarSet &set, QGraphicsItem *parent) | |
8 | : QGraphicsObject(parent) |
|
8 | : QGraphicsObject(parent), | |
9 |
|
|
9 | mBarSet(set), | |
|
10 | mXpos(0), | |||
|
11 | mYpos(0), | |||
|
12 | mWidth(0), | |||
|
13 | mHeight(0) | |||
10 | { |
|
14 | { | |
11 | setVisible(false); |
|
15 | setVisible(false); | |
12 | } |
|
16 | } | |
13 |
|
17 | |||
14 | void BarValue::setValueString(QString str) |
|
18 | void BarValue::setValueString(QString str) | |
15 | { |
|
19 | { | |
16 | mValueString = str; |
|
20 | mValueString = str; | |
17 | } |
|
21 | } | |
18 |
|
22 | |||
19 | QString BarValue::valueString() |
|
23 | QString BarValue::valueString() | |
20 | { |
|
24 | { | |
21 | return mValueString; |
|
25 | return mValueString; | |
22 | } |
|
26 | } | |
23 |
|
27 | |||
24 | void BarValue::setPen(const QPen pen) |
|
28 | void BarValue::setPen(const QPen pen) | |
25 | { |
|
29 | { | |
26 | mPen = pen; |
|
30 | mPen = pen; | |
27 | } |
|
31 | } | |
28 |
|
32 | |||
29 | QPen BarValue::pen() const |
|
33 | QPen BarValue::pen() const | |
30 | { |
|
34 | { | |
31 | return mPen; |
|
35 | return mPen; | |
32 | } |
|
36 | } | |
33 |
|
37 | |||
34 | void BarValue::resize(qreal w, qreal h) |
|
38 | void BarValue::resize(qreal w, qreal h) | |
35 | { |
|
39 | { | |
36 | mWidth = w; |
|
40 | mWidth = w; | |
37 | mHeight = h; |
|
41 | mHeight = h; | |
38 | } |
|
42 | } | |
39 |
|
43 | |||
40 | void BarValue::setPos(qreal x, qreal y) |
|
44 | void BarValue::setPos(qreal x, qreal y) | |
41 | { |
|
45 | { | |
42 | mXpos = x; |
|
46 | mXpos = x; | |
43 | mYpos = y; |
|
47 | mYpos = y; | |
44 | } |
|
48 | } | |
45 |
|
49 | |||
46 | void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
50 | void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
47 | { |
|
51 | { | |
48 | if (isVisible()) { |
|
52 | if (isVisible()) { | |
49 | painter->setPen(mPen); |
|
53 | painter->setPen(mPen); | |
50 | painter->drawText(boundingRect(),mValueString); |
|
54 | painter->drawText(boundingRect(),mValueString); | |
51 | } |
|
55 | } | |
52 | } |
|
56 | } | |
53 |
|
57 | |||
54 | QRectF BarValue::boundingRect() const |
|
58 | QRectF BarValue::boundingRect() const | |
55 | { |
|
59 | { | |
56 | QRectF r(mXpos, mYpos, mWidth, mHeight); |
|
60 | QRectF r(mXpos, mYpos, mWidth, mHeight); | |
57 | return r; |
|
61 | return r; | |
58 | } |
|
62 | } | |
59 |
|
63 | |||
60 | void BarValue::toggleVisible() |
|
64 | void BarValue::toggleVisible() | |
61 | { |
|
65 | { | |
62 | setVisible(!isVisible()); |
|
66 | setVisible(!isVisible()); | |
63 | } |
|
67 | } | |
64 |
|
68 | |||
65 | #include "moc_barvalue_p.cpp" |
|
69 | #include "moc_barvalue_p.cpp" | |
66 | QTCOMMERCIALCHART_END_NAMESPACE |
|
70 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now