mirror of
https://github.com/jkriege2/JKQtPlotter.git
synced 2024-11-16 02:25:50 +08:00
df257a6b1a
improved documentation
116 lines
6.0 KiB
Markdown
116 lines
6.0 KiB
Markdown
# Example (JKQTPlotter): Different Types of Errorindicators {#JKQTPlotterErrorBarStyles}
|
|
This project (see `./examples/simpletest_errorbarstyles/`) simply creates a JKQTPlotter widget (as a new window) and adds several curves show-casing different styles of error indicators. Data is initialized from two QVector<double> objects.
|
|
|
|
The source code of the main application can be found in [`jkqtplotter_simpletest_errorbarstyles.cpp`](../simpletest_errorbarstyles/jkqtplotter_simpletest_errorbarstyles.cpp).
|
|
|
|
First some data is added to the internal datastore (mostly, like explained in several other examples, like e.g. [Line Graph with Different Symbols and Line Styles](../simpletest_symbols_and_styles)). The (in a loop) several graphs are added, each with a distinct style for its error indicators:
|
|
|
|
```.cpp
|
|
// 3. now we make several plots with different error styles
|
|
// for that we iterate over every style from the vector errorStyles
|
|
// the array showXandYError indicates whether we want x- and y-error
|
|
// for a style for all stywhere this is false, only y-error-indicators
|
|
// are shown
|
|
QVector<JKQTPErrorPlotstyle> errorStyles {JKQTPNoError, JKQTPErrorBars, JKQTPErrorSimpleBars, JKQTPErrorLines, JKQTPErrorPolygons, JKQTPErrorBoxes, JKQTPErrorEllipses, JKQTPErrorBarsPolygons, JKQTPErrorBarsLines, JKQTPErrorSimpleBarsLines, JKQTPErrorSimpleBarsPolygons };
|
|
QVector<bool> showXandYError { false , true , true , false , false , true , true , false , false , false , false };
|
|
|
|
for (int errorID=0; errorID<errorStyles.size(); errorID++) {
|
|
// generate some plot data
|
|
QVector<double> Y;
|
|
for (auto& xx: X) {
|
|
Y<<xx*0.5+static_cast<double>(errorID)*2.5;
|
|
}
|
|
|
|
// create a graph object
|
|
JKQTPXYLineErrorGraph* graph=new JKQTPXYLineErrorGraph(&plot);
|
|
|
|
// copy data into datastore and immediately set the yColumn
|
|
graph->set_xColumn(columnX);
|
|
graph->set_yColumn(ds->addCopiedColumn(Y, "y"+QString::number(errorID)));
|
|
graph->set_xErrorColumn(columnXError);
|
|
graph->set_yErrorColumn(columnYError);
|
|
|
|
// set error style, for the y-axis
|
|
graph->set_yErrorStyle(errorStyles[errorID]);
|
|
// no error indicators for the x-values
|
|
graph->set_xErrorStyle(JKQTPNoError);
|
|
// ... unless: for some error styles we want error in both directions
|
|
if (showXandYError[errorID]) {
|
|
graph->set_xErrorStyle(errorStyles[errorID]);
|
|
graph->set_drawLine(false);
|
|
}
|
|
// make error indicator 30% transparent
|
|
QColor c=graph->get_errorFillColor();
|
|
c.setAlphaF(0.3);
|
|
graph->set_errorFillColor(c);
|
|
// set error indicator line width
|
|
graph->set_errorWidth(1);
|
|
// set length of small bars at the end of error bars
|
|
graph->set_errorbarSize(15);
|
|
|
|
|
|
// set symbol (cross/X) + pen style (and color)dashed)
|
|
graph->set_symbol(JKQTPCross);
|
|
graph->set_style(Qt::DashLine);
|
|
// set symbol size
|
|
graph->set_symbolSize(5);
|
|
// set width of symbol lines
|
|
graph->set_symbolWidth(1);
|
|
// set width of graph line
|
|
graph->setLineWidth(1);
|
|
|
|
// graph title is made from symbol+errorStylestyle, we use the LaTeX instruction \verb around the
|
|
// result of JKQTPErrorPlotstyle2String(), because it contains underscores that would otherwise
|
|
// lead to lower-case letter, which we don't want
|
|
graph->set_title("\\verb{"+JKQTPErrorPlotstyle2String(errorStyles[errorID])+"}");
|
|
|
|
// add the graph to the plot, so it is actually displayed
|
|
plot.addGraph(graph);
|
|
}
|
|
```
|
|
|
|
The error styles are set in these lines:
|
|
```.cpp
|
|
// set error style, for the y-axis
|
|
graph->set_yErrorStyle(errorStyles[errorID]);
|
|
// no error indicators for the x-values
|
|
graph->set_xErrorStyle(errorStyles[errorID]);
|
|
```
|
|
|
|
There are several variables that can be used to further style the error indicator, like:
|
|
```.cpp
|
|
// make error indicator 30% transparent
|
|
QColor c=graph->get_errorFillColor();
|
|
c.setAlphaF(0.3);
|
|
graph->set_errorFillColor(c);
|
|
// set error indicator line width
|
|
graph->set_errorWidth(1);
|
|
// set length of small bars at the end of error bars
|
|
graph->set_errorbarSize(15);
|
|
```
|
|
|
|
There are more properties that you can find in the documentation of the mix-in classes `JKQTPXYGraphErrors`, `JKQTPXGraphErrors`, `JKQTPYGraphErrors`, `JKQTPGraphErrors`.
|
|
|
|
In addition the plot key is moved outside the pot and the grid in the plot is switched off:
|
|
```.cpp
|
|
// 6. change locaion of key (outside top-right)
|
|
plot.getPlotter()->setKeyPosition(JKQTPKeyOutsideRightTop);
|
|
// ... and switch off the grid
|
|
plot.getXAxis()->set_drawGrid(false);
|
|
plot.getXAxis()->set_drawMinorGrid(false);
|
|
plot.getYAxis()->set_drawGrid(false);
|
|
plot.getYAxis()->set_drawMinorGrid(false);
|
|
```
|
|
|
|
The result looks like this:
|
|
|
|
![jkqtplotter_simpletest_errorbarstyles](../../screenshots/jkqtplotter_simpletest_errorbarstyles.png)
|
|
|
|
Error bars are implemented in the mixin-classes `JKQTPXYGraphErrors`, `JKQTPXGraphErrors` and `JKQTPYGraphErrors` that are all derived from `JKQTPGraphErrors`. With these it is simple to add error indicators to several different plot styles. Usually you can recognize these by looking at the class name, e.g. `JKQTPXYLineGraph` is a simple line+symbol graph, and `JKQTPXYLineErrorGraph` is the same with error indictaors (see above). There are also several other plots with error indicators:
|
|
- `JKQTPBarVerticalErrorGraph` for barcharts with errors:<br>![](../../screenshots/jkqtplotter_simpletest_errorbarstyles_barcharts.png)
|
|
- `JKQTPImpulsesVerticalGraph` for impulse/candle-stick charts with errors:<br>![](../../screenshots/jkqtplotter_simpletest_errorbarstyles_impulses.png)
|
|
- `JKQTPFilledCurveXErrorGraph` for filled curves with errors:<br>![](../../screenshots/jkqtplotter_simpletest_errorbarstyles_filledcurves.png)
|
|
.
|
|
|
|
|