2
#include <KDChartChart>
3
#include <KDChartLineDiagram>
4
#include <KDChartDataValueAttributes>
6
using namespace KDChart;
8
class ChartWidget : public QWidget {
11
explicit ChartWidget(QWidget* parent=0)
15
m_model.insertRows( 0,5, QModelIndex() );
16
m_model.insertColumns( 0,5, QModelIndex() );
17
double increase = 1.15;
18
for( int i = 0; i < 5; ++i ) {
20
for( int j = 0; j < 5; ++j ) {
21
m_model.setData( m_model.index( i,j,QModelIndex() ),
22
( increase + i ) * j * (1.0 + 0.1 * (rand() % 10)) );
26
LineDiagram* diagram = new LineDiagram;
27
diagram->setModel(&m_model);
30
// 1 - Call the relevant attributes
31
DataValueAttributes dva( diagram->dataValueAttributes() );
33
// 2 - We want to configure the font and colors
34
// for the data values text.
35
TextAttributes ta( dva.textAttributes() );
36
dva.setDecimalDigits( 2 );
38
// Append a prefix/suffix to the
39
// data value labels being displayed
41
//dva.setPrefix( "* " );
42
dva.setSuffix( " Ohm" );
45
//ta.setRotation( 0 );
46
// 3 - Set up your text attributes
47
ta.setFont( QFont( "Comic") );
48
ta .setPen( QPen( QColor( Qt::darkGreen ) ) );
49
ta.setVisible( true );
51
Measure me( ta.fontSize() );
52
me.setValue( me.value() * 0.25 );
55
// 4 - Assign the text attributes to your
56
// DataValuesAttributes
57
dva.setTextAttributes( ta );
58
dva.setVisible( true );
59
// 5 - Assign to the diagram
60
diagram->setDataValueAttributes( dva );
62
// Set thick line widths for all datasets
63
for( int i=0; i<m_model.rowCount(); ++i ){
64
QPen pen( diagram->pen( i ) );
66
diagram->setPen( i, pen );
69
// Draw one of the sections of a line differently.
70
// 1 - Retrieve the pen for the dataset and change
72
// This allow us to keep the line original color.
73
QPen linePen( diagram->pen( 1 ) );
74
linePen.setColor( Qt::yellow );
75
linePen.setWidth( 7 );
76
linePen.setStyle( Qt::DashLine );
77
// 2 - Change the Pen for a section within a line
78
// while assigning it to the diagram
79
diagram->setPen( m_model.index( 1, 1, QModelIndex() ), linePen );
80
// 3 - Assign to the chart
81
m_chart.coordinatePlane()->replaceDiagram(diagram);
82
m_chart.setGlobalLeadingRight( 50 );
84
QVBoxLayout* l = new QVBoxLayout(this);
85
l->addWidget(&m_chart);
91
QStandardItemModel m_model;
94
int main( int argc, char** argv ) {
95
QApplication app( argc, argv );