| 1 | /******************************************************************************
|
|---|
| 2 | * Qwt Widget Library
|
|---|
| 3 | * Copyright (C) 1997 Josef Wilgen
|
|---|
| 4 | * Copyright (C) 2002 Uwe Rathmann
|
|---|
| 5 | *
|
|---|
| 6 | * This library is free software; you can redistribute it and/or
|
|---|
| 7 | * modify it under the terms of the Qwt License, Version 1.0
|
|---|
| 8 | *****************************************************************************/
|
|---|
| 9 |
|
|---|
| 10 | #include "qwt_plot_multi_barchart.h"
|
|---|
| 11 | #include "qwt_scale_map.h"
|
|---|
| 12 | #include "qwt_column_symbol.h"
|
|---|
| 13 | #include "qwt_text.h"
|
|---|
| 14 | #include "qwt_graphic.h"
|
|---|
| 15 | #include "qwt_legend_data.h"
|
|---|
| 16 | #include "qwt_math.h"
|
|---|
| 17 |
|
|---|
| 18 | #include <qmap.h>
|
|---|
| 19 |
|
|---|
| 20 | inline static bool qwtIsIncreasing(
|
|---|
| 21 | const QwtScaleMap& map, const QVector< double >& values )
|
|---|
| 22 | {
|
|---|
| 23 | bool isInverting = map.isInverting();
|
|---|
| 24 |
|
|---|
| 25 | for ( int i = 0; i < values.size(); i++ )
|
|---|
| 26 | {
|
|---|
| 27 | const double y = values[ i ];
|
|---|
| 28 | if ( y != 0.0 )
|
|---|
| 29 | return ( map.isInverting() != ( y > 0.0 ) );
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | return !isInverting;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | class QwtPlotMultiBarChart::PrivateData
|
|---|
| 36 | {
|
|---|
| 37 | public:
|
|---|
| 38 | PrivateData()
|
|---|
| 39 | : style( QwtPlotMultiBarChart::Grouped )
|
|---|
| 40 | {
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | QwtPlotMultiBarChart::ChartStyle style;
|
|---|
| 44 | QList< QwtText > barTitles;
|
|---|
| 45 | QMap< int, QwtColumnSymbol* > symbolMap;
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | /*!
|
|---|
| 49 | Constructor
|
|---|
| 50 | \param title Title of the chart
|
|---|
| 51 | */
|
|---|
| 52 | QwtPlotMultiBarChart::QwtPlotMultiBarChart( const QwtText& title )
|
|---|
| 53 | : QwtPlotAbstractBarChart( title )
|
|---|
| 54 | {
|
|---|
| 55 | init();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /*!
|
|---|
| 59 | Constructor
|
|---|
| 60 | \param title Title of the chart
|
|---|
| 61 | */
|
|---|
| 62 | QwtPlotMultiBarChart::QwtPlotMultiBarChart( const QString& title )
|
|---|
| 63 | : QwtPlotAbstractBarChart( QwtText( title ) )
|
|---|
| 64 | {
|
|---|
| 65 | init();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | //! Destructor
|
|---|
| 69 | QwtPlotMultiBarChart::~QwtPlotMultiBarChart()
|
|---|
| 70 | {
|
|---|
| 71 | resetSymbolMap();
|
|---|
| 72 | delete m_data;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | void QwtPlotMultiBarChart::init()
|
|---|
| 76 | {
|
|---|
| 77 | m_data = new PrivateData;
|
|---|
| 78 | setData( new QwtSetSeriesData() );
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | //! \return QwtPlotItem::Rtti_PlotBarChart
|
|---|
| 82 | int QwtPlotMultiBarChart::rtti() const
|
|---|
| 83 | {
|
|---|
| 84 | return QwtPlotItem::Rtti_PlotMultiBarChart;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*!
|
|---|
| 88 | Initialize data with an array of samples.
|
|---|
| 89 | \param samples Vector of points
|
|---|
| 90 | */
|
|---|
| 91 | void QwtPlotMultiBarChart::setSamples(
|
|---|
| 92 | const QVector< QwtSetSample >& samples )
|
|---|
| 93 | {
|
|---|
| 94 | setData( new QwtSetSeriesData( samples ) );
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*!
|
|---|
| 98 | Initialize data with an array of samples.
|
|---|
| 99 | \param samples Vector of points
|
|---|
| 100 | */
|
|---|
| 101 | void QwtPlotMultiBarChart::setSamples(
|
|---|
| 102 | const QVector< QVector< double > >& samples )
|
|---|
| 103 | {
|
|---|
| 104 | QVector< QwtSetSample > s;
|
|---|
| 105 | s.reserve( samples.size() );
|
|---|
| 106 |
|
|---|
| 107 | for ( int i = 0; i < samples.size(); i++ )
|
|---|
| 108 | s += QwtSetSample( i, samples[ i ] );
|
|---|
| 109 |
|
|---|
| 110 | setData( new QwtSetSeriesData( s ) );
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /*!
|
|---|
| 114 | Assign a series of samples
|
|---|
| 115 |
|
|---|
| 116 | setSamples() is just a wrapper for setData() without any additional
|
|---|
| 117 | value - beside that it is easier to find for the developer.
|
|---|
| 118 |
|
|---|
| 119 | \param data Data
|
|---|
| 120 | \warning The item takes ownership of the data object, deleting
|
|---|
| 121 | it when its not used anymore.
|
|---|
| 122 | */
|
|---|
| 123 | void QwtPlotMultiBarChart::setSamples(
|
|---|
| 124 | QwtSeriesData< QwtSetSample >* data )
|
|---|
| 125 | {
|
|---|
| 126 | setData( data );
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /*!
|
|---|
| 130 | \brief Set the titles for the bars
|
|---|
| 131 |
|
|---|
| 132 | The titles are used for the legend.
|
|---|
| 133 |
|
|---|
| 134 | \param titles Bar titles
|
|---|
| 135 |
|
|---|
| 136 | \sa barTitles(), legendData()
|
|---|
| 137 | */
|
|---|
| 138 | void QwtPlotMultiBarChart::setBarTitles( const QList< QwtText >& titles )
|
|---|
| 139 | {
|
|---|
| 140 | m_data->barTitles = titles;
|
|---|
| 141 | itemChanged();
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*!
|
|---|
| 145 | \return Bar titles
|
|---|
| 146 | \sa setBarTitles(), legendData()
|
|---|
| 147 | */
|
|---|
| 148 | QList< QwtText > QwtPlotMultiBarChart::barTitles() const
|
|---|
| 149 | {
|
|---|
| 150 | return m_data->barTitles;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /*!
|
|---|
| 154 | \brief Add a symbol to the symbol map
|
|---|
| 155 |
|
|---|
| 156 | Assign a default symbol for drawing the bar representing all values
|
|---|
| 157 | with the same index in a set.
|
|---|
| 158 |
|
|---|
| 159 | \param valueIndex Index of a value in a set
|
|---|
| 160 | \param symbol Symbol used for drawing a bar
|
|---|
| 161 |
|
|---|
| 162 | \sa symbol(), resetSymbolMap(), specialSymbol()
|
|---|
| 163 | */
|
|---|
| 164 | void QwtPlotMultiBarChart::setSymbol( int valueIndex, QwtColumnSymbol* symbol )
|
|---|
| 165 | {
|
|---|
| 166 | if ( valueIndex < 0 )
|
|---|
| 167 | return;
|
|---|
| 168 |
|
|---|
| 169 | QMap< int, QwtColumnSymbol* >::iterator it =
|
|---|
| 170 | m_data->symbolMap.find(valueIndex);
|
|---|
| 171 | if ( it == m_data->symbolMap.end() )
|
|---|
| 172 | {
|
|---|
| 173 | if ( symbol != NULL )
|
|---|
| 174 | {
|
|---|
| 175 | m_data->symbolMap.insert( valueIndex, symbol );
|
|---|
| 176 |
|
|---|
| 177 | legendChanged();
|
|---|
| 178 | itemChanged();
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 | else
|
|---|
| 182 | {
|
|---|
| 183 | if ( symbol != it.value() )
|
|---|
| 184 | {
|
|---|
| 185 | delete it.value();
|
|---|
| 186 |
|
|---|
| 187 | if ( symbol == NULL )
|
|---|
| 188 | {
|
|---|
| 189 | m_data->symbolMap.remove( valueIndex );
|
|---|
| 190 | }
|
|---|
| 191 | else
|
|---|
| 192 | {
|
|---|
| 193 | it.value() = symbol;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | legendChanged();
|
|---|
| 197 | itemChanged();
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /*!
|
|---|
| 203 | Find a symbol in the symbol map
|
|---|
| 204 |
|
|---|
| 205 | \param valueIndex Index of a value in a set
|
|---|
| 206 | \return The symbol, that had been set by setSymbol() or NULL.
|
|---|
| 207 |
|
|---|
| 208 | \sa setSymbol(), specialSymbol(), drawBar()
|
|---|
| 209 | */
|
|---|
| 210 | const QwtColumnSymbol* QwtPlotMultiBarChart::symbol( int valueIndex ) const
|
|---|
| 211 | {
|
|---|
| 212 | QMap< int, QwtColumnSymbol* >::const_iterator it =
|
|---|
| 213 | m_data->symbolMap.constFind( valueIndex );
|
|---|
| 214 |
|
|---|
| 215 | return ( it == m_data->symbolMap.constEnd() ) ? NULL : it.value();
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /*!
|
|---|
| 219 | Find a symbol in the symbol map
|
|---|
| 220 |
|
|---|
| 221 | \param valueIndex Index of a value in a set
|
|---|
| 222 | \return The symbol, that had been set by setSymbol() or NULL.
|
|---|
| 223 |
|
|---|
| 224 | \sa setSymbol(), specialSymbol(), drawBar()
|
|---|
| 225 | */
|
|---|
| 226 | QwtColumnSymbol* QwtPlotMultiBarChart::symbol( int valueIndex )
|
|---|
| 227 | {
|
|---|
| 228 | QMap< int, QwtColumnSymbol* >::const_iterator it =
|
|---|
| 229 | m_data->symbolMap.constFind( valueIndex );
|
|---|
| 230 |
|
|---|
| 231 | return ( it == m_data->symbolMap.constEnd() ) ? NULL : it.value();
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /*!
|
|---|
| 235 | Remove all symbols from the symbol map
|
|---|
| 236 | */
|
|---|
| 237 | void QwtPlotMultiBarChart::resetSymbolMap()
|
|---|
| 238 | {
|
|---|
| 239 | qDeleteAll( m_data->symbolMap );
|
|---|
| 240 | m_data->symbolMap.clear();
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /*!
|
|---|
| 244 | \brief Create a symbol for special values
|
|---|
| 245 |
|
|---|
| 246 | Usually the symbols for displaying a bar are set by setSymbols() and
|
|---|
| 247 | common for all sets. By overloading specialSymbol() it is possible to
|
|---|
| 248 | create a temporary symbol() for displaying a special value.
|
|---|
| 249 |
|
|---|
| 250 | The symbol has to be created by new each time specialSymbol() is
|
|---|
| 251 | called. As soon as the symbol is painted this symbol gets deleted.
|
|---|
| 252 |
|
|---|
| 253 | When no symbol ( NULL ) is returned, the value will be displayed
|
|---|
| 254 | with the standard symbol that is used for all symbols with the same
|
|---|
| 255 | valueIndex.
|
|---|
| 256 |
|
|---|
| 257 | \param sampleIndex Index of the sample
|
|---|
| 258 | \param valueIndex Index of the value in the set
|
|---|
| 259 |
|
|---|
| 260 | \return NULL, meaning that the value is not special
|
|---|
| 261 |
|
|---|
| 262 | */
|
|---|
| 263 | QwtColumnSymbol* QwtPlotMultiBarChart::specialSymbol(
|
|---|
| 264 | int sampleIndex, int valueIndex ) const
|
|---|
| 265 | {
|
|---|
| 266 | Q_UNUSED( sampleIndex );
|
|---|
| 267 | Q_UNUSED( valueIndex );
|
|---|
| 268 |
|
|---|
| 269 | return NULL;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | /*!
|
|---|
| 273 | Set the style of the chart
|
|---|
| 274 |
|
|---|
| 275 | \param style Chart style
|
|---|
| 276 | \sa style()
|
|---|
| 277 | */
|
|---|
| 278 | void QwtPlotMultiBarChart::setStyle( ChartStyle style )
|
|---|
| 279 | {
|
|---|
| 280 | if ( style != m_data->style )
|
|---|
| 281 | {
|
|---|
| 282 | m_data->style = style;
|
|---|
| 283 |
|
|---|
| 284 | legendChanged();
|
|---|
| 285 | itemChanged();
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /*!
|
|---|
| 290 | \return Style of the chart
|
|---|
| 291 | \sa setStyle()
|
|---|
| 292 | */
|
|---|
| 293 | QwtPlotMultiBarChart::ChartStyle QwtPlotMultiBarChart::style() const
|
|---|
| 294 | {
|
|---|
| 295 | return m_data->style;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /*!
|
|---|
| 299 | \return Bounding rectangle of all samples.
|
|---|
| 300 | For an empty series the rectangle is invalid.
|
|---|
| 301 | */
|
|---|
| 302 | QRectF QwtPlotMultiBarChart::boundingRect() const
|
|---|
| 303 | {
|
|---|
| 304 | const size_t numSamples = dataSize();
|
|---|
| 305 |
|
|---|
| 306 | if ( numSamples == 0 )
|
|---|
| 307 | return QwtPlotSeriesItem::boundingRect();
|
|---|
| 308 |
|
|---|
| 309 | const double baseLine = baseline();
|
|---|
| 310 |
|
|---|
| 311 | QRectF rect;
|
|---|
| 312 |
|
|---|
| 313 | if ( m_data->style != QwtPlotMultiBarChart::Stacked )
|
|---|
| 314 | {
|
|---|
| 315 | rect = QwtPlotSeriesItem::boundingRect();
|
|---|
| 316 |
|
|---|
| 317 | if ( rect.height() >= 0 )
|
|---|
| 318 | {
|
|---|
| 319 | if ( rect.bottom() < baseLine )
|
|---|
| 320 | rect.setBottom( baseLine );
|
|---|
| 321 | if ( rect.top() > baseLine )
|
|---|
| 322 | rect.setTop( baseLine );
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | else
|
|---|
| 326 | {
|
|---|
| 327 | double xMin, xMax, yMin, yMax;
|
|---|
| 328 |
|
|---|
| 329 | xMin = xMax = 0.0;
|
|---|
| 330 | yMin = yMax = baseLine;
|
|---|
| 331 |
|
|---|
| 332 | const QwtSeriesData< QwtSetSample >* series = data();
|
|---|
| 333 |
|
|---|
| 334 | for ( size_t i = 0; i < numSamples; i++ )
|
|---|
| 335 | {
|
|---|
| 336 | const QwtSetSample sample = series->sample( i );
|
|---|
| 337 | if ( i == 0 )
|
|---|
| 338 | {
|
|---|
| 339 | xMin = xMax = sample.value;
|
|---|
| 340 | }
|
|---|
| 341 | else
|
|---|
| 342 | {
|
|---|
| 343 | xMin = qwtMinF( xMin, sample.value );
|
|---|
| 344 | xMax = qwtMaxF( xMax, sample.value );
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | const double y = baseLine + sample.added();
|
|---|
| 348 |
|
|---|
| 349 | yMin = qwtMinF( yMin, y );
|
|---|
| 350 | yMax = qwtMaxF( yMax, y );
|
|---|
| 351 | }
|
|---|
| 352 | rect.setRect( xMin, yMin, xMax - xMin, yMax - yMin );
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | if ( orientation() == Qt::Horizontal )
|
|---|
| 356 | rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );
|
|---|
| 357 |
|
|---|
| 358 | return rect;
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | /*!
|
|---|
| 362 | Draw an interval of the bar chart
|
|---|
| 363 |
|
|---|
| 364 | \param painter Painter
|
|---|
| 365 | \param xMap Maps x-values into pixel coordinates.
|
|---|
| 366 | \param yMap Maps y-values into pixel coordinates.
|
|---|
| 367 | \param canvasRect Contents rectangle of the canvas
|
|---|
| 368 | \param from Index of the first point to be painted
|
|---|
| 369 | \param to Index of the last point to be painted. If to < 0 the
|
|---|
| 370 | curve will be painted to its last point.
|
|---|
| 371 |
|
|---|
| 372 | \sa drawSymbols()
|
|---|
| 373 | */
|
|---|
| 374 | void QwtPlotMultiBarChart::drawSeries( QPainter* painter,
|
|---|
| 375 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 376 | const QRectF& canvasRect, int from, int to ) const
|
|---|
| 377 | {
|
|---|
| 378 | if ( to < 0 )
|
|---|
| 379 | to = dataSize() - 1;
|
|---|
| 380 |
|
|---|
| 381 | if ( from < 0 )
|
|---|
| 382 | from = 0;
|
|---|
| 383 |
|
|---|
| 384 | if ( from > to )
|
|---|
| 385 | return;
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 | const QRectF br = data()->boundingRect();
|
|---|
| 389 | const QwtInterval interval( br.left(), br.right() );
|
|---|
| 390 |
|
|---|
| 391 | painter->save();
|
|---|
| 392 |
|
|---|
| 393 | for ( int i = from; i <= to; i++ )
|
|---|
| 394 | {
|
|---|
| 395 | drawSample( painter, xMap, yMap,
|
|---|
| 396 | canvasRect, interval, i, sample( i ) );
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | painter->restore();
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | /*!
|
|---|
| 403 | Draw a sample
|
|---|
| 404 |
|
|---|
| 405 | \param painter Painter
|
|---|
| 406 | \param xMap x map
|
|---|
| 407 | \param yMap y map
|
|---|
| 408 | \param canvasRect Contents rectangle of the canvas
|
|---|
| 409 | \param boundingInterval Bounding interval of sample values
|
|---|
| 410 | \param index Index of the sample to be painted
|
|---|
| 411 | \param sample Sample value
|
|---|
| 412 |
|
|---|
| 413 | \sa drawSeries()
|
|---|
| 414 | */
|
|---|
| 415 | void QwtPlotMultiBarChart::drawSample( QPainter* painter,
|
|---|
| 416 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 417 | const QRectF& canvasRect, const QwtInterval& boundingInterval,
|
|---|
| 418 | int index, const QwtSetSample& sample ) const
|
|---|
| 419 | {
|
|---|
| 420 | if ( sample.set.size() <= 0 )
|
|---|
| 421 | return;
|
|---|
| 422 |
|
|---|
| 423 | double sampleW;
|
|---|
| 424 |
|
|---|
| 425 | if ( orientation() == Qt::Horizontal )
|
|---|
| 426 | {
|
|---|
| 427 | sampleW = sampleWidth( yMap, canvasRect.height(),
|
|---|
| 428 | boundingInterval.width(), sample.value );
|
|---|
| 429 | }
|
|---|
| 430 | else
|
|---|
| 431 | {
|
|---|
| 432 | sampleW = sampleWidth( xMap, canvasRect.width(),
|
|---|
| 433 | boundingInterval.width(), sample.value );
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | if ( m_data->style == Stacked )
|
|---|
| 437 | {
|
|---|
| 438 | drawStackedBars( painter, xMap, yMap,
|
|---|
| 439 | canvasRect, index, sampleW, sample );
|
|---|
| 440 | }
|
|---|
| 441 | else
|
|---|
| 442 | {
|
|---|
| 443 | drawGroupedBars( painter, xMap, yMap,
|
|---|
| 444 | canvasRect, index, sampleW, sample );
|
|---|
| 445 | }
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | /*!
|
|---|
| 449 | Draw a grouped sample
|
|---|
| 450 |
|
|---|
| 451 | \param painter Painter
|
|---|
| 452 | \param xMap x map
|
|---|
| 453 | \param yMap y map
|
|---|
| 454 | \param canvasRect Contents rectangle of the canvas
|
|---|
| 455 | \param index Index of the sample to be painted
|
|---|
| 456 | \param sampleWidth Bounding width for all bars of the sample
|
|---|
| 457 | \param sample Sample
|
|---|
| 458 |
|
|---|
| 459 | \sa drawSeries(), sampleWidth()
|
|---|
| 460 | */
|
|---|
| 461 | void QwtPlotMultiBarChart::drawGroupedBars( QPainter* painter,
|
|---|
| 462 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 463 | const QRectF& canvasRect, int index, double sampleWidth,
|
|---|
| 464 | const QwtSetSample& sample ) const
|
|---|
| 465 | {
|
|---|
| 466 | Q_UNUSED( canvasRect );
|
|---|
| 467 |
|
|---|
| 468 | const int numBars = sample.set.size();
|
|---|
| 469 | if ( numBars == 0 )
|
|---|
| 470 | return;
|
|---|
| 471 |
|
|---|
| 472 | if ( orientation() == Qt::Vertical )
|
|---|
| 473 | {
|
|---|
| 474 | const double barWidth = sampleWidth / numBars;
|
|---|
| 475 |
|
|---|
| 476 | const double y1 = yMap.transform( baseline() );
|
|---|
| 477 | const double x0 = xMap.transform( sample.value ) - 0.5 * sampleWidth;
|
|---|
| 478 |
|
|---|
| 479 | for ( int i = 0; i < numBars; i++ )
|
|---|
| 480 | {
|
|---|
| 481 | const double x1 = x0 + i * barWidth;
|
|---|
| 482 | const double x2 = x1 + barWidth;
|
|---|
| 483 |
|
|---|
| 484 | const double y2 = yMap.transform( sample.set[i] );
|
|---|
| 485 |
|
|---|
| 486 | QwtColumnRect barRect;
|
|---|
| 487 | barRect.direction = ( y1 < y2 ) ?
|
|---|
| 488 | QwtColumnRect::TopToBottom : QwtColumnRect::BottomToTop;
|
|---|
| 489 |
|
|---|
| 490 | barRect.hInterval = QwtInterval( x1, x2 ).normalized();
|
|---|
| 491 | if ( i != 0 )
|
|---|
| 492 | barRect.hInterval.setBorderFlags( QwtInterval::ExcludeMinimum );
|
|---|
| 493 |
|
|---|
| 494 | barRect.vInterval = QwtInterval( y1, y2 ).normalized();
|
|---|
| 495 |
|
|---|
| 496 | drawBar( painter, index, i, barRect );
|
|---|
| 497 | }
|
|---|
| 498 | }
|
|---|
| 499 | else
|
|---|
| 500 | {
|
|---|
| 501 | const double barHeight = sampleWidth / numBars;
|
|---|
| 502 |
|
|---|
| 503 | const double x1 = xMap.transform( baseline() );
|
|---|
| 504 | const double y0 = yMap.transform( sample.value ) - 0.5 * sampleWidth;
|
|---|
| 505 |
|
|---|
| 506 | for ( int i = 0; i < numBars; i++ )
|
|---|
| 507 | {
|
|---|
| 508 | double y1 = y0 + i * barHeight;
|
|---|
| 509 | double y2 = y1 + barHeight;
|
|---|
| 510 |
|
|---|
| 511 | double x2 = xMap.transform( sample.set[i] );
|
|---|
| 512 |
|
|---|
| 513 | QwtColumnRect barRect;
|
|---|
| 514 | barRect.direction = x1 < x2 ?
|
|---|
| 515 | QwtColumnRect::LeftToRight : QwtColumnRect::RightToLeft;
|
|---|
| 516 |
|
|---|
| 517 | barRect.hInterval = QwtInterval( x1, x2 ).normalized();
|
|---|
| 518 |
|
|---|
| 519 | barRect.vInterval = QwtInterval( y1, y2 );
|
|---|
| 520 | if ( i != 0 )
|
|---|
| 521 | barRect.vInterval.setBorderFlags( QwtInterval::ExcludeMinimum );
|
|---|
| 522 |
|
|---|
| 523 | drawBar( painter, index, i, barRect );
|
|---|
| 524 | }
|
|---|
| 525 | }
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /*!
|
|---|
| 529 | Draw a stacked sample
|
|---|
| 530 |
|
|---|
| 531 | \param painter Painter
|
|---|
| 532 | \param xMap x map
|
|---|
| 533 | \param yMap y map
|
|---|
| 534 | \param canvasRect Contents rectangle of the canvas
|
|---|
| 535 | \param index Index of the sample to be painted
|
|---|
| 536 | \param sampleWidth Width of the bars
|
|---|
| 537 | \param sample Sample
|
|---|
| 538 |
|
|---|
| 539 | \sa drawSeries(), sampleWidth()
|
|---|
| 540 | */
|
|---|
| 541 | void QwtPlotMultiBarChart::drawStackedBars( QPainter* painter,
|
|---|
| 542 | const QwtScaleMap& xMap, const QwtScaleMap& yMap,
|
|---|
| 543 | const QRectF& canvasRect, int index,
|
|---|
| 544 | double sampleWidth, const QwtSetSample& sample ) const
|
|---|
| 545 | {
|
|---|
| 546 | Q_UNUSED( canvasRect ); // clipping the bars ?
|
|---|
| 547 |
|
|---|
| 548 | const int numBars = sample.set.size();
|
|---|
| 549 | if ( numBars == 0 )
|
|---|
| 550 | return;
|
|---|
| 551 |
|
|---|
| 552 | QwtInterval::BorderFlag borderFlags = QwtInterval::IncludeBorders;
|
|---|
| 553 |
|
|---|
| 554 | if ( orientation() == Qt::Vertical )
|
|---|
| 555 | {
|
|---|
| 556 | const double x1 = xMap.transform( sample.value ) - 0.5 * sampleWidth;
|
|---|
| 557 | const double x2 = x1 + sampleWidth;
|
|---|
| 558 |
|
|---|
| 559 | const bool increasing = qwtIsIncreasing( yMap, sample.set );
|
|---|
| 560 |
|
|---|
| 561 | QwtColumnRect bar;
|
|---|
| 562 | bar.direction = increasing ?
|
|---|
| 563 | QwtColumnRect::TopToBottom : QwtColumnRect::BottomToTop;
|
|---|
| 564 |
|
|---|
| 565 | bar.hInterval = QwtInterval( x1, x2 ).normalized();
|
|---|
| 566 |
|
|---|
| 567 | double sum = baseline();
|
|---|
| 568 |
|
|---|
| 569 | for ( int i = 0; i < numBars; i++ )
|
|---|
| 570 | {
|
|---|
| 571 | const double si = sample.set[ i ];
|
|---|
| 572 | if ( si == 0.0 )
|
|---|
| 573 | continue;
|
|---|
| 574 |
|
|---|
| 575 | const double y1 = yMap.transform( sum );
|
|---|
| 576 | const double y2 = yMap.transform( sum + si );
|
|---|
| 577 |
|
|---|
| 578 | if ( ( y2 > y1 ) != increasing )
|
|---|
| 579 | {
|
|---|
| 580 | // stacked bars need to be in the same direction
|
|---|
| 581 | continue;
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | bar.vInterval = QwtInterval( y1, y2 ).normalized();
|
|---|
| 585 | bar.vInterval.setBorderFlags( borderFlags );
|
|---|
| 586 |
|
|---|
| 587 | drawBar( painter, index, i, bar );
|
|---|
| 588 |
|
|---|
| 589 | sum += si;
|
|---|
| 590 |
|
|---|
| 591 | if ( increasing )
|
|---|
| 592 | borderFlags = QwtInterval::ExcludeMinimum;
|
|---|
| 593 | else
|
|---|
| 594 | borderFlags = QwtInterval::ExcludeMaximum;
|
|---|
| 595 | }
|
|---|
| 596 | }
|
|---|
| 597 | else
|
|---|
| 598 | {
|
|---|
| 599 | const double y1 = yMap.transform( sample.value ) - 0.5 * sampleWidth;
|
|---|
| 600 | const double y2 = y1 + sampleWidth;
|
|---|
| 601 |
|
|---|
| 602 | const bool increasing = qwtIsIncreasing( xMap, sample.set );
|
|---|
| 603 |
|
|---|
| 604 | QwtColumnRect bar;
|
|---|
| 605 | bar.direction = increasing ?
|
|---|
| 606 | QwtColumnRect::LeftToRight : QwtColumnRect::RightToLeft;
|
|---|
| 607 | bar.vInterval = QwtInterval( y1, y2 ).normalized();
|
|---|
| 608 |
|
|---|
| 609 | double sum = baseline();
|
|---|
| 610 |
|
|---|
| 611 | for ( int i = 0; i < sample.set.size(); i++ )
|
|---|
| 612 | {
|
|---|
| 613 | const double si = sample.set[ i ];
|
|---|
| 614 | if ( si == 0.0 )
|
|---|
| 615 | continue;
|
|---|
| 616 |
|
|---|
| 617 | const double x1 = xMap.transform( sum );
|
|---|
| 618 | const double x2 = xMap.transform( sum + si );
|
|---|
| 619 |
|
|---|
| 620 | if ( ( x2 > x1 ) != increasing )
|
|---|
| 621 | {
|
|---|
| 622 | // stacked bars need to be in the same direction
|
|---|
| 623 | continue;
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | bar.hInterval = QwtInterval( x1, x2 ).normalized();
|
|---|
| 627 | bar.hInterval.setBorderFlags( borderFlags );
|
|---|
| 628 |
|
|---|
| 629 | drawBar( painter, index, i, bar );
|
|---|
| 630 |
|
|---|
| 631 | sum += si;
|
|---|
| 632 |
|
|---|
| 633 | if ( increasing )
|
|---|
| 634 | borderFlags = QwtInterval::ExcludeMinimum;
|
|---|
| 635 | else
|
|---|
| 636 | borderFlags = QwtInterval::ExcludeMaximum;
|
|---|
| 637 | }
|
|---|
| 638 | }
|
|---|
| 639 | }
|
|---|
| 640 |
|
|---|
| 641 | /*!
|
|---|
| 642 | Draw a bar
|
|---|
| 643 |
|
|---|
| 644 | \param painter Painter
|
|---|
| 645 | \param sampleIndex Index of the sample - might be -1 when the
|
|---|
| 646 | bar is painted for the legend
|
|---|
| 647 | \param valueIndex Index of a value in a set
|
|---|
| 648 | \param rect Directed target rectangle for the bar
|
|---|
| 649 |
|
|---|
| 650 | \sa drawSeries()
|
|---|
| 651 | */
|
|---|
| 652 | void QwtPlotMultiBarChart::drawBar( QPainter* painter,
|
|---|
| 653 | int sampleIndex, int valueIndex, const QwtColumnRect& rect ) const
|
|---|
| 654 | {
|
|---|
| 655 | const QwtColumnSymbol* specialSym = NULL;
|
|---|
| 656 | if ( sampleIndex >= 0 )
|
|---|
| 657 | specialSym = specialSymbol( sampleIndex, valueIndex );
|
|---|
| 658 |
|
|---|
| 659 | const QwtColumnSymbol* sym = specialSym;
|
|---|
| 660 | if ( sym == NULL )
|
|---|
| 661 | sym = symbol( valueIndex );
|
|---|
| 662 |
|
|---|
| 663 | if ( sym )
|
|---|
| 664 | {
|
|---|
| 665 | sym->draw( painter, rect );
|
|---|
| 666 | }
|
|---|
| 667 | else
|
|---|
| 668 | {
|
|---|
| 669 | // we build a temporary default symbol
|
|---|
| 670 | QwtColumnSymbol columnSymbol( QwtColumnSymbol::Box );
|
|---|
| 671 | columnSymbol.setLineWidth( 1 );
|
|---|
| 672 | columnSymbol.setFrameStyle( QwtColumnSymbol::Plain );
|
|---|
| 673 | columnSymbol.draw( painter, rect );
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | delete specialSym;
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | /*!
|
|---|
| 680 | \return Information to be displayed on the legend
|
|---|
| 681 |
|
|---|
| 682 | The chart is represented by a list of entries - one for each bar title.
|
|---|
| 683 | Each element contains a bar title and an icon showing its corresponding bar.
|
|---|
| 684 |
|
|---|
| 685 | \sa barTitles(), legendIcon(), legendIconSize()
|
|---|
| 686 | */
|
|---|
| 687 | QList< QwtLegendData > QwtPlotMultiBarChart::legendData() const
|
|---|
| 688 | {
|
|---|
| 689 | QList< QwtLegendData > list;
|
|---|
| 690 | list.reserve( m_data->barTitles.size() );
|
|---|
| 691 |
|
|---|
| 692 | for ( int i = 0; i < m_data->barTitles.size(); i++ )
|
|---|
| 693 | {
|
|---|
| 694 | QwtLegendData data;
|
|---|
| 695 |
|
|---|
| 696 | data.setValue( QwtLegendData::TitleRole,
|
|---|
| 697 | QVariant::fromValue( m_data->barTitles[i] ) );
|
|---|
| 698 |
|
|---|
| 699 | if ( !legendIconSize().isEmpty() )
|
|---|
| 700 | {
|
|---|
| 701 | data.setValue( QwtLegendData::IconRole,
|
|---|
| 702 | QVariant::fromValue( legendIcon( i, legendIconSize() ) ) );
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | list += data;
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | return list;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | /*!
|
|---|
| 712 | \return Icon for representing a bar on the legend
|
|---|
| 713 |
|
|---|
| 714 | \param index Index of the bar
|
|---|
| 715 | \param size Icon size
|
|---|
| 716 |
|
|---|
| 717 | \return An icon showing a bar
|
|---|
| 718 | \sa drawBar(), legendData()
|
|---|
| 719 | */
|
|---|
| 720 | QwtGraphic QwtPlotMultiBarChart::legendIcon( int index,
|
|---|
| 721 | const QSizeF& size ) const
|
|---|
| 722 | {
|
|---|
| 723 | QwtColumnRect column;
|
|---|
| 724 | column.hInterval = QwtInterval( 0.0, size.width() - 1.0 );
|
|---|
| 725 | column.vInterval = QwtInterval( 0.0, size.height() - 1.0 );
|
|---|
| 726 |
|
|---|
| 727 | QwtGraphic icon;
|
|---|
| 728 | icon.setDefaultSize( size );
|
|---|
| 729 | icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
|
|---|
| 730 |
|
|---|
| 731 | QPainter painter( &icon );
|
|---|
| 732 | painter.setRenderHint( QPainter::Antialiasing,
|
|---|
| 733 | testRenderHint( QwtPlotItem::RenderAntialiased ) );
|
|---|
| 734 |
|
|---|
| 735 | drawBar( &painter, -1, index, column );
|
|---|
| 736 |
|
|---|
| 737 | return icon;
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|