##// END OF EJS Templates
Added bar chart example
sauimone -
r78:681c12da395a
parent child
Show More
@@ -0,0 +1,17
1 !include( ../../common.pri ) {
2 error( "Couldn't find the common.pri file!" )
3 }
4
5 !include( ../../integrated.pri ) {
6 error( "Couldn't find the integrated.pri file !")
7 }
8
9 TARGET = barchart
10 TEMPLATE = app
11 QT += core gui
12 SOURCES += main.cpp \
13 chartwidget.cpp
14
15 HEADERS += \
16 chartwidget.h
17
@@ -0,0 +1,6
1 #include "chartwidget.h"
2
3 ChartWidget::ChartWidget(QWidget *parent) :
4 QChartView(parent)
5 {
6 }
@@ -0,0 +1,21
1 #ifndef CHARTWIDGET_H
2 #define CHARTWIDGET_H
3
4 #include <qchartview.h>
5
6 QTCOMMERCIALCHART_USE_NAMESPACE
7
8
9 class ChartWidget : public QChartView
10 {
11 Q_OBJECT
12 public:
13 explicit ChartWidget(QWidget *parent = 0);
14
15 signals:
16
17 public slots:
18
19 };
20
21 #endif // CHARTWIDGET_H
@@ -0,0 +1,47
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
4 #include <barchartseries.h>
5 #include "chartwidget.h"
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
9 int main(int argc, char *argv[])
10 {
11 QApplication a(argc, argv);
12 QMainWindow window;
13
14 BarChartSeries* series0 = new BarChartSeries();
15
16 // Create some test data to chart
17 QStandardItemModel dataModel(2,10);
18 QModelIndex index;
19 index = dataModel.index(0,0);
20 // Series 1, items 6 to 9 missing.
21 dataModel.setData(dataModel.index(0,0),1);
22 dataModel.setData(dataModel.index(0,1),12);
23 dataModel.setData(dataModel.index(0,2),5);
24 dataModel.setData(dataModel.index(0,3),8);
25 dataModel.setData(dataModel.index(0,4),17);
26 dataModel.setData(dataModel.index(0,5),9);
27
28 // Series 2, some other items missing
29 dataModel.setData(dataModel.index(1,0),5);
30 dataModel.setData(dataModel.index(1,3),4);
31 dataModel.setData(dataModel.index(1,5),7);
32 dataModel.setData(dataModel.index(1,6),8);
33 dataModel.setData(dataModel.index(1,8),9);
34 dataModel.setData(dataModel.index(1,9),9);
35
36 series0->setData(&dataModel);
37
38 ChartWidget* chartWidget = new ChartWidget(&window);
39 chartWidget->addSeries(series0);
40
41 window.setCentralWidget(chartWidget);
42 window.resize(400, 300);
43 window.show();
44
45 return a.exec();
46 }
47
@@ -1,4 +1,5
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += linechart \
2 SUBDIRS += linechart \
3 zoomlinechart \
3 zoomlinechart \
4 colorlinechart
4 colorlinechart \
5 barchart
@@ -1,62 +1,64
1 #include "bar.h"
1 #include "bar.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QPainter>
3 #include <QPainter>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 Bar::Bar(ChartItem *parent)
7 Bar::Bar(ChartItem *parent)
8 : ChartItem(parent)
8 : ChartItem(parent)
9 {
9 {
10 }
10 }
11
11
12 void Bar::setSize(const QSize& size)
12 void Bar::setSize(const QSize& size)
13 {
13 {
14 //mSize = size;
14 //mSize = size;
15 mWidth = size.width();
15 mWidth = size.width();
16 mHeight = size.height();
16 mHeight = size.height();
17 }
17 }
18
18
19 void Bar::setPlotDomain(const PlotDomain& data)
19 void Bar::setPlotDomain(const PlotDomain& data)
20 {
20 {
21 mPlotDomain = data;
21 mPlotDomain = data;
22 }
22 }
23
23
24 void Bar::resize( int w, int h )
24 void Bar::resize( int w, int h )
25 {
25 {
26 qDebug() << "bar::resize" << w << h;
26 mWidth = w;
27 mWidth = w;
27 mHeight = h;
28 mHeight = h;
28 }
29 }
29
30
30 void Bar::setColor( QColor col )
31 void Bar::setColor( QColor col )
31 {
32 {
32 mColor = col;
33 mColor = col;
33 }
34 }
34 void Bar::setPos(qreal x, qreal y)
35 void Bar::setPos(qreal x, qreal y)
35 {
36 {
37 qDebug() << "Bar::setpos" << x << y;
36 mXpos = x;
38 mXpos = x;
37 mYpos = y;
39 mYpos = y;
38 }
40 }
39
41
40 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
41 {
43 {
42 // Set color for bar. TODO: gradients, textures etc
44 // Set color for bar. TODO: gradients, textures etc
43 QPen pen = painter->pen();
45 QPen pen = painter->pen();
44 pen.setColor( mColor );
46 pen.setColor( mColor );
45 pen.setWidth( mWidth );
47 pen.setWidth( mWidth );
46 painter->setPen(pen);
48 painter->setPen(pen);
47
49
48 // Draw bar
50 // Draw bar
49 // TODO: Pen width affects bar height for now. This should be rect
51 // TODO: Pen width affects bar height for now. This should be rect
50 painter->drawLine(scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mHeight - mWidth,
52 painter->drawLine(scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mHeight - mWidth,
51 scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mWidth);
53 scenePos().x() + mXpos, scenePos().y() + mYpos + parentItem()->boundingRect().height() - mWidth);
52 }
54 }
53
55
54 QRectF Bar::boundingRect() const
56 QRectF Bar::boundingRect() const
55 {
57 {
56 // TODO: check validity of this (I suppose there is easier way, and currently this bit incorrect :)
58 // TODO: check validity of this (I suppose there is easier way, and currently this bit incorrect :)
57 // QRectF r(scenePos().x()+mXpos, scenePos().y()+mYpos, scenePos().x() + mWidth, scenePos().y() + mHeight );
59 // QRectF r(scenePos().x()+mXpos, scenePos().y()+mYpos, scenePos().x() + mWidth, scenePos().y() + mHeight );
58 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
60 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
59 return r;
61 return r;
60 }
62 }
61
63
62 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,117 +1,138
1 #include "bargroup.h"
1 #include "bargroup.h"
2 #include "bar.h"
2 #include "bar.h"
3 #include <QDebug>
3 #include <QDebug>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 // TODO: singleton?
7 // TODO: singleton?
8 //BarGroup* BarGroup::mBarGroupInstance = NULL;
8 //BarGroup* BarGroup::mBarGroupInstance = NULL;
9
9
10 //BarGroup::BarGroup(QGraphicsItem *parent) :
10 //BarGroup::BarGroup(QGraphicsItem *parent) :
11 // QGraphicsItem(parent)
11 // QGraphicsItem(parent)
12 // ,mSeries(series)
12 // ,mSeries(series)
13 BarGroup::BarGroup(BarChartSeries& series, ChartItem *parent) :
13 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
14 ChartItem(parent)
14 ChartItem(parent)
15 ,mSeries(series)
15 ,mSeries(series)
16 ,mLayoutSet(false)
16 ,mLayoutSet(false)
17 ,mLayoutDirty(true)
17 ,mLayoutDirty(true)
18 ,mBarDefaultWidth(10)
18 {
19 {
19 dataChanged();
20 dataChanged();
20 }
21 }
21
22
23
24 void BarGroup::setSize(const QSize& size)
25 {
26 // TODO: refactor this
27 qDebug() << "BarGroup::setSize";
28 resize(size.width(),size.height());
29 }
30
31 void BarGroup::setPlotDomain(const PlotDomain& data)
32 {
33 qDebug() << "BarGroup::setPlotDomain";
34 }
35
36
22 void BarGroup::resize( int w, int h )
37 void BarGroup::resize( int w, int h )
23 {
38 {
24 qDebug() << "QBarChart::resize";
39 qDebug() << "QBarChart::resize";
25 mWidth = w;
40 mWidth = w;
26 mHeight = h;
41 mHeight = h;
27 layoutChanged();
42 layoutChanged();
28 mLayoutSet = true;
43 mLayoutSet = true;
29 }
44 }
30
45
31 void BarGroup::setBarWidth( int w )
46 void BarGroup::setBarWidth( int w )
32 {
47 {
33 mBarDefaultWidth = w;
48 mBarDefaultWidth = w;
34 }
49 }
35
50
36 void BarGroup::setColor( QColor color )
51 void BarGroup::setColor( QColor color )
37 {
52 {
38 mColor = color;
53 mColor = color;
39 }
54 }
40
55
41 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
56 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42 {
57 {
43 if (!mLayoutSet) {
58 if (!mLayoutSet) {
44 qDebug() << "QBarChart::paint called without layout set. Aborting.";
59 qDebug() << "QBarChart::paint called without layout set. Aborting.";
45 return;
60 return;
46 }
61 }
47 if (mLayoutDirty) {
62 if (mLayoutDirty) {
48 // Layout or data has changed. Need to redraw.
63 // Layout or data has changed. Need to redraw.
49 foreach(QGraphicsItem* i, childItems()) {
64 foreach(QGraphicsItem* i, childItems()) {
50 i->paint(painter,option,widget);
65 i->paint(painter,option,widget);
51 }
66 }
52 }
67 }
53 }
68 }
54
69
55 QRectF BarGroup::boundingRect() const
70 QRectF BarGroup::boundingRect() const
56 {
71 {
57 // TODO: correct this (currently ignores position)
72 // TODO: correct this (currently ignores position)
58 return QRectF(0,0,mWidth,mHeight);
73 return QRectF(0,0,mWidth,mHeight);
59 }
74 }
60
75
61
76
62 void BarGroup::dataChanged()
77 void BarGroup::dataChanged()
63 {
78 {
64 qDebug() << "QBarChart::dataChanged mSeries";
79 qDebug() << "QBarChart::dataChanged mSeries";
65
80
66 // Find out maximum and minimum of all series
81 // Find out maximum and minimum of all series
67 mMax = mSeries.max();
82 mMax = mSeries.max();
68 mMin = mSeries.min();
83 mMin = mSeries.min();
69
84
70 // Delete old bars
85 // Delete old bars
71 // Is this correct way to delete childItems?
86 // Is this correct way to delete childItems?
72 foreach (QGraphicsItem* item, childItems()) {
87 foreach (QGraphicsItem* item, childItems()) {
73 delete item;
88 delete item;
74 }
89 }
75
90
76 // Create new graphic items for bars
91 // Create new graphic items for bars
77 int totalItems = mSeries.countTotalItems();
92 int totalItems = mSeries.countTotalItems();
78 for (int i=0; i<totalItems; i++) {
93 for (int i=0; i<totalItems; i++) {
79 Bar *bar = new Bar(this);
94 Bar *bar = new Bar(this);
80 childItems().append(bar);
95 childItems().append(bar);
81 }
96 }
82
97
83 mLayoutDirty = true;
98 mLayoutDirty = true;
84 }
99 }
85
100
86 void BarGroup::layoutChanged()
101 void BarGroup::layoutChanged()
87 {
102 {
88 // Scale bars to new layout
103 // Scale bars to new layout
89 // Layout for bars:
104 // Layout for bars:
90 int count = mSeries.countSeries();
105 if (mSeries.countSeries() <= 0) {
91 if (count <= 0) {
92 // Nothing to do.
106 // Nothing to do.
93 return;
107 return;
94 }
108 }
95
109
96 // Align center
110 // Align center
111 int count = mSeries.countItemsInSeries();
97 int posStep = (mWidth / (count));
112 int posStep = (mWidth / (count));
98 int startPos = (mWidth / count+1);
113 int startPos = (mWidth / count+1);
99 qDebug() << "startpos" << startPos;
114 qDebug() << "startpos" << startPos;
100
115
101 // Scaling. TODO: better one.
116 // Scaling. TODO: better one.
102 int itemIndex(0);
117 int itemIndex(0);
103 for (int series = 0; series < count; series++) {
118 for (int series = 0; series < mSeries.countSeries(); series++) {
104 for (int item=0; item < count; item++) {
119 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
120 qDebug() << itemIndex;
105 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
121 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
106 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
122 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
107
123
108 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
124 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
109 bar->setColor(mColor);
125 //TODO: test hack
110 bar->setPos(itemIndex*posStep+startPos, 0);
126 if (0 == series) {
127 bar->setColor(QColor(255,0,0,128));
128 } else {
129 bar->setColor(QColor(255,255,0,128));
130 }
131 bar->setPos(itemIndex*posStep+startPos + series * mBarDefaultWidth, 0);
111 itemIndex++;
132 itemIndex++;
112 }
133 }
113 }
134 }
114 mLayoutDirty = true;
135 mLayoutDirty = true;
115 }
136 }
116
137
117 QTCOMMERCIALCHART_END_NAMESPACE
138 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,67 +1,71
1 #ifndef QBARCHART_H
1 #ifndef QBARCHART_H
2 #define QBARCHART_H
2 #define QBARCHART_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "bar.h"
5 #include "bar.h"
6 #include "barchartseries.h"
6 #include "barchartseries.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 // TODO: Better name for this? The function of this class is to know where each bar in series is laid out.
10 // TODO: Better name for this? The function of this class is to know where each bar in series is laid out.
11 //class BarGroup : public QGraphicsItemGroup
11 //class BarGroup : public QGraphicsItemGroup
12
12
13 class BarGroup : public ChartItem
13 class BarGroup : public ChartItem
14 {
14 {
15 /* // TODO: implement as singleton?
15 /* // TODO: implement as singleton?
16 private:
16 private:
17 static BarGroup* mBarGroupInstance;
17 static BarGroup* mBarGroupInstance;
18
18
19 public:
19 public:
20 static BarGroup* instance()
20 static BarGroup* instance()
21 {
21 {
22 if (mBarGroupInstance == NULL) {
22 if (mBarGroupInstance == NULL) {
23 mBarGroupInstance = new BarGroup();
23 mBarGroupInstance = new BarGroup();
24 }
24 }
25 return mBarGroupInstance;
25 return mBarGroupInstance;
26 }
26 }
27 private:
27 private:
28 */
28 */
29 public:
29 public:
30 explicit BarGroup(BarChartSeries& series, ChartItem *parent = 0);
30 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
31
32 // From ChartItem
33 virtual void setSize(const QSize& size);
34 virtual void setPlotDomain(const PlotDomain& data);
31
35
32 // Layout "api"
36 // Layout "api"
33 void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area
37 void resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area
34 void setPos(qreal x, qreal y);
38 void setPos(qreal x, qreal y);
35 void setBarWidth( int w ); // Default width for each bar
39 void setBarWidth( int w ); // Default width for each bar
36 void setColor( QColor color ); // Default color for each bar
40 void setColor( QColor color ); // Default color for each bar
37
41
38 // From QGraphicsItem
42 // From QGraphicsItem
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
40 QRectF boundingRect() const;
44 QRectF boundingRect() const;
41
45
42 private:
46 private:
43
47
44 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
48 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
45 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
49 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
46
50
47 private:
51 private:
48
52
49 // Data
53 // Data
50 BarChartSeries& mSeries;
54 BarChartSeries& mSeries;
51 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
55 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
52 int mMax;
56 int mMax;
53
57
54 int mHeight; // Layout spesific
58 int mHeight; // Layout spesific
55 int mWidth;
59 int mWidth;
56 int mBarDefaultWidth;
60 int mBarDefaultWidth;
57
61
58 QColor mColor;
62 QColor mColor;
59
63
60 bool mLayoutSet; // True, if component has been laid out.
64 bool mLayoutSet; // True, if component has been laid out.
61 bool mLayoutDirty;
65 bool mLayoutDirty;
62
66
63 };
67 };
64
68
65 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
66
70
67 #endif // QBARCHART_H
71 #endif // QBARCHART_H
@@ -1,285 +1,285
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include "qscatterseries.h"
3 #include "qscatterseries.h"
4 #include "qscatterseries_p.h"
4 #include "qscatterseries_p.h"
5 #include "qpieseries.h"
5 #include "qpieseries.h"
6 #include "qxychartseries.h"
6 #include "qxychartseries.h"
7
7
8 #include "barchartseries.h"
8 #include "barchartseries.h"
9 #include "bargroup.h"
9 #include "bargroup.h"
10
10
11 #include "xylinechartitem_p.h"
11 #include "xylinechartitem_p.h"
12 #include "plotdomain_p.h"
12 #include "plotdomain_p.h"
13 #include "axisitem_p.h"
13 #include "axisitem_p.h"
14 #include <QGraphicsScene>
14 #include <QGraphicsScene>
15 #include <QDebug>
15 #include <QDebug>
16
16
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18
18
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
19 QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent),
20 m_background(new QGraphicsRectItem(this)),
20 m_background(new QGraphicsRectItem(this)),
21 m_title(new QGraphicsTextItem(this)),
21 m_title(new QGraphicsTextItem(this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
24 m_plotDataIndex(0),
24 m_plotDataIndex(0),
25 m_marginSize(0)
25 m_marginSize(0)
26 {
26 {
27 // TODO: the default theme?
27 // TODO: the default theme?
28 setTheme(QChart::ChartThemeVanilla);
28 setTheme(QChart::ChartThemeVanilla);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
29 // setFlags(QGraphicsItem::ItemClipsChildrenToShape);
30 PlotDomain domain;
30 PlotDomain domain;
31 m_plotDomainList<<domain;
31 m_plotDomainList<<domain;
32
32
33 m_chartItems<<m_axisX;
33 m_chartItems<<m_axisX;
34 m_chartItems<<m_axisY;
34 m_chartItems<<m_axisY;
35 }
35 }
36
36
37 QChart::~QChart(){}
37 QChart::~QChart(){}
38
38
39 QRectF QChart::boundingRect() const
39 QRectF QChart::boundingRect() const
40 {
40 {
41 return m_rect;
41 return m_rect;
42 }
42 }
43
43
44 void QChart::addSeries(QChartSeries* series)
44 void QChart::addSeries(QChartSeries* series)
45 {
45 {
46 // TODO: we should check the series not already added
46 // TODO: we should check the series not already added
47
47
48 m_series<<series;
48 m_series<<series;
49
49
50 switch(series->type())
50 switch(series->type())
51 {
51 {
52 case QChartSeries::SeriesTypeLine: {
52 case QChartSeries::SeriesTypeLine: {
53
53
54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
54 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
55 // Use color defined by theme in case the series does not define a custom color
55 // Use color defined by theme in case the series does not define a custom color
56 if (!xyseries->color().isValid() && m_themeColors.count())
56 if (!xyseries->color().isValid() && m_themeColors.count())
57 xyseries->setColor(m_themeColors.takeFirst());
57 xyseries->setColor(m_themeColors.takeFirst());
58
58
59 m_plotDataIndex = 0 ;
59 m_plotDataIndex = 0 ;
60 m_plotDomainList.resize(1);
60 m_plotDomainList.resize(1);
61
61
62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
62 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
63
63
64 for (int i = 0 ; i < xyseries->count() ; i++)
64 for (int i = 0 ; i < xyseries->count() ; i++)
65 {
65 {
66 qreal x = xyseries->x(i);
66 qreal x = xyseries->x(i);
67 qreal y = xyseries->y(i);
67 qreal y = xyseries->y(i);
68 domain.m_minX = qMin(domain.m_minX,x);
68 domain.m_minX = qMin(domain.m_minX,x);
69 domain.m_minY = qMin(domain.m_minY,y);
69 domain.m_minY = qMin(domain.m_minY,y);
70 domain.m_maxX = qMax(domain.m_maxX,x);
70 domain.m_maxX = qMax(domain.m_maxX,x);
71 domain.m_maxY = qMax(domain.m_maxY,y);
71 domain.m_maxY = qMax(domain.m_maxY,y);
72 }
72 }
73
73
74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
74 XYLineChartItem* item = new XYLineChartItem(xyseries,this);
75
75
76 m_chartItems<<item;
76 m_chartItems<<item;
77
77
78 foreach(ChartItem* i ,m_chartItems)
78 foreach(ChartItem* i ,m_chartItems)
79 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
79 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
80
80
81 break;
81 break;
82 }
82 }
83 case QChartSeries::SeriesTypeBar: {
83 case QChartSeries::SeriesTypeBar: {
84
84
85 qDebug() << "barSeries added";
85 qDebug() << "barSeries added";
86 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
86 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
87 connect(this, SIGNAL(sizeChanged(QRectF)),
87 BarGroup* barGroup = new BarGroup(*barSeries,this);
88 barSeries, SLOT(chartSizeChanged(QRectF)));
88 m_chartItems<<barGroup;
89
89 childItems().append(barGroup);
90 break;
90 break;
91 }
91 }
92 case QChartSeries::SeriesTypeScatter: {
92 case QChartSeries::SeriesTypeScatter: {
93 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
93 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
94 connect(this, SIGNAL(sizeChanged(QRectF)),
94 connect(this, SIGNAL(sizeChanged(QRectF)),
95 scatterSeries, SLOT(chartSizeChanged(QRectF)));
95 scatterSeries, SLOT(chartSizeChanged(QRectF)));
96 scatterSeries->d->setParentItem(this);
96 scatterSeries->d->setParentItem(this);
97 QColor nextColor = m_themeColors.takeFirst();
97 QColor nextColor = m_themeColors.takeFirst();
98 nextColor.setAlpha(150); // TODO: default opacity?
98 nextColor.setAlpha(150); // TODO: default opacity?
99 scatterSeries->setMarkerColor(nextColor);
99 scatterSeries->setMarkerColor(nextColor);
100 }
100 }
101 case QChartSeries::SeriesTypePie: {
101 case QChartSeries::SeriesTypePie: {
102 // TODO: we now have also a list of y values as a parameter, it is ignored
102 // TODO: we now have also a list of y values as a parameter, it is ignored
103 // we should use a generic data class instead of list of x and y values
103 // we should use a generic data class instead of list of x and y values
104 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
104 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
105 connect(this, SIGNAL(sizeChanged(QRectF)),
105 connect(this, SIGNAL(sizeChanged(QRectF)),
106 pieSeries, SLOT(chartSizeChanged(QRectF)));
106 pieSeries, SLOT(chartSizeChanged(QRectF)));
107 // TODO: how to define the color for all the slices of a pie?
107 // TODO: how to define the color for all the slices of a pie?
108 }
108 }
109 }
109 }
110 }
110 }
111
111
112 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
112 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
113 {
113 {
114 // TODO: support also other types; not only scatter and pie
114 // TODO: support also other types; not only scatter and pie
115
115
116 QChartSeries *series(0);
116 QChartSeries *series(0);
117
117
118 switch (type) {
118 switch (type) {
119 case QChartSeries::SeriesTypeLine: {
119 case QChartSeries::SeriesTypeLine: {
120 series = QXYChartSeries::create();
120 series = QXYChartSeries::create();
121 break;
121 break;
122 }
122 }
123 case QChartSeries::SeriesTypeBar: {
123 case QChartSeries::SeriesTypeBar: {
124 series = new BarChartSeries(this);
124 series = new BarChartSeries(this);
125 break;
125 break;
126 }
126 }
127 case QChartSeries::SeriesTypeScatter: {
127 case QChartSeries::SeriesTypeScatter: {
128 series = new QScatterSeries(this);
128 series = new QScatterSeries(this);
129 break;
129 break;
130 }
130 }
131 case QChartSeries::SeriesTypePie: {
131 case QChartSeries::SeriesTypePie: {
132 series = new QPieSeries(this);
132 series = new QPieSeries(this);
133 break;
133 break;
134 }
134 }
135 default:
135 default:
136 Q_ASSERT(false);
136 Q_ASSERT(false);
137 break;
137 break;
138 }
138 }
139
139
140 addSeries(series);
140 addSeries(series);
141 return series;
141 return series;
142 }
142 }
143
143
144 void QChart::setSize(const QSize& size)
144 void QChart::setSize(const QSize& size)
145 {
145 {
146 m_rect = QRect(QPoint(0,0),size);
146 m_rect = QRect(QPoint(0,0),size);
147 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
147 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
148
148
149
149
150 //recalculate background gradient
150 //recalculate background gradient
151 m_background->setRect(rect);
151 m_background->setRect(rect);
152 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
152 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
153 m_background->setBrush(m_backgroundGradient);
153 m_background->setBrush(m_backgroundGradient);
154
154
155 //resize elements
155 //resize elements
156 foreach (ChartItem* item ,m_chartItems) {
156 foreach (ChartItem* item ,m_chartItems) {
157 item->setPos(rect.topLeft());
157 item->setPos(rect.topLeft());
158 item->setSize(rect.size());
158 item->setSize(rect.size());
159
159
160 }
160 }
161 // TODO: TTD for setting scale
161 // TODO: TTD for setting scale
162 //emit scaleChanged(100, 100);
162 //emit scaleChanged(100, 100);
163 // TODO: calculate the origo
163 // TODO: calculate the origo
164 // TODO: not sure if emitting a signal here is the best from performance point of view
164 // TODO: not sure if emitting a signal here is the best from performance point of view
165 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
165 emit sizeChanged(QRectF(0, 0, size.width(), size.height()));
166
166
167 update();
167 update();
168 }
168 }
169
169
170 void QChart::setBackgroundColor(const QColor& color)
170 void QChart::setBackgroundColor(const QColor& color)
171 {
171 {
172 m_backgroundGradient.setColorAt( 0.0, Qt::white);
172 m_backgroundGradient.setColorAt( 0.0, Qt::white);
173 m_backgroundGradient.setColorAt( 1.0, color);
173 m_backgroundGradient.setColorAt( 1.0, color);
174 m_background->setBrush(m_backgroundGradient);
174 m_background->setBrush(m_backgroundGradient);
175 m_background->setPen(Qt::NoPen);
175 m_background->setPen(Qt::NoPen);
176 m_background->update();
176 m_background->update();
177 }
177 }
178
178
179 void QChart::setTitle(const QString& title)
179 void QChart::setTitle(const QString& title)
180 {
180 {
181 m_title->setPlainText(title);
181 m_title->setPlainText(title);
182 }
182 }
183
183
184 int QChart::margin() const
184 int QChart::margin() const
185 {
185 {
186 return m_marginSize;
186 return m_marginSize;
187 }
187 }
188
188
189 void QChart::setMargin(int margin)
189 void QChart::setMargin(int margin)
190 {
190 {
191 m_marginSize = margin;
191 m_marginSize = margin;
192 }
192 }
193
193
194 void QChart::setTheme(QChart::ChartTheme theme)
194 void QChart::setTheme(QChart::ChartTheme theme)
195 {
195 {
196 // TODO: define color themes
196 // TODO: define color themes
197 switch (theme) {
197 switch (theme) {
198 case ChartThemeVanilla:
198 case ChartThemeVanilla:
199 m_themeColors.append(QColor(255, 238, 174));
199 m_themeColors.append(QColor(255, 238, 174));
200 m_themeColors.append(QColor(228, 228, 160));
200 m_themeColors.append(QColor(228, 228, 160));
201 m_themeColors.append(QColor(228, 179, 160));
201 m_themeColors.append(QColor(228, 179, 160));
202 m_themeColors.append(QColor(180, 151, 18));
202 m_themeColors.append(QColor(180, 151, 18));
203 m_themeColors.append(QColor(252, 252, 37));
203 m_themeColors.append(QColor(252, 252, 37));
204 break;
204 break;
205 case ChartThemeIcy:
205 case ChartThemeIcy:
206 m_themeColors.append(QColor(255, 238, 174));
206 m_themeColors.append(QColor(255, 238, 174));
207 m_themeColors.append(QColor(228, 228, 160));
207 m_themeColors.append(QColor(228, 228, 160));
208 m_themeColors.append(QColor(228, 179, 160));
208 m_themeColors.append(QColor(228, 179, 160));
209 m_themeColors.append(QColor(180, 151, 18));
209 m_themeColors.append(QColor(180, 151, 18));
210 m_themeColors.append(QColor(252, 252, 37));
210 m_themeColors.append(QColor(252, 252, 37));
211 break;
211 break;
212 case ChartThemeGrayscale:
212 case ChartThemeGrayscale:
213 m_themeColors.append(QColor(255, 238, 174));
213 m_themeColors.append(QColor(255, 238, 174));
214 m_themeColors.append(QColor(228, 228, 160));
214 m_themeColors.append(QColor(228, 228, 160));
215 m_themeColors.append(QColor(228, 179, 160));
215 m_themeColors.append(QColor(228, 179, 160));
216 m_themeColors.append(QColor(180, 151, 18));
216 m_themeColors.append(QColor(180, 151, 18));
217 m_themeColors.append(QColor(252, 252, 37));
217 m_themeColors.append(QColor(252, 252, 37));
218 break;
218 break;
219 default:
219 default:
220 Q_ASSERT(false);
220 Q_ASSERT(false);
221 break;
221 break;
222 }
222 }
223
223
224 // TODO: update coloring of different elements to match the selected theme
224 // TODO: update coloring of different elements to match the selected theme
225 }
225 }
226
226
227 void QChart::zoomInToRect(const QRect& rectangle)
227 void QChart::zoomInToRect(const QRect& rectangle)
228 {
228 {
229
229
230 if(!rectangle.isValid()) return;
230 if(!rectangle.isValid()) return;
231
231
232 qreal margin = this->margin();
232 qreal margin = this->margin();
233
233
234 QRect rect = rectangle.normalized();
234 QRect rect = rectangle.normalized();
235 rect.translate(-margin, -margin);
235 rect.translate(-margin, -margin);
236
236
237 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
237 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
238 PlotDomain newDomain;
238 PlotDomain newDomain;
239
239
240 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
240 qreal dx = oldDomain.spanX() / (m_rect.width() - 2 * margin);
241 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
241 qreal dy = oldDomain.spanY() / (m_rect.height() - 2 * margin);
242
242
243 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
243 newDomain.m_minX = oldDomain.m_minX + dx * rect.left();
244 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
244 newDomain.m_maxX = oldDomain.m_minX + dx * rect.right();
245 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
245 newDomain.m_minY = oldDomain.m_maxY - dy * rect.bottom();
246 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
246 newDomain.m_maxY = oldDomain.m_maxY - dy * rect.top();
247
247
248 m_plotDomainList.resize(m_plotDataIndex + 1);
248 m_plotDomainList.resize(m_plotDataIndex + 1);
249 m_plotDomainList<<newDomain;
249 m_plotDomainList<<newDomain;
250 m_plotDataIndex++;
250 m_plotDataIndex++;
251
251
252 foreach (ChartItem* item ,m_chartItems)
252 foreach (ChartItem* item ,m_chartItems)
253 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
253 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
254 update();
254 update();
255 }
255 }
256
256
257 void QChart::zoomIn()
257 void QChart::zoomIn()
258 {
258 {
259 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
259 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
260 m_plotDataIndex++;
260 m_plotDataIndex++;
261 foreach (ChartItem* item ,m_chartItems)
261 foreach (ChartItem* item ,m_chartItems)
262 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
262 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
263 update();
263 update();
264 }else{
264 }else{
265 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
265 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
266 rect.setWidth(rect.width()/2);
266 rect.setWidth(rect.width()/2);
267 rect.setHeight(rect.height()/2);
267 rect.setHeight(rect.height()/2);
268 rect.moveCenter(m_rect.center());
268 rect.moveCenter(m_rect.center());
269 zoomInToRect(rect);
269 zoomInToRect(rect);
270 }
270 }
271 }
271 }
272
272
273 void QChart::zoomOut()
273 void QChart::zoomOut()
274 {
274 {
275 if (m_plotDataIndex > 0) {
275 if (m_plotDataIndex > 0) {
276 m_plotDataIndex--;
276 m_plotDataIndex--;
277 foreach (ChartItem* item ,m_chartItems)
277 foreach (ChartItem* item ,m_chartItems)
278 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
278 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
279 update();
279 update();
280 }
280 }
281 }
281 }
282
282
283 #include "moc_qchart.cpp"
283 #include "moc_qchart.cpp"
284
284
285 QTCOMMERCIALCHART_END_NAMESPACE
285 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,314 +1,331
1 #include "mainwidget.h"
1 #include "mainwidget.h"
2 #include "dataseriedialog.h"
2 #include "dataseriedialog.h"
3 #include "qchartseries.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include <qxychartseries.h>
5 #include <qxychartseries.h>
6 #include <barchartseries.h>
6 #include <barchartseries.h>
7 #include <QPushButton>
7 #include <QPushButton>
8 #include <QComboBox>
8 #include <QComboBox>
9 #include <QSpinBox>
9 #include <QSpinBox>
10 #include <QCheckBox>
10 #include <QCheckBox>
11 #include <QGridLayout>
11 #include <QGridLayout>
12 #include <QHBoxLayout>
12 #include <QHBoxLayout>
13 #include <QLabel>
13 #include <QLabel>
14 #include <QSpacerItem>
14 #include <QSpacerItem>
15 #include <QMessageBox>
15 #include <QMessageBox>
16 #include <cmath>
16 #include <cmath>
17 #include <QDebug>
17 #include <QDebug>
18 #include <QStandardItemModel>
19
18
20
19 QTCOMMERCIALCHART_USE_NAMESPACE
21 QTCOMMERCIALCHART_USE_NAMESPACE
20
22
21 MainWidget::MainWidget(QWidget *parent) :
23 MainWidget::MainWidget(QWidget *parent) :
22 QWidget(parent)
24 QWidget(parent)
23 {
25 {
24 QPushButton *addSeriesButton = new QPushButton("Add series");
26 QPushButton *addSeriesButton = new QPushButton("Add series");
25 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
27 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
26
28
27 // Chart background
29 // Chart background
28 QComboBox *backgroundCombo = new QComboBox(this);
30 QComboBox *backgroundCombo = new QComboBox(this);
29 backgroundCombo->addItem("None");
31 backgroundCombo->addItem("None");
30 backgroundCombo->addItem("TODO Grid");
32 backgroundCombo->addItem("TODO Grid");
31 backgroundCombo->addItem("TODO Image");
33 backgroundCombo->addItem("TODO Image");
32 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
34 connect(backgroundCombo, SIGNAL(currentIndexChanged(int)),
33 this, SLOT(backgroundChanged(int)));
35 this, SLOT(backgroundChanged(int)));
34
36
35 // Axis
37 // Axis
36 // TODO: multiple axes?
38 // TODO: multiple axes?
37 m_autoScaleCheck = new QCheckBox("Automatic scaling");
39 m_autoScaleCheck = new QCheckBox("Automatic scaling");
38 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
40 connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int)));
39 // Allow setting also non-sense values (like -2147483648 and 2147483647)
41 // Allow setting also non-sense values (like -2147483648 and 2147483647)
40 m_xMinSpin = new QSpinBox();
42 m_xMinSpin = new QSpinBox();
41 m_xMinSpin->setMinimum(INT_MIN);
43 m_xMinSpin->setMinimum(INT_MIN);
42 m_xMinSpin->setMaximum(INT_MAX);
44 m_xMinSpin->setMaximum(INT_MAX);
43 m_xMinSpin->setValue(0);
45 m_xMinSpin->setValue(0);
44 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
46 connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int)));
45 m_xMaxSpin = new QSpinBox();
47 m_xMaxSpin = new QSpinBox();
46 m_xMaxSpin->setMinimum(INT_MIN);
48 m_xMaxSpin->setMinimum(INT_MIN);
47 m_xMaxSpin->setMaximum(INT_MAX);
49 m_xMaxSpin->setMaximum(INT_MAX);
48 m_xMaxSpin->setValue(10);
50 m_xMaxSpin->setValue(10);
49 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
51 connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int)));
50 m_yMinSpin = new QSpinBox();
52 m_yMinSpin = new QSpinBox();
51 m_yMinSpin->setMinimum(INT_MIN);
53 m_yMinSpin->setMinimum(INT_MIN);
52 m_yMinSpin->setMaximum(INT_MAX);
54 m_yMinSpin->setMaximum(INT_MAX);
53 m_yMinSpin->setValue(0);
55 m_yMinSpin->setValue(0);
54 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
56 connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int)));
55 m_yMaxSpin = new QSpinBox();
57 m_yMaxSpin = new QSpinBox();
56 m_yMaxSpin->setMinimum(INT_MIN);
58 m_yMaxSpin->setMinimum(INT_MIN);
57 m_yMaxSpin->setMaximum(INT_MAX);
59 m_yMaxSpin->setMaximum(INT_MAX);
58 m_yMaxSpin->setValue(10);
60 m_yMaxSpin->setValue(10);
59 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
61 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
60
62
61 QComboBox *chartTheme = new QComboBox();
63 QComboBox *chartTheme = new QComboBox();
62 chartTheme->addItem("Vanilla");
64 chartTheme->addItem("Vanilla");
63 chartTheme->addItem("Icy");
65 chartTheme->addItem("Icy");
64 chartTheme->addItem("Grayscale");
66 chartTheme->addItem("Grayscale");
65 chartTheme->addItem("Tobedefined");
67 chartTheme->addItem("Tobedefined");
66 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
68 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
67 this, SLOT(changeChartTheme(int)));
69 this, SLOT(changeChartTheme(int)));
68
70
69 QGridLayout *grid = new QGridLayout();
71 QGridLayout *grid = new QGridLayout();
70 QGridLayout *mainLayout = new QGridLayout();
72 QGridLayout *mainLayout = new QGridLayout();
71 //grid->addWidget(new QLabel("Add series:"), 0, 0);
73 //grid->addWidget(new QLabel("Add series:"), 0, 0);
72 grid->addWidget(addSeriesButton, 0, 1);
74 grid->addWidget(addSeriesButton, 0, 1);
73 grid->addWidget(new QLabel("Background:"), 2, 0);
75 grid->addWidget(new QLabel("Background:"), 2, 0);
74 grid->addWidget(backgroundCombo, 2, 1);
76 grid->addWidget(backgroundCombo, 2, 1);
75 grid->addWidget(m_autoScaleCheck, 3, 0);
77 grid->addWidget(m_autoScaleCheck, 3, 0);
76 grid->addWidget(new QLabel("x min:"), 4, 0);
78 grid->addWidget(new QLabel("x min:"), 4, 0);
77 grid->addWidget(m_xMinSpin, 4, 1);
79 grid->addWidget(m_xMinSpin, 4, 1);
78 grid->addWidget(new QLabel("x max:"), 5, 0);
80 grid->addWidget(new QLabel("x max:"), 5, 0);
79 grid->addWidget(m_xMaxSpin, 5, 1);
81 grid->addWidget(m_xMaxSpin, 5, 1);
80 grid->addWidget(new QLabel("y min:"), 6, 0);
82 grid->addWidget(new QLabel("y min:"), 6, 0);
81 grid->addWidget(m_yMinSpin, 6, 1);
83 grid->addWidget(m_yMinSpin, 6, 1);
82 grid->addWidget(new QLabel("y max:"), 7, 0);
84 grid->addWidget(new QLabel("y max:"), 7, 0);
83 grid->addWidget(m_yMaxSpin, 7, 1);
85 grid->addWidget(m_yMaxSpin, 7, 1);
84 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
86 grid->addWidget(new QLabel("Chart theme:"), 8, 0);
85 grid->addWidget(chartTheme, 8, 1);
87 grid->addWidget(chartTheme, 8, 1);
86 // add row with empty label to make all the other rows static
88 // add row with empty label to make all the other rows static
87 grid->addWidget(new QLabel(""), 9, 0);
89 grid->addWidget(new QLabel(""), 9, 0);
88 grid->setRowStretch(9, 1);
90 grid->setRowStretch(9, 1);
89
91
90 mainLayout->addLayout(grid, 0, 0);
92 mainLayout->addLayout(grid, 0, 0);
91
93
92 // Scatter specific settings
94 // Scatter specific settings
93 m_scatterLayout = new QGridLayout();
95 m_scatterLayout = new QGridLayout();
94 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
96 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
95 m_scatterLayout->setEnabled(false);
97 m_scatterLayout->setEnabled(false);
96
98
97 // Pie specific settings
99 // Pie specific settings
98 m_pieLayout = new QGridLayout();
100 m_pieLayout = new QGridLayout();
99 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
101 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
100 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
102 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
101 pieSizeSpin->setMinimum(LONG_MIN);
103 pieSizeSpin->setMinimum(LONG_MIN);
102 pieSizeSpin->setMaximum(LONG_MAX);
104 pieSizeSpin->setMaximum(LONG_MAX);
103 pieSizeSpin->setValue(1.0);
105 pieSizeSpin->setValue(1.0);
104 pieSizeSpin->setSingleStep(0.1);
106 pieSizeSpin->setSingleStep(0.1);
105 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
107 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
106 m_pieLayout->setEnabled(false);
108 m_pieLayout->setEnabled(false);
107 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
109 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
108
110
109 mainLayout->addLayout(m_scatterLayout, 1, 0);
111 mainLayout->addLayout(m_scatterLayout, 1, 0);
110 mainLayout->addLayout(m_pieLayout, 2, 0);
112 mainLayout->addLayout(m_pieLayout, 2, 0);
111
113
112 m_chartWidget = new QChartWidget(this);
114 m_chartWidget = new QChartWidget(this);
113 //m_chartWidget->setColor(Qt::red);
115 //m_chartWidget->setColor(Qt::red);
114 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
116 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
115 // hbox->setStretch(1, 1);
117 // hbox->setStretch(1, 1);
116
118
117 setLayout(mainLayout);
119 setLayout(mainLayout);
118
120
119 m_autoScaleCheck->setChecked(true);
121 m_autoScaleCheck->setChecked(true);
120 testDataChanged(0);
122 testDataChanged(0);
121 }
123 }
122
124
123 void MainWidget::addSeries()
125 void MainWidget::addSeries()
124 {
126 {
125 DataSerieDialog dialog(m_defaultSeriesName, this);
127 DataSerieDialog dialog(m_defaultSeriesName, this);
126 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
128 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
127 dialog.exec();
129 dialog.exec();
128 }
130 }
129
131
130 void MainWidget::addSeries(QString series, QString data)
132 void MainWidget::addSeries(QString series, QString data)
131 {
133 {
132 qDebug() << "addSeries: " << series << " data: " << data;
134 qDebug() << "addSeries: " << series << " data: " << data;
133 m_defaultSeriesName = series;
135 m_defaultSeriesName = series;
134
136
135 // TODO: a dedicated data class for storing x and y values
137 // TODO: a dedicated data class for storing x and y values
136 QList<qreal> x;
138 QList<qreal> x;
137 QList<qreal> y;
139 QList<qreal> y;
138
140
139 if (data == "linear") {
141 if (data == "linear") {
140 for (int i = 0; i < 20; i++) {
142 for (int i = 0; i < 20; i++) {
141 x.append(i);
143 x.append(i);
142 y.append(i);
144 y.append(i);
143 }
145 }
144 } else if (data == "linear, 1M") {
146 } else if (data == "linear, 1M") {
145 for (int i = 0; i < 10000; i++) {
147 for (int i = 0; i < 10000; i++) {
146 x.append(i);
148 x.append(i);
147 y.append(20);
149 y.append(20);
148 }
150 }
149 } else if (data == "SIN") {
151 } else if (data == "SIN") {
150 for (int i = 0; i < 100; i++) {
152 for (int i = 0; i < 100; i++) {
151 x.append(i);
153 x.append(i);
152 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
154 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
153 }
155 }
154 } else if (data == "SIN + random") {
156 } else if (data == "SIN + random") {
155 for (qreal i = 0; i < 100; i += 0.1) {
157 for (qreal i = 0; i < 100; i += 0.1) {
156 x.append(i + (rand() % 5));
158 x.append(i + (rand() % 5));
157 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
159 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
158 }
160 }
159 } else {
161 } else {
160 // TODO: check if data has a valid file name
162 // TODO: check if data has a valid file name
161 Q_ASSERT(false);
163 Q_ASSERT(false);
162 }
164 }
163
165
164 // TODO: color of the series
166 // TODO: color of the series
165 QChartSeries *newSeries = 0;
167 QChartSeries *newSeries = 0;
166 if (series == "Scatter") {
168 if (series == "Scatter") {
167 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
169 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeScatter);
168 Q_ASSERT(newSeries->setData(x, y));
170 Q_ASSERT(newSeries->setData(x, y));
169 } else if (series == "Pie") {
171 } else if (series == "Pie") {
170 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
172 newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypePie);
171 Q_ASSERT(newSeries->setData(y));
173 Q_ASSERT(newSeries->setData(y));
172 } else if (series == "Line") {
174 } else if (series == "Line") {
173 // TODO: adding data to an existing line series does not give any visuals for some reason
175 // TODO: adding data to an existing line series does not give any visuals for some reason
174 // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine);
176 // newSeries = m_chartWidget->createSeries(QChartSeries::SeriesTypeLine);
175 // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries);
177 // QXYChartSeries *lineSeries = static_cast<QXYChartSeries *>(newSeries);
176 // lineSeries->setColor(Qt::blue);
178 // lineSeries->setColor(Qt::blue);
177 // for (int i(0); i < x.count() && i < y.count(); i++) {
179 // for (int i(0); i < x.count() && i < y.count(); i++) {
178 // lineSeries->add(x.at(i), y.at(i));
180 // lineSeries->add(x.at(i), y.at(i));
179 // }
181 // }
180 //Q_ASSERT(newSeries->setData(x, y));
182 //Q_ASSERT(newSeries->setData(x, y));
181 QXYChartSeries* series0 = QXYChartSeries::create();
183 QXYChartSeries* series0 = QXYChartSeries::create();
182 for (int i(0); i < x.count() && i < y.count(); i++)
184 for (int i(0); i < x.count() && i < y.count(); i++)
183 series0->add(x.at(i), y.at(i));
185 series0->add(x.at(i), y.at(i));
184 m_chartWidget->addSeries(series0);
186 m_chartWidget->addSeries(series0);
185 newSeries = series0;
187 newSeries = series0;
186 } else {
188 } else {
187 // TODO
189 // TODO
188 }
190 }
189
191
190 // BarChart
192 // BarChart
191 if (series == "Bar") {
193 if (series == "Bar") {
192 // This is the another way of creating series. Should we create test cases for both ways, if we support them?
194 // This is the another way of creating series. Should we create test cases for both ways, if we support them?
193 qDebug() << "Bar chart series";
195 qDebug() << "Bar chart series";
194 newSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this);
196 newSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this);
195 QList<int> barData;
197
196 barData << 1;
198 // Create some test data to chart
197 barData << 12;
199 QStandardItemModel dataModel(2,10,this);
198 barData << 5;
200 QModelIndex index;
199 barData << 8;
201 index = dataModel.index(0,0);
200 barData << 17;
202 // Series 1, items 6 to 9 missing.
201 barData << 9;
203 dataModel.setData(dataModel.index(0,0),1);
202 newSeries->setData(barData);
204 dataModel.setData(dataModel.index(0,1),12);
205 dataModel.setData(dataModel.index(0,2),5);
206 dataModel.setData(dataModel.index(0,3),8);
207 dataModel.setData(dataModel.index(0,4),17);
208 dataModel.setData(dataModel.index(0,5),9);
209
210 // Series 2, some other items missing
211 dataModel.setData(dataModel.index(1,0),5);
212 dataModel.setData(dataModel.index(1,3),4);
213 dataModel.setData(dataModel.index(1,5),7);
214 dataModel.setData(dataModel.index(1,6),8);
215 dataModel.setData(dataModel.index(1,8),9);
216 dataModel.setData(dataModel.index(1,9),9);
217
218 newSeries->setData(&dataModel);
219
203 m_chartWidget->addSeries(newSeries);
220 m_chartWidget->addSeries(newSeries);
204 }
221 }
205
222
206 setCurrentSeries(newSeries);
223 setCurrentSeries(newSeries);
207 }
224 }
208
225
209 void MainWidget::setCurrentSeries(QChartSeries *series)
226 void MainWidget::setCurrentSeries(QChartSeries *series)
210 {
227 {
211 m_currentSeries = series;
228 m_currentSeries = series;
212 switch (m_currentSeries->type()) {
229 switch (m_currentSeries->type()) {
213 case QChartSeries::SeriesTypeLine:
230 case QChartSeries::SeriesTypeLine:
214 break;
231 break;
215 case QChartSeries::SeriesTypeScatter:
232 case QChartSeries::SeriesTypeScatter:
216 break;
233 break;
217 case QChartSeries::SeriesTypePie:
234 case QChartSeries::SeriesTypePie:
218 break;
235 break;
219 case QChartSeries::SeriesTypeBar:
236 case QChartSeries::SeriesTypeBar:
220 qDebug() << "setCurrentSeries (bar)";
237 qDebug() << "setCurrentSeries (bar)";
221 break;
238 break;
222 default:
239 default:
223 Q_ASSERT(false);
240 Q_ASSERT(false);
224 break;
241 break;
225 }
242 }
226 }
243 }
227
244
228 void MainWidget::testDataChanged(int itemIndex)
245 void MainWidget::testDataChanged(int itemIndex)
229 {
246 {
230 qDebug() << "testDataChanged: " << itemIndex;
247 qDebug() << "testDataChanged: " << itemIndex;
231
248
232 // switch (itemIndex) {
249 // switch (itemIndex) {
233 // case 0: {
250 // case 0: {
234 // QList<QChartDataPoint> data;
251 // QList<QChartDataPoint> data;
235 // for (int x = 0; x < 20; x++) {
252 // for (int x = 0; x < 20; x++) {
236 // data.append(QChartDataPoint() << x << x / 2);
253 // data.append(QChartDataPoint() << x << x / 2);
237 // }
254 // }
238 // m_chartWidget->setData(data);
255 // m_chartWidget->setData(data);
239 // break;
256 // break;
240 // }
257 // }
241 // case 1: {
258 // case 1: {
242 // QList<QChartDataPoint> data;
259 // QList<QChartDataPoint> data;
243 // for (int x = 0; x < 100; x++) {
260 // for (int x = 0; x < 100; x++) {
244 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
261 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100));
245 // }
262 // }
246 // m_chartWidget->setData(data);
263 // m_chartWidget->setData(data);
247 // break;
264 // break;
248 // }
265 // }
249 // case 2: {
266 // case 2: {
250 // QList<QChartDataPoint> data;
267 // QList<QChartDataPoint> data;
251 // for (int x = 0; x < 1000; x++) {
268 // for (int x = 0; x < 1000; x++) {
252 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
269 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
253 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
270 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
254 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
271 // data.append(QChartDataPoint() << x - 200 << 2 * (uint(sin(3.14159/50*x)*80) % 100) + (rand() % 100 * 0.2));
255 // }
272 // }
256 // m_chartWidget->setData(data);
273 // m_chartWidget->setData(data);
257 // break;
274 // break;
258 // }
275 // }
259 // default:
276 // default:
260 // break;
277 // break;
261 // }
278 // }
262 }
279 }
263
280
264 void MainWidget::backgroundChanged(int itemIndex)
281 void MainWidget::backgroundChanged(int itemIndex)
265 {
282 {
266 qDebug() << "backgroundChanged: " << itemIndex;
283 qDebug() << "backgroundChanged: " << itemIndex;
267 }
284 }
268
285
269 void MainWidget::autoScaleChanged(int value)
286 void MainWidget::autoScaleChanged(int value)
270 {
287 {
271 if (value) {
288 if (value) {
272 // TODO: enable auto scaling
289 // TODO: enable auto scaling
273 } else {
290 } else {
274 // TODO: set scaling manually (and disable auto scaling)
291 // TODO: set scaling manually (and disable auto scaling)
275 }
292 }
276
293
277 m_xMinSpin->setEnabled(!value);
294 m_xMinSpin->setEnabled(!value);
278 m_xMaxSpin->setEnabled(!value);
295 m_xMaxSpin->setEnabled(!value);
279 m_yMinSpin->setEnabled(!value);
296 m_yMinSpin->setEnabled(!value);
280 m_yMaxSpin->setEnabled(!value);
297 m_yMaxSpin->setEnabled(!value);
281 }
298 }
282
299
283 void MainWidget::xMinChanged(int value)
300 void MainWidget::xMinChanged(int value)
284 {
301 {
285 qDebug() << "xMinChanged: " << value;
302 qDebug() << "xMinChanged: " << value;
286 }
303 }
287
304
288 void MainWidget::xMaxChanged(int value)
305 void MainWidget::xMaxChanged(int value)
289 {
306 {
290 qDebug() << "xMaxChanged: " << value;
307 qDebug() << "xMaxChanged: " << value;
291 }
308 }
292
309
293 void MainWidget::yMinChanged(int value)
310 void MainWidget::yMinChanged(int value)
294 {
311 {
295 qDebug() << "yMinChanged: " << value;
312 qDebug() << "yMinChanged: " << value;
296 }
313 }
297
314
298 void MainWidget::yMaxChanged(int value)
315 void MainWidget::yMaxChanged(int value)
299 {
316 {
300 qDebug() << "yMaxChanged: " << value;
317 qDebug() << "yMaxChanged: " << value;
301 }
318 }
302
319
303 void MainWidget::changeChartTheme(int themeIndex)
320 void MainWidget::changeChartTheme(int themeIndex)
304 {
321 {
305 qDebug() << "changeChartTheme: " << themeIndex;
322 qDebug() << "changeChartTheme: " << themeIndex;
306 // m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
323 // m_chartWidget->setTheme((QChart::ChartTheme) themeIndex);
307 }
324 }
308
325
309 void MainWidget::setPieSizeFactor(double size)
326 void MainWidget::setPieSizeFactor(double size)
310 {
327 {
311 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
328 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
312 Q_ASSERT(pie);
329 Q_ASSERT(pie);
313 pie->setSizeFactor(qreal(size));
330 pie->setSizeFactor(qreal(size));
314 }
331 }
General Comments 0
You need to be logged in to leave comments. Login now