在QML图表中自定义BarSet

时间:2017-11-28 20:37:23

标签: qt qml qt5 qtquick2 qchart

我正在尝试使用QtCharts 2.1更改BarChart的外观。目前,我将所需的颜色和样式应用到单个QBarSet中,如下所示:

import QtQuick 2.8
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtCharts 2.1

import QtQuick.Window 2.2

ChartView {
    id: root
    width: 600
    height: 400
    antialiasing: true
    backgroundColor: "transparent"
    legend.visible: false

    property var histogramProvider

    Connections {
        target: histogramProvider
        onHistogramChanged: {
            console.log("histogram changed")
            histogramProvider.updateSeries(histogramSeries)
            console.log(histogramSeries)
            console.log(histogramSeries.at(0))
            histogramSeries.at(0).color = Qt.rgba(0.9, 0.2, 0.1, 0.5)
        }
        onAxesChanged: {
            xAxis.min = xMin
            xAxis.max = xMax
            yAxis.min = yMin
            yAxis.max = yMax
        }
    }

    ValueAxis {
            id: yAxis
            min: 0
            max: 100
            tickCount: 3
            gridVisible: false
            labelsColor: Style.fontColorLight
            labelsFont: Qt.font({pointSize: 8})
            titleText: qsTr("Count")
            labelFormat: "%.1f"
    }

    ValueAxis {
        id: xAxis
        tickCount: 3
        gridVisible: false
        labelsColor: "white"
        labelFormat: "%i"
        min: 0
        max: 100
        titleText: qsTr("Elongation")
    }

    StackedBarSeries {
        id: histogramSeries
        axisX: xAxis
        axisY: yAxis
    }
}

我可以在图表中看到带有默认颜色的BarSets,但是我无法改变颜色。该代码从onHistogramChanged生成以下控制台输出:

qml: QtCharts::DeclarativeStackedBarSeries(0x1df550cd830)
qml: null
qrc:/qml/Histogram.qml:27: TypeError: Type error

我从C ++更新BarSeries,如下所示:

void HistogramProvider::updateSeries(QStackedBarSeries *barSeries)
{
    [...]
    barSeries->clear();
    QBarSet* set = new QBarSet("Histogram", barSeries);
    set->setBorderColor(QColor("transparent"));
    set->setColor(QColor("#6f74dd"));
    for(int i = 0; i < bins.size(); i++) {
        set->append(bins.at(i));
    }
    qDebug() << "blubb";
    barSeries->append(set);
}

我在这里做错了什么?

0 个答案:

没有答案