QCharts - 4.QValueAxis类详解

QValueAxis是数值型坐标轴,与QLineSeries正好配合使用。

一.常用方法

1.坐标轴相关的

//设置可见
void setVisible()

//设置最小值
void setMin()

//设置最大值
void setMax()

//设置范围
void setRange()

//设置轴标题
void setTitleText()

//设置轴标题可见
void setTitleVisible()

//设置轴标签格式
void setLabelFormat()

//设置标签角度
void setLabelsAngle()

//设置标签可见
void setLabelsVisible()

//设置刻度线数量
void setTickCount()

//设置刻度线可见
void setLineVisible()

2.主网格相关

//设置网格线颜色
void setGridLineColor()

//设置可见
void setGridLineVisible()

3.子网格线相关

//设置子刻度个数
void setMinorTickCount();

//设置颜色
void setMinorGridLineColor()

//设置可见
void setMinorGridLineVisible()

//设置pen
void setMinorGridLinePen()

二.示例

QCharts - 4.QValueAxis类详解_第1张图片

源码

#include "myvalueaxis.h"
#include "ui_myvalueaxis.h"

QValueAxis *curAxis;

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

    m_chartView = new QChartView(this);

    m_chart = new QChart();

    m_chart->setTitle("简单的曲线");
    m_chartView->setChart(m_chart);

    //this->setCentralWidget(chartView);
    ui->showLayout->addWidget(m_chartView);

    //创建QLineSeries
    QLineSeries *series0 = new QLineSeries();
    QLineSeries *series1 = new QLineSeries();

    series0->setName("sin曲线");
    series1->setName("Cos曲线");

    //图表添加曲线
    m_chart->addSeries(series0);
    m_chart->addSeries(series1);

    qreal t=0,y1,y2,intv=0.1;
    int cnt = 100;

    //设置内容
    for(int i=0;iappend(t,y1);
        y2 = qSin(t+20);
        series1->append(t,y2);
        t+=intv;
    }

    //创建坐标轴
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0,10);
    axisX->setTitleText("time(secs)");

    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(-2,2);
    axisY->setTitleText("value");


    //图表添加坐标轴
    m_chart->setAxisX(axisX,series0);
    m_chart->setAxisY(axisY,series0);

    m_chart->setAxisX(axisX,series1);
    m_chart->setAxisY(axisY,series1);

    ui->rbX->setChecked(true);
}

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

void MyValueAxis::on_rbX_toggled(bool checked)
{
    if(checked)
    {
        curAxis = (QValueAxis*)m_chartView->chart()->axisX();
        ui->dSpMin->setValue(curAxis->min());
        ui->dsPMax->setValue(curAxis->max());
        ui->cbVisible->setVisible(curAxis->isTitleVisible());
        ui->lineEdit->setText(curAxis->labelFormat());
        ui->cbxLabelVisible->setChecked(curAxis->labelsVisible());
        ui->cbGridVisible->setChecked(curAxis->isGridLineVisible());
        ui->cbTicksVisible->setChecked(curAxis->isLineVisible());
        ui->cbSubVisible->setChecked(curAxis->isMinorGridLineVisible());
        ui->spTicks->setValue(curAxis->tickCount());
    }
}

void MyValueAxis::on_rbY_toggled(bool checked)
{
    if(checked)
    {
        curAxis = (QValueAxis*)m_chartView->chart()->axisY();
        ui->dSpMin->setValue(curAxis->min());
        ui->dsPMax->setValue(curAxis->max());
        ui->cbVisible->setVisible(curAxis->isTitleVisible());
        ui->lineEdit->setText(curAxis->labelFormat());
        ui->cbxLabelVisible->setChecked(curAxis->labelsVisible());
        ui->cbGridVisible->setChecked(curAxis->isGridLineVisible());
        ui->cbTicksVisible->setChecked(curAxis->isLineVisible());
        ui->cbSubVisible->setChecked(curAxis->isMinorGridLineVisible());
        ui->spTicks->setValue(curAxis->tickCount());
    }
}

void MyValueAxis::on_dSpMin_valueChanged(double arg1)
{
    curAxis->setMin(arg1);
}

void MyValueAxis::on_dsPMax_valueChanged(double arg1)
{
    curAxis->setMax(arg1);
}

void MyValueAxis::on_cbVisible_toggled(bool checked)
{
    curAxis->setTitleVisible(checked);
}

void MyValueAxis::on_btnLabelSet_clicked()
{
    curAxis->setLabelFormat(ui->lineEdit->text());
}

void MyValueAxis::on_cbxLabelVisible_toggled(bool checked)
{
    curAxis->setLabelsVisible(checked);
}

void MyValueAxis::on_cbGridVisible_toggled(bool checked)
{
    curAxis->setGridLineVisible(checked);
}

void MyValueAxis::on_btnGridColor_clicked()
{
    curAxis->setGridLineColor(QColor(Qt::yellow));
}

void MyValueAxis::on_btnGridPen_clicked()
{
    QPen pen;
    curAxis->setGridLinePen(pen);
}

void MyValueAxis::on_spTicks_valueChanged(int arg1)
{
    curAxis->setTickCount(arg1);
}

void MyValueAxis::on_cbTicksVisible_toggled(bool checked)
{
    curAxis->setLineVisible(checked);
}

void MyValueAxis::on_btnLineColor_clicked()
{
    curAxis->setLinePenColor(QColor(Qt::yellow));
}

void MyValueAxis::on_btnLienPen_clicked()
{
    QPen pen;
    curAxis->setLinePen(pen);
}

void MyValueAxis::on_spSubTicks_valueChanged(int arg1)
{
    curAxis->setMinorTickCount(arg1);
}

void MyValueAxis::on_cbSubVisible_toggled(bool checked)
{
    curAxis->setMinorGridLineVisible(checked);
}

void MyValueAxis::on_btnSubColor_clicked()
{
    curAxis->setMinorGridLineColor(QColor(Qt::yellow));
}

void MyValueAxis::on_btnSubPen_clicked()
{
    QPen pen;
    curAxis->setMinorGridLinePen(pen);
}

三.源码包

QValueAxis详解-QT文档类资源-CSDN下载

你可能感兴趣的:(QCharts,QValueAxis,qt,qchart,坐标轴)