JKQtPlotter/examples/scatter
2022-08-30 09:44:40 +02:00
..
CMakeLists.txt added more auto-generated screenshots 2022-08-26 22:32:48 +02:00
README.md added Example for JKQTPXYScatterGraph and JKQTPXYScatterErrorGraph 2022-08-26 12:43:00 +02:00
scatter_and_lib.pro added Example for JKQTPXYScatterGraph and JKQTPXYScatterErrorGraph 2022-08-26 12:43:00 +02:00
scatter.cpp fixed issue #73: Symbol thickness differs in actual plot and legend 2022-08-30 09:44:40 +02:00
scatter.pro added Example for JKQTPXYScatterGraph and JKQTPXYScatterErrorGraph 2022-08-26 12:43:00 +02:00

Example (JKQTPlotter): Very simple scatter-graph

This project (see ./examples/scatter/) simply creates a JKQTPlotter widget (as a new window) and adds a single scatter graph of type JKQTPXYScatterGraph (a sine-wave with noise).

The source code of the example can be found in jkqtplotter_scatter.cpp.

First we create a plotter window and get a pointer to the internal datastore (for convenience):

    JKQTPlotter plot;
    JKQTPDatastore* ds=plot.getDatastore();

Now we add two columns to the JKQTPDatastore and obtain back-inserter iterators for these:

    size_t columnX=ds->addColumn("x");
    auto colXInserter=ds->backInserter(columnX);
    size_t columnY=ds->addColumn("y");
    auto colYInserter=ds->backInserter(columnY);

Now we create data for a simple plot (a sine curve with random noise):

    std::default_random_engine generator;
    std::normal_distribution<double> distribution(0,0.5);
    const int Ndata=100;
    for (int i=0; i<Ndata; i++) {
        // put data
        const double x=double(i)/double(Ndata)*8.0*JKQTPSTATISTICS_PI;
        *colXInserter=x;
        *colYInserter=sin(x)+distribution(generator);
        // advance back-inserters
        colXInserter++;
        colYInserter++;
    }

Here we create a graph in the plot, which plots the dataset X/Y:

    JKQTPXYScatterGraph* graph1=new JKQTPXYScatterGraph(&plot);
    graph1->setXColumn(columnX);
    graph1->setYColumn(columnY);
    graph1->setTitle(QObject::tr("sine graph"));

Now we add the graph to the plot, so it is actually displayed:

    plot.addGraph(graph1);

Finally we autoscale the plot so the graph is contained:

    plot.zoomToFit();

The result looks like this:

jkqtplotter_scatter