Loading...
Searching...
No Matches
qwt_plot_abstract_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_abstract_barchart.h"
#include "qwt_scale_map.h"
#include "qwt_math.h"
static inline double qwtTransformWidth(
const QwtScaleMap& map, double value, double width )
{
const double w2 = 0.5 * width;
const double v1 = map.transform( value - w2 );
const double v2 = map.transform( value + w2 );
return qAbs( v2 - v1 );
}
class QwtPlotAbstractBarChart::PrivateData
{
public:
PrivateData()
: layoutPolicy( QwtPlotAbstractBarChart::AutoAdjustSamples )
, layoutHint( 0.5 )
, spacing( 10 )
, margin( 5 )
, baseline( 0.0 )
{
}
QwtPlotAbstractBarChart::LayoutPolicy layoutPolicy;
double layoutHint;
int spacing;
int margin;
double baseline;
};
QwtPlotAbstractBarChart::QwtPlotAbstractBarChart( const QwtText& title )
: QwtPlotSeriesItem( title )
{
m_data = new PrivateData;
setItemAttribute( QwtPlotItem::Legend, true );
setItemAttribute( QwtPlotItem::AutoScale, true );
setItemAttribute( QwtPlotItem::Margins, true );
setZ( 19.0 );
}
QwtPlotAbstractBarChart::~QwtPlotAbstractBarChart()
{
delete m_data;
}
void QwtPlotAbstractBarChart::setLayoutPolicy( LayoutPolicy policy )
{
if ( policy != m_data->layoutPolicy )
{
m_data->layoutPolicy = policy;
itemChanged();
}
}
QwtPlotAbstractBarChart::LayoutPolicy QwtPlotAbstractBarChart::layoutPolicy() const
{
return m_data->layoutPolicy;
}
void QwtPlotAbstractBarChart::setLayoutHint( double hint )
{
hint = qwtMaxF( 0.0, hint );
if ( hint != m_data->layoutHint )
{
m_data->layoutHint = hint;
itemChanged();
}
}
double QwtPlotAbstractBarChart::layoutHint() const
{
return m_data->layoutHint;
}
void QwtPlotAbstractBarChart::setSpacing( int spacing )
{
spacing = qMax( spacing, 0 );
if ( spacing != m_data->spacing )
{
m_data->spacing = spacing;
itemChanged();
}
}
int QwtPlotAbstractBarChart::spacing() const
{
return m_data->spacing;
}
void QwtPlotAbstractBarChart::setMargin( int margin )
{
margin = qMax( margin, 0 );
if ( margin != m_data->margin )
{
m_data->margin = margin;
itemChanged();
}
}
int QwtPlotAbstractBarChart::margin() const
{
return m_data->margin;
}
void QwtPlotAbstractBarChart::setBaseline( double value )
{
if ( value != m_data->baseline )
{
m_data->baseline = value;
itemChanged();
}
}
double QwtPlotAbstractBarChart::baseline() const
{
return m_data->baseline;
}
double QwtPlotAbstractBarChart::sampleWidth( const QwtScaleMap& map,
double canvasSize, double boundingSize, double value ) const
{
double width;
switch( m_data->layoutPolicy )
{
case ScaleSamplesToAxes:
{
width = qwtTransformWidth( map, value, m_data->layoutHint );
break;
}
case ScaleSampleToCanvas:
{
width = canvasSize * m_data->layoutHint;
break;
}
case FixedSampleSize:
{
width = m_data->layoutHint;
break;
}
case AutoAdjustSamples:
default:
{
const size_t numSamples = dataSize();
double w = 1.0;
if ( numSamples > 1 )
{
w = qAbs( boundingSize / ( numSamples - 1 ) );
}
width = qwtTransformWidth( map, value, w );
width -= m_data->spacing;
width = qwtMaxF( width, m_data->layoutHint );
}
}
return width;
}
void QwtPlotAbstractBarChart::getCanvasMarginHint( const QwtScaleMap& xMap,
const QwtScaleMap& yMap, const QRectF& canvasRect,
double& left, double& top, double& right, double& bottom ) const
{
double hint = -1.0;
switch( layoutPolicy() )
{
case ScaleSampleToCanvas:
{
if ( orientation() == Qt::Vertical )
hint = 0.5 * canvasRect.width() * m_data->layoutHint;
else
hint = 0.5 * canvasRect.height() * m_data->layoutHint;
break;
}
case FixedSampleSize:
{
hint = 0.5 * m_data->layoutHint;
break;
}
case AutoAdjustSamples:
case ScaleSamplesToAxes:
default:
{
const size_t numSamples = dataSize();
if ( numSamples <= 0 )
break;
// doesn't work for nonlinear scales
const QRectF br = dataRect();
double spacing = 0.0;
double sampleWidthS = 1.0;
if ( layoutPolicy() == ScaleSamplesToAxes )
{
sampleWidthS = qwtMaxF( m_data->layoutHint, 0.0 );
}
else
{
spacing = m_data->spacing;
if ( numSamples > 1 )
{
sampleWidthS = qAbs( br.width() / ( numSamples - 1 ) );
}
}
double ds, w;
if ( orientation() == Qt::Vertical )
{
ds = qAbs( xMap.sDist() );
w = canvasRect.width();
}
else
{
ds = qAbs( yMap.sDist() );
w = canvasRect.height();
}
const double sampleWidthP = ( w - spacing * ( numSamples - 1 ) )
* sampleWidthS / ( ds + sampleWidthS );
hint = 0.5 * sampleWidthP;
hint += qMax( m_data->margin, 0 );
}
}
if ( orientation() == Qt::Vertical )
{
left = right = hint;
top = bottom = -1.0; // no hint
}
else
{
left = right = -1.0; // no hint
top = bottom = hint;
}
}
QwtAbstractSeriesStore::dataRect
virtual QRectF dataRect() const =0
QwtAbstractSeriesStore::dataSize
virtual size_t dataSize() const =0
QwtPlotAbstractBarChart
Abstract base class for bar chart items.
Definition qwt_plot_abstract_barchart.h:25
QwtPlotAbstractBarChart::setSpacing
void setSpacing(int)
Set the spacing.
Definition qwt_plot_abstract_barchart.cpp:132
QwtPlotAbstractBarChart::layoutHint
double layoutHint() const
Definition qwt_plot_abstract_barchart.cpp:119
QwtPlotAbstractBarChart::QwtPlotAbstractBarChart
QwtPlotAbstractBarChart(const QwtText &title)
Definition qwt_plot_abstract_barchart.cpp:48
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
QwtPlotAbstractBarChart::spacing
int spacing() const
Definition qwt_plot_abstract_barchart.cpp:146
QwtPlotAbstractBarChart::~QwtPlotAbstractBarChart
virtual ~QwtPlotAbstractBarChart()
Destructor.
Definition qwt_plot_abstract_barchart.cpp:60
QwtPlotAbstractBarChart::margin
int margin() const
Definition qwt_plot_abstract_barchart.cpp:176
QwtPlotAbstractBarChart::setMargin
void setMargin(int)
Set the margin.
Definition qwt_plot_abstract_barchart.cpp:160
QwtPlotAbstractBarChart::getCanvasMarginHint
virtual void getCanvasMarginHint(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, double &left, double &top, double &right, double &bottom) const override
Calculate a hint for the canvas margin.
Definition qwt_plot_abstract_barchart.cpp:287
QwtPlotAbstractBarChart::layoutPolicy
LayoutPolicy layoutPolicy() const
Definition qwt_plot_abstract_barchart.cpp:89
QwtPlotAbstractBarChart::setLayoutPolicy
void setLayoutPolicy(LayoutPolicy)
Definition qwt_plot_abstract_barchart.cpp:73
QwtPlotAbstractBarChart::setBaseline
void setBaseline(double)
Set the baseline.
Definition qwt_plot_abstract_barchart.cpp:195
QwtPlotAbstractBarChart::LayoutPolicy
LayoutPolicy
Mode how to calculate the bar width.
Definition qwt_plot_abstract_barchart.h:33
QwtPlotAbstractBarChart::FixedSampleSize
@ FixedSampleSize
Definition qwt_plot_abstract_barchart.h:59
QwtPlotAbstractBarChart::AutoAdjustSamples
@ AutoAdjustSamples
Definition qwt_plot_abstract_barchart.h:41
QwtPlotAbstractBarChart::ScaleSamplesToAxes
@ ScaleSamplesToAxes
Definition qwt_plot_abstract_barchart.h:46
QwtPlotAbstractBarChart::ScaleSampleToCanvas
@ ScaleSampleToCanvas
Definition qwt_plot_abstract_barchart.h:54
QwtPlotAbstractBarChart::setLayoutHint
void setLayoutHint(double)
Definition qwt_plot_abstract_barchart.cpp:102
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition qwt_plot_item.cpp:165
QwtPlotItem::setItemAttribute
void setItemAttribute(ItemAttribute, bool on=true)
Definition qwt_plot_item.cpp:228
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition qwt_plot_item.cpp:481
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition qwt_plot_item.h:150
QwtPlotItem::Margins
@ Margins
Definition qwt_plot_item.h:164
QwtPlotItem::AutoScale
@ AutoScale
Definition qwt_plot_item.h:157
QwtPlotSeriesItem
Base class for plot items representing a series of samples.
Definition qwt_plot_seriesitem.h:26
QwtPlotSeriesItem::orientation
Qt::Orientation orientation() const
Definition qwt_plot_seriesitem.cpp:77
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition qwt_scale_map.h:137
QwtScaleMap::sDist
double sDist() const
Definition qwt_scale_map.h:123
QwtText
A class representing a text.
Definition qwt_text.h:52