|
myplot.cpp
#include "mydataplot.h"
DataPlot:ataPlot(QWidget *parent):
QwtPlot(parent)
{
// Disable polygon clipping
QwtPainter::setDeviceClipping(false);
// We don't need the cache here
canvas()->setPaintAttribute(QwtPlotCanvas:aintCached, false);
canvas()->setPaintAttribute(QwtPlotCanvas:aintPacked, false);
alignScales();
// Initialize data
for (int i = 0; i< PLOT_SIZE; i++)
{
d_x[i] = i; // time axis
d_y[i] = 0;
d_z[i] = 0;
}
MaxZValue=10;
MaxFValue=-10;
//这个会根据画板中的图在RightLegend显示一个图例
//insertLegend(new QwtLegend(),QwtPlot::BottomLegend);
// Insert new curves
QwtPlotCurve *cRight = new QwtPlotCurve(tr("通道A"));
cRight->setRenderHint(QwtPlotItem::RenderAntialiased);
cRight->attach(this);
QwtPlotCurve *cLeft = new QwtPlotCurve(tr(""));
cLeft->attach(this);
// Set curve styles
cRight->setPen(QPen(Qt::red));
cLeft->setPen(QPen(Qt::blue));
// Attach (don't copy) data. Both curves use the same x array.
// cRight->setData(d_x, d_y, PLOT_SIZE);
// cLeft->setData(d_x, d_z, PLOT_SIZE);
// 设置坐标轴刻度
setAxisTitle(QwtPlot::xBottom, tr("时间(/50mS)"));
setAxisScale(QwtPlot::xBottom, 0, 100);
setAxisTitle(QwtPlot::yLeft, tr("输入电压值(/mV)"));
setAxisScale(QwtPlot::yLeft, -10,10);
//右键平移曲线
QwtPlotPanner *panner=new QwtPlotPanner(this->canvas());
panner->setMouseButton(Qt::RightButton);
//滚轮放大或缩小曲线,y轴在放大的时候,X坐标不变化
QwtPlotMagnifier *magnifier=new QwtPlotMagnifier(this->canvas());
magnifier->setMouseButton(Qt::NoButton);
magnifier->setAxisEnabled(QwtPlot::xBottom,false);
//QwtPlotZoomer *zoomer = new QwtPlotZoomer(this->canvas());
//zoomer->setRubberBandPen(QColor( Qt::yellow));
//zoomer->setTrackerPen(QColor( Qt::yellow ) );
//zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier);
//zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );
//一个选择器,十字线,以xBottom和yLeft坐标
// picker = new QwtPlotPicker(QwtPlot::xBottom,QwtPlot::yLeft,1,
// QwtPlotPicker::CrossRubberBand,QwtPicker::ActiveOnly,
// this->canvas());
//picker->setStateMachine(new QwtPickerDragPointMachine());//拖拽点起作用
picker->setRubberBandPen(QPen(QColor(Qt::white)));
picker->setTrackerPen(QColor(Qt::yellow));
//设置背景色
setAutoFillBackground(true);
this->canvas()->setPalette(QPalette (QColor(Qt::darkCyan)));
//设置网格
QwtPlotGrid *grid = new QwtPlotGrid;
grid->enableXMin(true);
grid->setMajorPen(QPen(Qt::white, 0, Qt:otLine));//大格子
grid->setMinorPen(QPen(Qt::gray, 0 , Qt:otLine));//大格子里的小格子
grid->attach(this);
}
void DataPlot::alignScales()
{
// canvas setFrameStyle(QFrame::Box | QFrame:lain );
//canvas->setLineWidth(1);//边框宽度
for ( int i = 0; i < QwtPlot::axisCnt; i++ )
{
QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
if ( scaleWidget )
scaleWidget->setMargin(0);
QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
if ( scaleDraw )
scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
}
}
void DataPlot::UpPlotdate(double x1,double x2)
{
for ( int i = PLOT_SIZE - 1 ; i > 0; i-- )
d_y[i] = d_y[i-1];
d_y[0]=x1;
//for ( int j = 0; j < PLOT_SIZE - 1; j++ )
// d_z[j] = d_z[j+1];
d_z[PLOT_SIZE - 1]=x2;
replot();
}
void DataPlot::setMaxYaxisValue(double value,int flag)
{
if(flag==1)
{
MaxZValue=value;
setAxisScale(QwtPlot::yLeft, MaxFValue, MaxZValue);
}
else if(flag==2)
{
MaxFValue=value;
setAxisScale(QwtPlot::yLeft, MaxFValue, MaxZValue);
}
}
double DataPlot::getMaxYaxisValue(int flag)
{
if(flag==1)
return MaxZValue;
else if(flag==2)
return MaxFValue;
else
return 0;
}
void DataPlot::resetplot()
{
MaxZValue=MaxFValue=0;
for (int i = 0; i< PLOT_SIZE; i++)
{
if(d_y[i]>MaxZValue)
MaxZValue=d_y[i];
if(d_y[i]<MaxFValue)
MaxFValue=d_y[i];
}
}
|
|