ChartView 不显示 BarSets 数据

ChartView not showing BarSets data

在 qml 中,我创建了一个 ChartView:

ChartView {
        id : mChart
        title: "Bash History Graph"
        anchors.fill: parent
        legend.alignment: Qt.AlignBottom
        antialiasing: true
        animationOptions: ChartView.SeriesAnimations
}

并在 Component.onCompleted 信号中:

Component.onCompleted: {
            var mHorSeries = Qt.createQmlObject('import QtCharts 2.2; HorizontalBarSeries {}', mChart);
            var categoryAxisY=Qt.createQmlObject('import QtCharts 2.2;BarCategoryAxis {}',mChart);
            var mBarSet = Qt.createQmlObject('import QtCharts 2.2; BarSet{}',mHorSeries);

            categoryAxisY.categories = ["2007", "2008", "2009", "2010"];
            mHorSeries.append("commands", [2, 2, 3, 4, 5, 6]);
            mChart.setAxisY(categoryAxisY, mHorSeries);
}

这是输出。

有解决问题的想法吗? 谢谢。

创建系列的一个简单选项是使用 createSeries(),然后我们添加 axisY 和其他属性:

ChartView {
    id : mChart
    title: "Bash History Graph"
    anchors.fill: parent
    antialiasing: true
    animationOptions: ChartView.SeriesAnimations
    legend.alignment: Qt.AlignBottom

    Component.onCompleted: {
        var mHorSeries = mChart.createSeries(ChartView.SeriesTypeHorizontalBar)

        var categoryAxisY=Qt.createQmlObject('import QtCharts 2.2;BarCategoryAxis {}',mChart);
        mHorSeries.axisY = categoryAxisY

        categoryAxisY.categories = ["2007", "2008", "2009", "2010", "2011", "2012" ]

        var mBarSet = mHorSeries.append("commands", [2, 2, 3, 4, 5, 6])

        mChart.axisX(mHorSeries).min= 0 //Math.min.apply(null, mBarSet.values)
        mChart.axisX(mHorSeries).max= 10 //Math.max.apply(null, mBarSet.values)
    }
}