##// END OF EJS Templates
Scaling for scatter series
Tero Ahola -
r111:0d50fe03d2e8
parent child
Show More
@@ -54,15 +54,15 void QChart::addSeries(QChartSeries* series)
54
54
55 m_chartSeries << series;
55 m_chartSeries << series;
56
56
57 m_plotDataIndex = 0 ;
58 m_plotDomainList.resize(1);
59 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
60
57 switch(series->type())
61 switch(series->type())
58 {
62 {
59 case QChartSeries::SeriesTypeLine: {
63 case QChartSeries::SeriesTypeLine: {
60
64
61 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
65 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
62 m_plotDataIndex = 0 ;
63 m_plotDomainList.resize(1);
64
65 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
66
66
67 for (int i = 0 ; i < xyseries->count() ; i++) {
67 for (int i = 0 ; i < xyseries->count() ; i++) {
68 qreal x = xyseries->x(i);
68 qreal x = xyseries->x(i);
@@ -97,15 +97,8 void QChart::addSeries(QChartSeries* series)
97 m_chartItems << barGroup;
97 m_chartItems << barGroup;
98 childItems().append(barGroup);
98 childItems().append(barGroup);
99
99
100 // TODO: setting of domain should this be somewhere else.
101 m_plotDataIndex = 0 ;
102 m_plotDomainList.resize(1);
103
104 qreal x = barSeries->countColumns();
100 qreal x = barSeries->countColumns();
105 qreal y = barSeries->max();
101 qreal y = barSeries->max();
106
107 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
108
109 domain.m_minX = qMin(domain.m_minX,x);
102 domain.m_minX = qMin(domain.m_minX,x);
110 domain.m_minY = qMin(domain.m_minY,y);
103 domain.m_minY = qMin(domain.m_minY,y);
111 domain.m_maxX = qMax(domain.m_maxX,x);
104 domain.m_maxX = qMax(domain.m_maxX,x);
@@ -128,15 +121,8 void QChart::addSeries(QChartSeries* series)
128 m_chartItems << stackedBarGroup;
121 m_chartItems << stackedBarGroup;
129 childItems().append(stackedBarGroup);
122 childItems().append(stackedBarGroup);
130
123
131 // TODO: setting of domain should this be somewhere else.
132 m_plotDataIndex = 0 ;
133 m_plotDomainList.resize(1);
134
135 qreal x = stackedBarSeries->countColumns();
124 qreal x = stackedBarSeries->countColumns();
136 qreal y = stackedBarSeries->maxColumnSum();
125 qreal y = stackedBarSeries->maxColumnSum();
137
138 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
139
140 domain.m_minX = qMin(domain.m_minX,x);
126 domain.m_minX = qMin(domain.m_minX,x);
141 domain.m_minY = qMin(domain.m_minY,y);
127 domain.m_minY = qMin(domain.m_minY,y);
142 domain.m_maxX = qMax(domain.m_maxX,x);
128 domain.m_maxX = qMax(domain.m_maxX,x);
@@ -159,14 +145,7 void QChart::addSeries(QChartSeries* series)
159 m_chartItems << percentBarGroup;
145 m_chartItems << percentBarGroup;
160 childItems().append(percentBarGroup);
146 childItems().append(percentBarGroup);
161
147
162 // TODO: setting of domain should this be somewhere else.
163 m_plotDataIndex = 0 ;
164 m_plotDomainList.resize(1);
165
166 qreal x = percentBarSeries->countColumns();
148 qreal x = percentBarSeries->countColumns();
167
168 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
169
170 domain.m_minX = qMin(domain.m_minX,x);
149 domain.m_minX = qMin(domain.m_minX,x);
171 domain.m_minY = 0;
150 domain.m_minY = 0;
172 domain.m_maxX = qMax(domain.m_maxX,x);
151 domain.m_maxX = qMax(domain.m_maxX,x);
@@ -177,8 +156,19 void QChart::addSeries(QChartSeries* series)
177 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
156 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
178 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
157 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
179 scatterSeries->d->setParentItem(this);
158 scatterSeries->d->setParentItem(this);
159 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
180 m_chartItems << scatterSeries->d;
160 m_chartItems << scatterSeries->d;
181 m_chartTheme->addObserver(scatterSeries->d);
161 m_chartTheme->addObserver(scatterSeries->d);
162
163 foreach (qreal x, scatterSeries->d->m_x) {
164 domain.m_minX = qMin(domain.m_minX, x);
165 domain.m_maxX = qMax(domain.m_maxX, x);
166 }
167 foreach (qreal y, scatterSeries->d->m_y) {
168 domain.m_minY = qMin(domain.m_minY, y);
169 domain.m_maxY = qMax(domain.m_maxY, y);
170 }
171
182 break;
172 break;
183 }
173 }
184 case QChartSeries::SeriesTypePie: {
174 case QChartSeries::SeriesTypePie: {
@@ -18,6 +18,8 QChartWidget::QChartWidget(QWidget *parent) :
18 setScene(m_scene);
18 setScene(m_scene);
19
19
20 m_chart = new QChart();
20 m_chart = new QChart();
21 // TODO: currently setting the margins explicitly is required, if you wish to have axis labels visible
22 m_chart->setMargin(25);
21 m_scene->addItem(m_chart);
23 m_scene->addItem(m_chart);
22 m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default?
24 m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default?
23 show();
25 show();
@@ -12,31 +12,36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
13 ChartItem(parent),
13 ChartItem(parent),
14 m_markerColor(QColor()),
14 m_markerColor(QColor()),
15 m_visibleChartArea()
15 m_visibleChartArea(),
16 m_boundingRect()
16 {
17 {
18 if (parent)
19 m_boundingRect = parent->boundingRect();
17 }
20 }
18
21
19 void QScatterSeriesPrivate::resize(QRectF rect)
22 void QScatterSeriesPrivate::changeGeometry()
20 {
23 {
21 qreal scalex = rect.width() / m_visibleChartArea.spanX();
24 if (m_boundingRect.isValid()) {
22 qreal scaley = rect.height() / m_visibleChartArea.spanY();
25 prepareGeometryChange();
23
26 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
27 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
24 m_scenex.clear();
28 m_scenex.clear();
25 m_sceney.clear();
29 m_sceney.clear();
26
30
27 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
31 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
28 foreach(qreal x, m_x)
32 foreach(qreal x, m_x)
29 m_scenex.append(rect.left() + x * scalex - m_visibleChartArea.m_minX * scalex);
33 m_scenex.append(m_boundingRect.left() + x * scalex - m_visibleChartArea.m_minX * scalex);
30
34
31 foreach(qreal y, m_y)
35 foreach(qreal y, m_y)
32 m_sceney.append(rect.bottom() - y * scaley - m_visibleChartArea.m_minY * scaley);
36 m_sceney.append(m_boundingRect.bottom() - y * scaley + m_visibleChartArea.m_minY * scaley);
37 }
33 }
38 }
34
39
35 void QScatterSeriesPrivate::setSize(const QSize &size)
40 void QScatterSeriesPrivate::setSize(const QSize &size)
36 {
41 {
37 QGraphicsItem *parent = this->parentItem();
42 // m_boundingRect = QRectF(pos().x(), pos().y(), size.width(), size.height());
38 if (parent)
43 m_boundingRect = QRectF(0, 0, size.width(), size.height());
39 resize(QRectF(parent->pos(), size));
44 changeGeometry();
40 }
45 }
41
46
42 void QScatterSeriesPrivate::themeChanged(ChartTheme *theme)
47 void QScatterSeriesPrivate::themeChanged(ChartTheme *theme)
@@ -47,12 +52,12 void QScatterSeriesPrivate::themeChanged(ChartTheme *theme)
47 void QScatterSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain)
52 void QScatterSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain)
48 {
53 {
49 m_visibleChartArea = plotDomain;
54 m_visibleChartArea = plotDomain;
50 resize(parentItem()->boundingRect());
55 changeGeometry();
51 }
56 }
52
57
53 QRectF QScatterSeriesPrivate::boundingRect() const
58 QRectF QScatterSeriesPrivate::boundingRect() const
54 {
59 {
55 return QRectF(0, 0, 55, 100);
60 return m_boundingRect;
56 }
61 }
57
62
58 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
63 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
@@ -86,16 +91,26 QScatterSeries::QScatterSeries(QObject *parent) :
86 {
91 {
87 }
92 }
88
93
89 bool QScatterSeries::setData(QList<qreal> x, QList<qreal> y)
94 bool QScatterSeries::setData(QList<qreal> xlist, QList<qreal> ylist)
90 {
95 {
91 // TODO: validate data
96 // TODO: validate data
92 d->m_x = x;
97 d->m_x = xlist;
93 d->m_y = y;
98 d->m_y = ylist;
94 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
99
95 Q_ASSERT(parentItem);
100 // TODO: the following updates the visible chart area setting of the series, we would instead
96 // d->setPos(parentItem->pos());
101 // need to update the _chart's_ visible area... this would require a callback or
97 // d->setSize(parentItem->boundingRect().size().toSize());
102 // similar to the parenting QChart object...
98 d->resize(parentItem->boundingRect());
103 foreach (qreal x, d->m_x) {
104 d->m_visibleChartArea.m_minX = qMin(d->m_visibleChartArea.m_minX, x);
105 d->m_visibleChartArea.m_maxX = qMax(d->m_visibleChartArea.m_maxX, x);
106 }
107 foreach (qreal y, d->m_y) {
108 d->m_visibleChartArea.m_minY = qMin(d->m_visibleChartArea.m_minY, y);
109 d->m_visibleChartArea.m_maxY = qMax(d->m_visibleChartArea.m_maxY, y);
110 }
111
112 d->changeGeometry();
113
99 return true;
114 return true;
100 }
115 }
101
116
@@ -29,13 +29,12 public: // from QGraphicsItem
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30
30
31 public:
31 public:
32 void resize(QRectF rect); // TODO: replace with setSize
32 void changeGeometry();
33
33
34 QRectF m_boundingRect;
34 // TODO: use the chart data class instead of list of x and y values?
35 // TODO: use the chart data class instead of list of x and y values?
35 QList<qreal> m_x;
36 QList<qreal> m_x;
36 QList<qreal> m_y;
37 QList<qreal> m_y;
37 qreal m_scalex;
38 qreal m_scaley;
39 QList<qreal> m_scenex;
38 QList<qreal> m_scenex;
40 QList<qreal> m_sceney;
39 QList<qreal> m_sceney;
41 QColor m_markerColor;
40 QColor m_markerColor;
@@ -25,22 +25,24 MainWidget::MainWidget(QWidget *parent) :
25 {
25 {
26 m_chartWidget = new QChartWidget(this);
26 m_chartWidget = new QChartWidget(this);
27
27
28 // GridLayout for the controls for configuring the chart widget
28 // Grid layout for the controls for configuring the chart widget
29 QGridLayout *grid = new QGridLayout();
29 QGridLayout *grid = new QGridLayout();
30 QGridLayout *mainLayout = new QGridLayout();
31 QPushButton *addSeriesButton = new QPushButton("Add series");
30 QPushButton *addSeriesButton = new QPushButton("Add series");
32 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
31 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
33 grid->addWidget(addSeriesButton, 0, 1);
32 grid->addWidget(addSeriesButton, 0, 1);
34 initBackroundCombo(grid);
33 initBackroundCombo(grid);
35 initScaleControls(grid);
34 initScaleControls(grid);
36 initThemeCombo(grid);
35 initThemeCombo(grid);
37 QCheckBox *zoomCheckBox = new QCheckBox("Zoom enabled");
36 QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom");
38 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
37 connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool)));
39 zoomCheckBox->setChecked(true);
38 zoomCheckBox->setChecked(true);
40 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
39 grid->addWidget(zoomCheckBox, grid->rowCount(), 0);
41 // add row with empty label to make all the other rows static
40 // add row with empty label to make all the other rows static
42 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
41 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
43 grid->setRowStretch(grid->rowCount() - 1, 1);
42 grid->setRowStretch(grid->rowCount() - 1, 1);
43
44 // Another grid layout as a main layout
45 QGridLayout *mainLayout = new QGridLayout();
44 mainLayout->addLayout(grid, 0, 0);
46 mainLayout->addLayout(grid, 0, 0);
45
47
46 // Init series type specific controls
48 // Init series type specific controls
General Comments 0
You need to be logged in to leave comments. Login now