工作已有一段時間,終於有時間把接觸到的各種問題及解決方案記錄在博客上了。
廢話不多說,開始正文。
由於項目(Qt-5.8.0 + MSVC-2013_x64, Win7 平台)涉及一個圖表模塊,這個模塊需要根據數據生成簡單的柱狀圖。要求代表數據系列的柱子外側上方顯示數據系列的標簽,表示當前柱子的數值。
自然而然地,小組成員想到了利用Qt Charts模塊完成需求。然而同事發現,以下代碼無法將數據系列標簽正確地顯示於柱子之外的上方。實際上,在同事的機器上,柱狀圖左側的數據柱沒有顯示任何數據系列標簽,柱狀圖右側的數據柱可以正確顯示數據系列標簽。
QBarSeries *series = new QBarSeries(); //...
series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd); // 設置數據系列標簽的位置於數據柱外側上方
series->setLabelsVisible(true); //顯示數據系列標簽
百思不得其解,我在我機器上調試同事的代碼,發現我這邊居然一個數據系列標簽都不顯示了。
打開Qt的Examples的BarChart Example並編譯運行之。確認是Qt的Bug,和同事的代碼無關。同時我在自己的筆記本上Qt-5.10.1運行Qt的這個例子,發現問題依舊。
效果如下圖(數據柱中間的小白線就是不正常顯示的數據系列標簽):
如果拉伸柱狀圖,直接就看不到數據系列標簽了:
但是,只要設置數據系列標簽的位置在其它位置(柱子內測上方),就可以正常顯示數據系列標簽了:
代碼如下:
/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Charts module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 or (at your option) any later version ** approved by the KDE Free Qt Foundation. The licenses are as published by ** the Free Software Foundation and appearing in the file LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <QtCharts/QChartView> #include <QtCharts/QBarSeries> #include <QtCharts/QBarSet> #include <QtCharts/QLegend> #include <QtCharts/QBarCategoryAxis> QT_CHARTS_USE_NAMESPACE int main(int argc, char *argv[]) { QApplication a(argc, argv); //![1]
QBarSet *set0 = new QBarSet("Jane"); QBarSet *set1 = new QBarSet("John"); QBarSet *set2 = new QBarSet("Axel"); QBarSet *set3 = new QBarSet("Mary"); QBarSet *set4 = new QBarSet("Samantha"); *set0 << 1 << 2 << 3 << 4 << 5 << 6; *set1 << 5 << 0 << 0 << 4 << 0 << 7; *set2 << 3 << 5 << 8 << 13 << 8 << 5; *set3 << 5 << 6 << 7 << 3 << 4 << 5; *set4 << 9 << 7 << 5 << 3 << 1 << 2; //![1] //![2]
QBarSeries *series = new QBarSeries(); series->append(set0); series->append(set1); series->append(set2); series->append(set3); series->append(set4); series->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd); // 設置數據系列標簽的位置於數據柱內測上方
series->setLabelsVisible(true); // 設置顯示數據系列標簽 //![2] //![3]
QChart *chart = new QChart(); chart->addSeries(series); chart->setTitle("Simple barchart example"); chart->setAnimationOptions(QChart::SeriesAnimations); //![3] //![4]
QStringList categories; categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes(); chart->setAxisX(axis, series); //![4] //![5]
chart->legend()->setVisible(true); chart->legend()->setAlignment(Qt::AlignBottom); //![5] //![6]
QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); //![6] //![7]
QMainWindow window; window.setCentralWidget(chartView); window.resize(420, 300); window.show(); //![7]
return a.exec(); }
結論:目前在Qt-5.10.1及以下的Qt版本中,無法讓數據系列標簽顯示在柱狀圖外側上方。
如有錯誤之處還請指正,謝謝!