上次在寫大屏數據可視化電子看板系統時候,提到過改造QCustomPlot來實現柱狀分組圖、橫向柱狀圖、橫向分組圖、鼠標懸停提示等。此次單獨列出來描述,有不少人疑問爲啥不用QChart,或者echart等形式,其實這兩種方式我都嘗試過,好比Qt5.7之後新增的QChart模塊,曲線這塊,支持數據量很小,並且用法極其不適應,很是彆扭,尤爲是10W以上數據量的支持,簡直是渣渣,優勢也是有不少的,好比動畫效果,我看過他的完整源碼,動畫這塊處理的很是好,連座標軸均可以有動畫效果,並且支持不少種效果,並且內置了不少套theme皮膚,省去了不少渣渣審美的程序員本身來配色,這個卻是挺方便的。而對於echart,必須依賴瀏覽器控件,資源佔用比較高,後面決定採用改造QCustomPlot來實現用戶須要的各類圖表效果。
在整個改造的過程當中,所有封裝成易用的函數,傳入參數便可,同時還支持全局樣式更改,支持樣式表控制總體顏色更改,考慮了不少細節,好比彈出懸停信息的位置等,都自動計算顯示在最佳最合理位置。考慮到不少人用的QCustomPlot1.0,特地還作了QCustomPlot1.0和2.0的徹底兼容。linux
void CustomPlot::setDataLine(int index, const QString &name, const QVector<double> &key, const QVector<double> &value) { if (customPlot->graphCount() > index) { customPlot->graph(index)->setName(name); customPlot->graph(index)->setData(key, value); customPlot->xAxis->setRange(-offsetX, key.count() + offsetX, Qt::AlignLeft); //超過3條線條顏色設置顏色集合的顏色 if (index >= 3) { setColor(index, colors.at(index)); } else { setColor(0, colors.at(0)); setColor(1, colors.at(1)); setColor(2, colors.at(2)); } } } void CustomPlot::setDataBarv(const QStringList &rowNames, const QStringList &columnNames, const QList<QVector<double> > &values, const QColor &borderColor, int valuePosition, int valuePrecision, const QColor &valueColor, bool checkData) { //只有1列的才能設置 if (columnNames.count() != 1) { return; } //能夠直接用堆積圖,由於只有一列的柱狀圖不會造成堆積 setDataBars(rowNames, columnNames, values, borderColor, valuePosition, valuePrecision, valueColor, checkData); } void CustomPlot::setDataBarvs(const QStringList &rowNames, const QStringList &columnNames, const QList<QVector<double> > &values, const QColor &borderColor, int valuePosition, int valuePrecision, const QColor &valueColor, bool checkData) { //過濾個數不一致數據,防止索引越界 int rowCount = rowNames.count(); int columnCount = columnNames.count(); int valueCount = values.count(); if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) { return; } //設置網格線不顯示,會更好看 customPlot->xAxis->grid()->setVisible(false); //customPlot->yAxis->grid()->setVisible(false); //設置橫座標文字描述 QVector<double> ticks; QVector<QString> labels; int count = rowCount * columnCount; for (int i = 0; i < rowCount; i++) { ticks << 1.5 + (i * columnCount); labels << rowNames.at(i); } setLabX(ticks, labels); customPlot->xAxis->setRange(0, count + 1); for (int i = 0; i < columnCount; i++) { //一樣也要先過濾個數是否符合要求 QVector<double> value = values.at(i); if (rowCount != value.count()) { continue; } //建立柱狀圖 CustomBarv *bar = new CustomBarv(customPlot->xAxis, customPlot->yAxis); bar->setCheckData(checkData); //設置寬度比例 bar->setWidth(0.9); //設置顯示值的位置 0-不繪製 1-頂部上面 2-頂部居中 3-中間居中 4-底部居中 bar->setValuePostion(valuePosition); bar->setValuePrecision(valuePrecision); bar->setValueColor(valueColor); //設置名稱 bar->setName(columnNames.at(i)); //設置顏色,取顏色集合 QColor color = QColor(51, 204, 255); if (i < colors.count()) { color = colors.at(i); } //邊緣高亮,若是傳入了邊框顏色則取邊框顏色 bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor)); bar->setBrush(color); //這個算法很巧妙,想了好久 QVector<double> ticks; double offset = i * 0.9; for (int j = 0; j < rowCount; j++) { ticks << 1.0 + (j * columnCount) + offset; } //設置數據 bar->setData(ticks, value); } } void CustomPlot::setDataBarh(const QStringList &rowNames, const QStringList &columnNames, const QList<QVector<double> > &values, const QColor &borderColor, int valuePosition, int valuePrecision, const QColor &valueColor, bool checkData) { //只有1列的才能設置 if (columnNames.count() != 1) { return; } //過濾個數不一致數據,防止索引越界 int rowCount = rowNames.count(); int columnCount = columnNames.count(); int valueCount = values.count(); if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) { return; } //設置網格線不顯示,會更好看 customPlot->xAxis->grid()->setVisible(false); customPlot->yAxis->grid()->setVisible(false); customPlot->yAxis->setTickLength(0, 0); //設置橫座標文字描述 QVector<double> ticks; QVector<QString> labels; int count = rowCount * columnCount; double padding = 1; for (int i = 0; i < rowCount; i++) { ticks << padding + (i * columnCount); labels << rowNames.at(i); } setLabY(ticks, labels); customPlot->yAxis->setRange(0, count + 1); //先計算出每一個柱子佔用的高度 double barHeight = 0.7; for (int i = 0; i < columnCount; i++) { //一樣也要先過濾個數是否符合要求 QVector<double> value = values.at(i); if (rowCount != value.count()) { continue; } //先繪製系列1的數據,再繪製系列2,依次類推 for (int j = 0; j < rowCount; j++) { //建立橫向柱狀圖 double y = (0.67 + (j * columnCount)); CustomBarh *bar = new CustomBarh(customPlot); bar->setCheckData(checkData); bar->setRect(QPointF(0, y), QPointF(value.at(j), y + barHeight)); bar->setValue(value.at(j)); //設置顯示值的位置 0-不繪製 1-頂部上面 2-頂部居中 3-中間居中 4-底部居中 bar->setValuePostion(valuePosition); bar->setValuePrecision(valuePrecision); bar->setValueColor(valueColor); //設置顏色,取顏色集合 QColor color = QColor(51, 204, 255); if (i < colors.count()) { color = colors.at(i); } //邊緣高亮,若是傳入了邊框顏色則取邊框顏色 bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor)); bar->setBrush(color); } } } void CustomPlot::setDataBarhs(const QStringList &rowNames, const QStringList &columnNames, const QList<QVector<double> > &values, const QColor &borderColor, int valuePosition, int valuePrecision, const QColor &valueColor, bool checkData) { //過濾個數不一致數據,防止索引越界 int rowCount = rowNames.count(); int columnCount = columnNames.count(); int valueCount = values.count(); if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) { return; } //設置網格線不顯示,會更好看 customPlot->xAxis->grid()->setVisible(false); customPlot->yAxis->grid()->setVisible(false); customPlot->yAxis->setTickLength(0, 0); customPlot->xAxis->setVisible(false); //設置橫座標文字描述 QVector<double> ticks; QVector<QString> labels; int count = rowCount * columnCount; //這個算法想了好久,很牛逼 double padding = 1.5 + (columnCount - 2) * 0.4; for (int i = 0; i < rowCount; i++) { ticks << padding + (i * columnCount); labels << rowNames.at(i); } setLabY(ticks, labels); customPlot->yAxis->setRange(0, count + 1); //先計算出每一個柱子佔用的高度 double barHeight = 0.8; for (int i = 0; i < columnCount; i++) { //一樣也要先過濾個數是否符合要求 QVector<double> value = values.at(i); if (rowCount != value.count()) { continue; } //先繪製系列1的數據,再繪製系列2,依次類推 for (int j = 0; j < rowCount; j++) { //建立橫向柱狀圖 double y = (0.7 + i * barHeight + (j * columnCount)); CustomBarh *bar = new CustomBarh(customPlot); bar->setCheckData(checkData); bar->setRect(QPointF(0, y), QPointF(value.at(j), y + barHeight)); bar->setValue(value.at(j)); //設置顯示值的位置 0-不繪製 1-頂部上面 2-頂部居中 3-中間居中 4-底部居中 bar->setValuePostion(valuePosition); bar->setValuePrecision(valuePrecision); bar->setValueColor(valueColor); //設置顏色,取顏色集合 QColor color = QColor(51, 204, 255); if (j < colors.count()) { color = colors.at(j); } //邊緣高亮,若是傳入了邊框顏色則取邊框顏色 bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor)); bar->setBrush(color); } } } void CustomPlot::setDataBars(const QStringList &rowNames, const QStringList &columnNames, const QList<QVector<double> > &values, const QColor &borderColor, int valuePosition, int valuePrecision, const QColor &valueColor, bool checkData) { //過濾個數不一致數據,防止索引越界 int rowCount = rowNames.count(); int columnCount = columnNames.count(); int valueCount = values.count(); if (columnCount == 0 || valueCount == 0 || columnCount != valueCount) { return; } //設置網格線不顯示,會更好看 customPlot->xAxis->grid()->setVisible(false); //customPlot->yAxis->grid()->setVisible(false); //先清空原有柱狀圖 bars.clear(); //設置橫座標文字描述 QVector<double> ticks; QVector<QString> labels; for (int i = 0; i < rowCount; i++) { ticks << i + 1; labels << rowNames.at(i); } setLabX(ticks, labels); customPlot->xAxis->setRange(0, rowCount + 1); for (int i = 0; i < columnCount; i++) { //一樣也要先過濾個數是否符合要求 QVector<double> value = values.at(i); if (rowCount != value.count()) { continue; } //建立柱狀堆積圖 CustomBarv *bar = new CustomBarv(customPlot->xAxis, customPlot->yAxis); bar->setCheckData(checkData); //設置寬度比例 bar->setWidth(0.6); //設置顯示值的位置 0-不繪製 1-頂部上面 2-頂部居中 3-中間居中 4-底部居中 bar->setValuePostion(valuePosition); bar->setValuePrecision(valuePrecision); bar->setValueColor(valueColor); #ifndef old //設置堆積間隙 if (borderColor != Qt::transparent) { bar->setStackingGap(1); } #endif //設置名稱 bar->setName(columnNames.at(i)); //設置顏色,取顏色集合 QColor color = QColor(51, 204, 255); if (i < colors.count()) { color = colors.at(i); } //邊緣高亮,若是傳入了邊框顏色則取邊框顏色 if (columnCount > 1 && borderColor == Qt::transparent) { bar->setPen(Qt::NoPen); } else { bar->setPen(QPen(borderColor == Qt::transparent ? color.light(150) : borderColor)); } bar->setBrush(color); //設置堆積層疊順序,後面那個移到前一個上面 bars << bar; if (i > 0) { bar->moveAbove(bars.at(i - 1)); } //設置數據 bar->setData(ticks, value); } }