mirror of
https://github.com/jkriege2/JKQtPlotter.git
synced 2024-11-15 10:05:47 +08:00
1.9 KiB
1.9 KiB
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: