JKQtPlotter/examples/barchart_errorbars
jkriege2 b0df7a1fd7 NEW/BREAKING: provide general targets JKQTPlotter5/6::JKQTPlotter5/6, JKQTPlotter5/6::JKQTMathText5/6, ... which are independent of the type of build (shared/static)
NEW/BREAKING: refactor CMake-Code, so static/dynamic switch is done via <code>BUILD_SHARED_LIBS</code>, which retires <code>JKQtPlotter_BUILD_STATIC_LIBS</code>, <code>JKQtPlotter_BUILD_SHARED_LIBS</code> and removes the capability to build static and shared libraries in one location (fixes issue #104)
NEW: prepareed library for CMake's <a href="https://cmake.org/cmake/help/latest/module/FetchContent.html">FetchContent</a>-API
NEW: the different sub-libraries JKQTPlotter, JKQTFastPlotter (DEPRECATED), JKQTMath, JKQTMathText can be activated/deactivated with CMake options JKQtPlotter_BUILD_LIB_JKQTPLOTTER, JKQtPlotter_BUILD_LIB_JKQTFASTPLOTTER, JKQtPlotter_BUILD_LIB_JKQTMATHTEXT, JKQtPlotter_BUILD_LIB_JKQTMATH
2024-01-16 13:07:08 +01:00
..
barchart_errorbars.cpp bugfix 2022-09-11 07:52:19 +02:00
CMakeLists.txt NEW/BREAKING: provide general targets JKQTPlotter5/6::JKQTPlotter5/6, JKQTPlotter5/6::JKQTMathText5/6, ... which are independent of the type of build (shared/static) 2024-01-16 13:07:08 +01:00
README.md JKQTPlotter: NEW: added new error indicator styles JKQTPErrorHalfBarsOutwards, JKQTPErrorHalfBarsInwards, JKQTPErrorHalfBarsAbove, JKQTPErrorHalfBarsBelow which are especially useful for barcharts 2022-09-11 00:03:31 +02:00

Example (JKQTPlotter): Barchart With Error Bars

This project (see barchart_errorbars shows how to draw barcharts with different styles of error indicators.

The source code of the main application is (see barchart_errorbars.cpp:

    // 1. create a plotter window and get a pointer to the internal datastore (for convenience)
    JKQTPDatastore* ds=plot.getDatastore();

    // 2. now we create three columns for key and value
    size_t columnK=ds->addLinearColumn(6, 0.4*JKQTPSTATISTICS_PI, 2.2*JKQTPSTATISTICS_PI,"k");
    size_t columnV=ds->addColumnCalculatedFromColumn(columnK, &cos, "v");
    size_t columnE=ds->addColumnCalculatedFromColumn(columnK, [](double x) { return 0.05+0.06*(1.0+sin(x)); }, "error");

    // 3. create a graph in the plot, which plots the dataset with symmetric:
    JKQTPBarVerticalErrorGraph* graph1=new JKQTPBarVerticalErrorGraph(&plot);
    graph1->setKeyColumn(columnK);
    graph1->setValueColumn(columnV);
    graph1->setBarErrorColumn(columnE);
    // set error indicator style
    graph1->setBarErrorStyle(JKQTPErrorBars);
    graph1->setTitle(QObject::tr("JKQTPErrorBars"));
    plot.addGraph(graph1);

    // 4. create a second graph in the plot, which plots the second dataset with outer error bars only:
    JKQTPBarVerticalErrorGraph* graph2=new JKQTPBarVerticalErrorGraph(&plot);
    graph2->setKeyColumn(columnK);
    graph2->setValueColumn(columnV);
    graph2->setBarErrorColumn(columnE);
    // set error indicator style
    graph2->setBarErrorStyle(JKQTPErrorHalfBarsOutwards);
    graph2->setTitle(QObject::tr("JKQTPErrorHalfBarsOutwards"));
    plot.addGraph(graph2);

    // 5. now we set the graphs, so they are plotted side-by-side
    //    This function searches all JKQTPBarHorizontalGraph in the current
    //    plot and sets their shift/scale so they form a nice plot with
    //    side-by-side groups
    graph1->autoscaleBarWidthAndShift(0.9, 0.9);


    // 6 autoscale the plot so the graph is contained
    plot.zoomToFit();

    // 7. show plotter and make it a decent size
    plot.getPlotter()->setKeyPosition(JKQTPKeyInsideTopLeft);
    plot.setWindowTitle(title);
    plot.show();
    plot.resize(400,400);

The result looks like this:

barchart_errorbars

In order to draw horizontal error bars, you have to use JKQTPBarHorizontalErrorGraph instead of JKQTPBarVerticalErrorGraph:

barchart_errorbars_hor