diff --git a/src/charts/axis/colorbaraxis/chartcolorbaraxisy.cpp b/src/charts/axis/colorbaraxis/chartcolorbaraxisy.cpp --- a/src/charts/axis/colorbaraxis/chartcolorbaraxisy.cpp +++ b/src/charts/axis/colorbaraxis/chartcolorbaraxisy.cpp @@ -23,6 +23,7 @@ ChartColorBarAxisY::ChartColorBarAxisY(Q QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString))); createColorBar(); + } ChartColorBarAxisY::~ChartColorBarAxisY() @@ -124,25 +125,32 @@ QSizeF ChartColorBarAxisY::sizeHint(Qt:: void ChartColorBarAxisY::createColorBar() { - //m_position.setX(80+m_position.x());// TODO : remove this when you know how to move labels + m_axis->setLabelsColor(QColor(0,0,255,0)); //make automatic labels disappear without changing layout (as it would be the case with setlabelVisible(false)) //gradient QGradientStops stops = m_gradient.stops(); - QLinearGradient gradient(0,0,1,m_height); + QLinearGradient gradient(0,m_position.y(),1,m_height); foreach(QGradientStop stop, stops) { gradient.setColorAt(1-stop.first,stop.second); } //colorbar - QRectF rect(m_position.x(),m_position.y(),m_height/20,m_height); + QRectF rect(m_position.x()+10,m_position.y(),m_height/20,m_height); //remove 10 if needed QGraphicsRectItem *colorbar = new QGraphicsRectItem(rect, this); colorbar->setZValue(ChartPresenter::AxisZValue); colorbar->setBrush(gradient); - setGraphicsItem(colorbar); + + for(int i=0;itickCount();i++) + { + qreal value = m_axis->max() - (i * (m_axis->max() - m_axis->min()) / (m_axis->tickCount() - 1)); + QGraphicsTextItem *label = new QGraphicsTextItem(this); + label->setPlainText(QString::number(value)); + label->setPos((m_position.x()+10+m_height/20+1),(m_position.y()*0.85+i*(m_height/(m_axis->tickCount()-1)))); //remove 10 if needed + } } #include "moc_chartcolorbaraxisy_p.cpp" - QT_CHARTS_END_NAMESPACE +QT_CHARTS_END_NAMESPACE diff --git a/src/charts/colormapchart/colormapchart.cpp b/src/charts/colormapchart/colormapchart.cpp --- a/src/charts/colormapchart/colormapchart.cpp +++ b/src/charts/colormapchart/colormapchart.cpp @@ -18,7 +18,10 @@ ColorMapChart::ColorMapChart(QColorMapSe m_series(series), m_dirty(true), m_gradientType(Rainbow), - m_colorbar(false) + m_colorbar(Q_NULLPTR), + m_isColorBarDrawn(false), + m_currentplotArea(QRectF()), + m_grid(Q_NULLPTR) { // QObject::connect(series, SIGNAL(pointReplaced(int)), this, SLOT(handlePointReplaced(int))); // QObject::connect(series, SIGNAL(pointsReplaced()), this, SLOT(handlePointsReplaced())); @@ -38,6 +41,12 @@ ColorMapChart::ColorMapChart(QColorMapSe populateColorTable(); } +ColorMapChart::~ColorMapChart() +{ + delete m_colorTable; + delete m_colorbar; +} + void ColorMapChart::setDirty(bool dirty) { m_dirty = dirty; @@ -61,18 +70,19 @@ void ColorMapChart::paint(QPainter *pain painter->setClipRect(clipRect); QRectF plotAreaRect = m_series->chart()->plotArea(); + if(m_currentplotArea !=plotAreaRect) + { + m_currentplotArea = plotAreaRect; + + m_grid = m_series->getUniformGrid(0,0,plotAreaRect.width(),plotAreaRect.height(),QColorMapSeries::LastPixel); + + addColorBar(plotAreaRect); + } + QImage colorMapImage(plotAreaRect.width(),plotAreaRect.height(),QImage::Format_RGB32); //http://doc.qt.io/qt-4.8/qimage.html#details :Warning: This will create a QImage with uninitialized data. Call fill() to fill the image with an appropriate pixel value before drawing onto it with QPainter. colorMapImage.fill(QColor(Qt::white).rgb()); - QElapsedTimer timer; - timer.start(); - - QVector * grid = m_series->getUniformGrid(0,0,plotAreaRect.width(),plotAreaRect.height(),QColorMapSeries::LastPixel); - - int msec = timer.elapsed(); - int msec2 = 0+msec; - double maxZ = m_series->maxZ(); double minZ = m_series->minZ(); double rangeZ = maxZ - minZ; @@ -84,22 +94,18 @@ void ColorMapChart::paint(QPainter *pain { for(int j=0;jat(i+j*(imageWidth)); + double value = m_grid->at(i+j*(imageWidth)); double pix=((value-minZ)/rangeZ); int indexInColorTable = pix*(nbOfColors-1); colorMapImage.setPixel(i,j,m_colorTable->at(indexInColorTable)); } } - addColorBar(plotAreaRect); - - painter->drawImage(clipRect,colorMapImage); - update();//Qt docs: Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion + //This line was causing re-painting loop + //update();//Qt docs: Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion painter->restore(); - - } void ColorMapChart::addColorBar(QRectF plotAreaRect) diff --git a/src/charts/colormapchart/colormapchart_p.h b/src/charts/colormapchart/colormapchart_p.h --- a/src/charts/colormapchart/colormapchart_p.h +++ b/src/charts/colormapchart/colormapchart_p.h @@ -29,7 +29,7 @@ public: }; explicit ColorMapChart(QColorMapSeries *series,QGraphicsItem *item = 0); - ~ColorMapChart() {} + ~ColorMapChart(); bool isDirty() const { return m_dirty; } void setDirty(bool dirty); @@ -72,6 +72,8 @@ protected: GradientType m_gradientType; bool m_isColorBarDrawn; QColorBarAxis *m_colorbar; + QRectF m_currentplotArea; + QVector * m_grid; }; //class PixmapMarker: public QGraphicsRectItem