##// END OF EJS Templates
Adds custom categories + example
Michal Klocek -
r706:989e7d47a51a
parent child
Show More
@@ -5,12 +5,9
5 #include <qchart.h>
5 #include <qchart.h>
6 #include <qchartaxis.h>
6 #include <qchartaxis.h>
7 #include <qchartaxiscategories.h>
7 #include <qchartaxiscategories.h>
8 #include <cmath>
9
8
10 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
11
10
12 #define PI 3.14159265358979
13
14 int main(int argc, char *argv[])
11 int main(int argc, char *argv[])
15 {
12 {
16 QApplication a(argc, argv);
13 QApplication a(argc, argv);
@@ -21,24 +18,14 int main(int argc, char *argv[])
21 QPen blue(Qt::blue);
18 QPen blue(Qt::blue);
22 blue.setWidth(3);
19 blue.setWidth(3);
23 series0->setPen(blue);
20 series0->setPen(blue);
24 QLineSeries* series1 = new QLineSeries();
25 QPen red(Qt::red);
26 red.setWidth(3);
27 series1->setPen(red);
28
29 int numPoints = 100;
30
21
31 for (int x = 0; x <= numPoints; ++x) {
22 *series0 << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10,5);
32 series0->add(x, fabs(sin(PI/50*x)*100));
33 series1->add(x, fabs(cos(PI/50*x)*100));
34 }
35
23
36 QChartView* chartView = new QChartView(&window);
24 QChartView* chartView = new QChartView(&window);
37
25
38 chartView->setRenderHint(QPainter::Antialiasing);
26 chartView->setRenderHint(QPainter::Antialiasing);
39 chartView->setChartTitle("This is custom axis chart example");
27 chartView->setChartTitle("Simple axis example");
40 chartView->addSeries(series0);
28 chartView->addSeries(series0);
41 chartView->addSeries(series1);
42
29
43 QLinearGradient backgroundGradient;
30 QLinearGradient backgroundGradient;
44 backgroundGradient.setColorAt(0.0, Qt::white);
31 backgroundGradient.setColorAt(0.0, Qt::white);
@@ -47,32 +34,30 int main(int argc, char *argv[])
47 chartView->setChartBackgroundBrush(backgroundGradient);
34 chartView->setChartBackgroundBrush(backgroundGradient);
48
35
49 QChartAxis* axisX = chartView->axisX();
36 QChartAxis* axisX = chartView->axisX();
50 axisX->setLabelsAngle(45);
37 axisX->setRange(0,10);
51 axisX->setGridLinePen(red);
38 axisX->setTicksCount(4);
52 axisX->setGridLineVisible(false);
39 axisX->setGridLineVisible(true);
53 axisX->setShadesVisible(true);
40 axisX->setShadesVisible(false);
54
55 QChartAxisCategories* categoriesX = axisX->categories();
56 categoriesX->insert(0,"low");
57 categoriesX->insert(50,"medium");
58 categoriesX->insert(100,"High");
59
60 // axisX->setMin(-10);
61 // axisX->setMax(2200);
62
41
63 QChartAxis* axisY = chartView->axisY();
42 QChartAxis* axisY = chartView->axisY();
43 axisY->setRange(0,10);
64 axisY->setLabelsAngle(45);
44 axisY->setLabelsAngle(45);
65 axisY->setShadesBrush(Qt::yellow);
45 axisY->setTicksCount(4);
66 axisY->setShadesVisible(true);
46 axisY->setShadesPen(Qt::NoPen);
47 axisY->setShadesOpacity(0.5);
48 axisY->setShadesBrush(Qt::blue);
67 axisY->setGridLineVisible(false);
49 axisY->setGridLineVisible(false);
50 axisY->setShadesVisible(true);
68
51
69 QChartAxisCategories* categoriesY = axisY->categories();
52 QChartAxisCategories* categoriesX = axisX->categories();
70 categoriesY->insert(0,"low");
53 categoriesX->insert(1,"low");
71 categoriesY->insert(50,"medium");
54 categoriesX->insert(5,"optimal");
72 categoriesY->insert(100,"High");
55 categoriesX->insert(10,"high");
73
56
74 // axisY->setMin(-10);
57 QChartAxisCategories* categoriesY = axisY->categories();
75 // axisY->setMax(200);
58 categoriesY->insert(1,"slow");
59 categoriesY->insert(5,"med");
60 categoriesY->insert(10,"fast");
76
61
77 window.setCentralWidget(chartView);
62 window.setCentralWidget(chartView);
78 window.resize(400, 300);
63 window.resize(400, 300);
@@ -93,8 +93,15 bool Axis::createLabels(QStringList& labels,qreal min, qreal max,int ticks) cons
93 }
93 }
94 }
94 }
95 else {
95 else {
96 QList<qreal> values = categories->values();
96 for(int i=0; i< ticks; i++) {
97 for(int i=0; i< ticks; i++) {
97 int value = ceil(min + (i * (max - min)/ (ticks-1)));
98 qreal value = (min + (i * (max - min)/ (ticks-1)));
99 int j=0;
100 for(; j<values.count(); j++){
101 if (values.at(j) > value) break;
102 }
103 if(j!=0) value=values.at(j-1);
104
98 QString label = categories->label(value);
105 QString label = categories->label(value);
99 labels << label;
106 labels << label;
100 }
107 }
@@ -269,7 +276,6 void Axis::setLayout(QVector<qreal>& layout)
269 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
276 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
270 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
277 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
271 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
278 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
272
273 if(!categories){
279 if(!categories){
274 labelItem->setText(ticksList.at(i));
280 labelItem->setText(ticksList.at(i));
275 QPointF center = labelItem->boundingRect().center();
281 QPointF center = labelItem->boundingRect().center();
@@ -301,10 +307,19 void Axis::setLayout(QVector<qreal>& layout)
301 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
307 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
302 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
308 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
303 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
309 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
304 labelItem->setText(ticksList.at(i));
310
305 QPointF center = labelItem->boundingRect().center();
311 if(!categories){
306 labelItem->setTransformOriginPoint(center.x(), center.y());
312 labelItem->setText(ticksList.at(i));
307 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
313 QPointF center = labelItem->boundingRect().center();
314 labelItem->setTransformOriginPoint(center.x(), center.y());
315 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
316 } else if(i>0){
317 labelItem->setText(ticksList.at(i));
318 QPointF center = labelItem->boundingRect().center();
319 labelItem->setTransformOriginPoint(center.x(), center.y());
320 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
321 }
322
308 if((i+1)%2 && i>1) {
323 if((i+1)%2 && i>1) {
309 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
324 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
310 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
325 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
@@ -1,7 +1,6
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "domain_p.h"
5 #include "chart_p.h"
4 #include "chart_p.h"
6 #include <QGraphicsItem>
5 #include <QGraphicsItem>
7
6
@@ -28,22 +28,30 void QChartAxisCategories::insert(qreal value,QString label)
28 m_map.insert(value,label);
28 m_map.insert(value,label);
29 emit updated();
29 emit updated();
30 }
30 }
31
31 void QChartAxisCategories::remove(qreal value)
32 void QChartAxisCategories::remove(qreal value)
32 {
33 {
33 m_map.remove(value);
34 m_map.remove(value);
34 emit updated();
35 emit updated();
35 }
36 }
37
36 void QChartAxisCategories::clear()
38 void QChartAxisCategories::clear()
37 {
39 {
38 m_map.clear();
40 m_map.clear();
39 emit updated();
41 emit updated();
40 }
42 }
43
41 int QChartAxisCategories::count()
44 int QChartAxisCategories::count()
42 {
45 {
43 return m_map.count();
46 return m_map.count();
44 emit updated();
47 emit updated();
45 }
48 }
46
49
50 QList<qreal> QChartAxisCategories::values() const
51 {
52 return m_map.keys();
53 }
54
47 QString QChartAxisCategories::label(qreal value) const
55 QString QChartAxisCategories::label(qreal value) const
48 {
56 {
49 return m_map.value(value);
57 return m_map.value(value);
@@ -17,6 +17,7 public:
17 void insert(const QBarCategories& category);
17 void insert(const QBarCategories& category);
18 void insert(qreal value,QString label);
18 void insert(qreal value,QString label);
19 void remove(qreal value);
19 void remove(qreal value);
20 QList<qreal> values() const;
20 QString label(qreal value) const;
21 QString label(qreal value) const;
21 void clear();
22 void clear();
22 int count();
23 int count();
@@ -2,7 +2,6
2 #define CHART_H_
2 #define CHART_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QObject>
6 #include <QRect>
5 #include <QRect>
7
6
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now