File indexing completed on 2024-06-16 04:09:02
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 0011 #include <KChartChart> 0012 #include <KChartGridAttributes> 0013 #include <KChartFrameAttributes> 0014 #include <KChartTextAttributes> 0015 #include <KChartDataValueAttributes> 0016 #include <KChartMarkerAttributes> 0017 #include <KChartPlotter> 0018 #include <KChartLegend> 0019 #include <KChartBackgroundAttributes> 0020 0021 #include <QApplication> 0022 0023 int main(int argc, char *argv[]) { 0024 QApplication a(argc, argv); 0025 KChart::Chart chart; 0026 0027 QStandardItemModel model; 0028 0029 const double offset = 600; 0030 model.setRowCount( 100 ); 0031 model.setColumnCount( 2 ); // 1 data set 0032 for ( int i = 0; i < 100; ++i ) { 0033 double t = i + offset; 0034 double v = i + offset; 0035 QModelIndex index = model.index( i, 0 ); 0036 model.setData( index, t ); 0037 index = model.index( i, 1 ); 0038 model.setData( index, v ); 0039 } 0040 0041 model.setHeaderData( 0, Qt::Horizontal, "Dataset 1" ); 0042 0043 // general chart layout 0044 KChart::FrameAttributes fm = chart.frameAttributes(); 0045 fm.setVisible( true ); 0046 fm.setPen( QPen( Qt::black ) ); 0047 chart.setFrameAttributes( fm ); 0048 chart.setGlobalLeading( 10, 0, 10, 10 ); 0049 0050 KChart::BackgroundAttributes chart_bg; 0051 chart_bg.setBrush( Qt::white ); 0052 chart_bg.setVisible( true ); 0053 chart.setBackgroundAttributes( chart_bg ); 0054 0055 // coordinate plane setup 0056 KChart::AbstractCoordinatePlane * plane1 = chart.coordinatePlane(); 0057 plane1->setRubberBandZoomingEnabled( true ); 0058 0059 // create cartesian diagrams 0060 KChart::Plotter * plotter = new KChart::Plotter; 0061 plotter->setAntiAliasing( false ); 0062 plotter->setModel( &model ); 0063 plane1->replaceDiagram( plotter ); 0064 0065 // customize grids 0066 KChart::CartesianCoordinatePlane * cp1 = static_cast< KChart::CartesianCoordinatePlane * >( plane1 ); 0067 KChart::GridAttributes gv = cp1->gridAttributes( Qt::Vertical ); 0068 QPen gridPen( QColor( 200, 100, 100 ) ); 0069 gridPen.setStyle( Qt::DashLine ); 0070 gv.setGridPen( gridPen ); 0071 gridPen.setStyle( Qt::DotLine ); 0072 gridPen.setColor( QColor( 255,155,155 ) ); 0073 gv.setSubGridPen( gridPen ); 0074 cp1->setGridAttributes( Qt::Vertical, gv ); 0075 0076 // Enable isometric scaling 0077 cp1->setIsometricScaling( true ); 0078 0079 // axis 0080 KChart::CartesianAxis * xAxis = new KChart::CartesianAxis( plotter ); 0081 xAxis->setPosition( KChart::CartesianAxis::Bottom ); 0082 xAxis->setTitleText("X-Title"); 0083 0084 KChart::TextAttributes att = xAxis->titleTextAttributes(); 0085 QFont f = att.font(); 0086 f.setBold( true ); 0087 att.setFont( f ); 0088 att.setAutoShrink( true ); 0089 att.setFontSize( KChart::Measure( 16 ) ); 0090 xAxis->setTitleTextAttributes( att ); 0091 0092 KChart::CartesianAxis * y1Axis = new KChart::CartesianAxis( plotter ); 0093 y1Axis->setPosition( KChart::CartesianAxis::Left ); 0094 y1Axis->setTitleText( "Y-Title" ); 0095 0096 att = y1Axis->titleTextAttributes(); 0097 f = att.font(); 0098 f.setBold( true ); 0099 att.setFont( f ); 0100 att.setAutoShrink( true ); 0101 att.setFontSize( KChart::Measure( 16 ) ); 0102 y1Axis->setTitleTextAttributes( att ); 0103 0104 // add the axis to the plotter 0105 plotter->addAxis( xAxis ); 0106 plotter->addAxis( y1Axis ); 0107 0108 // create legend 0109 KChart::Legend * legend = new KChart::Legend( plotter, &chart ); 0110 chart.addLegend( legend ); 0111 att = legend->textAttributes(); 0112 f = att.font(); 0113 f.setBold( false ); 0114 att.setFont( f ); 0115 att.setAutoShrink( true ); 0116 legend->setTextAttributes( att ); 0117 0118 legend->setPosition( KChart::Position::East ); 0119 legend->setAlignment( Qt::AlignCenter ); 0120 legend->setTitleText( "Curves" ); 0121 att = legend->titleTextAttributes(); 0122 f = att.font(); 0123 f.setBold( true ); 0124 att.setFont( f ); 0125 att.setAutoShrink( true ); 0126 att.setFontSize( KChart::Measure( 16 ) ); 0127 legend->setTitleTextAttributes( att ); 0128 0129 KChart::BackgroundAttributes legend_bg; 0130 legend_bg.setBrush( Qt::white ); 0131 legend_bg.setVisible( true ); 0132 legend->setBackgroundAttributes( legend_bg ); 0133 0134 KChart::DataValueAttributes attr = plotter->dataValueAttributes(); 0135 KChart::TextAttributes tattr = attr.textAttributes(); 0136 tattr.setFontSize( KChart::Measure( 12, KChartEnums::MeasureCalculationModeAbsolute ) ); 0137 tattr.setRotation( 0 ); 0138 attr.setTextAttributes( tattr ); 0139 plotter->setDataValueAttributes( attr ); 0140 0141 // customize marker properties 0142 0143 // Dataset 1 : green, MarkerRing, no line 0144 QColor SERIES_1_OUTLINE = QColor( 0, 128, 0 ); 0145 attr = plotter->dataValueAttributes( 0 ); 0146 KChart::MarkerAttributes mattr = attr.markerAttributes(); 0147 mattr.setMarkerColor( SERIES_1_OUTLINE ); 0148 // mattr.setMarkerStyle( KChart::MarkerAttributes::MarkerRing ); 0149 mattr.setMarkerSize( QSizeF( 6.0, 6.0 ) ); 0150 mattr.setVisible( true ); 0151 attr.setMarkerAttributes( mattr ); 0152 attr.setVisible( true ); 0153 plotter->setDataValueAttributes( 0, attr ); 0154 plotter->setPen( 0, Qt::NoPen ); 0155 0156 chart.show(); 0157 return a.exec(); 0158 }