0

如何在 QML ChartView 类型的绘图区域内的特定 (x,y) 位置放置文本?

例如,我想将文本放置在位置 XYPoint{x: -3; Y: 20} 我不想放在窗口的(x,y),我想放在绘图区的(x,y)

我阅读了文档,但我没有找到任何属性!!!!!!

//用于绘制点的ChartView

ChartView{
    id:chrt
    anchors.fill: parent
    height: parent.height
    width: parent.width
    legend.visible: false
    backgroundColor: "black"


    //X- axis
    ValueAxis{
        id: x_axis
        min: -5
        max: 0
        tickCount: 6
    }

    //Right Y axis
    ValueAxis{
        id:right_y_axis
        min:0
        max:40
        tickCount: 5
    }

    //Left Y axis
    ValueAxis{
        id:left_y_axis
        min:0
        max:40
        tickCount: 5
    }

    //Line series for wave 1
    LineSeries{
        id:l1
        axisY: left_y_axis
        axisX:x_axis
        color: "yellow"
        width: 1
    }

    //Line series for wave 2
    LineSeries{
        id:l2
        axisYRight: right_y_axis
        style: Qt.DashLine
        color: "yellow"
        width: 0.6
    }
}
4

1 回答 1

4

好的,您可以使用ChartView.mapToPosition从一个系列(或系列内的某个其他点)计算指定点的全局位置,例如:

ChartView{
    id:chart
    anchors.fill: parent
    backgroundColor: "black"

    LineSeries{
        id: series
        XYPoint { x: 10; y: 5  }
        XYPoint { x: 10; y: 1 }
        XYPoint { x: 15; y: 5 }
        XYPoint { x: 20; y: 10 }
        XYPoint { x: 25; y: 5 }
        XYPoint { x: 30; y: 20 }
        XYPoint { x: 40; y: 10 }
    }

    Text {
        id: txt
        text: "Hello"
        color: "white"
    }

    onWidthChanged: updatePointPosition();
    onHeightChanged: updatePointPosition();

    function updatePointPosition()
    {
        var p = chart.mapToPosition(series.at(3), series);
        txt.x = p.x;
        txt.y = p.y;
    }
}
于 2019-02-19T15:48:25.640 回答