当前位置:   article > 正文

QML charts 鼠标动态跟随数值(十字交叉定位)_maptoposition

maptoposition

有时需要在图表中使用鼠标动态的显示某个点的数据值,Series中提供了一些相关鼠标事件的信号,此文就记录使用hovered(point point, bool state)信号实现十字交叉定位。效果图:

 

hovered(point point, bool state)信号发出当前鼠标所处的图表坐标Point(x,y),x和y分别代表了Series中该点处的值。借助ChartView中的函数mapToPosition(point value, AbstractSeries series)可返回得到当前鼠标于ChartView的相对位置绘制一纵一横两条交叉线。

如果像我的效果图中那样显示的十字线仅仅只在图表区域内显示,那么可以使用chartview中的属性plotArea获得绘图区域的位置大小即可。

数据结果在ToolTip中进行显示(注意:ToolTip的parent必须为Item,因此ToolTip在ChartView中初始化或者在Series中对其指定父类),以下代码只贴出了重要的核心内容,并非QML全部,其余内容可自行添加。

  1. ToolTip{
  2. id:toolTip
  3. delay: 4
  4. font.family: "微软雅黑"
  5. font.bold: true
  6. font.pointSize: 13
  7. }
  8. LineSeries {
  9. id:lineSeries
  10. name: "LineSeries"
  11. axisX: myDateTimeAxis
  12. axisY:myAxisY
  13. color: "#00ffff"
  14. width: 3
  15. onHovered: {
  16. var chartPosition = chartView.mapToPosition(point,lineSeries)
  17. myCanvas.xx = chartPosition.x
  18. myCanvas.yy = chartPosition.y
  19. toolTip.visible = state
  20. toolTip.text = "X:"+point.x+" Y:"+point.y
  21. toolTip.x = chartPosition.x+10
  22. toolTip.y = chartPosition.y-40
  23. var d = new Date()
  24. myCanvas.requestPaint()
  25. }
  26. }
  27. Canvas{
  28. id:myCanvas
  29. anchors.fill: chartView
  30. property double xx: 0
  31. property double yy: 0
  32. onPaint: {
  33. if(xx+yy>0){
  34. var ctx = getContext("2d")
  35. ctx.clearRect(0,0,parent.width,parent.height)
  36. ctx.strokeStyle = '#ccf48993'
  37. ctx.lineWidth = 3
  38. ctx.beginPath()
  39. ctx.moveTo(xx,chartView.plotArea.y)
  40. ctx.lineTo(xx,chartView.plotArea.height+chartView.plotArea.y)
  41. ctx.stroke()
  42. ctx.beginPath()
  43. ctx.moveTo(chartView.plotArea.x,yy)
  44. ctx.lineTo(chartView.plotArea.x+chartView.plotArea.width,yy)
  45. ctx.stroke()
  46. }
  47. }
  48. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/805457
推荐阅读
相关标签
  

闽ICP备14008679号