我需要使用 a 滚动 QT Graph scrollbar
,为此,我创建了自己的自定义scrollbar
使用Rectangle
,MouseArea
并且可以拖动。
当我尝试使用ScrollRight
和ScrollLeft
方法滚动图表时,我无法将其ScrollBar
X
与ChartView
内容 X 链接/绑定。以下是代码:
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtCharts 2.0
Window {
id: window
width: 640
height: 480
visible: true
ChartView {
id: chartview
width: parent.width
height: 300
LineSeries {
name: "LineSeries"
axisX: ValueAxis {
min: 0
max: 100
tickCount: 12
labelFormat: "%.0f"
}
axisY: ValueAxis {
min: 0
max: 70
tickCount: 5
labelFormat: "%.0f"
}
XYPoint { x: 0; y: 0.0 }
XYPoint { x: 1.1; y: 3.2 }
XYPoint { x: 1.9; y: 2.4 }
XYPoint { x: 2.1; y: 2.1 }
XYPoint { x: 2.9; y: 2.6 }
XYPoint { x: 3.4; y: 2.3 }
XYPoint { x: 200.1; y: 3.1 }
}
}
/* Rectangle base for horizontal scroll bar */
Rectangle{
id:rectHorScrollBase
width:parent.width
height:parent.height * 0.10
anchors.top:chartview.bottom
anchors.topMargin: (parent.height * 0.01)
color:"transparent"
visible: true
/* Rectangle indicating scroll path*/
Rectangle {
id:rectHorScrollPath
width: parent.width
height: parent.height
anchors.left:parent.left
radius: 2
color: "lightblue"
}
/* Actual scroll rectaangle which will be dragged */
Rectangle {
id: rectHorScroll
property var prevX : 0
anchors.top : parent.top
width: 50
height: parent.height
color: "green"
/* Mouser area to drag the rectangle and to move Chart */
MouseArea {
id:mouseAreaHorScroll
anchors.fill: parent
drag.target: parent
drag.minimumX: 0
drag.maximumX: chartview.width - width
drag.axis: Drag.XAxis
onReleased: {
console.log("x in Released ===",rectHorScroll.x)
rectHorScroll.prevX = rectHorScroll.x
}
}
onXChanged: {
// HOW TO HANDLE THE SCROLL BASED ON x ?
if(mouseAreaHorScroll.drag.active)
{
if(x > prevX)
{
chartview.scrollRight(x) // NOT WORKING
}
else
{
chartview.scrollLeft(x) //NOT WORKING
}
}
}
}
}
}
1)如何scrollBar X
与ChartView
内容映射X
?
2)scrollbar
不应超过 X 轴的最大值和最小值。怎么做?
ScrollBar
3)和之间应该有同步ChartView
。