# Qwt 自定义坐标轴tick

2014年4月20日 由 Creater

Interval and all tick positions of a scale are stored in a QwtScaleDiv object.

You can manually create a QwtScaleDiv and assign it to a plot axis (QwtPlot::setAxisScaleDiv()) , or you can use a QwtScaleEngine ( QwtPlot has one for each axis), that calculates it for you. When autoscaling ( QwtPlot::setAxisAutoScale() ) is enabled, the interval for the calculation of the scale is taken from the bounding rectangle of the plot items ( f.e. curves ), otherwise interval/step size are passed with QwtPlot::setAxisScale().

The default scale engines try to calculate ticks for linear decimal scales, what doesn’t need to be the right thing for date/time scales. ( QwtDateTimeScaleEngine is on my TODO list ). To improve your scale divisions you can derive and assign ( QwtPlot::setAxisScaleEngine()) your own scale engine or simply use QwtPlot::setAxisScaleDiv instead.

QwtScaleDraw is responsible for painting a QwtScaleDiv. QwtScaleDraw::label() maps a tick value into a string.
In the cpuplot example the values on the x-axis are seconds elapsed since the system is up. TimeScaleDraw::label() maps them into time strings – that’s all.

    QList<double> ticks[QwtScaleDiv::NTickTypes];
ticks[QwtScaleDiv::MajorTick] << 60 << 120;  //只在60和120个sample点tick
QwtScaleDiv scaleDiv(
ticks[QwtScaleDiv::MajorTick].first(),
ticks[QwtScaleDiv::MajorTick].last(),
ticks );
plot->setAxisScaleDiv(QwtPlot::xBottom, scaleDiv);

class RealScaleDraw: public QwtScaleDraw
{
public:
RealScaleDraw()
{
}
virtual QwtText label(double v) const
{
//return QwtScaleDraw::label(v);
return QwtText(QString::number(v/10)); //60和120都除以10
}
};


plot->setAxisScaleDraw(QwtPlot::xBottom, new RealScaleDraw());