在QML中自定义Bar系列和BarCategoryAxis

时间:2017-11-10 06:29:11

标签: qt qml qtquickcontrols2 qtchart

我需要自定义BarCategoryAxis标签和BarSeries。以下是代码:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtCharts 2.2

ApplicationWindow {
    id: window
    width: 640
    height: 480
    visible: true

    ChartView {
        id: chartview
        width: parent.width
        height: 300

        BarSeries {
            name: "BarSeries"
            axisX: BarCategoryAxis {
                id:barAxis
                property int minCategory: 10
                property int maxCategory: 100
                property int category: 50
                categories: ["10","20","30","40","50","60","70","80","90","100"]


                min:  Math.floor(((minCategory + sb.position *
                                   (maxCategory - minCategory - category)) + 9)/10)*10

                max: Math.floor((((minCategory + sb.position *
                                   (maxCategory - minCategory - category)) + category) + 9)/10) *10
            }

            axisY: ValueAxis {
                id:axisY
                min: 0
                max: 70
                tickCount: 5
                labelFormat: "%.0f"
            }

            BarSet { values: [2, 2, 3, 4, 5, 6,7] }
            BarSet { values: [5, 1, 2, 4, 1, 7] }
            BarSet { values: [3, 5, 8, 13, 5, 8] }
        }

    }

    Slider {
        id: sb
        anchors {
            bottom: parent.bottom
            left: parent.left
            right: parent.right
        }
        height: 30
    }
}

1)如何将X Axis标签与网格正好对齐?目前,它位于以下中心:

enter image description here

我尝试使用ValueAxis,但它不会正确绘制条形图,但标签正好在网格下方。

2)目前,slider移动图表但不流畅。有谁能建议更好的方法?

1 个答案:

答案 0 :(得分:0)

1)您是否尝试使用CategoryAxis而不是 BarCategoryAxis ,它有一个属性labelsPosition

2)首先,您应该在Slider插槽中进行计算:

Slider {
    onPositionChanged: {
         barAxis.min = ...
         barAxis.max = ...
    }
}

然后我建议使用像<{3}}这样的 ChartView 函数,它更加流畅。