代码拉取完成,页面将自动刷新
#include "dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
{
setMinimumSize(QSize(800, 600));
QVBoxLayout *lay = new QVBoxLayout;
customPlot = new MyCustomPlot();
lay->addWidget(customPlot);
setLayout(lay);
}
Dialog::~Dialog()
{
}
MyCustomPlot::MyCustomPlot(QWidget *parent) :
QCustomPlot(parent)
{
int nCount = 10;
QVector<double> x(nCount), y0(nCount), y1(nCount); // initialize with entries 0..100
for (int i = 0; i < nCount; ++i)
{
x[i] = QDateTime::currentDateTime().toTime_t() + 3600*i; // x goes from -1 to 1
y0[i] = qSin(i * 10.0f / nCount); // sin
y1[i] = abs(qCos(i * 10.0f / nCount)); // cos
}
addGraph();
graph(0)->setData(x, y0);
QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
dateTicker->setDateTimeFormat("MM-dd\nHH:mm");
xAxis->setTicker(dateTicker);
xAxis2->setTicker(dateTicker);
xAxis2->setVisible(true);
xAxis2->setTickLabels(false);
graph(0)->setName(QStringLiteral("流量"));
graph(0)->setLineStyle(QCPGraph::lsLine);
graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
xAxis->setLabel(QStringLiteral("时间"));
yAxis->setLabel(QStringLiteral("流量(m^3/h)"));
// 降雨量曲线
myBars = new QCPBars(xAxis, yAxis2);
myBars->setWidth(250);
yAxis2->setRangeReversed(true);
yAxis2->setLabel(QStringLiteral("降雨量(mm)"));
yAxis2->setVisible(true);
yAxis2->setRangeReversed(true);
myBars->setName("降雨量");
myBars->setData(x, y1);
myBars->setPen(QPen(Qt::red));
myBars->setBrush(QBrush(QColor(255, 0, 0, 20)));
setInteractions(QCP::iRangeZoom | QCP::iSelectPlottables);
axisRect()->setRangeZoomFactor(1.2, 1);
rescaleAxes(true);
double yUpper = yAxis->range().upper;
yAxis->setRangeUpper(yUpper * 2);
yAxis2->setRangeUpper(yUpper * 3);
line = new QCPItemStraightLine(this);
line->setLayer("overlay");
line->setPen(QPen(Qt::gray, 1, Qt::DashLine));
line->setClipToAxisRect(true);
line->point1->setCoords(0, 0);
line->point2->setCoords(0, 0);
replot();
}
void MyCustomPlot::mouseMoveEvent(QMouseEvent *event)
{
QCustomPlot::mouseMoveEvent(event);
// 当前鼠标位置(像素坐标)
int x_pos = event->pos().x();
int y_pos = event->pos().y();
// 像素坐标转成实际的x,y轴的坐标
float x_val = xAxis->pixelToCoord(x_pos);
float y_val = yAxis->pixelToCoord(y_pos);
if (x_val > xAxis->range().lower && x_val < xAxis->range().upper
&& y_val > yAxis->range().lower && y_val < yAxis->range().upper)
{
for (int i = 0; i < graph(0)->dataCount(); i++)
{
if (abs(graph(0)->dataMainKey(i) - x_val) < 100
&& abs(graph(0)->dataMainValue(i) - y_val) < 100)
{
if (lastIdx == i)
break;
lastIdx = i;
line->point1->setCoords(graph(0)->dataMainKey(i), yAxis->range().lower);
line->point2->setCoords(graph(0)->dataMainKey(i), yAxis->range().upper);
QToolTip::hideText();
QToolTip::showText(event->globalPos(),
QString(QStringLiteral(
"<b>时间</b>:%1<br><b>流量</b>:%2<br><b>降雨量</b>:%3")).
arg(QDateTime::fromTime_t(graph(0)->dataMainKey(i)).toString(
"yyyy-MM-dd hh:mm")).
arg(graph(0)->dataMainValue(i)).
arg(myBars->dataMainValue(i)),
this, rect());
replot();
break;
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。