##// END OF EJS Templates
Add background to chart...
Michal Klocek -
r69:c7c3c4960b21
parent child
Show More
@@ -0,0 +1,15
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 = colorlineChart
10 TEMPLATE = app
11 QT += core gui
12 SOURCES += main.cpp
13
14
15
@@ -0,0 +1,40
1 #include <QApplication>
2 #include <QMainWindow>
3 #include <qchartview.h>
4 #include <qxychartseries.h>
5 #include <qchart.h>
6 #include <cmath>
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
10 #define PI 3.14159265358979
11
12 int main(int argc, char *argv[])
13 {
14 QApplication a(argc, argv);
15
16 QMainWindow window;
17
18 QXYChartSeries* series0 = QXYChartSeries::create();
19 series0->setColor(Qt::blue);
20 QXYChartSeries* series1 = QXYChartSeries::create();
21 series1->setColor(Qt::red);
22
23 int numPoints = 100;
24
25 for (int x = 0; x <= numPoints; ++x) {
26 series0->add(x, abs(sin(PI/50*x)*100));
27 series1->add(x, abs(cos(PI/50*x)*100));
28 }
29
30 QChartView* chartView = new QChartView(&window);
31 chartView->addSeries(series0);
32 chartView->addSeries(series1);
33 chartView->setBackgroundColor(Qt::yellow);
34
35 window.setCentralWidget(chartView);
36 window.resize(400, 300);
37 window.show();
38
39 return a.exec();
40 }
@@ -1,3 +1,4
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += linechart \
2 SUBDIRS += linechart \
3 zoomlinechart
3 zoomlinechart \
4 colorlinechart
@@ -17,6 +17,8
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)),
21 m_title(new QGraphicsTextItem(this)),
20 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
22 m_axisX(new AxisItem(AxisItem::X_AXIS,this)),
21 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
23 m_axisY(new AxisItem(AxisItem::Y_AXIS,this)),
22 m_plotDataIndex(0),
24 m_plotDataIndex(0),
@@ -146,6 +148,13 void QChart::setSize(const QSize& size)
146 m_rect = QRect(QPoint(0,0),size);
148 m_rect = QRect(QPoint(0,0),size);
147 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
149 QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
148
150
151
152 //recalculate background gradient
153 m_background->setRect(rect);
154 m_backgroundGradient.setFinalStop(0,m_background->rect().height());
155 m_background->setBrush(m_backgroundGradient);
156
157 //resize elements
149 foreach (ChartItem* item ,m_chartItems) {
158 foreach (ChartItem* item ,m_chartItems) {
150 item->setPos(rect.topLeft());
159 item->setPos(rect.topLeft());
151 item->setSize(rect.size());
160 item->setSize(rect.size());
@@ -160,6 +169,20 void QChart::setSize(const QSize& size)
160 update();
169 update();
161 }
170 }
162
171
172 void QChart::setBackgroundColor(const QColor& color)
173 {
174 m_backgroundGradient.setColorAt( 0.0, Qt::white);
175 m_backgroundGradient.setColorAt( 1.0, color);
176 m_background->setBrush(m_backgroundGradient);
177 m_background->setPen(Qt::NoPen);
178 m_background->update();
179 }
180
181 void QChart::setTitle(const QString& title)
182 {
183 m_title->setPlainText(title);
184 }
185
163 int QChart::margin() const
186 int QChart::margin() const
164 {
187 {
165 return m_marginSize;
188 return m_marginSize;
@@ -4,6 +4,7
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <qchartseries.h>
5 #include <qchartseries.h>
6 #include <QGraphicsObject>
6 #include <QGraphicsObject>
7 #include <QLinearGradient>
7
8
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10
@@ -49,6 +50,9 public:
49 int margin() const;
50 int margin() const;
50 void setTheme(QChart::ChartTheme theme);
51 void setTheme(QChart::ChartTheme theme);
51
52
53 void setTitle(const QString& title);
54 void setBackgroundColor(const QColor& color);
55
52 void zoomInToRect(const QRect& rectangle);
56 void zoomInToRect(const QRect& rectangle);
53 void zoomIn();
57 void zoomIn();
54 void zoomOut();
58 void zoomOut();
@@ -60,6 +64,9 signals:
60
64
61 private:
65 private:
62 Q_DISABLE_COPY(QChart)
66 Q_DISABLE_COPY(QChart)
67 QGraphicsRectItem* m_background;
68 QLinearGradient m_backgroundGradient;
69 QGraphicsTextItem* m_title;
63 AxisItem* m_axisX;
70 AxisItem* m_axisX;
64 AxisItem* m_axisY;
71 AxisItem* m_axisY;
65 QRect m_rect;
72 QRect m_rect;
@@ -71,7 +78,6 private:
71 int m_plotDataIndex;
78 int m_plotDataIndex;
72 int m_marginSize;
79 int m_marginSize;
73 QList<QColor> m_themeColors;
80 QList<QColor> m_themeColors;
74
75 QList<BarGroup*> m_BarGroupItems;
81 QList<BarGroup*> m_BarGroupItems;
76 };
82 };
77
83
@@ -63,4 +63,15 int QChartView::margin() const
63 {
63 {
64 return m_chart->margin();
64 return m_chart->margin();
65 }
65 }
66
67 void QChartView::setTitle(const QString& title)
68 {
69 m_chart->setTitle(title);
70 }
71
72 void QChartView::setBackgroundColor(const QColor& color)
73 {
74 m_chart->setBackgroundColor(color);
75 }
76
66 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -25,6 +25,8 public:
25 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
25 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
26
26
27 int margin() const;
27 int margin() const;
28 void setTitle(const QString& title);
29 void setBackgroundColor(const QColor& color);
28 void zoomInToRect(const QRect& rectangle);
30 void zoomInToRect(const QRect& rectangle);
29 void zoomIn();
31 void zoomIn();
30 void zoomOut();
32 void zoomOut();
General Comments 0
You need to be logged in to leave comments. Login now