##// END OF EJS Templates
Adds draft of axis bar label support
Michal Klocek -
r497:9455cddd70de
parent child
Show More
@@ -25,6 +25,7 m_axis(parent)
25 setFlags(QGraphicsItem::ItemHasNoContents);
25 setFlags(QGraphicsItem::ItemHasNoContents);
26
26
27 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
27 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
28 QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
28 }
29 }
29
30
30 AxisItem::~AxisItem()
31 AxisItem::~AxisItem()
@@ -95,21 +96,19 void AxisItem::updateItem()
95
96
96 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
97 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
97 {
98 {
98
99
100 Q_ASSERT(max>=min);
99 Q_ASSERT(max>=min);
101
100
102 QStringList labels;
101 QStringList labels;
103
102
104 //int ticks = axis->ticksCount()-1;
103 QChartAxisCategories* categories = m_chartAxis->categories();
105
104
106 for(int i=0; i<= ticks; i++) {
105 for(int i=0; i<= ticks; i++) {
107 qreal value = min + (i * (max - min)/ ticks);
106 qreal value = min + (i * (max - min)/ ticks);
108 QString label ;//= axis->axisTickLabel(value);
107 if(categories->count()==0) {
109 if(label.isEmpty()) {
110 labels << QString::number(value);
108 labels << QString::number(value);
111 }
109 }
112 else {
110 else {
111 QString label = categories->label(value);
113 labels << label;
112 labels << label;
114 }
113 }
115 }
114 }
@@ -270,7 +269,7 void AxisItem::setLayout(const QVector<qreal>& layout)
270 QPointF center = labelItem->boundingRect().center();
269 QPointF center = labelItem->boundingRect().center();
271 labelItem->setTransformOriginPoint(center.x(), center.y());
270 labelItem->setTransformOriginPoint(center.x(), center.y());
272 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
271 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
273 if(i%2){
272 if(i%2 && i+1 < layout.size()){
274 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
273 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
275 rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height());
274 rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height());
276 }
275 }
@@ -293,7 +292,7 void AxisItem::setLayout(const QVector<qreal>& layout)
293 QPointF center = labelItem->boundingRect().center();
292 QPointF center = labelItem->boundingRect().center();
294 labelItem->setTransformOriginPoint(center.x(), center.y());
293 labelItem->setTransformOriginPoint(center.x(), center.y());
295 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
294 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
296 if(i%2){
295 if(i%2 && i+1 < layout.size()){
297 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
296 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
298 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]);
297 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]);
299 }
298 }
@@ -312,8 +311,21 void AxisItem::setLayout(const QVector<qreal>& layout)
312
311
313 //handlers
312 //handlers
314
313
314 void AxisItem::handleAxisCategoriesUpdated()
315 {
316 updateItem();
317 }
318
315 void AxisItem::handleAxisUpdated()
319 void AxisItem::handleAxisUpdated()
316 {
320 {
321
322 int count = m_chartAxis->ticksCount();
323
324 if(m_ticks!=count){
325 m_ticks=count;
326 updateItem();
327 }
328
317 if(isEmpty()) return;
329 if(isEmpty()) return;
318
330
319 if(m_chartAxis->isAxisVisible()) {
331 if(m_chartAxis->isAxisVisible()) {
@@ -367,14 +379,6 void AxisItem::handleRangeChanged(qreal min, qreal max)
367
379
368 }
380 }
369
381
370 void AxisItem::handleTicksCountChanged(int ticks)
371 {
372 m_ticks=ticks;
373
374 if(isEmpty()) return;
375 updateItem();
376 }
377
378 void AxisItem::handleGeometryChanged(const QRectF& rect)
382 void AxisItem::handleGeometryChanged(const QRectF& rect)
379 {
383 {
380 m_rect = rect;
384 m_rect = rect;
@@ -51,8 +51,8 public:
51
51
52 public slots:
52 public slots:
53 void handleAxisUpdated();//qchartaxis update calls
53 void handleAxisUpdated();//qchartaxis update calls
54 void handleAxisCategoriesUpdated();//qchartaxis update calls
54 void handleRangeChanged(qreal min , qreal max); //domain update calls
55 void handleRangeChanged(qreal min , qreal max); //domain update calls
55 void handleTicksCountChanged(int ticks); //ticks changed
56 void handleGeometryChanged(const QRectF& size); //geometry update calls
56 void handleGeometryChanged(const QRectF& size); //geometry update calls
57
57
58 public:
58 public:
@@ -66,6 +66,7 private:
66 void clear(int count);
66 void clear(int count);
67 void createItems(int count);
67 void createItems(int count);
68 QStringList createLabels(int ticks, qreal min, qreal max);
68 QStringList createLabels(int ticks, qreal min, qreal max);
69
69 private:
70 private:
70 QChartAxis* m_chartAxis;
71 QChartAxis* m_chartAxis;
71 AxisType m_type;
72 AxisType m_type;
@@ -60,7 +60,7 public:
60 void setTicksCount(int count);
60 void setTicksCount(int count);
61 int ticksCount() const { return m_ticksCount;}
61 int ticksCount() const { return m_ticksCount;}
62
62
63 QChartAxisCategories& categories() { return m_category; }
63 QChartAxisCategories* categories() { return &m_category; }
64
64
65 signals:
65 signals:
66 void minChanged(qreal min);
66 void minChanged(qreal min);
@@ -34,6 +34,11 int QChartAxisCategories::count()
34 emit updated();
34 emit updated();
35 }
35 }
36
36
37 QString QChartAxisCategories::label(qreal value) const
38 {
39 return m_map.value(value);
40 }
41
37 #include "moc_qchartaxiscategories.cpp"
42 #include "moc_qchartaxiscategories.cpp"
38
43
39 QTCOMMERCIALCHART_END_NAMESPACE
44 QTCOMMERCIALCHART_END_NAMESPACE
@@ -14,6 +14,7 public:
14
14
15 void insert(qreal value,QString label);
15 void insert(qreal value,QString label);
16 void remove(qreal value);
16 void remove(qreal value);
17 QString label(qreal value) const;
17 void clear();
18 void clear();
18 int count();
19 int count();
19
20
@@ -100,21 +100,21 void BarPresenterBase::initAxisLabels()
100 if (0 == count) {
100 if (0 == count) {
101 return;
101 return;
102 }
102 }
103 count++;
103
104
104 mChart->axisX()->setTicksCount(count);
105 mChart->axisX()->setTicksCount(count);
105
106
106 qreal min = 0;
107 qreal min = 0;
107 qreal max = mSeries->categoryCount();
108 qreal max = count;
108
109
109 mChart->axisX()->setMin(min);
110 mChart->axisX()->setMin(min);
110 mChart->axisX()->setMax(max);
111 mChart->axisX()->setMax(max);
111 qreal step = (max-min)/count;
112 min++;
112 QChartAxisCategories& categories = mChart->axisX()->categories();
113 QChartAxisCategories* categories = mChart->axisX()->categories();
113 categories.clear();
114 categories->clear();
114 for (int i=0; i<count; i++) {
115 for (int i=0; i<count-1; i++) {
115 qDebug() << "initAxisLabels" << min << mSeries->categoryName(i);
116 categories->insert(min,mSeries->categoryName(i));
116 categories.insert(min,mSeries->categoryName(i));
117 min++;
117 min += step;
118 }
118 }
119 mChart->axisX()->setLabelsVisible(true);
119 mChart->axisX()->setLabelsVisible(true);
120 }
120 }
@@ -109,13 +109,12 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
109 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
109 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
110 //initialize
110 //initialize
111 item->handleRangeChanged(domain->minX(),domain->maxX());
111 item->handleRangeChanged(domain->minX(),domain->maxX());
112 item->handleTicksCountChanged(4);
113 }
112 }
114 else{
113 else{
115 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
114 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
116 //initialize
115 //initialize
117 item->handleRangeChanged(domain->minY(),domain->maxY());
116 item->handleRangeChanged(domain->minY(),domain->maxY());
118 item->handleTicksCountChanged(4);
117
119 }
118 }
120
119
121 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
General Comments 0
You need to be logged in to leave comments. Login now