2019-06-21 04:24:47 +08:00
|
|
|
/** \example symbols_and_styles.cpp
|
2019-01-20 23:15:10 +08:00
|
|
|
* JKQTPlotter: Examples: Line Graph with Different Symbols and Line Styles
|
2019-01-13 01:53:16 +08:00
|
|
|
*
|
2019-01-20 23:15:10 +08:00
|
|
|
* \ref JKQTPlotterSymbolsAndStyles
|
2019-01-13 01:53:16 +08:00
|
|
|
*/
|
|
|
|
|
2022-08-27 04:32:48 +08:00
|
|
|
#include "jkqtpexampleapplication.h"
|
2018-12-29 00:46:47 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include "jkqtplotter/jkqtplotter.h"
|
2019-06-20 22:06:31 +08:00
|
|
|
#include "jkqtplotter/graphs/jkqtpscatter.h"
|
2019-05-30 04:40:02 +08:00
|
|
|
#include "jkqtplotter/jkqtptools.h"
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-04-16 05:01:09 +08:00
|
|
|
|
2022-08-27 04:32:48 +08:00
|
|
|
JKQTPAppSettingController highDPIController(argc, argv);
|
|
|
|
JKQTPExampleApplication app(argc, argv);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
2022-04-16 05:01:09 +08:00
|
|
|
|
2018-12-29 00:46:47 +08:00
|
|
|
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
2019-01-20 23:15:10 +08:00
|
|
|
JKQTPlotter plot;
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPDatastore* ds=plot.getDatastore();
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// 2. now we create data a vector of x-values for a simple plot (a sine curve)
|
2019-05-12 23:35:08 +08:00
|
|
|
QVector<double> X1, X2;
|
2018-12-29 00:46:47 +08:00
|
|
|
const int Ndata=10; // number of plot points in each curve
|
|
|
|
for (int i=0; i<Ndata; i++) {
|
2019-11-24 17:42:44 +08:00
|
|
|
const double x=double(i)/double(Ndata)*2.0*JKQTPSTATISTICS_PI;
|
2019-05-12 23:35:08 +08:00
|
|
|
if (i<Ndata/2) X1<<x;
|
|
|
|
if (i>Ndata/2) X2<<x;
|
2018-12-29 00:46:47 +08:00
|
|
|
}
|
|
|
|
// and copy it to the datastore
|
2019-05-12 23:35:08 +08:00
|
|
|
size_t columnX1=ds->addCopiedColumn(X1, "x1");
|
|
|
|
size_t columnX2=ds->addCopiedColumn(X2, "x2");
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 3. now we make several plots with different symbol styles and line-styles
|
|
|
|
// for that we iterate over every symbol style and at the same time over
|
|
|
|
// pen styles from the vector pens
|
|
|
|
QVector<Qt::PenStyle> pens {Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine, Qt::DashDotDotLine };
|
|
|
|
int ipen=0;
|
2019-05-12 23:35:08 +08:00
|
|
|
for (int symbolID=0; symbolID<=static_cast<int>(JKQTPMaxSymbolID); symbolID++) {
|
2018-12-29 00:46:47 +08:00
|
|
|
// generate some plot data
|
|
|
|
QVector<double> Y;
|
2019-05-12 23:35:08 +08:00
|
|
|
const QVector<double>& X=(symbolID<=static_cast<int>(JKQTPMaxSymbolID)/2)?X1:X2;
|
|
|
|
const double dy=(symbolID<=static_cast<int>(JKQTPMaxSymbolID)/2)?(static_cast<double>(symbolID)*1.5):(static_cast<double>(symbolID-static_cast<int>(JKQTPMaxSymbolID)/2)*1.5);
|
2018-12-29 00:46:47 +08:00
|
|
|
for (auto& xx: X) {
|
2019-05-12 23:35:08 +08:00
|
|
|
Y<<sin(xx)+dy;
|
2018-12-29 00:46:47 +08:00
|
|
|
}
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXYLineGraph* graph=new JKQTPXYLineGraph(&plot);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// copy data into datastore and immediately set the yColumn
|
2024-01-23 20:09:37 +08:00
|
|
|
graph->setXColumn((symbolID<=static_cast<int>(JKQTPMaxSymbolID)/2)?columnX1:columnX2);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph->setYColumn(ds->addCopiedColumn(Y, "y"+QString::number(symbolID)));
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// set symbol + pen style and color
|
2019-04-22 19:27:50 +08:00
|
|
|
graph->setSymbolType(static_cast<JKQTPGraphSymbols>(symbolID));
|
|
|
|
graph->setLineStyle(pens[ipen]);
|
|
|
|
QString lname=jkqtp_QPenStyle2String(graph->getLineStyle());
|
2019-01-26 20:00:40 +08:00
|
|
|
graph->setDrawLine(static_cast<JKQTPGraphSymbols>(symbolID)!=JKQTPDot);
|
|
|
|
if (!graph->getDrawLine()) lname="";
|
2018-12-29 00:46:47 +08:00
|
|
|
// set symbol size
|
2019-05-12 23:35:08 +08:00
|
|
|
graph->setSymbolSize(12);
|
2018-12-29 00:46:47 +08:00
|
|
|
// set width of symbol lines
|
2019-04-22 19:27:50 +08:00
|
|
|
graph->setSymbolLineWidth(1.5);
|
2018-12-29 00:46:47 +08:00
|
|
|
// set width of graph line
|
2019-01-26 03:16:04 +08:00
|
|
|
graph->setLineWidth(1);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// graph title is made from symbol+penstyle
|
2019-04-22 19:27:50 +08:00
|
|
|
graph->setTitle(JKQTPGraphSymbols2NameString(static_cast<JKQTPGraphSymbols>(graph->getSymbolType()))+QString(", ")+lname);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// add the graph to the plot, so it is actually displayed
|
|
|
|
plot.addGraph(graph);
|
|
|
|
|
|
|
|
ipen++;
|
|
|
|
if (ipen>=pens.size()) ipen=0;
|
2019-05-12 23:35:08 +08:00
|
|
|
|
|
|
|
// create PNG with the symbol
|
|
|
|
/*QPixmap pix(24,24);
|
|
|
|
pix.fill(Qt::transparent);
|
|
|
|
JKQTPEnhancedPainter p;
|
|
|
|
p.begin(&pix);
|
|
|
|
p.setRenderHint(JKQTPEnhancedPainter::Antialiasing);
|
|
|
|
p.setRenderHint(JKQTPEnhancedPainter::TextAntialiasing);
|
|
|
|
JKQTPPlotSymbol(p, pix.width()/2,pix.height()/2,static_cast<JKQTPGraphSymbols>(symbolID),pix.width()/1.2,1.5,QColor("blue"), QColor("blue").lighter());
|
|
|
|
p.end();
|
|
|
|
pix.save(JKQTPGraphSymbols2String(static_cast<JKQTPGraphSymbols>(symbolID))+".png", "PNG");*/
|
|
|
|
|
2018-12-29 00:46:47 +08:00
|
|
|
}
|
|
|
|
|
2019-05-12 23:35:08 +08:00
|
|
|
|
2018-12-29 00:46:47 +08:00
|
|
|
// 4. autoscale the plot so the graph is contained
|
2023-12-19 05:24:59 +08:00
|
|
|
plot.getPlotter()->setKeyPosition(JKQTPKeyOutsideBottomLeft);
|
|
|
|
plot.getMainKey()->setLayout(JKQTPKeyLayout::JKQTPKeyLayoutMultiColumn);
|
2019-05-12 23:35:08 +08:00
|
|
|
plot.getXAxis()->setShowZeroAxis(false);
|
|
|
|
plot.getYAxis()->setShowZeroAxis(false);
|
|
|
|
plot.setGrid(false);
|
2018-12-29 00:46:47 +08:00
|
|
|
plot.zoomToFit();
|
|
|
|
|
|
|
|
// 5. show plotter and make it a decent size
|
|
|
|
plot.show();
|
2022-08-29 04:48:14 +08:00
|
|
|
plot.resize(1700/plot.devicePixelRatioF(), 1100/plot.devicePixelRatioF());
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|