##// END OF EJS Templates
Updated customchart example. Now it uses QIntervalsAxis
Marek Rosa -
r1702:f2951e50a7b8
parent child
Show More
@@ -1,122 +1,125
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QApplication>
21 #include <QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QLineSeries>
24 #include <QLineSeries>
25 #include <QValuesAxis>
25 #include <QIntervalsAxis>
26 #include <QBarCategoriesAxis>
27
26
28 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
29
28
30 int main(int argc, char *argv[])
29 int main(int argc, char *argv[])
31 {
30 {
32 QApplication a(argc, argv);
31 QApplication a(argc, argv);
33
32
34 //![1]
33 //![1]
35 QLineSeries* series = new QLineSeries();
34 QLineSeries* series = new QLineSeries();
36 *series << QPointF(0, 0.6) << QPointF(0.5, 0.4) << QPointF(1, 2) << QPointF(1.5, 1.2) << QPointF(2, 1.0);
35 *series << QPointF(0, 6) << QPointF(9, 4) << QPointF(15, 20) << QPointF(25, 12) << QPointF(29, 26);
37 QChart* chart = new QChart();
36 QChart* chart = new QChart();
38 chart->legend()->hide();
37 chart->legend()->hide();
39 chart->addSeries(series);
38 chart->addSeries(series);
40 //![1]
39 //![1]
41
40
42 //![2]
41 //![2]
43 // Customize series
42 // Customize series
44 QPen pen(QRgb(0xfdb157));
43 QPen pen(QRgb(0xfdb157));
45 pen.setWidth(5);
44 pen.setWidth(5);
46 series->setPen(pen);
45 series->setPen(pen);
47
46
48 // Customize chart title
47 // Customize chart title
49 QFont font;
48 QFont font;
50 font.setPixelSize(18);
49 font.setPixelSize(18);
51 chart->setTitleFont(font);
50 chart->setTitleFont(font);
52 chart->setTitleBrush(QBrush(Qt::white));
51 chart->setTitleBrush(QBrush(Qt::white));
53 chart->setTitle("Customchart example");
52 chart->setTitle("Customchart example");
54
53
55 // Customize chart background
54 // Customize chart background
56 QLinearGradient backgroundGradient;
55 QLinearGradient backgroundGradient;
57 backgroundGradient.setStart(QPointF(0,0));
56 backgroundGradient.setStart(QPointF(0,0));
58 backgroundGradient.setFinalStop(QPointF(0,1));
57 backgroundGradient.setFinalStop(QPointF(0,1));
59 backgroundGradient.setColorAt(0.0, QRgb(0xd2d0d1));
58 backgroundGradient.setColorAt(0.0, QRgb(0xd2d0d1));
60 backgroundGradient.setColorAt(1.0, QRgb(0x4c4547));
59 backgroundGradient.setColorAt(1.0, QRgb(0x4c4547));
61 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
60 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
62 chart->setBackgroundBrush(backgroundGradient);
61 chart->setBackgroundBrush(backgroundGradient);
63 //![2]
62 //![2]
64
63
65 //![3]
64 //![3]
66 QBarCategoriesAxis* axisX = new QBarCategoriesAxis();
65 QIntervalsAxis* axisX = new QIntervalsAxis();
67 QBarCategoriesAxis* axisY = new QBarCategoriesAxis();
66 QIntervalsAxis* axisY = new QIntervalsAxis();
68
67
69 // Customize axis label font
68 // Customize axis label font
70 QFont labelsFont;
69 QFont labelsFont;
71 labelsFont.setPixelSize(12);
70 labelsFont.setPixelSize(12);
72 axisX->setLabelsFont(labelsFont);
71 axisX->setLabelsFont(labelsFont);
73 axisY->setLabelsFont(labelsFont);
72 axisY->setLabelsFont(labelsFont);
74
73
75 // Customize axis colors
74 // Customize axis colors
76 QPen axisPen(QRgb(0xd18952));
75 QPen axisPen(QRgb(0xd18952));
77 axisPen.setWidth(2);
76 axisPen.setWidth(2);
78 axisX->setAxisPen(axisPen);
77 axisX->setAxisPen(axisPen);
79 axisY->setAxisPen(axisPen);
78 axisY->setAxisPen(axisPen);
80
79
81 // Customize axis label colors
80 // Customize axis label colors
82 QBrush axisBrush(Qt::white);
81 QBrush axisBrush(Qt::white);
83 axisX->setLabelsBrush(axisBrush);
82 axisX->setLabelsBrush(axisBrush);
84 axisY->setLabelsBrush(axisBrush);
83 axisY->setLabelsBrush(axisBrush);
85
84
86 // Customize grid lines and shades
85 // Customize grid lines and shades
87 axisX->setGridLineVisible(false);
86 axisX->setGridLineVisible(false);
88 axisY->setGridLineVisible(false);
87 axisY->setGridLineVisible(false);
89 axisY->setShadesPen(Qt::NoPen);
88 axisY->setShadesPen(Qt::NoPen);
90 axisY->setShadesBrush(QBrush(QRgb(0xa5a2a3)));
89 axisY->setShadesBrush(QBrush(QRgb(0xa5a2a3)));
91 axisY->setShadesVisible(true);
90 axisY->setShadesVisible(true);
92 //![3]
91 //![3]
93
92
94 //![4]
93 //![4]
95 axisX->append("low");
94 axisX->append("low", 10);
96 axisX->append("optimal");
95 axisX->append("optimal", 20);
97 axisX->append("high");
96 axisX->append("high", 30);
98 axisX->setRange("low","high");
97 axisX->setRange(0, 30);
99
98 // axisX->setRange("low","high");
100 axisY->append("slow");
99
101 axisY->append("med");
100 axisY->append("slow", 10);
102 axisY->append("fast");
101 axisY->append("med", 20);
103 axisY->setRange("slow","fast");
102 axisY->append("fast", 30);
103 axisY->setRange(0, 30);
104 // axisY->setRange("slow","fast");
104
105
105 chart->setAxisX(axisX, series);
106 chart->setAxisX(axisX, series);
106 chart->setAxisY(axisY, series);
107 chart->setAxisY(axisY, series);
108 axisX->setRange(0, 30);
109 axisY->setRange(0, 30);
107 //![4]
110 //![4]
108
111
109 //![5]
112 //![5]
110 QChartView* chartView = new QChartView(chart);
113 QChartView* chartView = new QChartView(chart);
111 chartView->setRenderHint(QPainter::Antialiasing);
114 chartView->setRenderHint(QPainter::Antialiasing);
112 //![5]
115 //![5]
113
116
114 //![6]
117 //![6]
115 QMainWindow window;
118 QMainWindow window;
116 window.setCentralWidget(chartView);
119 window.setCentralWidget(chartView);
117 window.resize(400, 300);
120 window.resize(400, 300);
118 window.show();
121 window.show();
119 //![6]
122 //![6]
120
123
121 return a.exec();
124 return a.exec();
122 }
125 }
@@ -1,131 +1,128
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartintervalsaxisy_p.h"
21 #include "chartintervalsaxisy_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include "chartanimator_p.h"
24 #include "chartanimator_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QDebug>
26 #include <QDebug>
27 #include <QFontMetrics>
27 #include <QFontMetrics>
28 #include <cmath>
28 #include <cmath>
29 #include <QIntervalsAxis>
29 #include <QIntervalsAxis>
30
30
31 static int label_padding = 5;
31 static int label_padding = 5;
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 ChartIntervalAxisY::ChartIntervalAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
35 ChartIntervalAxisY::ChartIntervalAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
36 {
36 {
37 }
37 }
38
38
39 ChartIntervalAxisY::~ChartIntervalAxisY()
39 ChartIntervalAxisY::~ChartIntervalAxisY()
40 {
40 {
41 }
41 }
42
42
43 QVector<qreal> ChartIntervalAxisY::calculateLayout() const
43 QVector<qreal> ChartIntervalAxisY::calculateLayout() const
44 {
44 {
45 QIntervalsAxis *axis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
45 QIntervalsAxis *axis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
46 int tickCount = axis->intervalsLabels().count() + 1;
46 int tickCount = axis->intervalsLabels().count() + 1;
47 QVector<qreal> points;
47 QVector<qreal> points;
48 points.resize(tickCount);
48 points.resize(tickCount);
49
49
50 qreal scale = m_rect.height() / axis->max();
50 qreal scale = m_rect.height() / axis->max();
51 for (int i = 0; i < tickCount; ++i)
51 for (int i = 0; i < tickCount; ++i)
52 if (i < tickCount - 1) {
52 if (i < tickCount - 1) {
53 int y = -axis->intervalMin(axis->intervalsLabels().at(i)) * scale + m_rect.bottom();
53 int y = -axis->intervalMin(axis->intervalsLabels().at(i)) * scale + m_rect.bottom();
54 points[i] = y;
54 points[i] = y;
55 } else {
55 } else {
56 int y = -axis->intervalMax(axis->intervalsLabels().at(i - 1)) * scale + m_rect.bottom();
56 int y = -axis->intervalMax(axis->intervalsLabels().at(i - 1)) * scale + m_rect.bottom();
57 points[i] = y;
57 points[i] = y;
58 }
58 }
59
59
60 return points;
60 return points;
61 }
61 }
62
62
63 void ChartIntervalAxisY::updateGeometry()
63 void ChartIntervalAxisY::updateGeometry()
64 {
64 {
65 const QVector<qreal> &layout = ChartAxis::layout();
65 const QVector<qreal> &layout = ChartAxis::layout();
66 m_minWidth = 0;
66 m_minWidth = 0;
67 m_minHeight = 0;
67 m_minHeight = 0;
68
68
69 if(layout.isEmpty()) return;
69 if(layout.isEmpty()) return;
70
70
71 QIntervalsAxis *intervalAxis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
71 QIntervalsAxis *intervalAxis = qobject_cast<QIntervalsAxis *>(m_chartAxis);
72
72
73 QStringList ticksList = intervalAxis->intervalsLabels();
73 QStringList ticksList = intervalAxis->intervalsLabels();
74
74
75 QList<QGraphicsItem *> lines = m_grid->childItems();
75 QList<QGraphicsItem *> lines = m_grid->childItems();
76 QList<QGraphicsItem *> labels = m_labels->childItems();
76 QList<QGraphicsItem *> labels = m_labels->childItems();
77 QList<QGraphicsItem *> shades = m_shades->childItems();
77 QList<QGraphicsItem *> shades = m_shades->childItems();
78 QList<QGraphicsItem *> axis = m_axis->childItems();
78 QList<QGraphicsItem *> axis = m_axis->childItems();
79
79
80 // Q_ASSERT(labels.size() == ticksList.size());
80 // Q_ASSERT(labels.size() == ticksList.size());
81 // Q_ASSERT(layout.size() == ticksList.size());
81 // Q_ASSERT(layout.size() == ticksList.size());
82
82
83 qreal height = 2*m_rect.bottom();
83 qreal height = 2*m_rect.bottom();
84
84
85 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
85 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
86 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
86 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
87
87
88 for (int i = 0; i < layout.size(); ++i) {
88 for (int i = 0; i < layout.size(); ++i) {
89 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
89 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
90 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
90 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
91 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
91 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
92
92
93 if (i < ticksList.count()) {
93 if (i < ticksList.count()) {
94 labelItem->setText(ticksList.at(i));
94 labelItem->setText(ticksList.at(i));
95 }
95 }
96 const QRectF& rect = labelItem->boundingRect();
96 const QRectF& rect = labelItem->boundingRect();
97
97
98 QPointF center = rect.center();
98 QPointF center = rect.center();
99 labelItem->setTransformOriginPoint(center.x(), center.y());
99 labelItem->setTransformOriginPoint(center.x(), center.y());
100
100
101 if (i < ticksList.count())
101 if (i < ticksList.count())
102 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y());
102 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y());
103 // labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), m_rect.bottom() + label_padding);
104 else
103 else
105 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
104 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
106
105
107 // labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
108
109 if(labelItem->pos().y()+rect.height()>height) {
106 if(labelItem->pos().y()+rect.height()>height) {
110 labelItem->setVisible(false);
107 labelItem->setVisible(false);
111 lineItem->setVisible(false);
108 lineItem->setVisible(false);
112 }
109 }
113 else {
110 else {
114 labelItem->setVisible(true);
111 labelItem->setVisible(true);
115 lineItem->setVisible(true);
112 lineItem->setVisible(true);
116 height=labelItem->pos().y();
113 height=labelItem->pos().y();
117 }
114 }
118
115
119 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
116 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
120 m_minHeight+=rect.height();
117 m_minHeight+=rect.height();
121
118
122 if ((i+1)%2 && i>1) {
119 if ((i+1)%2 && i>1) {
123 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
120 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
124 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
121 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
125 }
122 }
126 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
123 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
127 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
124 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
128 }
125 }
129 }
126 }
130
127
131 QTCOMMERCIALCHART_END_NAMESPACE
128 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now