Newer
Older
PhaseMeasure / ChannelCharts.cpp
#include "ChannelCharts.h"
#include "ui_ChannelCharts.h"

#include <iostream>

extern QVector<QVector<QStringList>> phaseVector;
extern QVector<QVector<double>> channelAllan;

ChannelCharts::ChannelCharts(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChannelCharts)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose); // 窗口关闭后释放本对象
}

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

void ChannelCharts::initDataChart(int index)
{
    QCPGraph * graph = ui->dataPlot->addGraph();
    graph->setName("时差(ns)");
    graph->setAdaptiveSampling(true);    // 默认启用自适应采样(绘图)
    graph->setPen(QPen(QColor("#ff4757")));

    // 时间坐标轴  seconds since Epoch (January 1, 1970, 00:00 UTC).
    QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
    timeTicker->setDateTimeFormat("MM-dd hh:mm:ss");    // 刻度单位:分钟
    ui->dataPlot->xAxis->setTicker(timeTicker);
    ui->dataPlot->yAxis->setNumberFormat("gbc");      // Y 轴:g灵活的格式,b漂亮的指数形式,c乘号改成×
    ui->dataPlot->yAxis->setNumberPrecision(4);       // Y 轴:精度,有效数字的位数

    ui->dataPlot->xAxis->setLabel("时间");
    ui->dataPlot->yAxis->setLabel("时差测量值");
/*
    // 游标
    QCPItemTracer * tracer = new QCPItemTracer(ui->dataPlot);
    tracer->setInterpolating(false);              // 禁用插值,游标自动吸附到最近的点
    tracer->setPen(QPen(QColor("#FAA732"), 1, Qt::DashLine));  // 颜色、宽度、线型    // 标签色(warning)  #FAA732
    tracer->setStyle(QCPItemTracer::tsCircle);    // 样式:十字星、圆圈、方框等
    tracer->setSize(10.0);

    // 游标数据标签
    QCPItemText * tracerLabel = new QCPItemText(ui->dataPlot);
    tracerLabel->setLayer("overlay");
    tracerLabel->setPositionAlignment(Qt::AlignLeft | Qt::AlignTop);
    tracerLabel->setTextAlignment(Qt::AlignLeft);
    tracerLabel->setPadding(QMargins(5, 5, 5, 5));               // 边界宽度
    tracerLabel->position->setParentAnchor(tracer->position);    // 锚固在tracer位置处,实现自动跟随

    // 游标垂直线
    QCPItemStraightLine * tracerLineV = new QCPItemStraightLine(ui->dataPlot);
    tracerLineV->setLayer("overlay");
    tracerLineV->setPen(QPen(QColor("#FAA732"), 1, Qt::DashLine));    // 标签色(warning)  #FAA732
    tracerLineV->setClipToAxisRect(true);
    tracerLineV->point1->setCoords(0, 0);
    tracerLineV->point2->setCoords(0, 0);

    // 游标水平线
    QCPItemStraightLine * tracerLineH = new QCPItemStraightLine(ui->dataPlot);
    tracerLineH->setLayer("overlay");
    tracerLineH->setPen(QPen(QColor("#FAA732"), 1, Qt::DashLine));    // 标签色(warning)  #FAA732
    tracerLineH->setClipToAxisRect(true);
    tracerLineH->point1->setCoords(0, 0);
    tracerLineH->point2->setCoords(0, 0);
*/
    double minTm = phaseVector[index].first().at(0).toLongLong() / 1000;
    double maxTm = phaseVector[index].last().at(0).toLongLong() / 1000;
    double minValue = phaseVector[index].first().at(1).toDouble();
    double maxValue = phaseVector[index].first().at(1).toDouble();
    QVector<QCPGraphData> vecPoints;

    for (int i = 0; i < phaseVector[index].size(); i++)
    {
        // 新的数据点
        QCPGraphData onePoint;

        QDateTime dtime = QDateTime::fromSecsSinceEpoch(phaseVector[index].at(i).at(0).toLongLong() / 1000);    // 日期时间
        onePoint.key = QCPAxisTickerDateTime::dateTimeToKey(dtime);
        onePoint.value = phaseVector[index].at(i).at(1).toDouble();

        vecPoints.append(onePoint);

        if (phaseVector[index].at(i).at(1).toDouble() < minValue)
        {
            minValue = phaseVector[index][i].at(1).toDouble();
        }

        if (phaseVector[index].at(i).at(1).toDouble() > maxValue)
        {
            maxValue = phaseVector[index][i].at(1).toDouble();
        }
    }
    graph->data()->clear();
    graph->data()->set(vecPoints, true);
    ui->dataPlot->xAxis->setRange(minTm, maxTm);
    ui->dataPlot->yAxis->setRange(minValue, maxValue);

    ui->dataPlot->legend->setVisible(false);
//    ui->dataPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}

void ChannelCharts::initAllenChart(int index)
{
    /*
    QLinearGradient gradient(0, 0, 0, 400);
    gradient.setColorAt(0, QColor(90, 90, 90));
    gradient.setColorAt(0.38, QColor(105, 105, 105));
    gradient.setColorAt(1, QColor(70, 70, 70));
    ui->allenPlot->setBackground(QBrush(gradient));

    // create empty bar chart objects:
    QCPBars * allen = new QCPBars(ui->allenPlot->xAxis, ui->allenPlot->yAxis);
    allen->setAntialiased(false); // gives more crisp, pixel aligned bar borders

    allen->setStackingGap(1);
    // set names and colors:
    allen->setName("allen");
    allen->setPen(QPen(QColor(0, 168, 140).lighter(130)));
    allen->setBrush(QColor(0, 168, 140));
    // stack bars on top of each other:

    // prepare x axis with country labels:
    QVector<double> ticks;
    QVector<QString> labels;
    ticks << 1 << 2 << 3 << 4 << 5;
    labels << "1" << "10" << "100" << "1000" << "10000";
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTicks(ticks, labels);
    ui->allenPlot->xAxis->setTicker(textTicker);
    ui->allenPlot->xAxis->setSubTicks(false);
    ui->allenPlot->xAxis->setTickLength(0, 4);
    ui->allenPlot->xAxis->setRange(0, 6);
    ui->allenPlot->xAxis->setBasePen(QPen(Qt::white));
    ui->allenPlot->xAxis->setTickPen(QPen(Qt::white));
    ui->allenPlot->xAxis->grid()->setVisible(true);
    ui->allenPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
    ui->allenPlot->xAxis->setTickLabelColor(Qt::white);
    ui->allenPlot->xAxis->setLabelColor(Qt::white);
    ui->allenPlot->xAxis->setLabel("平均时间(秒)");

    // prepare y axis:
    ui->allenPlot->yAxis->setRange(-1e-15, channelAllan[index].isEmpty() ? 0.1 : channelAllan[index][0]);
    ui->allenPlot->yAxis->setPadding(5); // a bit more space to the left border
    ui->allenPlot->yAxis->setLabel("阿伦方差");
    ui->allenPlot->yAxis->setBasePen(QPen(Qt::white));
    ui->allenPlot->yAxis->setTickPen(QPen(Qt::white));
    ui->allenPlot->yAxis->setSubTickPen(QPen(Qt::white));
    ui->allenPlot->yAxis->grid()->setSubGridVisible(true);
    ui->allenPlot->yAxis->setTickLabelColor(Qt::white);
    ui->allenPlot->yAxis->setLabelColor(Qt::white);
    ui->allenPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
    ui->allenPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));

    // Add data:
    QVector<double> allenData;
    if (channelAllan[index].isEmpty() == true) {
        allenData  << 0.0  << 0.0 << 0.0 << 0.0 << 0.0;
    } else
    {
        allenData  << channelAllan[index][0]
                << channelAllan[index][1]
                << channelAllan[index][2]
                << channelAllan[index][3]
                << channelAllan[index][4];
    }

    allen->setData(ticks, allenData);

    // setup legend:
    ui->allenPlot->legend->setVisible(false);
//    ui->allenPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
//    ui->allenPlot->legend->setBrush(QColor(255, 255, 255, 100));
//    ui->allenPlot->legend->setBorderPen(Qt::NoPen);
//    QFont legendFont = font();
//    legendFont.setPointSize(10);
//    ui->allenPlot->legend->setFont(legendFont);
    ui->allenPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    */

    QCPGraph * graph = ui->allenPlot->addGraph();
    graph->setName("时差(ns)");
    graph->setAdaptiveSampling(true);    // 默认启用自适应采样(绘图)
    QPen pen;
    pen.setColor(QColor(0, 168, 140));
    pen.setWidth(3);//线宽
    graph->setPen(pen);

    QLinearGradient gradient(0, 0, 0, 400);
    gradient.setColorAt(0, QColor(90, 90, 90));
    gradient.setColorAt(0.38, QColor(105, 105, 105));
    gradient.setColorAt(1, QColor(70, 70, 70));
    ui->allenPlot->setBackground(QBrush(gradient));

    ui->allenPlot->xAxis->setBasePen(QPen(Qt::white));
    ui->allenPlot->xAxis->setTickPen(QPen(Qt::white));
    ui->allenPlot->xAxis->setPadding(5);
    ui->allenPlot->xAxis->grid()->setVisible(true);
    ui->allenPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));
    ui->allenPlot->xAxis->setTickLabelColor(Qt::white);
    ui->allenPlot->xAxis->setLabelColor(Qt::white);
    ui->allenPlot->xAxis->setLabel("平均时间(秒)");

    ui->allenPlot->yAxis->setPadding(5); // a bit more space to the left border
    ui->allenPlot->yAxis->setLabel("阿伦方差");
    ui->allenPlot->yAxis->setBasePen(QPen(Qt::white));
    ui->allenPlot->yAxis->setTickPen(QPen(Qt::white));
    ui->allenPlot->yAxis->setSubTickPen(QPen(Qt::white));
    ui->allenPlot->yAxis->grid()->setSubGridVisible(true);
    ui->allenPlot->yAxis->setTickLabelColor(Qt::white);
    ui->allenPlot->yAxis->setLabelColor(Qt::white);
    ui->allenPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
    ui->allenPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));


    // 时间坐标轴  seconds since Epoch (January 1, 1970, 00:00 UTC).
    QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
    ui->allenPlot->xAxis->setTicker(logTicker);
    ui->allenPlot->yAxis->setNumberFormat("gbc");      // Y 轴:g灵活的格式,b漂亮的指数形式,c乘号改成×
    ui->allenPlot->yAxis->setNumberPrecision(4);       // Y 轴:精度,有效数字的位数
    ui->allenPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);

    channelAllan[index] = {2.48e-13, 1.11e-13, 2.6e-14, 5.26e-15, 1.28e-15};

    double minValue = channelAllan[index].first();
    double maxValue = channelAllan[index].first();
    QVector<QCPGraphData> vecPoints;

    for (int i = 0; i < channelAllan[index].size(); i++)
    {
        // 新的数据点
        QCPGraphData onePoint;

        onePoint.key = pow(10, i);
        onePoint.value = channelAllan[index].at(i);
        vecPoints.append(onePoint);

        if (channelAllan[index].at(i) < minValue)
        {
            minValue = channelAllan[index].at(i);
        }

        if (channelAllan[index].at(i) > maxValue)
        {
            maxValue = channelAllan[index].at(i);
        }
    }
    graph->data()->clear();
    graph->data()->set(vecPoints, true);
    ui->allenPlot->xAxis->setRange(1, 11000);
    ui->allenPlot->yAxis->setRange(minValue, maxValue);

    ui->allenPlot->legend->setVisible(false);
    ui->allenPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}