Skip to content

Instantly share code, notes, and snippets.

@gastmaier
Created March 15, 2023 11:24
Show Gist options
  • Save gastmaier/bb20dac732ba48765195cf7a766f569d to your computer and use it in GitHub Desktop.
Save gastmaier/bb20dac732ba48765195cf7a766f569d to your computer and use it in GitHub Desktop.
Demo showing Qwt (multiaxis branch) with multiple Y-Axis labels
/**
* Demo showing Qwt (multiaxis branch) with multiple Y-Axis labels
**/
#include <QApplication>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include <qwt_point_data.h>
#include <qwt_plot_canvas.h>
#include <qwt_axis_id.h>
#include <cmath>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QwtAxisId axisY1(0,0);
QwtAxisId axisY2(0,1);
QwtAxisId axisY3(0,2);
QwtPlot plot;
plot.setTitle( "Plot Demo" );
plot.setCanvasBackground( Qt::white );
plot.setAxesCount(0, 3);
plot.setAxisAutoScale(axisY1,true);
plot.setAxisAutoScale(axisY2,true);
plot.setAxisAutoScale(axisY3,true);
// Enable differents combinations,
// Qwt seems to allocate the widths correctly.
//plot.setAxisVisible(axisY1,false);
//plot.setAxisVisible(axisY2,false);
plot.setAxisVisible(axisY3,false);
plot.insertLegend( new QwtLegend() );
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setLineWidth( 1 );
canvas->setFrameStyle( QFrame::Box | QFrame::Plain );
QPalette canvasPalette( Qt::white );
canvasPalette.setColor( QPalette::Foreground, QColor( 131, 131, 131 ) );
canvas->setPalette( canvasPalette );
plot.setCanvas(canvas);
QVector<double> listX;
QVector<double> list1y, list2y;
for (int var = 0.1; var < 6.28; ++var) {
listX.push_back(var);
list1y.push_back(std::sin(var));
list2y.push_back(2*cos(var));
}
QwtPointArrayData data1(listX,list1y);
QwtPointArrayData data2(listX,list2y);
QwtPlotCurve *curve1 = new QwtPlotCurve();
QwtPlotCurve *curve2 = new QwtPlotCurve();
curve1->setTitle( "Curve Sin(x)" );
curve1->setPen( Qt::blue, 4 ),
curve1->setRenderHint( QwtPlotItem::RenderAntialiased, true );
curve1->setSamples(&data1);
curve2->setTitle( "Curve 2*cos(x)" );
curve2->setPen( Qt::red, 4 ),
curve2->setRenderHint( QwtPlotItem::RenderAntialiased, true );
curve2->setSamples(&data2);
curve1->setYAxis(axisY1);
curve2->setYAxis(axisY2);
curve1->attach( &plot );
curve2->attach( &plot );
plot.replot();
plot.resize( 600, 400 );
plot.show();
return a.exec();
}
me@precision:~/Documents/multiaxis$ cat main.cpp
// Demo showing Qwt (multiaxis branch)
// with multiple Y-Axis labels
#include <QApplication>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include <qwt_point_data.h>
#include <qwt_plot_canvas.h>
#include <qwt_axis_id.h>
#include <cmath>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QwtAxisId axisY1(0,0);
QwtAxisId axisY2(0,1);
QwtAxisId axisY3(0,2);
QwtPlot plot;
plot.setTitle( "Plot Demo" );
plot.setCanvasBackground( Qt::white );
plot.setAxesCount(0, 3);
plot.setAxisAutoScale(axisY1,true);
plot.setAxisAutoScale(axisY2,true);
plot.setAxisAutoScale(axisY3,true);
// Enable differents combinations,
// Qwt seems to allocate the widths correctly.
//plot.setAxisVisible(axisY1,false);
//plot.setAxisVisible(axisY2,false);
plot.setAxisVisible(axisY3,false);
plot.insertLegend( new QwtLegend() );
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setLineWidth( 1 );
canvas->setFrameStyle( QFrame::Box | QFrame::Plain );
QPalette canvasPalette( Qt::white );
canvasPalette.setColor( QPalette::Foreground, QColor( 131, 131, 131 ) );
canvas->setPalette( canvasPalette );
plot.setCanvas(canvas);
QVector<double> listX;
QVector<double> list1y, list2y;
for (int var = 0.1; var < 6.28; ++var) {
listX.push_back(var);
list1y.push_back(std::sin(var));
list2y.push_back(2*cos(var));
}
QwtPointArrayData data1(listX,list1y);
QwtPointArrayData data2(listX,list2y);
QwtPlotCurve *curve1 = new QwtPlotCurve();
QwtPlotCurve *curve2 = new QwtPlotCurve();
curve1->setTitle( "Curve Sin(x)" );
curve1->setPen( Qt::blue, 4 ),
curve1->setRenderHint( QwtPlotItem::RenderAntialiased, true );
curve1->setSamples(&data1);
curve2->setTitle( "Curve 2*cos(x)" );
curve2->setPen( Qt::red, 4 ),
curve2->setRenderHint( QwtPlotItem::RenderAntialiased, true );
curve2->setSamples(&data2);
curve1->setYAxis(axisY1);
curve2->setYAxis(axisY2);
curve1->attach( &plot );
curve2->attach( &plot );
plot.replot();
plot.resize( 600, 400 );
plot.show();
return a.exec();
}
@YashKarpe96
Copy link

hey @gastmaier

thank you for showcasing this approach, Can i ask you some questions if possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment