.H
#ifndef GAI_CHART_VIEW_H
#define GAI_CHART_VIEW_H
#include <QtCharts/QChartView>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QLineSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QChart>
#include <QtCharts/QBarSet>
using namespace QtCharts;
class GaiChartView : public QChartView
{
Q_OBJECT
public:
GaiChartView(QWidget *parent);
~GaiChartView();
public:
//画曲线图X-Y,vvData[0]为横坐标数据,其他为纵轴数据
void DrawCurve(QVector<QVector<double> > &vvData, QStringList &lstLegends, QString strXLabel ="", QString strYLabel ="",QString strViewLabel ="");
//画曲线图X-Y,vvData[奇数]为横坐标数据,vvData[奇数+1]纵轴数据
void DrawCurve_ave(QVector<QVector<double> > &vvData, QString strViewLabel ="", QString strXLabel ="", QString strYLabel ="");
//绘制简单的直方图
void DrawBar(QVector<double> &vData);
public slots:
//xianshi
void DisplaySeriesValue(const QPointF &point, bool state);
private:
QChart *m_chart;
};
#endif // GAI_CHART_VIEW_H
.cpp
#include "gai_chart_view.h"
#include <QGraphicsLayout>
#include <QToolTip>
GaiChartView::GaiChartView(QWidget *parent)
: QChartView(parent)
{
m_chart = new QChart();
QLineSeries *ptrSplineSeries = new QLineSeries();//曲线图
*ptrSplineSeries << QPointF(0.0, 0.0) << QPointF(3.0, 0.0);
m_chart->addSeries(ptrSplineSeries);
m_chart->createDefaultAxes();
//设置以下属性使显示区域最大化
m_chart->layout()->setContentsMargins(1, 1, 1, 1);//设置外边界全部为1
m_chart->setMargins(QMargins(1, 1, 1, 1));//设置内边界全部为1
m_chart->setBackgroundRoundness(0); //设置背景区域无圆角
setChart(m_chart);
}
GaiChartView::~GaiChartView()
{
}
void GaiChartView::DrawCurve(QVector<QVector<double> > &vvData, QStringList &lstLegends, QString strXLabel /*=""*/, QString strYLabel /*=""*/, QString strViewLabel /*=""*/)
{
if (vvData.isEmpty() || vvData.size() - 1 != lstLegends.size())
{
return ;
}
m_chart->removeAllSeries();
QVector<QSplineSeries *> vecSplineSeries;
QVector<double> &vRow0 = vvData[0];
for (int i = 1; i < vvData.size(); ++i)
{
QSplineSeries *ptrSplineSeries = new QSplineSeries();//曲线图
QVector<double> &vRow = vvData[i];
for (int j = 0; j < vRow.size(); ++j)
{
*ptrSplineSeries << QPointF(vRow0[j], vRow[j]);
}
vecSplineSeries.push_back(ptrSplineSeries);
}
for (int i = 0; i < vecSplineSeries.size(); ++i)
{
QString strName = lstLegends[i];
vecSplineSeries[i]->setName(strName);
m_chart->addSeries(vecSplineSeries[i]);
connect(vecSplineSeries[i], SIGNAL(hovered(const QPointF&, bool)), this, SLOT(DisplaySeriesValue(const QPointF &, bool)));
}
auto xMax = std::max_element(std::begin(vvData[0]), std::end(vvData[0]));
auto xMin = std::min_element(std::begin(vvData[0]), std::end(vvData[0]));
double xLow = *xMin;
double xHigh = *xMax;
double yLow = DBL_MAX;
double yHigh = DBL_MIN;
for (int i = 1; i < vvData.size(); ++i)
{
auto yMax = std::max_element(std::begin(vvData[i]), std::end(vvData[i]));
auto yMin = std::min_element(std::begin(vvData[i]), std::end(vvData[i]));
if (*yMax > yHigh)
{
yHigh = *yMax;
}
if (*yMin < yLow)
{
yLow = *yMin;
}
}
QValueAxis *axisX = dynamic_cast<QValueAxis *>(m_chart->axisX());
axisX->setRange(xLow, xHigh);
axisX->setTitleText("");
if (!strXLabel.isEmpty())
{
axisX->setTitleText(strXLabel);
}
QValueAxis *axisY = dynamic_cast<QValueAxis *>(m_chart->axisY());
axisY->setRange(yLow, yHigh);
axisY->setTitleText("");
if (!strYLabel.isEmpty())
{
axisY->setTitleText(strYLabel);
}
//曲线和坐标绑定,否则会出现数据和坐标对应不上的情况
for (int i = 0; i < vecSplineSeries.size(); ++i)
{
vecSplineSeries[i]->attachAxis(axisX);
vecSplineSeries[i]->attachAxis(axisY);
}
m_chart->setTitle("");
if (!strViewLabel.isEmpty())
{
m_chart->setTitle(strViewLabel);
}
}
void GaiChartView::DrawCurve_ave(QVector<QVector<double> > &vvData, QString strViewLabel /*=""*/, QString strXLabel /*=""*/, QString strYLabel /*=""*/)
{
if (vvData.isEmpty())
{
return ;
}
m_chart->removeAllSeries();
QVector<QSplineSeries *> vecSplineSeries;
for (int i = 0; i < vvData.size(); i += 2)
{
QSplineSeries *ptrSplineSeries = new QSplineSeries();//曲线图
QVector<double> &vRowX = vvData[i];
QVector<double> &vRowY = vvData[i+1];
for (int j = 0; j < vRowX.size(); ++j)
{
*ptrSplineSeries << QPointF(vRowX[j], vRowY[j]);
}
vecSplineSeries.push_back(ptrSplineSeries);
}
for (int i = 0; i < vecSplineSeries.size(); ++i)
{
m_chart->addSeries(vecSplineSeries[i]);
connect(vecSplineSeries[i], SIGNAL(hovered(const QPointF&, bool)), this, SLOT(DisplaySeriesValue(const QPointF &, bool)));
}
double xLow = DBL_MAX;
double xHigh = DBL_MIN;
for (int i = 0; i < vvData.size(); i += 2)
{
auto xMax = std::max_element(std::begin(vvData[i]), std::end(vvData[i]));
auto xMin = std::min_element(std::begin(vvData[i]), std::end(vvData[i]));
if (*xMax > xHigh)
{
xHigh = *xMax;
}
if (*xMin < xLow)
{
xLow = *xMin;
}
}
double yLow = DBL_MAX;
double yHigh = DBL_MIN;
for (int i = 1; i < vvData.size(); i += 2)
{
auto yMax = std::max_element(std::begin(vvData[i]), std::end(vvData[i]));
auto yMin = std::min_element(std::begin(vvData[i]), std::end(vvData[i]));
if (*yMax > yHigh)
{
yHigh = *yMax;
}
if (*yMin < yLow)
{
yLow = *yMin;
}
}
QValueAxis *axisX = dynamic_cast<QValueAxis *>(m_chart->axisX());
axisX->setRange(xLow, xHigh);
axisX->setTitleText("");
if (!strXLabel.isEmpty())
{
axisX->setTitleText(strXLabel);
}
QValueAxis *axisY = dynamic_cast<QValueAxis *>(m_chart->axisY());
axisY->setRange(yLow, yHigh);
axisY->setTitleText("");
if (!strYLabel.isEmpty())
{
axisY->setTitleText(strYLabel);
}
//曲线和坐标绑定,否则会出现数据和坐标对应不上的情况
for (int i = 0; i < vecSplineSeries.size(); ++i)
{
vecSplineSeries[i]->attachAxis(axisX);
vecSplineSeries[i]->attachAxis(axisY);
}
m_chart->setTitle("");
if (!strViewLabel.isEmpty())
{
m_chart->setTitle(strViewLabel);
}
}
void GaiChartView::DrawBar(QVector<double> &vData)
{
if (vData.isEmpty())
{
return ;
}
m_chart->removeAllSeries();
QBarSet *pBarSet = new QBarSet("");
pBarSet->append(vData.toList());
QValueAxis *axisX = dynamic_cast<QValueAxis *>(m_chart->axisX());
axisX->setRange(0, vData.size());
auto yMax = std::max_element(std::begin(vData), std::end(vData));
auto yMin = std::min_element(std::begin(vData), std::end(vData));
double yLow = *yMin;
double yHigh = *yMax;
QValueAxis *axisY = dynamic_cast<QValueAxis *>(m_chart->axisY());
axisY->setRange(yLow, yHigh);
QBarSeries *pBarSeries = new QBarSeries();
pBarSeries->append(pBarSet);
m_chart->addSeries(pBarSeries);
pBarSeries->attachAxis(axisY);
}
void GaiChartView::DisplaySeriesValue(const QPointF &point, bool state)
{
if (state)
{
QToolTip::showText(QCursor::pos(),QString::asprintf("(%g,%g)", point.x(), point.y()),this,this->rect(),5000);
}
else
{
QToolTip::hideText();
}
}
网友评论