Example #1
0
MainWindow::MainWindow( QWidget* parent ) :
    QWidget( parent )
{
    setupUi( this );

    QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
    m_chart = new KDChart::Chart;
    chartLayout->addWidget( m_chart );
    hSBar->setVisible( false );
    vSBar->setVisible( false );

    m_model.loadFromCSV( ":/data" );

    // Set up the diagram
    m_lines = new KDChart::LineDiagram();
    m_lines->setModel( &m_model );

    KDChart::CartesianAxis *xAxis = new KDChart::CartesianAxis( m_lines );
    KDChart::TextAttributes ta( xAxis->textAttributes() );

    AdjustedCartesianAxis *yAxis = new AdjustedCartesianAxis( m_lines );
    yAxis->setBounds( 3, 6 );
    xAxis->setPosition ( KDChart::CartesianAxis::Bottom );
    yAxis->setPosition ( KDChart::CartesianAxis::Left );

/*
// set the following to 0 to have only one of the axes with background
#if 1
    // colourize the axes' backgrounds
    KDChart::BackgroundAttributes ba = yAxis->backgroundAttributes();
    ba.setVisible( true );
    ba.setBrush( QBrush( QColor( 0xff, 0xff, 0x40 ) ) );
    yAxis->setBackgroundAttributes( ba );
    xAxis->setBackgroundAttributes( ba );
#else
    // colourize the Ordinate axis' background
    QLinearGradient linearGrad( QPointF( 0, 100 ), QPointF( 0, 400 ) );
    linearGrad.setColorAt( 0.0, QColor( 0xff, 0xff, 0xc0 ) );
    linearGrad.setColorAt( 1.0, QColor( 0xa0, 0xc0, 0xff ) );
    ba = yAxis->backgroundAttributes();
    ba.setVisible( true );
    ba.setBrush( linearGrad );
    yAxis->setBackgroundAttributes( ba );
#endif
*/
    // add 1 pixel space at the left and at the top edge, because the
    // axis area would otherwise overwrite the left/top borders
    m_chart->setGlobalLeading( 1, 1, 0, 0 );

// set the following to 0, to see the default Abscissa labels (== X headers, as read from the data file)
#if 1
    QStringList daysOfWeek;
    daysOfWeek << "Monday" << "Tuesday" << "Wednesday" << "Thursday" << "Friday" << "Saturday" << "Sunday";
    xAxis->setLabels( daysOfWeek );

    //QStringList shortDays;
    //shortDays << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat" << "Sun";
    //xAxis->setShortLabels( shortDays );
#endif

// Use HTML for drawing the text in the axis labels.
#if 0
    QStringList htmlStyles;
    htmlStyles << "<b>Bold</b>" << "<i>Italic</i>" << "<u>Underline</u>" << "<font color='red'>Red</font>";
    xAxis->setLabels( htmlStyles );
    ta.setTextDocument(new QTextDocument);
#endif


// set custom axis labels at custom positions
#if 0
    QMap< qreal, QString > annotations;
    annotations[ 0.5 ] = "Left";
    annotations[ 3.5 ] = "Center";
    annotations[ 4.2 ] = "After Center";
    annotations[ 6.5 ] = "Right";
    xAxis->setAnnotations(annotations);
#endif

    // Illustration of custom ticks
    QList<qreal> ticks;
    ticks.append( 0.5 );
    ticks.append( 3.5 );
    ticks.append( 4.2 );
    ticks.append( 6.5 );
    xAxis->setCustomTicks(ticks);
    yAxis->setCustomTicks(ticks);

    // rotate abscissa labels by -60 degrees:
    ta.setRotation( -60 );

    xAxis->setTextAttributes( ta );
    m_lines->addAxis( xAxis );
    m_lines->addAxis( yAxis );
    m_chart->coordinatePlane()->replaceDiagram( m_lines );
    // Set up the legend
    xAxis->setCustomTickLength( 11 );
    yAxis->setCustomTickLength( 11 );
    m_legend = new KDChart::Legend( m_lines, m_chart );
    m_legend->setPosition( KDChart::Position::East );
    m_legend->setAlignment( Qt::AlignTop );
    m_chart->addLegend( m_legend );
}
Example #2
0
MainWindow::MainWindow( QWidget* parent ) :
    QWidget( parent )
{
    setupUi( this );

    m_chartLayout = new QHBoxLayout( chartFrame );
    m_chart = new KDChart::Chart();
    m_chartLayout->addWidget( m_chart );

    // add a small left-side leading because we use a coloured Y axis background
    m_chart->setGlobalLeadingLeft( 1 );


    initializeDataModel();


    // register our own serializer for saving / loading the Y axis:
    KDChart::registerElementSerializer<
            AdjustedCartesianAxisSerializer,
            AdjustedCartesianAxis >( 0 ); // will be un-registered in ~Mainwindow()


    // Set up the diagrams
    KDChart::LineDiagram* lines = new KDChart::LineDiagram();
    lines->setModel( m_model );

    // increase the line width
    for ( int iRow = 0; iRow < lines->model()->rowCount(); ++iRow ) {
        QPen pen( lines->pen( iRow ) );
        pen.setWidth(2);
        pen.setColor( pen.color().dark(133) );
        lines->setPen( iRow, pen );
    }

    // Assign some axes
    KDChart::CartesianAxis *xAxis = new KDChart::CartesianAxis( lines );
    AdjustedCartesianAxis *yAxis = new AdjustedCartesianAxis( lines );
    yAxis->setBounds(29.9, 31.0);
    xAxis->setPosition ( KDChart::CartesianAxis::Bottom );
    yAxis->setPosition ( KDChart::CartesianAxis::Left );
    yAxis->setTitleText ( "an axis class with customized labels" );

    lines->addAxis( xAxis );
    lines->addAxis( yAxis );


    KDChart::CartesianCoordinatePlane* plane =
            static_cast< KDChart::CartesianCoordinatePlane * >(m_chart->coordinatePlane());

    plane->replaceDiagram( lines );

    KDChart::HeaderFooter* headerFooter = new KDChart::HeaderFooter( m_chart );
    m_chart->addHeaderFooter( headerFooter );
    headerFooter->setText( "Line diagram using a custom axis class" );

    KDChart::TextAttributes textAttrs( headerFooter->textAttributes() );
    textAttrs.setPen( QPen( Qt::red ) );
    headerFooter->setTextAttributes( textAttrs );
    headerFooter->setType( KDChart::HeaderFooter::Header );
    headerFooter->setPosition( KDChart::Position::North );

    // assign some bg colors
    KDChart::BackgroundAttributes ba = yAxis->backgroundAttributes();
    ba.setVisible(true);
    ba.setBrush(QBrush(QColor(255,255,200)));
    yAxis->setBackgroundAttributes(ba);
}
Example #3
0
int main(int argc, char *argv[]) {
	QApplication a(argc, argv);
	KDChart::Chart chart;

	QStandardItemModel model;

        const double offset = 600;
	model.setRowCount( 100 );
	model.setColumnCount( 2 ); // 1 data set
	for ( int i = 0; i < 100; ++i ) {
		double t = i + offset;
		double v = i + offset;
		QModelIndex index = model.index( i, 0 );
		model.setData( index, t );
		index = model.index( i, 1 );
		model.setData( index, v );
	}

	model.setHeaderData( 0, Qt::Horizontal, "Dataset 1" );

	// general chart layout
	KDChart::FrameAttributes fm = chart.frameAttributes();
	fm.setVisible( true );
	fm.setPen( QPen( Qt::black ) );
	chart.setFrameAttributes( fm );
	chart.setGlobalLeading( 10, 0, 10, 10 );

	KDChart::BackgroundAttributes chart_bg;
	chart_bg.setBrush( Qt::white );
	chart_bg.setVisible( true );
	chart.setBackgroundAttributes( chart_bg );

	// coordinate plane setup
	KDChart::AbstractCoordinatePlane * plane1 = chart.coordinatePlane();
	plane1->setRubberBandZoomingEnabled( true );

	// create cartesian diagrams
	KDChart::Plotter * plotter = new KDChart::Plotter;
	plotter->setAntiAliasing( false );
	plotter->setModel( &model );
	plane1->replaceDiagram( plotter );

	// customize grids
	KDChart::CartesianCoordinatePlane * cp1 = static_cast< KDChart::CartesianCoordinatePlane * >( plane1 );
	KDChart::GridAttributes gv = cp1->gridAttributes( Qt::Vertical );
	QPen gridPen( QColor( 200, 100, 100 ) );
	gridPen.setStyle( Qt::DashLine );
	gv.setGridPen( gridPen );
	gridPen.setStyle( Qt::DotLine );
	gridPen.setColor( QColor( 255,155,155 ) );
	gv.setSubGridPen( gridPen );
	cp1->setGridAttributes( Qt::Vertical,  gv );

        // Enable isometric scaling
        cp1->setIsometricScaling( true );

	// axis
	KDChart::CartesianAxis * xAxis = new KDChart::CartesianAxis( plotter );
	xAxis->setPosition( KDChart::CartesianAxis::Bottom );
	xAxis->setTitleText("X-Title");

	KDChart::TextAttributes att = xAxis->titleTextAttributes();
	QFont f = att.font();
	f.setBold( true );
	att.setFont( f );
	att.setAutoShrink( true );
	att.setFontSize( KDChart::Measure( 16 ) );
	xAxis->setTitleTextAttributes( att );

	KDChart::CartesianAxis * y1Axis = new KDChart::CartesianAxis( plotter );
	y1Axis->setPosition( KDChart::CartesianAxis::Left );
	y1Axis->setTitleText( "Y-Title" );

	att = y1Axis->titleTextAttributes();
	f = att.font();
	f.setBold( true );
	att.setFont( f );
	att.setAutoShrink( true );
	att.setFontSize( KDChart::Measure( 16 ) );
	y1Axis->setTitleTextAttributes( att );

	// add the axis to the plotter
	plotter->addAxis( xAxis );
	plotter->addAxis( y1Axis );

	// create legend
	KDChart::Legend * legend = new KDChart::Legend( plotter, &chart );
	chart.addLegend( legend );
	att = legend->textAttributes();
	f = att.font();
	f.setBold( false );
	att.setFont( f );
	att.setAutoShrink( true );
	legend->setTextAttributes( att );

	legend->setPosition( KDChart::Position::East );
	legend->setAlignment( Qt::AlignCenter );
	legend->setTitleText( "Curves" );
	att = legend->titleTextAttributes();
	f = att.font();
	f.setBold( true );
	att.setFont( f );
	att.setAutoShrink( true );
	att.setFontSize( KDChart::Measure( 16 ) );
	legend->setTitleTextAttributes( att );

	KDChart::BackgroundAttributes legend_bg;
	legend_bg.setBrush( Qt::white );
	legend_bg.setVisible( true );
	legend->setBackgroundAttributes( legend_bg );

	KDChart::DataValueAttributes attr = plotter->dataValueAttributes();
	KDChart::TextAttributes tattr = attr.textAttributes();
        tattr.setFontSize( KDChart::Measure( 12, KDChartEnums::MeasureCalculationModeAbsolute ) );
	tattr.setRotation( 0 );
	attr.setTextAttributes( tattr );
	plotter->setDataValueAttributes( attr );

	// customize marker properties

	// Dataset 1 : green, MarkerRing, no line
	QColor SERIES_1_OUTLINE = QColor( 0, 128, 0 );
	attr = plotter->dataValueAttributes( 0 );
	KDChart::MarkerAttributes mattr = attr.markerAttributes();
	mattr.setMarkerColor( SERIES_1_OUTLINE );
//        mattr.setMarkerStyle( KDChart::MarkerAttributes::MarkerRing );
	mattr.setMarkerSize( QSizeF( 6.0, 6.0 ) );
	mattr.setVisible( true );
	attr.setMarkerAttributes( mattr );
	attr.setVisible( true );
	plotter->setDataValueAttributes( 0, attr );
	plotter->setPen( 0, Qt::NoPen );

	chart.show();
	return a.exec();
}
Example #4
0
int main( int argc, char** argv )
{
    QApplication app( argc, argv );

    const int points = 420;
    const double xMin = 0;
    const double xMax = 20;
    const double step = ( xMax - xMin ) / ( points - 1 );

    QStandardItemModel model( points, 6 );

    double x = xMin;
    for ( int n = 0; n < points; ++n, x += step) {
        QModelIndex index = model.index( n, 0 );
        model.setData( index, x );
        index = model.index( n, 1 );
        model.setData( index, sin( x ) * 100.0 );

        index = model.index( n, 2 );
        model.setData( index, x );
        index = model.index( n, 3 );
        model.setData( index, x );

        index = model.index( n, 4 );
        model.setData( index, x );
        index = model.index( n, 5 );
        model.setData( index, x * x * x );
    }

    model.setHeaderData( 0, Qt::Horizontal, QString::fromLatin1( "100 * sin(x)" ) );
    model.setHeaderData( 2, Qt::Horizontal, QString::fromLatin1( "x" ) );
    model.setHeaderData( 4, Qt::Horizontal, QString::fromLatin1( "x^3" ) );

    KDChart::Chart* chart = new KDChart::Chart();

    KDChart::AbstractCartesianDiagram* diagram = new KDChart::Plotter;
    diagram->setModel( &model );
    chart->coordinatePlane()->replaceDiagram( diagram );

    KDChart::CartesianAxis* xAxis = new KDChart::CartesianAxis( diagram );
    KDChart::CartesianAxis* yAxis = new KDChart::CartesianAxis( diagram );
    xAxis->setPosition( KDChart::CartesianAxis::Bottom );
    yAxis->setPosition( KDChart::CartesianAxis::Left );
    diagram->addAxis( xAxis );
    diagram->addAxis( yAxis );

    KDChart::Legend* legend = new KDChart::Legend( diagram, chart );
    KDChart::FrameAttributes legendAtt = legend->frameAttributes();
    legendAtt.setCornerRadius( 9 );
    legend->setFrameAttributes( legendAtt );
    legend->setPosition( KDChart::Position::East );
    legend->setAlignment( Qt::AlignCenter );
    legend->setTitleText( "Legend" );
    chart->addLegend( legend );

    KDChart::CartesianCoordinatePlane* cart_plane = dynamic_cast<KDChart::CartesianCoordinatePlane*>(chart->coordinatePlane());
    Q_ASSERT(cart_plane);

    cart_plane->setAxesCalcModeX(KDChart::AbstractCoordinatePlane::Logarithmic);
    cart_plane->setAxesCalcModeY(KDChart::AbstractCoordinatePlane::Logarithmic);

    // Set the vertical range from 15 to 75 - with a logarithmic axis I actually get 1 to 100
    //cart_plane->setVerticalRange(QPair<qreal,qreal>( 0.005, 1000 ) );

    // Set the horizontal range from 1 to 9 - with a linear axis this works OK
    cart_plane->setHorizontalRange(QPair<qreal,qreal>( 0.001, 100 ) );

    chart->show();

    int ret = app.exec();

    delete chart;

    return ret;
}