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