File indexing completed on 2024-05-26 04:23:43
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 model.setRowCount(100); 0030 model.setColumnCount(6); // 3 data sets 0031 for (int i=0; i<100; ++i) { 0032 // dateset 1 0033 qreal t = i; 0034 qreal v = qreal(i)*i; 0035 QModelIndex index = model.index( i, 0 ); 0036 model.setData( index, QVariant( t ) ); 0037 model.setData( index, QVariant( t ), Qt::ToolTipRole ); 0038 index = model.index( i, 1 ); 0039 model.setData( index, QVariant( v ) ); 0040 model.setData( index, QVariant( v ), Qt::ToolTipRole ); 0041 0042 // dateset 2 0043 t = i*2; 0044 v = qreal(i)*i-i; 0045 index = model.index( i, 2 ); 0046 model.setData( index, QVariant( t ) ); 0047 model.setData( index, QVariant( t ), Qt::ToolTipRole ); 0048 index = model.index( i, 3 ); 0049 model.setData( index, QVariant( v ) ); 0050 model.setData( index, QVariant( v ), Qt::ToolTipRole ); 0051 0052 // dateset 3 0053 t = qreal(i)/2; 0054 v = qreal(i)*i+i; 0055 index = model.index( i, 4 ); 0056 model.setData( index, QVariant( t ) ); 0057 model.setData( index, QVariant( t ), Qt::ToolTipRole ); 0058 index = model.index( i, 5 ); 0059 model.setData( index, QVariant( v ) ); 0060 model.setData( index, QVariant( v ), Qt::ToolTipRole ); 0061 } 0062 0063 model.setHeaderData( 0, Qt::Horizontal, "Dataset 1" ); 0064 model.setHeaderData( 2, Qt::Horizontal, "Dataset 2" ); 0065 model.setHeaderData( 4, Qt::Horizontal, "Dataset 3" ); 0066 0067 // general chart layout 0068 KChart::FrameAttributes fm = chart.frameAttributes(); 0069 fm.setVisible(true); 0070 fm.setPen( QPen(Qt::black) ); 0071 chart.setFrameAttributes(fm); 0072 chart.setGlobalLeading( 10, 0, 10, 10 ); 0073 0074 KChart::BackgroundAttributes chart_bg; 0075 chart_bg.setBrush(Qt::white); 0076 chart_bg.setVisible(true); 0077 chart.setBackgroundAttributes(chart_bg); 0078 0079 // coordinate plane setup 0080 KChart::AbstractCoordinatePlane * plane1 = chart.coordinatePlane(); 0081 plane1->setRubberBandZoomingEnabled(true); 0082 0083 // create cartesian diagrams 0084 KChart::Plotter * plotter = new KChart::Plotter; 0085 plotter->setHidden( 0, true ); 0086 plotter->setAntiAliasing(false); 0087 plotter->setModel( &model ); 0088 plane1->replaceDiagram( plotter ); 0089 0090 // customize grids 0091 KChart::CartesianCoordinatePlane * cp1 = static_cast<KChart::CartesianCoordinatePlane *>(plane1); 0092 KChart::GridAttributes gv = cp1->gridAttributes( Qt::Vertical ); 0093 QPen gridPen(QColor(200,100,100)); 0094 gridPen.setStyle(Qt::DashLine); 0095 gv.setGridPen(gridPen); 0096 gridPen.setStyle(Qt::DotLine); 0097 gridPen.setColor( QColor(255,155,155) ); 0098 gv.setSubGridPen(gridPen); 0099 cp1->setGridAttributes( Qt::Vertical, gv ); 0100 0101 // axis 0102 KChart::CartesianAxis * xAxis = new KChart::CartesianAxis( plotter ); 0103 xAxis->setPosition( KChart::CartesianAxis::Bottom ); 0104 xAxis->setTitleText("X-Title"); 0105 0106 KChart::TextAttributes att = xAxis->titleTextAttributes(); 0107 QFont f = att.font(); 0108 f.setBold(true); 0109 att.setFont(f); 0110 att.setAutoShrink(true); 0111 att.setFontSize( KChart::Measure(16) ); 0112 xAxis->setTitleTextAttributes(att); 0113 0114 KChart::CartesianAxis * y1Axis = new KChart::CartesianAxis( plotter ); 0115 y1Axis->setPosition( KChart::CartesianAxis::Left ); 0116 y1Axis->setTitleText("Y-Title"); 0117 0118 att = y1Axis->titleTextAttributes(); 0119 f = att.font(); 0120 f.setBold(true); 0121 att.setFont(f); 0122 att.setAutoShrink(true); 0123 att.setFontSize( KChart::Measure(16) ); 0124 y1Axis->setTitleTextAttributes(att); 0125 0126 // add the axis to the plotter 0127 plotter->addAxis( xAxis ); 0128 plotter->addAxis( y1Axis ); 0129 0130 // create legend 0131 KChart::Legend * legend = new KChart::Legend( plotter, &chart ); 0132 chart.addLegend( legend ); 0133 att = legend->textAttributes(); 0134 f = att.font(); 0135 f.setBold(false); 0136 att.setFont(f); 0137 att.setAutoShrink(true); 0138 att.setFontSize( KChart::Measure(16) ); 0139 legend->setTextAttributes(att); 0140 0141 legend->setPosition( KChart::Position::East ); 0142 legend->setAlignment( Qt::AlignCenter ); 0143 legend->setTitleText( "Curves" ); 0144 att = legend->titleTextAttributes(); 0145 f = att.font(); 0146 f.setBold(true); 0147 att.setFont(f); 0148 att.setAutoShrink(true); 0149 att.setFontSize( KChart::Measure(16) ); 0150 legend->setTitleTextAttributes(att); 0151 0152 KChart::BackgroundAttributes legend_bg; 0153 legend_bg.setBrush(Qt::white); 0154 legend_bg.setVisible(true); 0155 legend->setBackgroundAttributes(legend_bg); 0156 0157 KChart::DataValueAttributes attr = plotter->dataValueAttributes(); 0158 KChart::TextAttributes tattr = attr.textAttributes(); 0159 tattr.setRotation( 0 ); 0160 attr.setTextAttributes( tattr ); 0161 plotter->setDataValueAttributes( attr ); 0162 0163 // customize marker properties 0164 0165 // Dataset 1 : green, MarkerRing, no line 0166 QColor SERIES_1_OUTLINE = QColor(0,128,0); 0167 attr = plotter->dataValueAttributes(0); 0168 KChart::MarkerAttributes mattr = attr.markerAttributes(); 0169 mattr.setMarkerColor(SERIES_1_OUTLINE); 0170 mattr.setMarkerStyle(KChart::MarkerAttributes::MarkerRing); 0171 mattr.setMarkerSize(QSizeF(6.0, 6.0)); 0172 mattr.setVisible(true); 0173 attr.setMarkerAttributes(mattr); 0174 attr.setVisible(true); 0175 plotter->setDataValueAttributes(0, attr); 0176 plotter->setPen(0, Qt::NoPen); 0177 0178 // Dataset 2 : MarkerDiamond, (black outline, red inside), no line 0179 QColor SERIES_2_INSIDE = QColor(255,100,100); 0180 attr = plotter->dataValueAttributes(1); 0181 mattr = attr.markerAttributes(); 0182 mattr.setMarkerColor(SERIES_2_INSIDE); 0183 mattr.setMarkerStyle(KChart::MarkerAttributes::MarkerDiamond); 0184 mattr.setMarkerSize(QSizeF(8.0, 8.0)); 0185 mattr.setVisible(true); 0186 attr.setMarkerAttributes(mattr); 0187 attr.setVisible(true); 0188 plotter->setDataValueAttributes(1, attr); 0189 plotter->setPen(1, Qt::NoPen); 0190 0191 // Dataset 3 : MarkerCircle, (green outline, yellow inside), blue line 0192 QColor SERIES_3_INSIDE = QColor("yellow"); 0193 QColor SERIES_3_LINE = QColor("navy"); 0194 attr = plotter->dataValueAttributes(2); 0195 mattr = attr.markerAttributes(); 0196 mattr.setMarkerColor(SERIES_3_INSIDE); 0197 mattr.setMarkerStyle(KChart::MarkerAttributes::MarkerCircle); 0198 mattr.setMarkerSize(QSizeF(8.0, 8.0)); 0199 mattr.setVisible(true); 0200 attr.setMarkerAttributes(mattr); 0201 attr.setVisible(true); 0202 plotter->setDataValueAttributes(2, attr); 0203 plotter->setPen(2, QPen(SERIES_3_LINE)); 0204 0205 chart.show(); 0206 return a.exec(); 0207 }