2016-11-02 01:34:45 +03:00
|
|
|
#include "lrchartitem.h"
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
#include "lrdesignelementsfactory.h"
|
|
|
|
#include "lrchartitemeditor.h"
|
|
|
|
#include "lrdatasourcemanager.h"
|
|
|
|
#include "lrpagedesignintf.h"
|
|
|
|
#include "lrreportengine_p.h"
|
|
|
|
#include "lrdatadesignintf.h"
|
2022-03-10 22:35:48 +03:00
|
|
|
#include "lrchartaxiseditor.h"
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
#include "charts/lrpiechart.h"
|
|
|
|
#include "charts/lrverticalbarchart.h"
|
|
|
|
#include "charts/lrhorizontalbarchart.h"
|
|
|
|
#include "charts/lrlineschart.h"
|
2022-01-25 21:46:14 +03:00
|
|
|
#include "charts/lrgridlineschart.h"
|
2020-01-11 02:11:55 +03:00
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
namespace{
|
|
|
|
|
|
|
|
const QString xmlTag = "ChartItem";
|
|
|
|
|
|
|
|
LimeReport::BaseDesignIntf * createChartItem(QObject* owner, LimeReport::BaseDesignIntf* parent){
|
|
|
|
return new LimeReport::ChartItem(owner,parent);
|
|
|
|
}
|
|
|
|
bool registred = LimeReport::DesignElementsFactory::instance().registerCreator(
|
|
|
|
xmlTag, LimeReport::ItemAttribs(QObject::tr("Chart Item"),"Item"), createChartItem
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace LimeReport{
|
|
|
|
|
|
|
|
QColor generateColor()
|
|
|
|
{
|
2021-08-23 08:07:08 +03:00
|
|
|
int red = (rand()%(256 - 1)) + 1;
|
|
|
|
int green = (rand()%(256 - 1)) + 1;
|
|
|
|
int blue = (rand()%(256 - 1)) + 1;;
|
2016-11-02 01:34:45 +03:00
|
|
|
return QColor(red,green,blue);
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor color_map[39] = {
|
2020-01-11 02:11:55 +03:00
|
|
|
QColor(51,102,204), QColor(220,57,18), QColor(225, 153, 0), QColor(16, 150, 24),
|
|
|
|
QColor(153,0,153), QColor(0,153,198), QColor(221, 68, 119), QColor(255,0,0),
|
|
|
|
QColor(0,0,139), QColor(0,205,0), QColor(233,30,99), QColor(255,255,0),
|
|
|
|
QColor(244,67,54), QColor(156,39,176), QColor(103,58,183), QColor(63,81,181),
|
|
|
|
QColor(33,153,243), QColor(0,150,136), QColor(78,175,80), QColor(139,195,74),
|
|
|
|
QColor(205,228,57), QColor(0,139,0), QColor(0,0,255), QColor(255,235,59),
|
|
|
|
QColor(255,193,7), QColor(255,152,0), QColor(255,87,34), QColor(121,85,72),
|
|
|
|
QColor(158,158,158), QColor(96,125,139), QColor(241,153,185), QColor(64,64,64),
|
|
|
|
QColor(188,229,218), QColor(139,0,0), QColor(139,139,0), QColor(171, 130, 255),
|
2016-11-02 01:34:45 +03:00
|
|
|
QColor(139, 123, 139), QColor(255, 0, 255), QColor(139, 69, 19)
|
2017-07-13 14:39:07 +03:00
|
|
|
};
|
2016-11-02 01:34:45 +03:00
|
|
|
|
|
|
|
QString SeriesItem::name() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeriesItem::setName(const QString &name)
|
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SeriesItem::valuesColumn() const
|
|
|
|
{
|
|
|
|
return m_valuesColumn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeriesItem::setValuesColumn(const QString &valuesColumn)
|
|
|
|
{
|
|
|
|
m_valuesColumn = valuesColumn;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SeriesItem::labelsColumn() const
|
|
|
|
{
|
|
|
|
return m_labelsColumn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeriesItem::setLabelsColumn(const QString &labelsColumn)
|
|
|
|
{
|
|
|
|
m_labelsColumn = labelsColumn;
|
|
|
|
}
|
|
|
|
|
2022-01-25 21:13:00 +03:00
|
|
|
QString SeriesItem::xAxisColumn() const
|
|
|
|
{
|
|
|
|
return m_xAxisColumn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeriesItem::setXAxisColumn(const QString &xAxisColumn)
|
|
|
|
{
|
|
|
|
m_xAxisColumn = xAxisColumn;
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
SeriesItem *SeriesItem::clone()
|
|
|
|
{
|
|
|
|
SeriesItem* result = new SeriesItem();
|
2017-09-27 01:05:22 +03:00
|
|
|
for (int i = 0; i < this->metaObject()->propertyCount(); ++i){
|
|
|
|
result->setProperty(this->metaObject()->property(i).name(),property(this->metaObject()->property(i).name()));
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeriesItem::fillSeriesData(IDataSource *dataSource)
|
|
|
|
{
|
2019-12-18 23:13:33 +03:00
|
|
|
m_data.clear();
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
if (dataSource){
|
|
|
|
dataSource->first();
|
|
|
|
int currentColorIndex = 0;
|
|
|
|
while(!dataSource->eof()){
|
|
|
|
if (!m_labelsColumn.isEmpty())
|
|
|
|
m_data.labels().append(dataSource->data(m_labelsColumn).toString());
|
2022-01-25 21:13:00 +03:00
|
|
|
if (!m_xAxisColumn.isEmpty())
|
|
|
|
m_data.xAxisValues().append(dataSource->data(m_xAxisColumn).toDouble());
|
2016-11-02 01:34:45 +03:00
|
|
|
m_data.values().append(dataSource->data(m_valuesColumn).toDouble());
|
|
|
|
m_data.colors().append((currentColorIndex<32)?color_map[currentColorIndex]:generateColor());
|
|
|
|
dataSource->next();
|
|
|
|
currentColorIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor SeriesItem::color() const
|
|
|
|
{
|
|
|
|
return m_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeriesItem::setColor(const QColor &color)
|
|
|
|
{
|
|
|
|
m_color = color;
|
|
|
|
}
|
|
|
|
|
2017-09-27 01:05:22 +03:00
|
|
|
SeriesItem::SeriesItemPreferredType SeriesItem::preferredType() const
|
|
|
|
{
|
|
|
|
return m_preferredType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeriesItem::setPreferredType(const SeriesItemPreferredType& type)
|
|
|
|
{
|
|
|
|
m_preferredType = type;
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
ChartItem::ChartItem(QObject *owner, QGraphicsItem *parent)
|
|
|
|
: ItemDesignIntf(xmlTag, owner, parent), m_legendBorder(true),
|
2022-02-08 01:16:07 +03:00
|
|
|
m_legendAlign(LegendAlignRightCenter), m_titleAlign(TitleAlignCenter),
|
2020-04-06 19:48:16 +03:00
|
|
|
m_chartType(Pie), m_labelsField(""), m_isEmpty(true),
|
2022-01-25 21:13:00 +03:00
|
|
|
m_showLegend(true), m_drawPoints(true), m_seriesLineWidth(4),
|
2022-02-08 01:16:07 +03:00
|
|
|
m_horizontalAxisOnTop(false), m_gridChartLines(AllLines),
|
|
|
|
m_legendStyle(LegendPoints)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
2022-03-14 00:49:39 +03:00
|
|
|
m_xAxisData = new AxisData(AxisData::XAxis, this);
|
2022-03-09 01:44:49 +03:00
|
|
|
m_xAxisData->setReverseDirection(true);
|
2022-03-14 00:49:39 +03:00
|
|
|
m_yAxisData = new AxisData(AxisData::YAxis, this);
|
2016-11-02 01:34:45 +03:00
|
|
|
m_labels<<"First"<<"Second"<<"Thrid";
|
|
|
|
m_chart = new PieChart(this);
|
2022-01-31 18:43:47 +03:00
|
|
|
m_chart->setTitleFont(font());
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ChartItem::~ChartItem()
|
|
|
|
{
|
|
|
|
foreach (SeriesItem* series, m_series) {
|
|
|
|
delete series;
|
|
|
|
}
|
|
|
|
m_series.clear();
|
|
|
|
delete m_chart;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartItem::TitleAlign ChartItem::titleAlign() const
|
|
|
|
{
|
|
|
|
return m_titleAlign;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setTitleAlign(const TitleAlign &titleAlign)
|
|
|
|
{
|
|
|
|
if (m_titleAlign != titleAlign){
|
|
|
|
TitleAlign oldValue = m_titleAlign;
|
|
|
|
m_titleAlign = titleAlign;
|
|
|
|
notify("titleAlign",oldValue,m_titleAlign);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
painter->save();
|
|
|
|
setupPainter(painter);
|
|
|
|
painter->setFont(transformToSceneFont(painter->font()));
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing,true);
|
|
|
|
painter->setRenderHint(QPainter::TextAntialiasing,true);
|
|
|
|
qreal borderMargin = (rect().height()*0.01>10)?(10):(rect().height()*0.01);
|
2022-01-31 18:43:47 +03:00
|
|
|
qreal maxTitleHeight = rect().height()*0.5;
|
2016-11-02 01:34:45 +03:00
|
|
|
|
|
|
|
QFont tmpFont = painter->font();
|
|
|
|
|
2022-01-31 18:43:47 +03:00
|
|
|
qreal titleOffset = 0;
|
|
|
|
if (!m_title.isEmpty()) {
|
|
|
|
QFontMetrics fm(titleFont());
|
|
|
|
const qreal titleHeight = fm.boundingRect(rect().toRect(), Qt::TextWordWrap,chartTitle()).height() + borderMargin * 2;
|
|
|
|
titleOffset = std::min(titleHeight, maxTitleHeight);
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2022-01-31 18:43:47 +03:00
|
|
|
const QRectF titleRect = QRectF(borderMargin,borderMargin,rect().width()-borderMargin*2,titleOffset);
|
2022-02-08 01:16:07 +03:00
|
|
|
QRectF legendRect = QRectF(0, 0, 0, 0);
|
2022-02-15 19:39:14 +03:00
|
|
|
QRectF diagramRect = rect().adjusted(borderMargin, titleOffset + borderMargin,
|
|
|
|
-(borderMargin * 2), -borderMargin);
|
2022-02-08 01:16:07 +03:00
|
|
|
if (m_showLegend) {
|
2020-04-06 19:48:16 +03:00
|
|
|
legendRect = m_chart->calcChartLegendRect(painter->font(), rect(), false, borderMargin, titleOffset);
|
2022-02-08 01:16:07 +03:00
|
|
|
switch(legendAlign()) {
|
|
|
|
case LegendAlignRightTop:
|
|
|
|
case LegendAlignRightBottom:
|
|
|
|
case LegendAlignRightCenter:
|
2022-02-15 19:39:14 +03:00
|
|
|
diagramRect.adjust(0, 0, -legendRect.width(), 0);
|
2022-02-08 01:16:07 +03:00
|
|
|
break;
|
|
|
|
case LegendAlignBottomLeft:
|
|
|
|
case LegendAlignBottomCenter:
|
|
|
|
case LegendAlignBottomRight:
|
2022-02-15 19:39:14 +03:00
|
|
|
diagramRect.adjust(0, 0, 0, -(legendRect.height() + borderMargin * 2));
|
2022-02-08 01:16:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
|
|
|
|
paintChartTitle(painter, titleRect);
|
2020-04-06 19:48:16 +03:00
|
|
|
if (m_showLegend)
|
|
|
|
m_chart->paintChartLegend(painter,legendRect);
|
2016-11-02 01:34:45 +03:00
|
|
|
m_chart->paintChart(painter,diagramRect);
|
|
|
|
|
|
|
|
painter->restore();
|
|
|
|
ItemDesignIntf::paint(painter,option,widget);
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:44:49 +03:00
|
|
|
QObject *ChartItem::xAxisSettings()
|
|
|
|
{
|
|
|
|
return m_xAxisData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setYAxisSettings(QObject *axis)
|
|
|
|
{
|
|
|
|
AxisData *data = dynamic_cast<AxisData*>(axis);
|
|
|
|
if (data) {
|
|
|
|
m_yAxisData->copy(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *ChartItem::yAxisSettings()
|
|
|
|
{
|
|
|
|
return m_yAxisData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setXAxisSettings(QObject *axis)
|
|
|
|
{
|
|
|
|
AxisData *data = static_cast<AxisData*>(axis);
|
|
|
|
if (data) {
|
|
|
|
m_xAxisData->copy(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AxisData *ChartItem::xAxisData()
|
|
|
|
{
|
|
|
|
return m_xAxisData;
|
|
|
|
}
|
|
|
|
|
|
|
|
AxisData *ChartItem::yAxisData()
|
|
|
|
{
|
|
|
|
return m_yAxisData;
|
|
|
|
}
|
|
|
|
|
2022-03-10 22:35:48 +03:00
|
|
|
void ChartItem::showAxisEditorDialog(bool isXAxis)
|
|
|
|
{
|
|
|
|
showDialog(new ChartAxisEditor(this, page(), isXAxis, settings()));
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
BaseDesignIntf *ChartItem::createSameTypeItem(QObject *owner, QGraphicsItem *parent)
|
|
|
|
{
|
|
|
|
ChartItem* result = new ChartItem(owner,parent);
|
|
|
|
foreach (SeriesItem* series, m_series) {
|
|
|
|
result->m_series.append(series->clone());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *ChartItem::createElement(const QString &collectionName, const QString &elementType)
|
|
|
|
{
|
|
|
|
Q_UNUSED(elementType);
|
|
|
|
if (collectionName.compare("series")==0){
|
|
|
|
SeriesItem* seriesItem = new SeriesItem();
|
|
|
|
m_series.append(seriesItem);
|
|
|
|
return seriesItem;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ChartItem::elementsCount(const QString &collectionName)
|
|
|
|
{
|
|
|
|
if (collectionName.compare("series")==0)
|
|
|
|
return m_series.count();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *ChartItem::elementAt(const QString &collectionName, int index)
|
|
|
|
{
|
|
|
|
if (collectionName.compare("series")==0)
|
|
|
|
return m_series.at(index);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::updateItemSize(DataSourceManager *dataManager, RenderPass , int )
|
|
|
|
{
|
2020-04-06 19:48:16 +03:00
|
|
|
|
|
|
|
m_isEmpty = false;
|
2016-11-02 01:34:45 +03:00
|
|
|
if (dataManager && dataManager->dataSource(m_datasource)){
|
|
|
|
IDataSource* ds = dataManager->dataSource(m_datasource);
|
|
|
|
foreach (SeriesItem* series, m_series) {
|
2020-04-06 19:48:16 +03:00
|
|
|
if (series->isEmpty()){
|
|
|
|
series->setLabelsColumn(m_labelsField);
|
2022-01-25 21:13:00 +03:00
|
|
|
series->setXAxisColumn(m_xAxisField);
|
2020-04-06 19:48:16 +03:00
|
|
|
series->fillSeriesData(ds);
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
fillLabels(ds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::fillLabels(IDataSource *dataSource)
|
|
|
|
{
|
|
|
|
m_labels.clear();
|
|
|
|
if (dataSource && !m_labelsField.isEmpty()){
|
|
|
|
dataSource->first();
|
|
|
|
while(!dataSource->eof()){
|
|
|
|
m_labels.append(dataSource->data(m_labelsField).toString());
|
|
|
|
dataSource->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *ChartItem::defaultEditor()
|
|
|
|
{
|
2022-03-09 01:44:49 +03:00
|
|
|
QWidget* editor = new ChartItemEditor(this, page(), settings());
|
2016-11-02 01:34:45 +03:00
|
|
|
editor->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:40:36 +03:00
|
|
|
bool ChartItem::isNeedUpdateSize(RenderPass pass) const
|
|
|
|
{
|
2020-04-06 19:48:16 +03:00
|
|
|
return pass == FirstPass && m_isEmpty;
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:44:49 +03:00
|
|
|
QSettings *ChartItem::settings()
|
|
|
|
{
|
|
|
|
PageDesignIntf *page = this->page();
|
|
|
|
if (page->settings()) {
|
|
|
|
return page->settings();
|
|
|
|
}
|
|
|
|
if (page->reportEditor()) {
|
|
|
|
return page->reportEditor()->settings();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:48:16 +03:00
|
|
|
bool ChartItem::showLegend() const
|
|
|
|
{
|
|
|
|
return m_showLegend;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setShowLegend(bool showLegend)
|
|
|
|
{
|
|
|
|
if (m_showLegend != showLegend){
|
|
|
|
m_showLegend = showLegend;
|
|
|
|
notify("showLegend", !m_showLegend, m_showLegend);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
m_showLegend = showLegend;
|
2019-11-26 23:40:36 +03:00
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
QList<QString> ChartItem::labels() const
|
|
|
|
{
|
|
|
|
return m_labels;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setLabels(const QList<QString> &labels)
|
|
|
|
{
|
|
|
|
m_labels = labels;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChartItem::labelsField() const
|
|
|
|
{
|
|
|
|
return m_labelsField;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setLabelsField(const QString &labelsField)
|
|
|
|
{
|
|
|
|
m_labelsField = labelsField;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChartItem::ChartType ChartItem::chartType() const
|
|
|
|
{
|
|
|
|
return m_chartType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setChartType(const ChartType &chartType)
|
|
|
|
{
|
|
|
|
if (m_chartType != chartType){
|
|
|
|
ChartType oldValue = m_chartType;
|
|
|
|
m_chartType = chartType;
|
2022-01-31 18:43:47 +03:00
|
|
|
QFont oldTitleFont = m_chart->titleFont();
|
2016-11-02 01:34:45 +03:00
|
|
|
delete m_chart;
|
|
|
|
switch (m_chartType) {
|
|
|
|
case Pie:
|
|
|
|
m_chart = new PieChart(this);
|
|
|
|
break;
|
|
|
|
case VerticalBar:
|
|
|
|
m_chart = new VerticalBarChart(this);
|
|
|
|
break;
|
|
|
|
case HorizontalBar:
|
|
|
|
m_chart = new HorizontalBarChart(this);
|
|
|
|
break;
|
2020-01-11 02:11:55 +03:00
|
|
|
case Lines:
|
|
|
|
m_chart = new LinesChart(this);
|
2022-01-25 21:46:14 +03:00
|
|
|
break;
|
|
|
|
case GridLines:
|
|
|
|
m_chart = new GridLinesChart(this);
|
|
|
|
break;
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2022-01-31 18:43:47 +03:00
|
|
|
m_chart->setTitleFont(oldTitleFont);
|
2016-11-02 01:34:45 +03:00
|
|
|
notify("chartType",oldValue,m_chartType);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChartItem::datasource() const
|
|
|
|
{
|
|
|
|
return m_datasource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setDatasource(const QString &datasource)
|
|
|
|
{
|
|
|
|
m_datasource = datasource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::paintChartTitle(QPainter *painter, QRectF titleRect)
|
|
|
|
{
|
|
|
|
painter->save();
|
2022-01-31 18:43:47 +03:00
|
|
|
QFont tmpFont = transformToSceneFont(titleFont());
|
|
|
|
QRect titleBoundingRect = QFontMetrics(tmpFont).boundingRect(rect().toRect(), Qt::TextWordWrap, chartTitle());
|
|
|
|
|
|
|
|
while ((titleBoundingRect.height() > titleRect.height() || titleBoundingRect.width() > titleRect.width())
|
|
|
|
&& tmpFont.pixelSize() > 1)
|
|
|
|
{
|
2016-11-02 01:34:45 +03:00
|
|
|
tmpFont.setPixelSize(tmpFont.pixelSize()-1);
|
2022-01-31 18:43:47 +03:00
|
|
|
titleBoundingRect = QFontMetrics(tmpFont).boundingRect(rect().toRect(), Qt::TextWordWrap, chartTitle());
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2022-01-31 18:43:47 +03:00
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
painter->setFont(tmpFont);
|
|
|
|
Qt::AlignmentFlag align = Qt::AlignCenter;
|
|
|
|
switch (m_titleAlign) {
|
|
|
|
case TitleAlignLeft:
|
|
|
|
align = Qt::AlignLeft;
|
|
|
|
break;
|
|
|
|
case TitleAlignCenter:
|
|
|
|
align = Qt::AlignCenter;
|
|
|
|
break;
|
|
|
|
case TitleAlignRight:
|
|
|
|
align = Qt::AlignRight;
|
|
|
|
break;
|
|
|
|
}
|
2022-01-31 18:43:47 +03:00
|
|
|
painter->drawText(titleRect, align | Qt::TextWordWrap, m_title);
|
2016-11-02 01:34:45 +03:00
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChartItem::LegendAlign ChartItem::legendAlign() const
|
|
|
|
{
|
|
|
|
return m_legendAlign;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setLegendAlign(const LegendAlign &legendAlign)
|
|
|
|
{
|
|
|
|
if (m_legendAlign != legendAlign){
|
|
|
|
LegendAlign oldValue = m_legendAlign;
|
|
|
|
m_legendAlign = legendAlign;
|
|
|
|
notify("legendAlign",oldValue,m_legendAlign);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 01:16:07 +03:00
|
|
|
ChartItem::LegendStyle ChartItem::legendStyle() const
|
|
|
|
{
|
|
|
|
return m_legendStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setLegendStyle(const LegendStyle &legendStyle)
|
|
|
|
{
|
|
|
|
if (m_legendStyle == legendStyle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LegendStyle oldValue = m_legendStyle;
|
|
|
|
m_legendStyle = legendStyle;
|
|
|
|
notify("legendStyle", oldValue, m_legendStyle);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
bool ChartItem::drawLegendBorder() const
|
|
|
|
{
|
|
|
|
return m_legendBorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setDrawLegendBorder(bool legendBorder)
|
|
|
|
{
|
|
|
|
if (m_legendBorder!=legendBorder){
|
|
|
|
m_legendBorder = legendBorder;
|
|
|
|
notify("legendBorder",!m_legendBorder,m_legendBorder);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChartItem::chartTitle() const
|
|
|
|
{
|
|
|
|
return m_title;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setChartTitle(const QString &title)
|
|
|
|
{
|
|
|
|
if (m_title != title){
|
|
|
|
QString oldValue = m_title;
|
|
|
|
m_title = title;
|
|
|
|
update();
|
|
|
|
notify("chartTitle",oldValue,title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<SeriesItem *> &ChartItem::series()
|
|
|
|
{
|
|
|
|
return m_series;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setSeries(const QList<SeriesItem *> &series)
|
|
|
|
{
|
|
|
|
m_series = series;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChartItem::isSeriesExists(const QString &name)
|
|
|
|
{
|
|
|
|
foreach (SeriesItem* series, m_series) {
|
|
|
|
if (series->name().compare(name)==0) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-18 12:56:05 +03:00
|
|
|
int ChartItem::seriesLineWidth() const
|
|
|
|
{
|
|
|
|
return m_seriesLineWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChartItem::drawPoints() const
|
|
|
|
{
|
|
|
|
return m_drawPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setDrawPoints(bool drawPoints)
|
|
|
|
{
|
|
|
|
if (m_drawPoints != drawPoints){
|
|
|
|
m_drawPoints = drawPoints;
|
|
|
|
notify("drawPoints", !m_drawPoints, m_drawPoints);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
m_drawPoints = drawPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setSeriesLineWidth(int newSeriesLineWidth)
|
|
|
|
{
|
|
|
|
if (m_seriesLineWidth != newSeriesLineWidth){
|
|
|
|
int oldValue = m_seriesLineWidth;
|
|
|
|
m_seriesLineWidth = newSeriesLineWidth;
|
|
|
|
notify("seriesLineWidth", oldValue, m_seriesLineWidth);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
m_seriesLineWidth = newSeriesLineWidth;
|
|
|
|
}
|
|
|
|
|
2022-01-25 21:13:00 +03:00
|
|
|
QString ChartItem::xAxisField() const
|
|
|
|
{
|
|
|
|
return m_xAxisField;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setXAxisField(const QString &xAxisField)
|
|
|
|
{
|
|
|
|
m_xAxisField = xAxisField;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChartItem::horizontalAxisOnTop() const
|
|
|
|
{
|
|
|
|
return m_horizontalAxisOnTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setHorizontalAxisOnTop(bool horizontalAxisOnTop)
|
|
|
|
{
|
|
|
|
if (m_horizontalAxisOnTop != horizontalAxisOnTop){
|
|
|
|
m_horizontalAxisOnTop = horizontalAxisOnTop;
|
|
|
|
notify("horizontalAxisOnTop", !m_horizontalAxisOnTop, m_horizontalAxisOnTop);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
m_horizontalAxisOnTop = horizontalAxisOnTop;
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:51:40 +03:00
|
|
|
ChartItem::GridChartLines ChartItem::gridChartLines() const
|
|
|
|
{
|
|
|
|
return m_gridChartLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setGridChartLines(GridChartLines flags)
|
|
|
|
{
|
|
|
|
if (m_gridChartLines == flags) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GridChartLines oldValue = m_gridChartLines;
|
|
|
|
m_gridChartLines = flags;
|
|
|
|
if (isLoading()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update(rect());
|
|
|
|
notify("gridChartLines",QVariant(oldValue),QVariant(flags));
|
|
|
|
}
|
|
|
|
|
2022-01-31 18:43:47 +03:00
|
|
|
void ChartItem::setCharItemFont(QFont value)
|
|
|
|
{
|
|
|
|
if (font() == value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QFont oldValue = font();
|
|
|
|
setFont(value);
|
|
|
|
if (!isLoading()) update();
|
|
|
|
notify("font",oldValue,value);
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont ChartItem::titleFont() const
|
|
|
|
{
|
|
|
|
return m_chart->titleFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChartItem::setTitleFont(QFont value)
|
|
|
|
{
|
|
|
|
if (m_chart->titleFont() == value){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QFont oldValue = value;
|
|
|
|
m_chart->setTitleFont(value);
|
|
|
|
if (!isLoading()) update();
|
|
|
|
notify("titleFont", oldValue, value);
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
AbstractChart::AbstractChart(ChartItem *chartItem)
|
|
|
|
:m_chartItem(chartItem)
|
|
|
|
{
|
|
|
|
m_designLabels<<QObject::tr("First")<<QObject::tr("Second")<<QObject::tr("Thrid");
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF AbstractChart::calcChartLegendRect(const QFont &font, const QRectF &parentRect, bool takeAllRect, qreal borderMargin, qreal titleOffset)
|
|
|
|
{
|
2022-02-08 01:16:07 +03:00
|
|
|
const QSizeF legendSize = calcChartLegendSize(font, parentRect.width() * 0.9);
|
2016-11-02 01:34:45 +03:00
|
|
|
qreal legendTopMargin = 0;
|
|
|
|
qreal legendBottomMargin = 0;
|
2022-02-08 01:16:07 +03:00
|
|
|
qreal legendLeftMargin = 0;
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2022-02-08 01:16:07 +03:00
|
|
|
bool isVertical = true;
|
2016-11-02 01:34:45 +03:00
|
|
|
switch (m_chartItem->legendAlign()) {
|
2022-02-08 01:16:07 +03:00
|
|
|
case ChartItem::LegendAlignRightTop:
|
|
|
|
legendTopMargin = titleOffset + borderMargin;
|
|
|
|
legendBottomMargin = parentRect.height() - (legendSize.height() + titleOffset);
|
|
|
|
isVertical = true;
|
|
|
|
break;
|
|
|
|
case ChartItem::LegendAlignRightCenter:
|
|
|
|
legendTopMargin = titleOffset + (parentRect.height() - titleOffset - legendSize.height()) / 2;
|
|
|
|
legendBottomMargin = (parentRect.height() - titleOffset - legendSize.height()) / 2;
|
|
|
|
isVertical = true;
|
2016-11-02 01:34:45 +03:00
|
|
|
break;
|
2022-02-08 01:16:07 +03:00
|
|
|
case ChartItem::LegendAlignRightBottom:
|
|
|
|
legendTopMargin = parentRect.height() - (legendSize.height() + titleOffset);
|
|
|
|
legendBottomMargin = borderMargin;
|
|
|
|
isVertical = true;
|
2016-11-02 01:34:45 +03:00
|
|
|
break;
|
2022-02-08 01:16:07 +03:00
|
|
|
case ChartItem::LegendAlignBottomLeft:
|
|
|
|
legendLeftMargin = QFontMetrics(font).height() / 2;
|
|
|
|
isVertical = false;
|
|
|
|
break;
|
|
|
|
case ChartItem::LegendAlignBottomCenter:
|
|
|
|
legendLeftMargin = (parentRect.width() - legendSize.width()) / 2;
|
|
|
|
isVertical = false;
|
|
|
|
break;
|
|
|
|
case ChartItem::LegendAlignBottomRight:
|
|
|
|
legendLeftMargin = parentRect.width() - legendSize.width() - QFontMetrics(font).height() / 2;
|
|
|
|
isVertical = false;
|
2016-11-02 01:34:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-08 01:16:07 +03:00
|
|
|
if (isVertical) {
|
|
|
|
qreal rightOffset = !takeAllRect ? ((legendSize.width() > parentRect.width() / 2 - borderMargin) ?
|
|
|
|
(parentRect.width() / 2) :
|
|
|
|
(parentRect.width() - legendSize.width())) : 0;
|
|
|
|
return parentRect.adjusted(
|
|
|
|
rightOffset,
|
|
|
|
(legendSize.height()>(parentRect.height()-titleOffset))?(titleOffset):(legendTopMargin),
|
|
|
|
-borderMargin,
|
|
|
|
(legendSize.height()>(parentRect.height()-titleOffset))?(0):(-legendBottomMargin)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const qreal verticalOffset = borderMargin * 2;
|
|
|
|
return parentRect.adjusted(
|
|
|
|
legendLeftMargin,
|
|
|
|
(parentRect.height()) - (legendSize.height() + verticalOffset),
|
|
|
|
-(parentRect.width() - (legendSize.width() + legendLeftMargin)),
|
|
|
|
-verticalOffset
|
|
|
|
);
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2022-01-31 18:43:47 +03:00
|
|
|
|
|
|
|
QFont AbstractChart::titleFont()
|
|
|
|
{
|
|
|
|
return m_titleFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractChart::setTitleFont(const QFont &value)
|
|
|
|
{
|
|
|
|
m_titleFont = value;
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
void AbstractChart::prepareLegendToPaint(QRectF &legendRect, QPainter *painter)
|
|
|
|
{
|
|
|
|
QFont tmpFont = painter->font();
|
2022-02-08 01:16:07 +03:00
|
|
|
switch(m_chartItem->legendAlign()) {
|
|
|
|
case ChartItem::LegendAlignBottomLeft:
|
|
|
|
case ChartItem::LegendAlignBottomCenter:
|
|
|
|
case ChartItem::LegendAlignBottomRight: {
|
2022-03-06 17:00:55 +03:00
|
|
|
const qreal maxWidth = legendRect.width() * 0.95;
|
2022-03-05 19:36:37 +03:00
|
|
|
qreal legendWidth = std::accumulate(m_legendColumnWidths.cbegin(), m_legendColumnWidths.cend(), 0.0);
|
|
|
|
if (legendWidth < maxWidth) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while ( (legendWidth > maxWidth) && tmpFont.pixelSize() > 1) {
|
|
|
|
tmpFont.setPixelSize(tmpFont.pixelSize() - 1);
|
|
|
|
calcChartLegendSize(tmpFont, legendRect.width());
|
|
|
|
legendWidth = std::accumulate(m_legendColumnWidths.cbegin(), m_legendColumnWidths.cend(), 0.0);
|
|
|
|
}
|
|
|
|
painter->setFont(tmpFont);
|
2022-02-08 01:16:07 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ChartItem::LegendAlignRightTop:
|
|
|
|
case ChartItem::LegendAlignRightCenter:
|
|
|
|
case ChartItem::LegendAlignRightBottom:
|
|
|
|
QSizeF legendSize = calcChartLegendSize(tmpFont, legendRect.width());
|
2022-03-05 19:36:37 +03:00
|
|
|
if ((legendSize.height() <= legendRect.height() && legendSize.width() <= legendRect.width())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while ((legendSize.height() > legendRect.height() || legendSize.width() > legendRect.width())
|
|
|
|
&& tmpFont.pixelSize() > 1)
|
|
|
|
{
|
|
|
|
tmpFont.setPixelSize(tmpFont.pixelSize() - 1);
|
|
|
|
legendSize = calcChartLegendSize(tmpFont, legendRect.width());
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2022-03-05 19:36:37 +03:00
|
|
|
painter->setFont(tmpFont);
|
|
|
|
legendRect = calcChartLegendRect(tmpFont, legendRect, true, 0, 0);
|
2022-02-08 01:16:07 +03:00
|
|
|
break;
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractSeriesChart::AbstractSeriesChart(ChartItem *chartItem)
|
|
|
|
:AbstractChart(chartItem)
|
|
|
|
{
|
|
|
|
m_designValues[0] = 10;
|
|
|
|
m_designValues[1] = 35;
|
|
|
|
m_designValues[2] = 15;
|
|
|
|
m_designValues[3] = 5;
|
|
|
|
m_designValues[4] = 20;
|
|
|
|
m_designValues[5] = 10;
|
|
|
|
m_designValues[6] = 40;
|
|
|
|
m_designValues[7] = 20;
|
|
|
|
m_designValues[8] = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal AbstractSeriesChart::maxValue()
|
|
|
|
{
|
2022-03-09 01:44:49 +03:00
|
|
|
return m_chartItem->yAxisData()->maxValue();
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal AbstractSeriesChart::minValue()
|
|
|
|
{
|
2022-03-09 01:44:49 +03:00
|
|
|
return m_chartItem->yAxisData()->minValue();
|
2022-01-18 12:56:05 +03:00
|
|
|
}
|
|
|
|
|
2022-03-09 01:44:49 +03:00
|
|
|
AxisData &AbstractSeriesChart::xAxisData() const
|
2022-01-18 12:56:05 +03:00
|
|
|
{
|
2022-03-09 01:44:49 +03:00
|
|
|
return *m_chartItem->xAxisData();
|
2022-01-25 21:13:00 +03:00
|
|
|
}
|
|
|
|
|
2022-03-09 01:44:49 +03:00
|
|
|
AxisData &AbstractSeriesChart::yAxisData() const
|
2022-01-25 21:28:32 +03:00
|
|
|
{
|
2022-03-09 01:44:49 +03:00
|
|
|
return *m_chartItem->yAxisData();
|
2022-01-05 14:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractSeriesChart::updateMinAndMaxValues()
|
|
|
|
{
|
|
|
|
if (m_chartItem->itemMode() == DesignMode) {
|
2022-03-10 22:35:48 +03:00
|
|
|
m_chartItem->xAxisData()->updateForDesignMode();
|
|
|
|
m_chartItem->yAxisData()->updateForDesignMode();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-13 09:02:52 +03:00
|
|
|
qreal maxYValue = 0;
|
2022-03-14 12:04:13 +03:00
|
|
|
qreal minYValue = std::numeric_limits<qreal>::max();
|
2022-03-13 09:02:52 +03:00
|
|
|
qreal maxXValue = 0;
|
2022-03-14 12:04:13 +03:00
|
|
|
qreal minXValue = std::numeric_limits<qreal>::max();
|
2022-03-13 09:02:52 +03:00
|
|
|
|
2022-03-10 22:35:48 +03:00
|
|
|
for (SeriesItem* series : m_chartItem->series()){
|
|
|
|
for (qreal value : series->data()->values()){
|
|
|
|
minYValue = std::min(minYValue, value);
|
|
|
|
maxYValue = std::max(maxYValue, value);
|
|
|
|
}
|
|
|
|
if (series->data()->xAxisValues().isEmpty()) {
|
|
|
|
// Grid plot starts from 0 on x axis so x range must be decresed by 1
|
|
|
|
const bool startingFromZero = m_chartItem->chartType() == ChartItem::GridLines;
|
|
|
|
const qreal valuesCount = this->valuesCount() - (startingFromZero ? 1 : 0);
|
|
|
|
minXValue = std::min(0.0, minXValue);
|
|
|
|
maxXValue = std::max(valuesCount, maxXValue);
|
|
|
|
} else {
|
|
|
|
for (qreal value : series->data()->xAxisValues()){
|
|
|
|
minXValue = std::min(value, minXValue);
|
|
|
|
maxXValue = std::max(value, maxXValue);
|
2022-01-25 21:13:00 +03:00
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-18 12:56:05 +03:00
|
|
|
|
2022-03-09 01:44:49 +03:00
|
|
|
m_chartItem->xAxisData()->update(minXValue, maxXValue);
|
|
|
|
m_chartItem->yAxisData()->update(minYValue, maxYValue);
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
qreal AbstractSeriesChart::hPadding(QRectF chartRect)
|
|
|
|
{
|
|
|
|
return (chartRect.width() * 0.02);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal AbstractSeriesChart::vPadding(QRectF chartRect)
|
|
|
|
{
|
|
|
|
return (chartRect.height() * 0.02);
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
int AbstractSeriesChart::valuesCount()
|
|
|
|
{
|
2020-01-11 02:11:55 +03:00
|
|
|
if (m_chartItem->itemMode() == DesignMode) return 3;
|
|
|
|
return (m_chartItem->series().isEmpty()) ? 0 : m_chartItem->series().at(0)->data()->values().count();
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int AbstractSeriesChart::seriesCount()
|
|
|
|
{
|
2020-01-11 02:11:55 +03:00
|
|
|
if (m_chartItem->itemMode() == DesignMode) return 3;
|
2016-11-02 01:34:45 +03:00
|
|
|
return m_chartItem->series().count();
|
|
|
|
}
|
|
|
|
|
2022-02-08 01:16:07 +03:00
|
|
|
QSizeF AbstractSeriesChart::calcChartLegendSize(const QFont &font, const qreal maxWidth)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
|
|
|
QFontMetrics fm(font);
|
|
|
|
|
2022-02-08 01:16:07 +03:00
|
|
|
switch(m_chartItem->legendAlign()) {
|
|
|
|
case ChartItem::LegendAlignBottomLeft:
|
|
|
|
case ChartItem::LegendAlignBottomCenter:
|
|
|
|
case ChartItem::LegendAlignBottomRight: {
|
|
|
|
const qreal seriesCount = m_chartItem->series().isEmpty() ? m_designLabels.size() : m_chartItem->series().size();
|
|
|
|
const qreal indicatorWidth = fm.height() * 1.5;
|
|
|
|
m_legendColumnWidths.clear();
|
|
|
|
while (!calculateLegendColumnWidths(indicatorWidth, maxWidth, fm)) {
|
|
|
|
// Nothing to do here
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2022-02-08 01:16:07 +03:00
|
|
|
if (m_legendColumnWidths.isEmpty()) {
|
|
|
|
m_legendColumnWidths.append(0);
|
|
|
|
}
|
|
|
|
const qreal columnCount = m_legendColumnWidths.size();
|
|
|
|
const qreal rowCount = std::ceil(seriesCount / columnCount);
|
|
|
|
QSizeF legendSize(std::accumulate(m_legendColumnWidths.cbegin(), m_legendColumnWidths.cend(), 0.0) + fm.height() / 2,
|
|
|
|
(rowCount + 1) * fm.height());
|
|
|
|
if (legendSize.width() > maxWidth) {
|
|
|
|
legendSize.setWidth(maxWidth);
|
|
|
|
}
|
|
|
|
return legendSize;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
qreal cw = 0;
|
|
|
|
qreal maxWidth = 0;
|
|
|
|
|
|
|
|
if (m_chartItem->series().isEmpty()) {
|
|
|
|
foreach(QString label, m_designLabels){
|
|
|
|
cw += fm.height();
|
|
|
|
if (maxWidth<fm.boundingRect(label).width())
|
|
|
|
maxWidth = fm.boundingRect(label).width()+10;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach(SeriesItem* series, m_chartItem->series()){
|
|
|
|
cw += fm.height();
|
|
|
|
if (maxWidth<fm.boundingRect(series->name()).width())
|
|
|
|
maxWidth = fm.boundingRect(series->name()).width()+10;
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2022-02-08 01:16:07 +03:00
|
|
|
cw += fm.height();
|
|
|
|
return QSizeF(maxWidth+fm.height()*2,cw);
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2022-02-08 01:16:07 +03:00
|
|
|
return QSizeF();
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2020-05-18 22:12:05 +03:00
|
|
|
bool AbstractSeriesChart::verticalLabels(QPainter* painter, QRectF labelsRect)
|
|
|
|
{
|
|
|
|
if (valuesCount() == 0) return false;
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
qreal hStep = (labelsRect.width() / valuesCount());
|
|
|
|
QFontMetrics fm = painter->fontMetrics();
|
|
|
|
foreach(QString label, m_chartItem->labels()){
|
2021-08-23 08:07:08 +03:00
|
|
|
if (fm.boundingRect(label).width() > hStep){
|
2020-01-11 02:11:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
void AbstractSeriesChart::paintHorizontalLabels(QPainter *painter, QRectF labelsRect)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
2020-05-18 22:12:05 +03:00
|
|
|
if (valuesCount() == 0) return;
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
painter->save();
|
|
|
|
qreal hStep = (labelsRect.width() / valuesCount());
|
|
|
|
if (!m_chartItem->labels().isEmpty()){
|
|
|
|
if (verticalLabels(painter, labelsRect)){
|
|
|
|
painter->rotate(270);
|
|
|
|
painter->translate( -(labelsRect.top()+labelsRect.height()), labelsRect.left() );
|
|
|
|
foreach (QString label, m_chartItem->labels()) {
|
|
|
|
painter->drawText(QRectF(QPoint(0,0),
|
|
|
|
QSize(labelsRect.height()-4, hStep)), Qt::AlignVCenter | Qt::AlignRight, label);
|
|
|
|
painter->translate(0,hStep);
|
|
|
|
}
|
|
|
|
painter->rotate(-270);
|
|
|
|
} else {
|
|
|
|
painter->translate( labelsRect.left(), labelsRect.top() );
|
|
|
|
foreach (QString label, m_chartItem->labels()) {
|
|
|
|
painter->drawText(QRectF(QPoint(0, 4),
|
|
|
|
QSize(hStep, labelsRect.height()-4)), Qt::AlignHCenter | Qt::AlignTop, label);
|
|
|
|
painter->translate(hStep, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
painter->restore();
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
void AbstractSeriesChart::paintVerticalLabels(QPainter *painter, QRectF labelsRect)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
2020-05-18 22:12:05 +03:00
|
|
|
if (valuesCount() == 0) return;
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
painter->save();
|
|
|
|
painter->setFont(adaptLabelsFont(labelsRect.adjusted(0, 0, -hPadding(m_chartItem->rect()), 0),
|
|
|
|
painter->font()));
|
|
|
|
qreal vStep = (labelsRect.height() / valuesCount());
|
|
|
|
int curLabel = 0;
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
painter->translate(labelsRect.topLeft());
|
|
|
|
if (!m_chartItem->labels().isEmpty()){
|
|
|
|
foreach (QString label, m_chartItem->labels()) {
|
|
|
|
painter->drawText(QRectF(QPoint(0,vStep*curLabel),
|
|
|
|
QSize(labelsRect.width()-hPadding(m_chartItem->rect()),vStep)),
|
|
|
|
Qt::AlignVCenter | Qt::AlignRight | Qt::TextWordWrap,label);
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
curLabel++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
painter->restore();
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
void AbstractSeriesChart::paintHorizontalGrid(QPainter *painter, QRectF gridRect)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
|
|
|
painter->save();
|
2022-01-18 12:56:05 +03:00
|
|
|
|
|
|
|
const AxisData &yAxisData = this->yAxisData();
|
|
|
|
|
|
|
|
const int segmentCount = yAxisData.segmentCount();
|
|
|
|
const int lineCount = segmentCount + 1;
|
2016-11-02 01:34:45 +03:00
|
|
|
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing,false);
|
2022-01-18 12:56:05 +03:00
|
|
|
qreal hStep = (gridRect.width() - painter->fontMetrics().boundingRect(QString::number(maxValue())).width()) / segmentCount;
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2022-01-30 19:02:24 +03:00
|
|
|
painter->setFont(adaptFont(hStep-4, painter->font(), yAxisData));
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2022-01-27 21:20:44 +03:00
|
|
|
QPointF textPos;
|
|
|
|
if (m_chartItem->horizontalAxisOnTop()) {
|
|
|
|
textPos.setY(gridRect.top());
|
|
|
|
} else {
|
|
|
|
textPos.setY(gridRect.bottom() - painter->fontMetrics().height());
|
|
|
|
}
|
2022-01-18 12:56:05 +03:00
|
|
|
for (int i = 0 ; i < lineCount ; i++ ) {
|
2022-01-27 21:20:44 +03:00
|
|
|
const qreal x = gridRect.left() + hStep * i;
|
|
|
|
textPos.setX(x + 4);
|
|
|
|
painter->drawText(QRectF(textPos, QSizeF(hStep, painter->fontMetrics().height())),
|
2022-01-25 22:05:18 +03:00
|
|
|
axisLabel(i, yAxisData));
|
2022-01-27 21:20:44 +03:00
|
|
|
painter->drawLine(x, gridRect.bottom(), x, gridRect.top());
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
void AbstractSeriesChart::paintVerticalGrid(QPainter *painter, QRectF gridRect)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
2022-01-18 12:56:05 +03:00
|
|
|
const AxisData &yAxisData = this->yAxisData();
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing,false);
|
2022-01-18 12:56:05 +03:00
|
|
|
|
|
|
|
const int segmentCount = yAxisData.segmentCount();
|
|
|
|
const int lineCount = segmentCount + 1;
|
|
|
|
qreal vStep = gridRect.height() / segmentCount;
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2022-01-05 14:13:05 +03:00
|
|
|
const qreal valuesHMargin = this->valuesHMargin(painter);
|
2022-01-18 12:56:05 +03:00
|
|
|
const int fontHeight = painter->fontMetrics().height();
|
|
|
|
const int halfFontHeight = fontHeight / 2;
|
2022-01-21 10:14:21 +03:00
|
|
|
const qreal textPositionOffset = valuesHMargin * 0.2;
|
2022-01-05 14:13:05 +03:00
|
|
|
|
2022-01-18 12:56:05 +03:00
|
|
|
const QTextOption verticalTextOption(Qt::AlignRight);
|
|
|
|
for (int i = 0 ; i < lineCount ; i++ ) {
|
|
|
|
const qreal y = vStep * i;
|
2022-01-21 10:14:21 +03:00
|
|
|
painter->drawText(QRectF(gridRect.bottomLeft()-QPointF(textPositionOffset,y+halfFontHeight),
|
2022-01-18 12:56:05 +03:00
|
|
|
QSizeF(valuesHMargin,fontHeight)),
|
2022-01-25 21:46:14 +03:00
|
|
|
axisLabel(i, yAxisData),
|
2022-01-18 12:56:05 +03:00
|
|
|
verticalTextOption);
|
|
|
|
painter->drawLine(gridRect.bottomLeft()-QPointF(-valuesHMargin,y),
|
|
|
|
gridRect.bottomRight()-QPointF(0,y));
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2020-01-11 02:11:55 +03:00
|
|
|
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing,true);
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2022-01-25 21:46:14 +03:00
|
|
|
void AbstractSeriesChart::paintGrid(QPainter *painter, QRectF gridRect)
|
|
|
|
{
|
|
|
|
painter->save();
|
|
|
|
|
|
|
|
const AxisData &yAxisData = this->yAxisData();
|
2022-03-14 00:49:39 +03:00
|
|
|
AxisData &xAxisData = this->xAxisData();
|
2022-01-25 21:46:14 +03:00
|
|
|
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing,false);
|
|
|
|
|
|
|
|
const int xAxisSegmentCount = xAxisData.segmentCount();
|
|
|
|
const int xAxisLineCount = xAxisSegmentCount + 1;
|
|
|
|
const int yAxisSegmentCount = yAxisData.segmentCount();
|
|
|
|
const int yAxisLineCount = yAxisSegmentCount + 1;
|
|
|
|
|
|
|
|
const int fontHeight = painter->fontMetrics().height();
|
|
|
|
const int halfFontHeight = fontHeight / 2;
|
2022-01-29 10:57:50 +03:00
|
|
|
const QSizeF gridOffset = QSizeF(hPadding(gridRect), vPadding(gridRect));
|
2022-01-25 21:46:14 +03:00
|
|
|
const qreal valuesHMargin = this->valuesHMargin(painter);
|
|
|
|
const qreal vStep = gridRect.height() / yAxisSegmentCount;
|
2022-01-29 10:57:50 +03:00
|
|
|
const qreal hStep = (gridRect.width() - valuesHMargin - gridOffset.width()) / xAxisSegmentCount;
|
2022-01-30 19:46:04 +03:00
|
|
|
const qreal textPositionHOffset = valuesHMargin * 0.1;
|
2022-01-29 10:57:50 +03:00
|
|
|
|
2022-01-25 21:46:14 +03:00
|
|
|
// Vertical axis lines
|
|
|
|
const QTextOption verticalTextOption(Qt::AlignRight);
|
|
|
|
for (int i = 0 ; i < yAxisLineCount ; i++ ) {
|
|
|
|
const qreal y = vStep * i;
|
2022-01-27 21:51:40 +03:00
|
|
|
const bool drawFullLine = m_chartItem->gridChartLines() & ChartItem::HorizontalLine
|
|
|
|
|| i == 0 || i == xAxisSegmentCount;
|
2022-01-29 10:57:50 +03:00
|
|
|
painter->drawText(QRectF(gridRect.bottomLeft()-QPointF(textPositionHOffset, y + halfFontHeight),
|
2022-01-25 21:46:14 +03:00
|
|
|
QSizeF(valuesHMargin,fontHeight)),
|
|
|
|
axisLabel(i, yAxisData),
|
|
|
|
verticalTextOption);
|
2022-01-27 21:51:40 +03:00
|
|
|
|
|
|
|
QPointF lineEndPos = gridRect.bottomRight() - QPointF(0, y);
|
|
|
|
if (!drawFullLine) {
|
2022-01-29 10:57:50 +03:00
|
|
|
lineEndPos.setX(gridRect.left() + valuesHMargin + gridOffset.width());
|
2022-01-27 21:51:40 +03:00
|
|
|
}
|
|
|
|
painter->drawLine(gridRect.bottomLeft() - QPointF(-valuesHMargin, y), lineEndPos);
|
2022-01-25 21:46:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Horizontal axis lines
|
|
|
|
for (int i = 0 ; i < xAxisLineCount ; i++) {
|
2022-01-29 10:57:50 +03:00
|
|
|
const qreal x = gridRect.left() + hStep * i + valuesHMargin + gridOffset.width();
|
2022-01-27 21:51:40 +03:00
|
|
|
const bool drawFullLine = m_chartItem->gridChartLines() & ChartItem::VerticalLine
|
|
|
|
|| i == 0 || i == xAxisSegmentCount;
|
2022-01-25 22:05:18 +03:00
|
|
|
const QString text = axisLabel(i, xAxisData);
|
2022-01-25 21:46:14 +03:00
|
|
|
|
|
|
|
if (m_chartItem->horizontalAxisOnTop()) {
|
2022-01-29 10:57:50 +03:00
|
|
|
painter->drawLine(x, gridRect.top() - gridOffset.height(),
|
2022-01-25 21:46:14 +03:00
|
|
|
x, (drawFullLine ? gridRect.bottom() : gridRect.top()));
|
2022-03-20 16:12:28 +03:00
|
|
|
painter->drawText(QRectF(x - painter->fontMetrics().boundingRect(text).width() / 2,
|
2022-01-29 10:57:50 +03:00
|
|
|
gridRect.top() - (fontHeight + gridOffset.height()),
|
2022-01-25 21:46:14 +03:00
|
|
|
hStep, fontHeight),
|
|
|
|
text);
|
|
|
|
} else {
|
2022-01-29 10:57:50 +03:00
|
|
|
painter->drawLine(x, gridRect.bottom() + gridOffset.height(),
|
2022-01-25 21:46:14 +03:00
|
|
|
x, (drawFullLine ? gridRect.top() : gridRect.bottom()));
|
2022-03-20 16:12:28 +03:00
|
|
|
painter->drawText(QRectF(x - painter->fontMetrics().boundingRect(text).width() / 2,
|
2022-01-29 10:57:50 +03:00
|
|
|
gridRect.bottom() + halfFontHeight * 0 + gridOffset.height(),
|
2022-01-25 21:46:14 +03:00
|
|
|
hStep, fontHeight),
|
|
|
|
text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
void AbstractSeriesChart::drawSegment(QPainter *painter, QPoint startPoint, QPoint endPoint, QColor color)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
2022-01-18 12:56:05 +03:00
|
|
|
int radius = m_chartItem->seriesLineWidth();
|
2020-01-11 02:11:55 +03:00
|
|
|
QPen pen(color);
|
|
|
|
pen.setWidth(radius);
|
|
|
|
painter->setPen(pen);
|
|
|
|
painter->drawLine(startPoint, endPoint);
|
2022-01-18 12:56:05 +03:00
|
|
|
if (!m_chartItem->drawPoints()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-11 02:11:55 +03:00
|
|
|
QRect startPointRect(startPoint,startPoint);
|
|
|
|
QRect endPointRect(endPoint,endPoint);
|
|
|
|
painter->setBrush(color);
|
|
|
|
painter->drawEllipse(startPointRect.adjusted(radius,radius,-radius,-radius));
|
|
|
|
painter->drawEllipse(endPointRect.adjusted(radius,radius,-radius,-radius));
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
qreal AbstractSeriesChart::valuesHMargin(QPainter *painter)
|
|
|
|
{
|
2022-01-18 12:56:05 +03:00
|
|
|
qreal max = 0;
|
|
|
|
const AxisData &yAxisData = this->yAxisData();
|
|
|
|
const int offset = 4;
|
|
|
|
const int yAxisLineCount = yAxisData.segmentCount() + 1;
|
|
|
|
|
|
|
|
for (int i = 0 ; i < yAxisLineCount ; i++) {
|
2022-01-25 21:46:14 +03:00
|
|
|
const QString label = axisLabel(i, yAxisData);
|
2022-01-18 12:56:05 +03:00
|
|
|
max = std::max(max, (qreal)painter->fontMetrics().boundingRect(label).width()+offset);
|
|
|
|
}
|
|
|
|
return max;
|
2020-01-11 02:11:55 +03:00
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
qreal AbstractSeriesChart::valuesVMargin(QPainter *painter)
|
|
|
|
{
|
|
|
|
return painter->fontMetrics().height();
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
QFont AbstractSeriesChart::adaptLabelsFont(QRectF rect, QFont font)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
|
|
|
QString maxWord;
|
|
|
|
QFontMetrics fm(font);
|
|
|
|
|
|
|
|
foreach(QString label, m_chartItem->labels()){
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2016-11-02 01:34:45 +03:00
|
|
|
foreach (QString currentWord, label.split(QRegExp("\\W+"))){
|
2021-08-23 08:07:08 +03:00
|
|
|
#else
|
|
|
|
foreach (QString currentWord, label.split(QRegularExpression("\\W+"))){
|
|
|
|
#endif
|
|
|
|
if (fm.boundingRect(maxWord).width() < fm.boundingRect(currentWord).width()) maxWord = currentWord;
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:07:08 +03:00
|
|
|
qreal curWidth = fm.boundingRect(maxWord).width();
|
2016-11-02 01:34:45 +03:00
|
|
|
QFont tmpFont = font;
|
2020-01-11 02:11:55 +03:00
|
|
|
while (curWidth>rect.width() && tmpFont.pixelSize() > 1){
|
|
|
|
tmpFont.setPixelSize(tmpFont.pixelSize() - 1);
|
2016-11-02 01:34:45 +03:00
|
|
|
QFontMetricsF tmpFM(tmpFont);
|
2021-08-23 08:07:08 +03:00
|
|
|
curWidth = tmpFM.boundingRect(maxWord).width();
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
return tmpFont;
|
|
|
|
}
|
|
|
|
|
2022-01-30 19:02:24 +03:00
|
|
|
QFont AbstractSeriesChart::adaptFont(qreal width, QFont font, const AxisData &axisData)
|
2016-11-02 01:34:45 +03:00
|
|
|
{
|
|
|
|
QFont tmpFont = font;
|
2022-01-30 19:02:24 +03:00
|
|
|
const int axisLineCount = axisData.segmentCount() + 1;
|
2016-11-02 01:34:45 +03:00
|
|
|
QScopedPointer<QFontMetricsF> fm(new QFontMetricsF(tmpFont));
|
2022-01-30 19:02:24 +03:00
|
|
|
for (int i = 0 ; i < axisLineCount ; i++) {
|
|
|
|
QString strValue = axisLabel(i, axisData);
|
|
|
|
qreal curWidth = fm->boundingRect(strValue).width();
|
|
|
|
while (curWidth > width && tmpFont.pixelSize() > 1){
|
|
|
|
tmpFont.setPixelSize(tmpFont.pixelSize() - 1);
|
|
|
|
fm.reset(new QFontMetricsF(tmpFont));
|
|
|
|
curWidth = fm->boundingRect(strValue).width();
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
return tmpFont;
|
|
|
|
}
|
|
|
|
|
2022-01-25 21:46:14 +03:00
|
|
|
QString AbstractSeriesChart::axisLabel(int i, const AxisData &axisData)
|
2022-01-18 12:56:05 +03:00
|
|
|
{
|
2022-01-25 21:46:14 +03:00
|
|
|
const qreal min = axisData.rangeMin();
|
|
|
|
const qreal step = axisData.step();
|
2022-03-13 13:13:22 +03:00
|
|
|
qreal value = 0;
|
2022-03-14 00:49:39 +03:00
|
|
|
if (axisData.type() == AxisData::YAxis && axisData.reverseDirection() && min >= 0) {
|
2022-03-13 13:13:22 +03:00
|
|
|
value = min + (axisData.segmentCount() - i) * step;
|
|
|
|
} else {
|
|
|
|
value = min + i * step;
|
|
|
|
}
|
2022-01-18 12:56:05 +03:00
|
|
|
if (std::floor(step) == step) {
|
|
|
|
return QString::number(value);
|
|
|
|
}
|
|
|
|
// For float round numbers to small precision
|
2022-01-25 21:46:14 +03:00
|
|
|
return QString::number(round(value * 100.0) / 100.0);
|
2022-01-18 12:56:05 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 01:16:07 +03:00
|
|
|
bool AbstractSeriesChart::calculateLegendColumnWidths(qreal indicatorWidth, qreal maxWidth, const QFontMetrics &fm)
|
|
|
|
{
|
2022-03-05 18:40:12 +03:00
|
|
|
// This method is called in the loop, because to handle case when we get
|
|
|
|
// 3 small series names in first row and then in second row small name and long name.
|
|
|
|
// In this case we need to set maximum column count to 2 and iterate from the start to recalculate
|
|
|
|
// all the sizes
|
2022-02-08 01:16:07 +03:00
|
|
|
qreal currentRowWidth = 0;
|
|
|
|
int currentColumn = 0;
|
2022-03-05 18:40:12 +03:00
|
|
|
// During first iteration it is updated when moving to second row
|
|
|
|
// After first iteration some column width are already calculated and are set as max,
|
|
|
|
// because all rows need to have same column count (except last one)
|
2022-02-08 01:16:07 +03:00
|
|
|
int maxColumnCount = m_legendColumnWidths.size();
|
|
|
|
if (m_chartItem->series().isEmpty()) {
|
|
|
|
for (int i=0 ; i < m_designLabels.size() ; ++i) {
|
|
|
|
const qreal itemWidth = (qreal)(fm.boundingRect(m_designLabels[i]).width()) + indicatorWidth;
|
|
|
|
if (!calculateLegendSingleColumnWidth(currentRowWidth, currentColumn, maxColumnCount, itemWidth, maxWidth)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0 ; i < m_chartItem->series().size() ; ++i) {
|
|
|
|
SeriesItem* series = m_chartItem->series().at(i);
|
|
|
|
const qreal itemWidth = (qreal)(fm.boundingRect(series->name()).width()) + indicatorWidth;
|
|
|
|
if (!calculateLegendSingleColumnWidth(currentRowWidth, currentColumn, maxColumnCount, itemWidth, maxWidth)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-05 18:40:12 +03:00
|
|
|
bool AbstractSeriesChart::calculateLegendSingleColumnWidth(qreal ¤tRowWidth, int ¤tColumn, int &maxColumnCount,
|
|
|
|
const qreal itemWidth, const qreal maxRowWidth)
|
2022-02-08 01:16:07 +03:00
|
|
|
{
|
2022-03-05 18:40:12 +03:00
|
|
|
const bool maxColumnCountDefined = maxColumnCount > 0;
|
|
|
|
// Check if there is enough space for current item in the row
|
|
|
|
const bool isEnoughSpaceInRowForItem = currentRowWidth + itemWidth > maxRowWidth;
|
|
|
|
// Check if it is last column already
|
|
|
|
const bool lastColumnReached = (maxColumnCountDefined && currentColumn >= maxColumnCount);
|
2022-02-08 01:16:07 +03:00
|
|
|
if (isEnoughSpaceInRowForItem || lastColumnReached) {
|
2022-03-05 18:40:12 +03:00
|
|
|
// Move to next row
|
2022-02-08 01:16:07 +03:00
|
|
|
currentColumn = 0;
|
2022-03-05 18:40:12 +03:00
|
|
|
// Set column count when moving to second row (next rows cannot have more columns)
|
|
|
|
if (!maxColumnCountDefined) {
|
2022-02-08 01:16:07 +03:00
|
|
|
maxColumnCount = currentColumn + 1;
|
|
|
|
}
|
|
|
|
currentRowWidth = itemWidth;
|
|
|
|
} else {
|
2022-03-05 18:40:12 +03:00
|
|
|
// Add next column in the row
|
2022-02-08 01:16:07 +03:00
|
|
|
currentRowWidth += itemWidth;
|
|
|
|
}
|
2022-03-05 18:40:12 +03:00
|
|
|
|
|
|
|
// Add new column or update already existing column width
|
2022-02-08 01:16:07 +03:00
|
|
|
if (currentColumn >= m_legendColumnWidths.size()) {
|
2022-03-05 18:40:12 +03:00
|
|
|
// Append new column
|
2022-02-08 01:16:07 +03:00
|
|
|
m_legendColumnWidths.append(itemWidth);
|
|
|
|
} else if (m_legendColumnWidths.at(currentColumn) < itemWidth) {
|
2022-03-05 18:40:12 +03:00
|
|
|
// Update size if item in column is bigger than items in same column in previous rows
|
2022-02-08 01:16:07 +03:00
|
|
|
m_legendColumnWidths[currentColumn] = itemWidth;
|
2022-03-05 18:40:12 +03:00
|
|
|
// After any updating column size we must recheck if all columns fit in the max row width
|
|
|
|
qreal rowWidth = itemWidth;
|
2022-02-08 01:16:07 +03:00
|
|
|
for (int c = 1 ; c < m_legendColumnWidths.size() ; c++) {
|
2022-03-05 18:40:12 +03:00
|
|
|
rowWidth += m_legendColumnWidths.at(c);
|
|
|
|
// When column widths exceed max row width remove columns at the end
|
|
|
|
if (rowWidth > maxRowWidth) {
|
2022-02-08 01:16:07 +03:00
|
|
|
m_legendColumnWidths.remove(c, m_legendColumnWidths.size() - c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 18:40:12 +03:00
|
|
|
// Return back and re-iterate from start to make sure everything fits
|
2022-02-08 01:16:07 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++currentColumn;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<qreal> AbstractChart::legendColumnWidths() const
|
|
|
|
{
|
|
|
|
return m_legendColumnWidths;
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
void AbstractBarChart::paintChartLegend(QPainter *painter, QRectF legendRect)
|
|
|
|
{
|
|
|
|
prepareLegendToPaint(legendRect, painter);
|
|
|
|
painter->setPen(Qt::black);
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing,false);
|
|
|
|
if (m_chartItem->drawLegendBorder())
|
|
|
|
painter->drawRect(legendRect);
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing,true);
|
2022-02-08 01:16:07 +03:00
|
|
|
|
|
|
|
const qreal halfFontSize = painter->fontMetrics().height() / 2;
|
|
|
|
int indicatorSize = halfFontSize;
|
|
|
|
const QRectF indicatorsRect = legendRect.adjusted(halfFontSize, halfFontSize, 0, 0);
|
|
|
|
|
|
|
|
bool isHorizontal = false;
|
|
|
|
switch(m_chartItem->legendAlign()) {
|
|
|
|
case ChartItem::LegendAlignBottomLeft:
|
|
|
|
case ChartItem::LegendAlignBottomCenter:
|
|
|
|
case ChartItem::LegendAlignBottomRight:
|
|
|
|
isHorizontal = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isHorizontal = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
|
|
|
|
if (!m_chartItem->series().isEmpty()){
|
2022-02-08 01:16:07 +03:00
|
|
|
for (int i = 0 ; i < m_chartItem->series().size() ; ++i) {
|
|
|
|
SeriesItem* series = m_chartItem->series().at(i);
|
|
|
|
if (isHorizontal) {
|
|
|
|
drawHorizontalLegendItem(painter, i, series->name(), indicatorSize, indicatorsRect, series->color());
|
|
|
|
} else {
|
|
|
|
drawVerticalLegendItem(painter, i, series->name(), indicatorSize, indicatorsRect, series->color());
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
2022-02-08 01:16:07 +03:00
|
|
|
} else if (m_chartItem->itemMode() == DesignMode) {
|
|
|
|
for (int i = 0 ; i < m_designLabels.size() ; ++i){
|
|
|
|
if (isHorizontal) {
|
|
|
|
drawHorizontalLegendItem(painter, i, m_designLabels.at(i), indicatorSize, indicatorsRect, color_map[i]);
|
|
|
|
} else {
|
|
|
|
drawVerticalLegendItem(painter, i, m_designLabels.at(i), indicatorSize, indicatorsRect, color_map[i]);
|
|
|
|
}
|
2016-11-02 01:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:55 +03:00
|
|
|
QRectF AbstractBarChart::verticalLabelsRect(QPainter *painter, QRectF labelsRect)
|
|
|
|
{
|
|
|
|
qreal maxWidth = 0;
|
|
|
|
|
|
|
|
foreach (QString label, m_chartItem->labels()) {
|
2021-08-23 08:07:08 +03:00
|
|
|
if (painter->fontMetrics().boundingRect(label).width()>maxWidth)
|
|
|
|
maxWidth = painter->fontMetrics().boundingRect(label).width();
|
2020-01-11 02:11:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (maxWidth + hPadding(m_chartItem->rect()) * 2 < labelsRect.width())
|
|
|
|
return labelsRect;
|
|
|
|
else
|
|
|
|
return labelsRect.adjusted(0, 0, -(labelsRect.width() - (maxWidth + hPadding(m_chartItem->rect()) * 2)), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF AbstractBarChart::horizontalLabelsRect(QPainter *painter, QRectF labelsRect)
|
|
|
|
{
|
|
|
|
qreal maxWidth = 0;
|
|
|
|
|
|
|
|
foreach (QString label, m_chartItem->labels()) {
|
2021-08-23 08:07:08 +03:00
|
|
|
if (painter->fontMetrics().boundingRect(label).width()>maxWidth)
|
|
|
|
maxWidth = painter->fontMetrics().boundingRect(label).width();
|
2020-01-11 02:11:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((maxWidth + vPadding(m_chartItem->rect()) < labelsRect.height()) || !verticalLabels(painter, labelsRect))
|
|
|
|
return labelsRect;
|
|
|
|
else
|
|
|
|
return labelsRect.adjusted(0, (labelsRect.height() - maxWidth), 0, 0);
|
|
|
|
}
|
2022-01-27 21:51:40 +03:00
|
|
|
|
2022-02-08 01:16:07 +03:00
|
|
|
void AbstractBarChart::drawVerticalLegendItem(QPainter *painter, int i, const QString &text, int indicatorSize,
|
|
|
|
const QRectF &indicatorsRect, const QColor &indicatorColor)
|
|
|
|
{
|
|
|
|
const qreal y = i * painter->fontMetrics().height();
|
|
|
|
painter->drawText(indicatorsRect.adjusted(indicatorSize+indicatorSize * 1.5, y, 0, 0),text);
|
|
|
|
switch(m_chartItem->legendStyle()) {
|
|
|
|
case ChartItem::LegendPoints: {
|
|
|
|
painter->setBrush(indicatorColor);
|
|
|
|
painter->drawEllipse(
|
|
|
|
indicatorsRect.adjusted(
|
|
|
|
0,
|
|
|
|
y+indicatorSize/2,
|
|
|
|
-(indicatorsRect.width()-indicatorSize),
|
|
|
|
-(indicatorsRect.height()-(y+indicatorSize+indicatorSize/2))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ChartItem::LegendLines: {
|
|
|
|
const QPen tmpPen = painter->pen();
|
|
|
|
QPen indicatorPen(indicatorColor);
|
|
|
|
indicatorPen.setWidth(4);
|
|
|
|
painter->setPen(indicatorPen);
|
|
|
|
const QPointF linePos = QPointF(indicatorsRect.left(), indicatorsRect.top() + y + painter->fontMetrics().height()/2);
|
|
|
|
painter->drawLine(linePos, linePos + QPointF(indicatorSize, 0));
|
|
|
|
painter->setPen(tmpPen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractBarChart::drawHorizontalLegendItem(QPainter *painter, int i, const QString &text,
|
|
|
|
int indicatorSize, const QRectF &indicatorsRect, const QColor &indicatorColor)
|
|
|
|
{
|
|
|
|
const QVector<qreal> &columnWidths = legendColumnWidths();
|
|
|
|
if (columnWidths.isEmpty())
|
|
|
|
return;
|
|
|
|
const int column = i % columnWidths.size();
|
|
|
|
const int row = std::floor(i / columnWidths.size());
|
|
|
|
const qreal halfTextSize = painter->fontMetrics().height() / 2;
|
|
|
|
|
|
|
|
const qreal x = indicatorsRect.x() + std::accumulate(columnWidths.cbegin(), columnWidths.cbegin() + column, 0.0);
|
|
|
|
const qreal y = indicatorsRect.y() + (row + 1) * painter->fontMetrics().height();
|
|
|
|
painter->drawText(QPointF(x + indicatorSize * 1.5, y), text);
|
|
|
|
switch(m_chartItem->legendStyle()) {
|
|
|
|
case ChartItem::LegendPoints: {
|
|
|
|
painter->setBrush(indicatorColor);
|
|
|
|
painter->drawEllipse(x, y - halfTextSize, indicatorSize, indicatorSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ChartItem::LegendLines: {
|
|
|
|
const QPen tmpPen = painter->pen();
|
|
|
|
QPen indicatorPen(indicatorColor);
|
|
|
|
indicatorPen.setWidth(4);
|
|
|
|
painter->setPen(indicatorPen);
|
|
|
|
painter->drawLine(x, y - halfTextSize * 0.7, x + indicatorSize, y - halfTextSize * 0.7);
|
|
|
|
painter->setPen(tmpPen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 01:34:45 +03:00
|
|
|
} // namespace LimeReport
|