2023-04-20 11:15:46 +02:00
|
|
|
/******************************************************************************
|
2024-01-17 14:31:45 +01:00
|
|
|
* QSkinny - Copyright (C) The authors
|
2023-04-20 11:15:46 +02:00
|
|
|
* This file may be used under the terms of the 3-clause BSD License
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "StackedChart.h"
|
|
|
|
#include "ChartSample.h"
|
|
|
|
|
|
|
|
#include <QskIntervalF.h>
|
|
|
|
|
|
|
|
class StackedChart::PrivateData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
qreal totalValue = 0.0;
|
|
|
|
|
|
|
|
QVector< ChartSample > samples;
|
|
|
|
QVector< qreal > cumulatedValues;
|
|
|
|
};
|
|
|
|
|
|
|
|
StackedChart::StackedChart( QQuickItem* parentItem )
|
|
|
|
: QskControl( parentItem )
|
|
|
|
, m_data( new PrivateData() )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StackedChart::~StackedChart()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackedChart::setTotalValue( qreal value )
|
|
|
|
{
|
|
|
|
if ( value < 0.0 )
|
|
|
|
value = 0.0;
|
|
|
|
|
|
|
|
if ( value != m_data->totalValue )
|
|
|
|
{
|
|
|
|
m_data->totalValue = value;
|
|
|
|
update();
|
|
|
|
|
|
|
|
Q_EMIT totalValueChanged( m_data->totalValue );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal StackedChart::totalValue() const
|
|
|
|
{
|
|
|
|
return m_data->totalValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal StackedChart::effectiveTotalValue() const
|
|
|
|
{
|
|
|
|
qreal cummulated = 0.0;
|
|
|
|
|
|
|
|
if ( !m_data->samples.isEmpty() )
|
|
|
|
{
|
|
|
|
cummulated = m_data->cumulatedValues.last()
|
|
|
|
+ m_data->samples.last().value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return qMax( m_data->totalValue, cummulated );
|
|
|
|
}
|
|
|
|
|
|
|
|
QskIntervalF StackedChart::stackedInterval( int index ) const
|
|
|
|
{
|
|
|
|
QskIntervalF interval;
|
|
|
|
|
|
|
|
if ( index >= 0 && index < m_data->samples.size() )
|
|
|
|
{
|
|
|
|
interval.setLowerBound( m_data->cumulatedValues[index] );
|
2023-11-28 13:12:52 +01:00
|
|
|
interval.setLength( m_data->samples[index].value() );
|
2023-04-20 11:15:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackedChart::setSeries( const QVector< ChartSample >& samples )
|
|
|
|
{
|
|
|
|
m_data->samples = samples;
|
|
|
|
|
|
|
|
{
|
|
|
|
// caching the cumulated values
|
|
|
|
|
2023-06-14 12:20:46 +02:00
|
|
|
m_data->cumulatedValues.clear();
|
2023-04-20 11:15:46 +02:00
|
|
|
m_data->cumulatedValues.reserve( samples.size() );
|
|
|
|
|
|
|
|
qreal total = 0.0;
|
|
|
|
for ( const auto& sample : samples )
|
|
|
|
{
|
|
|
|
m_data->cumulatedValues += total;
|
|
|
|
total += sample.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
|
|
Q_EMIT seriesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector< ChartSample > StackedChart::series() const
|
|
|
|
{
|
|
|
|
return m_data->samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString StackedChart::labelAt( int index ) const
|
|
|
|
{
|
|
|
|
return m_data->samples.value( index ).title();
|
|
|
|
}
|
|
|
|
|
|
|
|
QskGradient StackedChart::gradientAt( int index ) const
|
|
|
|
{
|
|
|
|
return m_data->samples.value( index ).gradient();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_StackedChart.cpp"
|