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