QtChart x坐标轴是时间坐标

#pragma execution_character_set("utf-8")

#pragma once

#include 
#include 
#include "ui_QtChartTimeAxis.h"
#include 
#include 
class QtChartTimeAxis : public QWidget
{
	Q_OBJECT

public:
	QtChartTimeAxis(QWidget *parent = Q_NULLPTR);

public:
    void addPoint(QDateTime dateTime, int y_value);

private slots:
	void on_startButton_clicked();
	void oneSecondAction();

private:
	Ui::QtChartTimeAxisClass ui;
	void initLineChart();
	void setLineChartMargins(QChart* chart, int margin);
	void addOnePointToChart(QLineSeries* targetSeries, int secondCount, int y_value);
	static void setAxisFormat(QValueAxis* axis, QXYSeries* lineSeries, QString titleString, QColor color);
	static QString getFileContent(QString strFile);
	
    QLineSeries* m_LeftSeries = NULL;
    QDateTimeAxis* m_AxisX_Time = NULL;
	QValueAxis* m_AxisY_Left;
    QChart* m_LineChart = NULL;
    QTimer* m_DrawTimer = NULL;
    const QDate m_BaseDate = QDate::currentDate();
	const QTime m_BaseTime = QTime(0, 0, 0);
	QStringList m_DataList;
    int second_count = 0;
};
#include "QtChartTimeAxis.h"

QtChartTimeAxis::QtChartTimeAxis(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	//初始化折线图的设置
	initLineChart();
	//初始化一个定时器
	m_DrawTimer = new QTimer(this);
	connect(m_DrawTimer, &QTimer::timeout, this, &QtChartTimeAxis::oneSecondAction);
}


void QtChartTimeAxis::addPoint(QDateTime dateTime, int y_value)
{
    if (m_LeftSeries != NULL)
    {
//        QTime time = dateTime.time();
//        if (!time.setHMS(time.hour(), time.minute(), time.second(), 0))
//        {
//            Q_ASSERT(0);
//        }

//        dateTime.setTime(time);
//        qDebug() << dateTime;
        m_LeftSeries->append(dateTime.toMSecsSinceEpoch(), y_value);
    }
}

/**
 * 按键的响应槽函数
 */
void QtChartTimeAxis::on_startButton_clicked()
{
	second_count = 0;
	//从文本中读取数据,用于绘制
    QString txt = getFileContent("data.csv");
	m_DataList = txt.split(",");
	m_DrawTimer->start(100);//1秒执行一次

}

void QtChartTimeAxis::oneSecondAction()
{
	second_count++;
	//所有数据绘制完毕,结束定时器
	if (second_count > m_DataList.count() - 1)
	{
		m_DrawTimer->stop();
		return;
	}

	addOnePointToChart(m_LeftSeries, second_count, m_DataList[second_count].toInt());
}

//初始化折线图
void QtChartTimeAxis::initLineChart() {
	m_AxisX_Time = new QDateTimeAxis();
	m_AxisY_Left = new QValueAxis();

	m_LeftSeries = new QLineSeries();
	m_LineChart = new QChart();
	//添加曲线到chart中
	m_LineChart->addSeries(m_LeftSeries);

    QDateTime temp_StartTime(m_BaseDate, QTime(0, 0, 0));
    m_AxisX_Time->setTickCount(25); // 网格数
    m_AxisX_Time->setRange(temp_StartTime, temp_StartTime.addDays(1));//时间显示范围

    //坐标轴显示方式: 时:分
    m_AxisX_Time->setFormat("hh:mm");

	//设置坐标轴显示范围
    m_AxisY_Left->setRange(0, 30);
    setAxisFormat(m_AxisY_Left, m_LeftSeries, "left", QColor(Qt::green));


	//把坐标轴添加到chart中,
	//addAxis函数的第二个参数是设置坐标轴的位置,
	//只有四个选项,下方:Qt::AlignBottom,左边:Qt::AlignLeft,右边:Qt::AlignRight,上方:Qt::AlignTop
	m_LineChart->addAxis(m_AxisX_Time, Qt::AlignBottom);
	m_LineChart->addAxis(m_AxisY_Left, Qt::AlignLeft);

	//把曲线关联到坐标轴
	m_LeftSeries->attachAxis(m_AxisX_Time);
	m_LeftSeries->attachAxis(m_AxisY_Left);

	setLineChartMargins(m_LineChart, -1);//设置折线图边距
	m_LineChart->legend()->hide();//将图例关闭
	ui.graphicsView->setChart(m_LineChart);
}

/**
 * 用于设置折线图边距
 */
void QtChartTimeAxis::setLineChartMargins(QChart *chart, int margin) {
	QMargins m_Margin;
	m_Margin.setLeft(margin);
	m_Margin.setBottom(margin);
	m_Margin.setRight(margin);
	m_Margin.setTop(margin);
	chart->setMargins(m_Margin);
}

/**
 * 添添加一个点
 */
void QtChartTimeAxis::addOnePointToChart(QLineSeries *targetSeries, int secondCount, int y_value) {
    QDateTime temp_AddTimePos(m_BaseDate, m_BaseTime.addSecs(secondCount * 60)); // 每分钟增加一个点
    qDebug() << temp_AddTimePos;
    targetSeries->append(temp_AddTimePos.toMSecsSinceEpoch(), y_value);
}

/**
 * 设置坐标轴样式
 */
void QtChartTimeAxis::setAxisFormat(QValueAxis *axis, QXYSeries *lineSeries, QString titleString, QColor color) {
//	axis->setTitleText(titleString);
//	axis->setTitleFont(QFont("黑体", 8, QFont::Medium, false));
//	axis->setTitleBrush(color);
//	axis->setLinePenColor(color);
	lineSeries->setPen(QPen(color, 0.6, Qt::SolidLine, Qt::SquareCap, Qt::RoundJoin));
}

//读取文件内容
QString QtChartTimeAxis::getFileContent(QString strFile)
{
	QFile file(strFile);
	QString txt;
	if (!file.open(QIODevice::ReadWrite))
	{
		txt = "";
	}
	else
	{
		txt = file.readAll();
	}
	file.close();
	return txt;
}

 

#include "dialog.h"
#include "ui_dialog.h"
#include 



Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    timeChart = new QtChartTimeAxis();
    ui->horizontalLayout_2->addWidget(timeChart);


    m_addPointTimer = new QTimer(this);
    connect(m_addPointTimer, &QTimer::timeout, this, &Dialog::onAddPointTimer);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    m_addPointTimer->start(500);
    dateTime = QDateTime::currentDateTime();
}


void Dialog::onAddPointTimer()
{

    dateTime = dateTime.addSecs(60 * 1);
    qsrand(QTime::currentTime().msec());
    timeChart->addPoint(dateTime, 15 + qrand() % 10);
}

 

你可能感兴趣的:(QT,C++)