File indexing completed on 2024-11-24 03:57:46

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 <QWidget>
0010 #include <QStandardItemModel>
0011 #include <QtSvg/QSvgGenerator>
0012 #include <KChartChart>
0013 #include <KChartBarDiagram>
0014 #include <QApplication>
0015 
0016 class ChartWidget : public QWidget {
0017   Q_OBJECT
0018 public:
0019   explicit ChartWidget(QWidget* parent = nullptr)
0020     : QWidget(parent)
0021   {
0022 
0023     m_model.insertRows( 0, 2, QModelIndex() );
0024     m_model.insertColumns(  0,  3,  QModelIndex() );
0025     for (int row = 0; row < 3; ++row) {
0026             for (int column = 0; column < 3; ++column) {
0027                 QModelIndex index = m_model.index(row, column, QModelIndex());
0028                 m_model.setData(index, QVariant(row+1 * column) );
0029                 /*
0030                 // show tooltips:
0031                 m_model.setData(index,
0032                 QString("<table>"
0033                     "<tr><td>Row</td><td>Column</td><td>Value</td></tr>"
0034                     "<tr><th>%1</th><th>%2</th><th>%3</th></tr></table>")
0035                 .arg(row).arg(column).arg(row+1 * column), Qt::ToolTipRole );
0036                 */
0037             }
0038     }
0039 
0040     /*
0041     {
0042     // show a comment at one data item:
0043         const int row = 0;
0044         const int column = 2;
0045         const QModelIndex index = m_model.index(row, column, QModelIndex());
0046         m_model.setData(
0047             index,
0048             QString("Value %1/%2: %3")
0049                     .arg( row )
0050                     .arg( column )
0051                     .arg( m_model.data( index ).toInt() ),
0052             KChart::CommentRole );
0053     }
0054     */
0055 
0056     KChart::BarDiagram* diagram = new KChart::BarDiagram;
0057     diagram->setModel(&m_model);
0058     diagram->setPen( QPen( Qt::black, 0 ) );
0059 
0060     m_chart.coordinatePlane()->replaceDiagram(diagram);
0061 
0062     QVBoxLayout* l = new QVBoxLayout(this);
0063     l->addWidget(&m_chart);
0064     setLayout(l);
0065 
0066     /*
0067     // render chart to a SVG
0068     QSvgGenerator generator;
0069     generator.setFileName("/home/kdab/chart.svg");
0070     generator.setSize(QSize(300, 300));
0071     generator.setViewBox(QRect(0, 0, 300, 300));
0072     generator.setTitle(tr("SVG Chart"));
0073     QPainter painter;
0074     painter.begin(&generator);
0075     painter.setRenderHint(QPainter::Antialiasing);
0076     m_chart.paint(&painter, generator.viewBox());
0077     painter.end();
0078     */
0079   }
0080 
0081 private:
0082   KChart::Chart m_chart;
0083   QStandardItemModel m_model;
0084 };
0085 
0086 int main( int argc, char** argv ) {
0087     QApplication app( argc, argv );
0088 
0089     ChartWidget w;
0090     w.show();
0091 
0092     return app.exec();
0093 }
0094 
0095 #include "main.moc"