@@ -0,0 +1,30 | |||||
|
1 | #------------------------------------------------- | |||
|
2 | # | |||
|
3 | # Project created by QtCreator 2012-01-11T15:40:15 | |||
|
4 | # | |||
|
5 | #------------------------------------------------- | |||
|
6 | ||||
|
7 | QT += core gui | |||
|
8 | ||||
|
9 | TARGET = QBarChart | |||
|
10 | TEMPLATE = app | |||
|
11 | ||||
|
12 | ||||
|
13 | SOURCES += main.cpp\ | |||
|
14 | widget.cpp \ | |||
|
15 | qbarchart.cpp \ | |||
|
16 | qbarchartcontrol.cpp \ | |||
|
17 | qbarchartmodel.cpp \ | |||
|
18 | qbarchartbar.cpp \ | |||
|
19 | qbarchartview.cpp \ | |||
|
20 | qbarchartgrid.cpp \ | |||
|
21 | qchartview.cpp | |||
|
22 | ||||
|
23 | HEADERS += widget.h \ | |||
|
24 | qbarchart.h \ | |||
|
25 | qbarchartcontrol.h \ | |||
|
26 | qbarchartmodel.h \ | |||
|
27 | qbarchartbar.h \ | |||
|
28 | qbarchartview.h \ | |||
|
29 | qbarchartgrid.h \ | |||
|
30 | qchartview.h |
@@ -0,0 +1,39 | |||||
|
1 | #include <QtGui/QApplication> | |||
|
2 | #include <QGraphicsScene> | |||
|
3 | //QGraphicsScene *scene, QWidget *parent = 0#include <QGraphicsView> | |||
|
4 | //#include "widget.h" | |||
|
5 | //#include "qbarchart.h" | |||
|
6 | #include "qchartview.h" | |||
|
7 | ||||
|
8 | int main(int argc, char *argv[]) | |||
|
9 | { | |||
|
10 | QApplication a(argc, argv); | |||
|
11 | ||||
|
12 | QGraphicsScene scene; | |||
|
13 | //QGraphicsView view(&scene); | |||
|
14 | QChartView view(&scene); | |||
|
15 | /* | |||
|
16 | QBarChart *barChart = new QBarChart(); | |||
|
17 | barChart->setSize(640,480); | |||
|
18 | scene.addItem(barChart); | |||
|
19 | ||||
|
20 | QList<int> data; | |||
|
21 | data << 1; | |||
|
22 | data << 1; | |||
|
23 | data << 2; | |||
|
24 | data << 3; | |||
|
25 | data << 5; | |||
|
26 | data << 8; | |||
|
27 | data << 13; | |||
|
28 | data << 21; | |||
|
29 | data << 42; | |||
|
30 | barChart->addSeries(data); | |||
|
31 | */ | |||
|
32 | view.resize(640, 480); | |||
|
33 | QPalette p(view.palette()); | |||
|
34 | p.setColor(QPalette::Base, QColor(0,0,0)); | |||
|
35 | view.setPalette(p); | |||
|
36 | view.show(); | |||
|
37 | ||||
|
38 | return a.exec(); | |||
|
39 | } |
@@ -0,0 +1,92 | |||||
|
1 | #include "qbarchart.h" | |||
|
2 | #include "qbarchartbar.h" | |||
|
3 | #include <QDebug> | |||
|
4 | ||||
|
5 | QBarChart::QBarChart(QGraphicsItem *parent) : | |||
|
6 | QGraphicsItem(parent) | |||
|
7 | ,mBarDefaultWidth( 1 ) | |||
|
8 | { | |||
|
9 | } | |||
|
10 | ||||
|
11 | void QBarChart::addSeries(QList<int> data) | |||
|
12 | { | |||
|
13 | mData.clear(); | |||
|
14 | mData.append(data); | |||
|
15 | dataChanged(); | |||
|
16 | } | |||
|
17 | ||||
|
18 | void QBarChart::setSize( int h, int w ) | |||
|
19 | { | |||
|
20 | mHeight = h; | |||
|
21 | mWidth = w; | |||
|
22 | dataChanged(); // TODO: separate data from grid | |||
|
23 | } | |||
|
24 | ||||
|
25 | void QBarChart::setBarWidth( int w ) | |||
|
26 | { | |||
|
27 | mBarDefaultWidth = w; | |||
|
28 | } | |||
|
29 | ||||
|
30 | void QBarChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
|
31 | { | |||
|
32 | qDebug() << "QBarChart::paint"; | |||
|
33 | } | |||
|
34 | ||||
|
35 | QRectF QBarChart::boundingRect() const | |||
|
36 | { | |||
|
37 | return QRectF(0,0,mWidth,mHeight); // TODO: size from layout | |||
|
38 | } | |||
|
39 | ||||
|
40 | ||||
|
41 | void QBarChart::dataChanged() | |||
|
42 | { | |||
|
43 | int count = mData.count(); | |||
|
44 | ||||
|
45 | if (count <= 0) { | |||
|
46 | // No items | |||
|
47 | return; | |||
|
48 | } | |||
|
49 | ||||
|
50 | mMax = mData.at(0); | |||
|
51 | mMin = mData.at(0); | |||
|
52 | ||||
|
53 | // Find out min and max values. | |||
|
54 | for (int i=0; i<count; i++) { | |||
|
55 | if (mData.at(i) > mMax) { | |||
|
56 | mMax = mData.at(i); | |||
|
57 | } else if (mData.at(i) < mMin) { | |||
|
58 | mMin = mData.at(i); | |||
|
59 | } | |||
|
60 | } | |||
|
61 | ||||
|
62 | // Create items for data, delete old ones | |||
|
63 | foreach (QGraphicsItem* i, childItems()) { | |||
|
64 | delete i; | |||
|
65 | } | |||
|
66 | ||||
|
67 | gridChanged(); | |||
|
68 | ||||
|
69 | int posStep = (mWidth / count) / 2; // TODO: Why I need magical 2 here to get correct step? | |||
|
70 | ||||
|
71 | for (int i=0; i<count; i++) { | |||
|
72 | QBarChartBar *bar = new QBarChartBar(this); | |||
|
73 | ||||
|
74 | // Scaling. TODO: better one. | |||
|
75 | int barHeight = mData.at(i) * mHeight / mMax; | |||
|
76 | bar->setSize(barHeight, mBarDefaultWidth * 3 * i); // TODO: widht settable by style or something. | |||
|
77 | bar->setColor(QColor(255 - i*25, i*10, i*15)); | |||
|
78 | bar->setPos(i*posStep, 0); | |||
|
79 | childItems().append(bar); | |||
|
80 | } | |||
|
81 | } | |||
|
82 | ||||
|
83 | void QBarChart::gridChanged() | |||
|
84 | { | |||
|
85 | // Scale of grid has changed | |||
|
86 | QBarChartGrid *grid = new QBarChartGrid(this); | |||
|
87 | grid->setSize(mHeight, mWidth); | |||
|
88 | grid->setLimits(mMin,mMax); | |||
|
89 | grid->setHorizontalLineCount(5); | |||
|
90 | childItems().append(grid); | |||
|
91 | } | |||
|
92 |
@@ -0,0 +1,56 | |||||
|
1 | #ifndef QBARCHART_H | |||
|
2 | #define QBARCHART_H | |||
|
3 | ||||
|
4 | #include <QGraphicsItem> | |||
|
5 | ||||
|
6 | #include "qbarchartbar.h" | |||
|
7 | #include "qbarchartcontrol.h" | |||
|
8 | #include "qbarchartview.h" | |||
|
9 | #include "qbarchartgrid.h" | |||
|
10 | ||||
|
11 | class QBarChart : public QGraphicsItem | |||
|
12 | { | |||
|
13 | ||||
|
14 | public: | |||
|
15 | explicit QBarChart(QGraphicsItem *parent = 0); | |||
|
16 | ||||
|
17 | // Data "api" | |||
|
18 | void addSeries(QList<int> data); // TODO: maybe some better structure | |||
|
19 | ||||
|
20 | // Layout "api" | |||
|
21 | void setSize( int h, int w ); | |||
|
22 | void setPos(qreal x, qreal y); | |||
|
23 | void setBarWidth( int w ); | |||
|
24 | ||||
|
25 | // From QGraphicsItem | |||
|
26 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |||
|
27 | QRectF boundingRect() const; | |||
|
28 | ||||
|
29 | private: | |||
|
30 | ||||
|
31 | void dataChanged(); | |||
|
32 | void gridChanged(); | |||
|
33 | ||||
|
34 | private: | |||
|
35 | ||||
|
36 | // Data | |||
|
37 | QList<int> mData; // use ints for now.. | |||
|
38 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |||
|
39 | int mMax; | |||
|
40 | ||||
|
41 | // View components | |||
|
42 | QList<QBarChartBar> mItems; // 2 first items in list are x and y axis, in that order | |||
|
43 | QBarChartGrid *mGrid; | |||
|
44 | ||||
|
45 | int mHeight; // Layout spesific | |||
|
46 | int mWidth; | |||
|
47 | int mBarDefaultWidth; | |||
|
48 | ||||
|
49 | // Controll stuff | |||
|
50 | QBarChartControl mBarControl; | |||
|
51 | ||||
|
52 | ||||
|
53 | ||||
|
54 | }; | |||
|
55 | ||||
|
56 | #endif // QBARCHART_H |
@@ -0,0 +1,39 | |||||
|
1 | #include "qbarchartbar.h" | |||
|
2 | #include <QDebug> | |||
|
3 | #include <Qpainter> | |||
|
4 | ||||
|
5 | QBarChartBar::QBarChartBar(QGraphicsItem *parent) | |||
|
6 | : QGraphicsItem(parent) | |||
|
7 | { | |||
|
8 | } | |||
|
9 | ||||
|
10 | void QBarChartBar::setSize( int h, int w ) | |||
|
11 | { | |||
|
12 | mHeight = h; | |||
|
13 | mWidth = w; | |||
|
14 | } | |||
|
15 | ||||
|
16 | void QBarChartBar::setColor( QColor col ) | |||
|
17 | { | |||
|
18 | mColor = col; | |||
|
19 | } | |||
|
20 | ||||
|
21 | void QBarChartBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
|
22 | { | |||
|
23 | qDebug() << "QBarChartBar::paint" << scenePos(); | |||
|
24 | // Set color for bar. TODO: gradients, textures etc | |||
|
25 | QPen pen = painter->pen(); | |||
|
26 | pen.setColor( mColor ); | |||
|
27 | pen.setWidth( mWidth ); | |||
|
28 | painter->setPen(pen); | |||
|
29 | ||||
|
30 | // Draw bar | |||
|
31 | painter->drawLine(scenePos().x(), scenePos().y() + parentItem()->boundingRect().height(), | |||
|
32 | scenePos().x(), scenePos().y() + parentItem()->boundingRect().height() - mHeight ); | |||
|
33 | } | |||
|
34 | ||||
|
35 | QRectF QBarChartBar::boundingRect() const | |||
|
36 | { | |||
|
37 | QRectF r(scenePos().x(), scenePos().y(), scenePos().x() + mWidth, scenePos().y() + mHeight ); | |||
|
38 | return r; | |||
|
39 | } |
@@ -0,0 +1,31 | |||||
|
1 | #ifndef QBARCHARTBAR_H | |||
|
2 | #define QBARCHARTBAR_H | |||
|
3 | ||||
|
4 | #include <QGraphicsItem> | |||
|
5 | ||||
|
6 | // Single bar item of chart | |||
|
7 | ||||
|
8 | class QBarChartBar : public QGraphicsItem | |||
|
9 | { | |||
|
10 | public: | |||
|
11 | QBarChartBar(QGraphicsItem *parent=0); | |||
|
12 | ||||
|
13 | // Layout Stuff | |||
|
14 | void setSize( int h, int w ); // Size of bar. in screen coordinates. | |||
|
15 | void setColor( QColor col ); // Color of bar | |||
|
16 | ||||
|
17 | public: | |||
|
18 | // From QGraphicsItem | |||
|
19 | ||||
|
20 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); | |||
|
21 | QRectF boundingRect() const; | |||
|
22 | ||||
|
23 | private: | |||
|
24 | ||||
|
25 | int mHeight; | |||
|
26 | int mWidth; | |||
|
27 | QPointF mPos; | |||
|
28 | QColor mColor; | |||
|
29 | }; | |||
|
30 | ||||
|
31 | #endif // QBARCHARTBAR_H |
@@ -0,0 +1,6 | |||||
|
1 | #include "qbarchartcontrol.h" | |||
|
2 | ||||
|
3 | QBarChartControl::QBarChartControl(QObject *parent) : | |||
|
4 | QObject(parent) | |||
|
5 | { | |||
|
6 | } |
@@ -0,0 +1,18 | |||||
|
1 | #ifndef QBARCHARTCONTROL_H | |||
|
2 | #define QBARCHARTCONTROL_H | |||
|
3 | ||||
|
4 | #include <QObject> | |||
|
5 | ||||
|
6 | class QBarChartControl : public QObject | |||
|
7 | { | |||
|
8 | Q_OBJECT | |||
|
9 | public: | |||
|
10 | explicit QBarChartControl(QObject *parent = 0); | |||
|
11 | ||||
|
12 | signals: | |||
|
13 | ||||
|
14 | public slots: | |||
|
15 | ||||
|
16 | }; | |||
|
17 | ||||
|
18 | #endif // QBARCHARTCONTROL_H |
@@ -0,0 +1,52 | |||||
|
1 | #include "qbarchartgrid.h" | |||
|
2 | #include <QDebug> | |||
|
3 | #include <QPainter> | |||
|
4 | ||||
|
5 | QBarChartGrid::QBarChartGrid(QGraphicsItem *parent) : | |||
|
6 | QGraphicsItem(parent) | |||
|
7 | { | |||
|
8 | } | |||
|
9 | ||||
|
10 | ||||
|
11 | void QBarChartGrid::setLimits( int min, int max ) | |||
|
12 | { | |||
|
13 | mMin = min; | |||
|
14 | mMax = max; | |||
|
15 | } | |||
|
16 | ||||
|
17 | void QBarChartGrid::setHorizontalLineCount(int count) | |||
|
18 | { | |||
|
19 | mHorizontalLineCount = count; | |||
|
20 | } | |||
|
21 | ||||
|
22 | void QBarChartGrid::setSize( int h, int w ) | |||
|
23 | { | |||
|
24 | mHeight = h; | |||
|
25 | mWidth = w; | |||
|
26 | } | |||
|
27 | ||||
|
28 | void QBarChartGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
|
29 | { | |||
|
30 | qDebug() << "QBarChartGrid::paint w =" << mWidth << "h =" << mHeight; | |||
|
31 | ||||
|
32 | QPen p = painter->pen(); | |||
|
33 | p.setColor( QColor(0,255,0) ); | |||
|
34 | p.setWidth( 1 ); | |||
|
35 | painter->setPen(p); | |||
|
36 | ||||
|
37 | // Draw horizontal lines | |||
|
38 | if ( mHorizontalLineCount >0 ) { | |||
|
39 | int step = mHeight / mHorizontalLineCount; | |||
|
40 | if (step > 0) { | |||
|
41 | for (int y=0; y<mHeight; y+=step) { | |||
|
42 | painter->drawLine(0,y,mWidth,y); | |||
|
43 | } | |||
|
44 | } | |||
|
45 | } | |||
|
46 | } | |||
|
47 | ||||
|
48 | QRectF QBarChartGrid::boundingRect() const | |||
|
49 | { | |||
|
50 | QRectF r(scenePos().x(), scenePos().y(), scenePos().x() + mWidth, scenePos().y() + mHeight ); | |||
|
51 | return r; | |||
|
52 | } |
@@ -0,0 +1,33 | |||||
|
1 | #ifndef QBARCHARTGRID_H | |||
|
2 | #define QBARCHARTGRID_H | |||
|
3 | ||||
|
4 | #include <QGraphicsItem> | |||
|
5 | ||||
|
6 | class QBarChartGrid : public QGraphicsItem | |||
|
7 | { | |||
|
8 | ||||
|
9 | public: | |||
|
10 | QBarChartGrid(QGraphicsItem *parent = 0); | |||
|
11 | ||||
|
12 | // Data api: | |||
|
13 | void setLimits( int min, int max ); | |||
|
14 | void setHorizontalLineCount( int count ); | |||
|
15 | ||||
|
16 | // Layout api: | |||
|
17 | void setSize( int h, int w ); | |||
|
18 | ||||
|
19 | // From QGraphicsItem | |||
|
20 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |||
|
21 | QRectF boundingRect() const; | |||
|
22 | ||||
|
23 | private: | |||
|
24 | ||||
|
25 | int mWidth; | |||
|
26 | int mHeight; | |||
|
27 | ||||
|
28 | int mMin; | |||
|
29 | int mMax; | |||
|
30 | int mHorizontalLineCount; // Draw this many horizontal lines to grid | |||
|
31 | }; | |||
|
32 | ||||
|
33 | #endif // QBARCHARTGRID_H |
@@ -0,0 +1,6 | |||||
|
1 | #include "qbarchartmodel.h" | |||
|
2 | ||||
|
3 | QBarChartModel::QBarChartModel(QObject *parent) : | |||
|
4 | QAbstractItemModel(parent) | |||
|
5 | { | |||
|
6 | } |
@@ -0,0 +1,18 | |||||
|
1 | #ifndef QBARCHARTMODEL_H | |||
|
2 | #define QBARCHARTMODEL_H | |||
|
3 | ||||
|
4 | #include <QAbstractItemModel> | |||
|
5 | ||||
|
6 | class QBarChartModel : public QAbstractItemModel | |||
|
7 | { | |||
|
8 | Q_OBJECT | |||
|
9 | public: | |||
|
10 | explicit QBarChartModel(QObject *parent = 0); | |||
|
11 | ||||
|
12 | signals: | |||
|
13 | ||||
|
14 | public slots: | |||
|
15 | ||||
|
16 | }; | |||
|
17 | ||||
|
18 | #endif // QBARCHARTMODEL_H |
@@ -0,0 +1,10 | |||||
|
1 | #ifndef QBARCHARTVIEW_H | |||
|
2 | #define QBARCHARTVIEW_H | |||
|
3 | ||||
|
4 | class QBarChartView | |||
|
5 | { | |||
|
6 | public: | |||
|
7 | QBarChartView(); | |||
|
8 | }; | |||
|
9 | ||||
|
10 | #endif // QBARCHARTVIEW_H |
@@ -0,0 +1,18 | |||||
|
1 | #include "qbarchartxaxis.h" | |||
|
2 | ||||
|
3 | QBarChartXAxis::QBarChartXAxis(QGraphicsItem *parent) : | |||
|
4 | QGraphicsItem(parent) | |||
|
5 | { | |||
|
6 | } | |||
|
7 | ||||
|
8 | ||||
|
9 | void QBarChartXAxis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
|
10 | { | |||
|
11 | ||||
|
12 | } | |||
|
13 | ||||
|
14 | QRectF QBarChartXAxis::boundingRect() const | |||
|
15 | { | |||
|
16 | QRectF r(scenePos().x(), scenePos().y(), scenePos().x() + mWidth, scenePos().y() + mHeight ); | |||
|
17 | return r; | |||
|
18 | } |
@@ -0,0 +1,22 | |||||
|
1 | #ifndef QBARCHARTXAXIS_H | |||
|
2 | #define QBARCHARTXAXIS_H | |||
|
3 | ||||
|
4 | #include <QGraphicsItem> | |||
|
5 | ||||
|
6 | class QBarChartXAxis : public QGraphicsItem | |||
|
7 | { | |||
|
8 | ||||
|
9 | public: | |||
|
10 | QBarChartXAxis(QGraphicsItem *parent = 0); | |||
|
11 | ||||
|
12 | // From QGraphicsItem | |||
|
13 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |||
|
14 | QRectF boundingRect() const; | |||
|
15 | ||||
|
16 | private: | |||
|
17 | ||||
|
18 | int mWidth; | |||
|
19 | int mHeight; | |||
|
20 | }; | |||
|
21 | ||||
|
22 | #endif // QBARCHARTXAXIS_H |
@@ -0,0 +1,30 | |||||
|
1 | #include "qchartview.h" | |||
|
2 | #include "qbarchart.h" | |||
|
3 | #include <qevent.h> | |||
|
4 | ||||
|
5 | QChartView::QChartView(QGraphicsScene *scene, QWidget *parent ) : | |||
|
6 | QGraphicsView(scene,parent) | |||
|
7 | { | |||
|
8 | mChart = new QBarChart(); | |||
|
9 | mChart->setSize(640,480); | |||
|
10 | scene->addItem(mChart); | |||
|
11 | ||||
|
12 | QList<int> data; | |||
|
13 | data << 1; | |||
|
14 | data << 1; | |||
|
15 | data << 2; | |||
|
16 | data << 3; | |||
|
17 | data << 5; | |||
|
18 | data << 8; | |||
|
19 | data << 13; | |||
|
20 | data << 21; | |||
|
21 | data << 42; | |||
|
22 | mChart->addSeries(data); | |||
|
23 | ||||
|
24 | } | |||
|
25 | ||||
|
26 | ||||
|
27 | void QChartView::resizeEvent(QResizeEvent *event) | |||
|
28 | { | |||
|
29 | mChart->setSize(event->size().height(), event->size().width()); | |||
|
30 | } |
@@ -0,0 +1,25 | |||||
|
1 | #ifndef QCHARTVIEW_H | |||
|
2 | #define QCHARTVIEW_H | |||
|
3 | ||||
|
4 | #include <QGraphicsView> | |||
|
5 | #include "qbarchart.h" | |||
|
6 | ||||
|
7 | class QChartView : public QGraphicsView | |||
|
8 | { | |||
|
9 | Q_OBJECT | |||
|
10 | public: | |||
|
11 | explicit QChartView(QGraphicsScene *scene, QWidget *parent = 0); | |||
|
12 | ||||
|
13 | signals: | |||
|
14 | ||||
|
15 | public slots: | |||
|
16 | ||||
|
17 | protected: | |||
|
18 | void resizeEvent(QResizeEvent *event); | |||
|
19 | ||||
|
20 | private: | |||
|
21 | ||||
|
22 | QBarChart *mChart; | |||
|
23 | }; | |||
|
24 | ||||
|
25 | #endif // QCHARTVIEW_H |
General Comments 0
You need to be logged in to leave comments.
Login now