qwt_plot_barchart.cpp
/******************************************************************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_plot_barchart.h"
#include "qwt_scale_map.h"
#include "qwt_column_symbol.h"
#include "qwt_text.h"
#include "qwt_graphic.h"
#include "qwt_legend_data.h"
#include <qpainter.h>
class QwtPlotBarChart::PrivateData
{
public:
PrivateData()
: symbol( NULL )
, legendMode( QwtPlotBarChart::LegendChartTitle )
{
}
~PrivateData()
{
delete symbol;
}
QwtColumnSymbol* symbol;
QwtPlotBarChart::LegendMode legendMode;
};
QwtPlotBarChart::QwtPlotBarChart( const QwtText& title )
: QwtPlotAbstractBarChart( title )
{
init();
}
QwtPlotBarChart::QwtPlotBarChart( const QString& title )
: QwtPlotAbstractBarChart( QwtText( title ) )
{
init();
}
QwtPlotBarChart::~QwtPlotBarChart()
{
delete m_data;
}
void QwtPlotBarChart::init()
{
m_data = new PrivateData;
setData( new QwtPointSeriesData() );
}
int QwtPlotBarChart::rtti() const
{
return QwtPlotItem::Rtti_PlotBarChart;
}
void QwtPlotBarChart::setSamples(
const QVector< QPointF >& samples )
{
setData( new QwtPointSeriesData( samples ) );
}
void QwtPlotBarChart::setSamples(
const QVector< double >& samples )
{
QVector< QPointF > points;
points.reserve( samples.size() );
for ( int i = 0; i < samples.size(); i++ )
points += QPointF( i, samples[ i ] );
setData( new QwtPointSeriesData( points ) );
}
void QwtPlotBarChart::setSamples( QwtSeriesData< QPointF >* data )
{
setData( data );
}
void QwtPlotBarChart::setSymbol( QwtColumnSymbol* symbol )
{
if ( symbol != m_data->symbol )
{
delete m_data->symbol;
m_data->symbol = symbol;
legendChanged();
itemChanged();
}
}
const QwtColumnSymbol* QwtPlotBarChart::symbol() const
{
return m_data->symbol;
}
void QwtPlotBarChart::setLegendMode( LegendMode mode )
{
if ( mode != m_data->legendMode )
{
m_data->legendMode = mode;
legendChanged();
}
}
QwtPlotBarChart::LegendMode QwtPlotBarChart::legendMode() const
{
return m_data->legendMode;
}
QRectF QwtPlotBarChart::boundingRect() const
{
const size_t numSamples = dataSize();
if ( numSamples == 0 )
return QwtPlotSeriesItem::boundingRect();
QRectF rect = QwtPlotSeriesItem::boundingRect();
if ( rect.height() >= 0 )
{
const double baseLine = baseline();
if ( rect.bottom() < baseLine )
rect.setBottom( baseLine );
if ( rect.top() > baseLine )
rect.setTop( baseLine );
}
if ( orientation() == Qt::Horizontal )
rect.setRect( rect.y(), rect.x(), rect.height(), rect.width() );
return rect;
}
void QwtPlotBarChart::drawSeries( QPainter* painter,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QRectF& canvasRect, int from, int to ) const
{
if ( to < 0 )
to = dataSize() - 1;
if ( from < 0 )
from = 0;
if ( from > to )
return;
const QRectF br = data()->boundingRect();
const QwtInterval interval( br.left(), br.right() );
painter->save();
for ( int i = from; i <= to; i++ )
{
drawSample( painter, xMap, yMap,
canvasRect, interval, i, sample( i ) );
}
painter->restore();
}
QwtColumnRect QwtPlotBarChart::columnRect(
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QRectF& canvasRect, const QwtInterval& boundingInterval,
const QPointF& sample ) const
{
QwtColumnRect barRect;
if ( orientation() == Qt::Horizontal )
{
const double barHeight = sampleWidth( yMap, canvasRect.height(),
boundingInterval.width(), sample.y() );
const double x1 = xMap.transform( baseline() );
const double x2 = xMap.transform( sample.y() );
const double y = yMap.transform( sample.x() );
const double y1 = y - 0.5 * barHeight;
const double y2 = y + 0.5 * barHeight;
barRect.direction = ( x1 < x2 ) ?
QwtColumnRect::LeftToRight : QwtColumnRect::RightToLeft;
barRect.hInterval = QwtInterval( x1, x2 ).normalized();
barRect.vInterval = QwtInterval( y1, y2 );
}
else
{
const double barWidth = sampleWidth( xMap, canvasRect.width(),
boundingInterval.width(), sample.y() );
const double x = xMap.transform( sample.x() );
const double x1 = x - 0.5 * barWidth;
const double x2 = x + 0.5 * barWidth;
const double y1 = yMap.transform( baseline() );
const double y2 = yMap.transform( sample.y() );
barRect.direction = ( y1 < y2 ) ?
QwtColumnRect::TopToBottom : QwtColumnRect::BottomToTop;
barRect.hInterval = QwtInterval( x1, x2 );
barRect.vInterval = QwtInterval( y1, y2 ).normalized();
}
return barRect;
}
void QwtPlotBarChart::drawSample( QPainter* painter,
const QwtScaleMap& xMap, const QwtScaleMap& yMap,
const QRectF& canvasRect, const QwtInterval& boundingInterval,
int index, const QPointF& sample ) const
{
const QwtColumnRect barRect = columnRect( xMap, yMap,
canvasRect, boundingInterval, sample );
drawBar( painter, index, sample, barRect );
}
void QwtPlotBarChart::drawBar( QPainter* painter,
int sampleIndex, const QPointF& sample,
const QwtColumnRect& rect ) const
{
const QwtColumnSymbol* specialSym =
specialSymbol( sampleIndex, sample );
const QwtColumnSymbol* sym = specialSym;
if ( sym == NULL )
sym = m_data->symbol;
if ( sym )
{
sym->draw( painter, rect );
}
else
{
// we build a temporary default symbol
QwtColumnSymbol columnSymbol( QwtColumnSymbol::Box );
columnSymbol.setLineWidth( 1 );
columnSymbol.setFrameStyle( QwtColumnSymbol::Plain );
columnSymbol.draw( painter, rect );
}
delete specialSym;
}
QwtColumnSymbol* QwtPlotBarChart::specialSymbol(
int sampleIndex, const QPointF& sample ) const
{
Q_UNUSED( sampleIndex );
Q_UNUSED( sample );
return NULL;
}
QwtText QwtPlotBarChart::barTitle( int sampleIndex ) const
{
Q_UNUSED( sampleIndex );
return QwtText();
}
QList< QwtLegendData > QwtPlotBarChart::legendData() const
{
QList< QwtLegendData > list;
if ( m_data->legendMode == LegendBarTitles )
{
const size_t numSamples = dataSize();
list.reserve( numSamples );
for ( size_t i = 0; i < numSamples; i++ )
{
QwtLegendData data;
data.setValue( QwtLegendData::TitleRole,
QVariant::fromValue( barTitle( i ) ) );
if ( !legendIconSize().isEmpty() )
{
data.setValue( QwtLegendData::IconRole,
QVariant::fromValue( legendIcon( i, legendIconSize() ) ) );
}
list += data;
}
}
else
{
return QwtPlotAbstractBarChart::legendData();
}
return list;
}
QwtGraphic QwtPlotBarChart::legendIcon(
int index, const QSizeF& size ) const
{
QwtColumnRect column;
column.hInterval = QwtInterval( 0.0, size.width() - 1.0 );
column.vInterval = QwtInterval( 0.0, size.height() - 1.0 );
QwtGraphic icon;
icon.setDefaultSize( size );
icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
QPainter painter( &icon );
painter.setRenderHint( QPainter::Antialiasing,
testRenderHint( QwtPlotItem::RenderAntialiased ) );
int barIndex = -1;
if ( m_data->legendMode == QwtPlotBarChart::LegendBarTitles )
barIndex = index;
drawBar( &painter, barIndex, QPointF(), column );
return icon;
}
QList
Definition: qwt_abstract_legend.h:17
QVector
Definition: qwt_clipper.h:23
QwtColumnRect
Directed rectangle representing bounding rectangle and orientation of a column.
Definition: qwt_column_symbol.h:27
QwtColumnRect::vInterval
QwtInterval vInterval
Interval for the vertical coordinates.
Definition: qwt_column_symbol.h:67
QwtColumnRect::hInterval
QwtInterval hInterval
Interval for the horizontal coordinates.
Definition: qwt_column_symbol.h:64
QwtColumnRect::LeftToRight
@ LeftToRight
From left to right.
Definition: qwt_column_symbol.h:33
QwtColumnRect::TopToBottom
@ TopToBottom
From top to bottom.
Definition: qwt_column_symbol.h:42
QwtColumnRect::BottomToTop
@ BottomToTop
From bottom to top.
Definition: qwt_column_symbol.h:39
QwtColumnRect::RightToLeft
@ RightToLeft
From right to left.
Definition: qwt_column_symbol.h:36
QwtColumnRect::direction
Direction direction
Direction.
Definition: qwt_column_symbol.h:70
QwtColumnSymbol
A drawing primitive for columns.
Definition: qwt_column_symbol.h:75
QwtColumnSymbol::Plain
@ Plain
A plain frame style.
Definition: qwt_column_symbol.h:110
QwtColumnSymbol::draw
virtual void draw(QPainter *, const QwtColumnRect &) const
Definition: qwt_column_symbol.cpp:238
QwtColumnSymbol::Box
@ Box
Definition: qwt_column_symbol.h:90
QwtColumnSymbol::setFrameStyle
void setFrameStyle(FrameStyle)
Definition: qwt_column_symbol.cpp:193
QwtColumnSymbol::setLineWidth
void setLineWidth(int width)
Definition: qwt_column_symbol.cpp:213
QwtGraphic
A paint device for scalable graphics.
Definition: qwt_graphic.h:76
QwtGraphic::RenderPensUnscaled
@ RenderPensUnscaled
Definition: qwt_graphic.h:96
QwtGraphic::setRenderHint
void setRenderHint(RenderHint, bool on=true)
Definition: qwt_graphic.cpp:443
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition: qwt_graphic.cpp:553
QwtInterval
A class representing an interval.
Definition: qwt_interval.h:23
QwtInterval::normalized
QwtInterval normalized() const
Normalize the limits of the interval.
Definition: qwt_interval.cpp:29
QwtInterval::width
double width() const
Return the width of an interval.
Definition: qwt_interval.h:227
QwtLegendData
Attributes of an entry on a legend.
Definition: qwt_legend_data.h:37
QwtPlotAbstractBarChart
Abstract base class for bar chart items.
Definition: qwt_plot_abstract_barchart.h:25
QwtPlotAbstractBarChart::sampleWidth
double sampleWidth(const QwtScaleMap &map, double canvasSize, double boundingSize, double value) const
Definition: qwt_plot_abstract_barchart.cpp:225
QwtPlotAbstractBarChart::baseline
double baseline() const
Definition: qwt_plot_abstract_barchart.cpp:208
QwtPlotBarChart
QwtPlotBarChart displays a series of a values as bars.
Definition: qwt_plot_barchart.h:44
QwtPlotBarChart::QwtPlotBarChart
QwtPlotBarChart(const QString &title=QString())
Definition: qwt_plot_barchart.cpp:51
QwtPlotBarChart::setLegendMode
void setLegendMode(LegendMode)
Definition: qwt_plot_barchart.cpp:164
QwtPlotBarChart::legendMode
LegendMode legendMode() const
Definition: qwt_plot_barchart.cpp:177
QwtPlotBarChart::setSymbol
void setSymbol(QwtColumnSymbol *)
Assign a symbol.
Definition: qwt_plot_barchart.cpp:134
QwtPlotBarChart::specialSymbol
virtual QwtColumnSymbol * specialSymbol(int sampleIndex, const QPointF &) const
Definition: qwt_plot_barchart.cpp:377
QwtPlotBarChart::LegendMode
LegendMode
Legend modes.
Definition: qwt_plot_barchart.h:53
QwtPlotBarChart::LegendBarTitles
@ LegendBarTitles
Definition: qwt_plot_barchart.h:68
QwtPlotBarChart::LegendChartTitle
@ LegendChartTitle
Definition: qwt_plot_barchart.h:60
QwtPlotBarChart::legendData
QList< QwtLegendData > legendData() const override
Return all information, that is needed to represent the item on the legend.
Definition: qwt_plot_barchart.cpp:416
QwtPlotBarChart::setSamples
void setSamples(const QVector< QPointF > &)
Definition: qwt_plot_barchart.cpp:82
QwtPlotBarChart::drawSeries
virtual void drawSeries(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, int from, int to) const override
Definition: qwt_plot_barchart.cpp:223
QwtPlotBarChart::rtti
virtual int rtti() const override
Definition: qwt_plot_barchart.cpp:70
QwtPlotBarChart::barTitle
virtual QwtText barTitle(int sampleIndex) const
Return the title of a bar.
Definition: qwt_plot_barchart.cpp:399
QwtPlotBarChart::symbol
const QwtColumnSymbol * symbol() const
Definition: qwt_plot_barchart.cpp:150
QwtPlotBarChart::drawBar
virtual void drawBar(QPainter *, int sampleIndex, const QPointF &sample, const QwtColumnRect &) const
Definition: qwt_plot_barchart.cpp:341
QwtPlotBarChart::columnRect
QwtColumnRect columnRect(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, const QPointF &sample) const
Definition: qwt_plot_barchart.cpp:262
QwtPlotBarChart::~QwtPlotBarChart
virtual ~QwtPlotBarChart()
Destructor.
Definition: qwt_plot_barchart.cpp:58
QwtPlotBarChart::boundingRect
virtual QRectF boundingRect() const override
Definition: qwt_plot_barchart.cpp:186
QwtPlotBarChart::legendIcon
QwtGraphic legendIcon(int index, const QSizeF &) const override
Definition: qwt_plot_barchart.cpp:462
QwtPlotBarChart::drawSample
virtual void drawSample(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, const QwtInterval &boundingInterval, int index, const QPointF &sample) const
Definition: qwt_plot_barchart.cpp:322
QwtPlotItem::legendData
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
Definition: qwt_plot_item.cpp:626
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtPlotItem::Rtti_PlotBarChart
@ Rtti_PlotBarChart
For QwtPlotBarChart.
Definition: qwt_plot_item.h:114
QwtPlotItem::RenderAntialiased
@ RenderAntialiased
Enable antialiasing.
Definition: qwt_plot_item.h:206
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition: qwt_plot_item.cpp:332
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotItem::legendIconSize
QSize legendIconSize() const
Definition: qwt_plot_item.cpp:386
QwtPlotSeriesItem::orientation
Qt::Orientation orientation() const
Definition: qwt_plot_seriesitem.cpp:77
QwtPlotSeriesItem::boundingRect
virtual QRectF boundingRect() const override
Definition: qwt_plot_seriesitem.cpp:97
QwtPointSeriesData
Interface for iterating over an array of points.
Definition: qwt_series_data.h:210
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtSeriesData< QPointF >
QwtSeriesData::boundingRect
virtual QRectF boundingRect() const =0
QwtSeriesStore< QPointF >::sample
QPointF sample(int index) const
Definition: qwt_series_store.h:158
QwtSeriesStore< QPointF >::dataSize
virtual size_t dataSize() const override
Definition: qwt_series_store.h:175
QwtSeriesStore< QPointF >::data
QwtSeriesData< QPointF > * data()
Definition: qwt_series_store.h:146
QwtSeriesStore< QPointF >::setData
void setData(QwtSeriesData< QPointF > *series)
Definition: qwt_series_store.h:164
QwtText
A class representing a text.
Definition: qwt_text.h:52