Di qml, saya membuat ChartView:

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

Dan dalam sinyal 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);
}

Ini adalah outputnya. ini adalah outputnya qt qml qtquick2 qchart

0
linarcx 29 Desember 2017, 22:02

1 menjawab

Jawaban Terbaik

Opsi sederhana untuk membuat seri adalah dengan menggunakan createSeries() , lalu kita tambahkan axisY dan properti lainnya:

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)
    }
}

enter image description here

1
eyllanesc 30 Desember 2017, 00:03