QT之qwt绘图时间横坐标如何显示跨月(1/31-3/2)
class MyDateScaleDraw : public QwtScaleDraw
{
public:
MyDateScaleDraw(const QList<QDate>& listDate) : m_listDate(listDate)
{
}
virtual QwtText label(double value) const
{
QDate date = m_listDate[(int)value];
return QString::asprintf("%02d/%02d",date.month(),date.day());
}
private:
QList<QDate> m_listDate;
};
void TestDraw()
{
QDate fromDate = QDateTime::fromString("2023-01-31", "yyyy-MM-dd");
QList<QDate> listDate = { };
for(int i = 0; i < 32; i++)
{
listDate.append(fromDate.addDays(i));
}
//从0开始,步长为1,若从负数开始,则要加个偏移量,可在CustomScakeDraw类中加
ui->plot->setAxisScale(QwtPlot::yBottom, 0, valueY.size() - 1, 1);
//取消小格
ui->plot->setAxisMaxMinor(QwtPlot::yBottom, 0);
ui->plot->setAxisScaleDraw(QwtPlot::yBottom, new MyDateScaleDraw(listDate));
}