File indexing completed on 2024-06-16 04:09:00

0001 /**
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include <QStandardItemModel>
0010 #include <KChartChart>
0011 #include <KChartBarDiagram>
0012 #include <KChartLegend>
0013 #include <KChartPosition>
0014 #include <KChartBackgroundAttributes>
0015 #include <KChartFrameAttributes>
0016 #include <QApplication>
0017 
0018 using namespace KChart;
0019 
0020 class ChartWidget : public QWidget {
0021   Q_OBJECT
0022 public:
0023   explicit ChartWidget(QWidget* parent = nullptr)
0024     : QWidget(parent)
0025   {
0026 
0027     m_model.insertRows( 0, 2, QModelIndex() );
0028     m_model.insertColumns(  0,  3,  QModelIndex() );
0029     for (int row = 0; row < 3; ++row) {
0030             for (int column = 0; column < 3; ++column) {
0031                 QModelIndex index = m_model.index(row, column, QModelIndex());
0032                 m_model.setData(index, QVariant(row+1 * column) );
0033             }
0034     }
0035 
0036     BarDiagram* diagram = new BarDiagram;
0037     diagram->setModel(&m_model);
0038 
0039     m_chart.coordinatePlane()->replaceDiagram(diagram);
0040 
0041     // Add at one legend and set it up
0042     Legend* legend = new Legend( diagram, &m_chart );
0043     legend->setPosition( Position::North );
0044     legend->setAlignment( Qt::AlignCenter );
0045     legend->setShowLines( true );
0046     legend->setSpacing( 5 );
0047     legend->setTitleText( tr( "Legend" ) );
0048     legend->setOrientation( Qt::Horizontal );
0049     m_chart.addLegend( legend );
0050 
0051     // Configure the items markers
0052     MarkerAttributes lma ( legend->markerAttributes( 0 ) );
0053     lma.setMarkerStyle( MarkerAttributes::MarkerDiamond );
0054     legend->setMarkerAttributes( 0,  lma );
0055     lma.setMarkerStyle( MarkerAttributes::MarkerCircle );
0056     legend->setMarkerAttributes( 1,  lma );
0057 
0058     // Configure Legend Title and labels
0059     legend->setTitleText( "Bars" );
0060     legend->setText( 0,  "Series 1" );
0061     legend->setText( 1,  "Series 2" );
0062     legend->setText( 2,  "Series 3" );
0063 
0064     // adjust the legend item's font:
0065     TextAttributes lta( legend->textAttributes() );
0066     lta.setPen( QPen( Qt::darkGray ) );
0067     Measure me( lta.fontSize() );
0068     me.setValue( me.value() * 1.5 );
0069     lta.setFontSize( Measure( 9, KChartEnums::MeasureCalculationModeAbsolute ) );
0070     legend->setTextAttributes(  lta );
0071 
0072     // adjust the legend title's font:
0073     lta = legend->titleTextAttributes();
0074     lta.setPen( QPen( Qt::darkGray ) );
0075     me = lta.fontSize();
0076     me.setValue( me.value() * 1.5 );
0077     lta.setFontSize( me );
0078     legend->setTitleTextAttributes(  lta );
0079 
0080     // Configure a pen to surround
0081     // the markers with a border
0082     QPen markerPen;
0083     markerPen.setColor(  Qt::darkGray );
0084     markerPen.setWidth( 2 );
0085     // Pending Michel use datasetCount() here as soon
0086     // as it is fixed
0087     for (  uint i = 0; i < /*legend->datasetCount()*/ 3; i++ )
0088         legend->setPen( i,  markerPen );
0089 
0090     // Add a background to your legend
0091     BackgroundAttributes ba;
0092     ba.setBrush(  Qt::white );
0093     ba.setVisible( true );
0094     legend->setBackgroundAttributes(  ba );
0095 
0096     FrameAttributes fa;
0097     fa.setPen( markerPen );
0098     fa.setVisible( true );
0099     legend->setFrameAttributes(  fa );
0100 
0101     QVBoxLayout* l = new QVBoxLayout(this);
0102     l->addWidget(&m_chart);
0103     m_chart.setGlobalLeadingTop( 10 );
0104     setLayout(l);
0105   }
0106 
0107 private:
0108   Chart m_chart;
0109   QStandardItemModel m_model;
0110 };
0111 
0112 int main( int argc, char** argv ) {
0113     QApplication app( argc, argv );
0114 
0115     ChartWidget w;
0116     w.show();
0117 
0118     return app.exec();
0119 }
0120 
0121 #include "main.moc"