QCPAxis中增加两个函数,设置x轴的值和标签映射关系,要把中午午休的时间去掉;
void setTickVector(QVector<double> tickVector) { mTickVector = tickVector; };
void setTickLabels(QVector<QString> tickLabel ) { mTickVectorLabels= tickLabel; }
void NGraph::SetXTimeLabel(QString strTimeFormat) {//ui.NGragh->xAxis->setTickLabelType(QCPAxis::ltDateTime);//ui.NGragh->xAxis->setDateTimeFormat(strTimeFormat);//ui.NGragh->xAxis->setDateTimeSpec(Qt::LocalTime);//QSharedPointer<QCPAxisTickerDateTime> dateTimeTicker(new QCPAxisTickerDateTime);//dateTimeTicker->setDateTimeFormat(strTimeFormat);//dateTimeTicker->setDateTimeSpec(Qt::LocalTime);//ui.NGragh->xAxis->setTicker(dateTimeTicker);ui.NGragh->xAxis->setFixTickLabel(true);ui.NGragh->xAxis->setTickLabels(true); // 不显示这一段的刻度标签ui.NGragh->xAxis->setSubTicks(false); // 不显示子刻度QTime time=QTime::fromString("0930", "hhmm");double starttime= time.hour()*3600+ time.minute()*60;ui.NGragh->xAxis->setTickVector(QVector<double>({ starttime,starttime +1800,starttime+1800*2,starttime+1800*3,starttime+1800*4,starttime+1800*5,starttime + 1800 *6,starttime + 1800 *7,starttime + 1800 * 8 }));ui.NGragh->xAxis->setTickLabels(QVector<QString>({"09:30","10:00","1030","11:00","11:30","13:30","14:00","14:30","15:00"})); }
因为会自动生成坐标轴,所以需要再顶一个个变量,设置固定坐标轴
ui.NGragh->xAxis->setFixTickLabel(true);
void setFixTickLabel(bool fix) { mbFixTickLabel = fix; };
然后在生成坐标轴的地方加上判断,如果设置了true,则不自动生成标签
void QCPAxis::setupTickVectors() {if (!mParentPlot) return;if ((!mTicks && !mTickLabels && !mGrid->visible()) || mRange.size() <= 0) return;if (mbFixTickLabel == false){QVector<QString> oldLabels = mTickVectorLabels;mTicker->generate(mRange, mParentPlot->locale(), mNumberFormatChar, mNumberPrecision, mTickVector, mSubTicks ? &mSubTickVector : nullptr, mTickLabels ? &mTickVectorLabels : nullptr);mCachedMarginValid &= mTickVectorLabels == oldLabels; // if labels have changed, margin might have changed, too } }
最后在生成数据的地方,如果是下午时间,则自动减去中午的休市的1个半小时,也就是5400秒,也是时间值前移5400秒,去掉休市时间,标签值映射关系则是向后移;这样看起来中间没有中断,而且时间标签也能对的上
if (it != m_mapMiniteData.end()){QVariantList listdata = it.value().value("listdata").toList();QVector<double> x, y;for (int i = 0; i < listdata.size(); i++){if (listdata[i].toMap().value("time").toString() >= "1300"){QTime time = QTime::fromString(listdata[i].toMap().value("time").toString(), "hhmm").addSecs(-5400);x.push_back(time.hour() * 3600 + time.minute() * 60);}else{QTime time = QTime::fromString(listdata[i].toMap().value("time").toString(), "hhmm");x.push_back(time.hour() * 3600 + time.minute() * 60);}y.push_back(listdata[i].toMap().value("price").toDouble());}m_Graph.hide();m_Graph.SetData(0, x, y);m_Graph.show();}