QCustomPlot 屬性介紹
圖表曲線作為一種直覺分析的工具,在軟體開發中應用的非常廣泛,QCustomPlot是Qt的第三方類庫,下面整理下QCustomPlot常用的屬性。
日期 | 作者 | 版本 |
---|---|---|
2021年7月5日 | Mister H | V1.0 |
文章目錄
- QCustomPlot 屬性介紹
-
-
- 1. 建立配置繪圖表
- 2. 設定線的樣式
- 3. 設定畫布曲線的顔色、抗鋸齒、名稱
- 4. 添加、清空、重新整理資料
- 5. 設定散點樣式
- 6. 設定 y 軸屬性(x軸同理)
- 7. 設定背景顔色、設定矩形軸框内的背景顔色
- 8. 設定刻度标簽類型(預設是QCPAxisTickerFixed坐标)
-
- 1. QCPAxisTickerDateTime 日期時間坐标軸
- 2. QCPAxisTickerText 文本刻度标簽
- 3. QCPAxisTickerLog log對數刻度标簽
-
1. 建立配置繪圖表
2. 設定線的樣式
graph1->setLineStyle(QCPGraph::lsNone); //無線
graph1->setLineStyle(QCPGraph::lsLine); //有線
3. 設定畫布曲線的顔色、抗鋸齒、名稱
QPen pen;
pen.setWidth(2);//曲線的粗細
pen.setColor(Qt::red);
graph1->setPen(pen);//曲線顔色設定
graph1->setAntialiasedFill(true);//設定抗鋸齒
graph1->setName("名稱");//設定畫布曲線名稱
4. 添加、清空、重新整理資料
ui.customPlot->graph(0)->addData(key,value);//可以輸入單點、也可以多點資料同時傳入
ui.customPlot->graph(0)->data().data()->clear();//清空資料
ui.customPlot->replot();//重新整理
5. 設定散點樣式
graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle,
QPen(Color, LineWidth),
QBrush(Color), DotWidth));//設定圓點的樣式,還有其他樣式選擇,如正方形QCPScatterStyle::ssSquare。
6. 設定 y 軸屬性(x軸同理)
ui->customPlot->xAxis->setLabel("x"); //x軸标簽
ui->customPlot->yAxis->setLabel("y"); //y軸标簽
ui->customPlot->yAxis->setLabelColor(Color);//設定y軸标簽顔色
ui->customPlot->yAxis->setLabelFont("微軟雅黑"); //y軸标題 字型
ui->customPlot->yAxis->setVisible(true); //隐藏y軸
ui->customPlot->yAxis->setRange(0, 2); //設定y軸坐标範圍
ui->customPlot->yAxis->setAutoTickCount(8);//設定y軸刻度線個數
ui->customPlot->yAxis->->setTickLength(5, 0);//設定x軸刻度線的長度
ui->customPlot->yAxis->setSubTickLength(2, 0);//設定x軸子刻度線長度
ui->customPlot->yAxis->setTickPen(QPen(Color,Width));//設定y軸刻度顔色和大小
ui->customPlot->yAxis->setSubTickPen(QPen(Color, Width));//設定y軸子刻度線的顔色和大小
ui->customPlot->xAxis->setAutoTicks(true);//設定x軸自動刻度線
7. 設定背景顔色、設定矩形軸框内的背景顔色
ui->customPlot->setBackground(Color);//設定背景顔色
ui->customPlot->axisRect()->setBackground(Color);//設定矩形軸框内的背景顔色
8. 設定刻度标簽類型(預設是QCPAxisTickerFixed坐标)
1. QCPAxisTickerDateTime 日期時間坐标軸
customPlot->setInteraction(QCP::iRangeDrag, true);
customPlot->setInteraction(QCP::iRangeZoom, true);
QDateTime dateTime = QDateTime::currentDateTime();
double now = dateTime.toTime_t();//目前時間轉化為秒
//生成時間刻度對象
QSharedPointer<QCPAxisTickerDateTime> dateTimeTicker(new QCPAxisTickerDateTime);
customPlot->xAxis->setTicker(dateTimeTicker);
//dateTimeTicker->setDateTimeSpec(Qt::UTC);//設施世界時間,即不加上時區的時間
dateTimeTicker->setTickCount(12);
dateTimeTicker->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount);
customPlot->xAxis->setSubTicks(false);
customPlot->xAxis->setRange(now, now+3600*24);//x軸範圍,從目前時間起往後推24小時
2. QCPAxisTickerText 文本刻度标簽
//畫柱狀圖
QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis);
regen->setAntialiased(false); // 提供更多的清晰度,像素對齊的條形邊框
regen->setStackingGap(1);
regen->setName("Regenerative");
regen->setPen(QPen(QColor(0, 168, 140).lighter(130)));
regen->setBrush(QColor(0, 168, 140));
// regen->moveAbove(nuclear);
QVector<double> ticks;
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;
labels << "USA" << "Japan" << "Germany" << "France" << "UK" << "Italy" << "Canada";
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, labels);
customPlot->xAxis->setTicker(textTicker);
customPlot->xAxis->setTickLabelRotation(60);
customPlot->xAxis->setSubTicks(false);
customPlot->xAxis->setTickLength(0, 4);
customPlot->xAxis->setRange(0, 8);
customPlot->xAxis->setTickPen(QPen(Qt::white));
// 準備y軸:
customPlot->yAxis->setRange(0, 12.1);
customPlot->yAxis->setPadding(5); // 左邊框多留一點空間
customPlot->yAxis->setLabel("Power Consumption in\nKilowatts per Capita (2007)");
//customPlot->yAxis->setBasePen(QPen(Qt::white));
customPlot->yAxis->setTickPen(QPen(Qt::white));
customPlot->yAxis->setSubTickPen(QPen(Qt::white));
// 添加資料:
QVector<double> regenData;
regenData << 0.06*10.5 << 0.05*5.5 << 0.04*5.5 << 0.06*5.8 << 0.02*5.2 << 0.07*4.2 << 0.25*11.2;
regen->setData(ticks, regenData);
3. QCPAxisTickerLog log對數刻度标簽
QSharedPointer<QCPAxisTickerLog> logTicker(new QCPAxisTickerLog);
customPlot->xAxis->setTicker(logTicker);
customPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
customPlot->xAxis->setRange(0,250);
logTicker->setLogBase(10);
QVector<double> x2(250), y2(250);
for (int i=0; i<250; ++i)
{
x2[i] = i;
y2[i] = log(i);
}
customPlot->addGraph(customPlot->xAxis, customPlot->yAxis);
customPlot->graph(0)->setData(x2, y2);