猿问

具有折线图的虚拟测量设备

我正在使用 LightningChart JS 并想实现一个虚拟测量设备,我可以在其中单击 A 点,然后拖动到 B 点并获取 A 点和 B 点的 x、y 值。

据我研究事件处理程序,它们只返回一个鼠标事件,其中包含屏幕位置的开始和停止位置。如果我错了,请纠正我。还请提出一种有效的方法来做到这一点。提前致谢。


开满天机
浏览 86回答 1
1回答

撒科打诨

鼠标事件在与普通 JS 鼠标事件相同的坐标空间中返回鼠标坐标。要在系列坐标空间中获取点击位置,需要采取几个步骤。首先需要将鼠标坐标转换为引擎坐标空间。引擎坐标空间是画布左下角为0,0的画布区域。这可以通过chart.engine.clientLocation2Engine(ev.clientX,ev.clientY).&nbsp;这将使用图表引擎比例返回引擎坐标空间中的事件坐标。然后需要将其转换为系列坐标。这可以通过translatePoint方法来完成。translatePoint可用于在两个不同比例之间转换点。LightningChart JS 中的比例基本上是一个坐标空间。const&nbsp;m&nbsp;=&nbsp;chart.engine.clientLocation2Engine(ev.clientX,&nbsp;ev.clientY) const&nbsp;translated&nbsp;=&nbsp;translatePoint(m,&nbsp;chart.engine.scale,&nbsp;lineSeries.scale)现在translated变量包含系列坐标空间中的点击位置。请参阅下面的完整代码片段,您可以在其中拖动系列区域,并在停止拖动时将标记放置到拖动的开始和结束位置。const {&nbsp; &nbsp; lightningChart,&nbsp; &nbsp; SolidLine,&nbsp; &nbsp; SolidFill,&nbsp; &nbsp; ColorRGBA,&nbsp; &nbsp; AxisTickStrategies,&nbsp; &nbsp; UIOrigins,&nbsp; &nbsp; DataPatterns,&nbsp; &nbsp; translatePoint,&nbsp; &nbsp; ColorHEX} = lcjsconst chart = lightningChart().ChartXY()const diesel = [&nbsp; &nbsp; { x: 0, y: 1.52 },&nbsp; &nbsp; { x: 1, y: 1.52 },&nbsp; &nbsp; { x: 2, y: 1.52 },&nbsp; &nbsp; { x: 3, y: 1.58 },&nbsp; &nbsp; { x: 4, y: 2.00 },&nbsp; &nbsp; { x: 5, y: 2.00 },&nbsp; &nbsp; { x: 6, y: 2.00 },&nbsp; &nbsp; { x: 7, y: 2.00 },&nbsp; &nbsp; { x: 8, y: 2.26 },&nbsp; &nbsp; { x: 9, y: 1.90 },&nbsp; &nbsp; { x: 10, y: 1.90 },&nbsp; &nbsp; { x: 11, y: 1.90 },&nbsp; &nbsp; { x: 12, y: 1.90 },&nbsp; &nbsp; { x: 13, y: 1.60 },&nbsp; &nbsp; { x: 14, y: 1.60 },&nbsp; &nbsp; { x: 15, y: 1.60 },&nbsp; &nbsp; { x: 16, y: 1.00 },&nbsp; &nbsp; { x: 17, y: 1.00 },&nbsp; &nbsp; { x: 18, y: 1.00 },&nbsp; &nbsp; { x: 19, y: 1.74 },&nbsp; &nbsp; { x: 20, y: 1.47 },&nbsp; &nbsp; { x: 21, y: 1.47 },&nbsp; &nbsp; { x: 22, y: 1.47 },&nbsp; &nbsp; { x: 23, y: 1.74 },&nbsp; &nbsp; { x: 24, y: 1.74 },&nbsp; &nbsp; { x: 25, y: 1.74 },&nbsp; &nbsp; { x: 27, y: 1.5 },&nbsp; &nbsp; { x: 28, y: 1.5 },&nbsp; &nbsp; { x: 29, y: 1.5 }]const gasoline = [&nbsp; &nbsp; { x: 0, y: 1.35 },&nbsp; &nbsp; { x: 1, y: 1.35 },&nbsp; &nbsp; { x: 2, y: 1.35 },&nbsp; &nbsp; { x: 3, y: 1.35 },&nbsp; &nbsp; { x: 4, y: 1.90 },&nbsp; &nbsp; { x: 5, y: 1.90 },&nbsp; &nbsp; { x: 6, y: 1.90 },&nbsp; &nbsp; { x: 7, y: 1.92 },&nbsp; &nbsp; { x: 8, y: 1.50 },&nbsp; &nbsp; { x: 9, y: 1.50 },&nbsp; &nbsp; { x: 10, y: 1.3 },&nbsp; &nbsp; { x: 11, y: 1.3 },&nbsp; &nbsp; { x: 12, y: 1.3 },&nbsp; &nbsp; { x: 13, y: 1.3 },&nbsp; &nbsp; { x: 14, y: 1.3 },&nbsp; &nbsp; { x: 15, y: 1.32 },&nbsp; &nbsp; { x: 16, y: 1.40 },&nbsp; &nbsp; { x: 17, y: 1.44 },&nbsp; &nbsp; { x: 18, y: 1.02 },&nbsp; &nbsp; { x: 19, y: 1.02 },&nbsp; &nbsp; { x: 20, y: 1.02 },&nbsp; &nbsp; { x: 21, y: 1.02 },&nbsp; &nbsp; { x: 22, y: 1.02 },&nbsp; &nbsp; { x: 23, y: 1.02 },&nbsp; &nbsp; { x: 24, y: 1.02 },&nbsp; &nbsp; { x: 25, y: 1.02 },&nbsp; &nbsp; { x: 27, y: 1.30 },&nbsp; &nbsp; { x: 28, y: 1.30 },&nbsp; &nbsp; { x: 29, y: 1.30 }]const lineSeries = chart.addLineSeries({ dataPattern: DataPatterns.horizontalProgressive })const lineSeries2 = chart.addLineSeries({ dataPattern: DataPatterns.horizontalProgressive })lineSeries2.add(diesel.map((point) => ({ x: point.x, y: point.y })))lineSeries.add(gasoline.map((point) => ({ x: point.x, y: point.y })))const markerA = chart.addChartMarkerXY()&nbsp; &nbsp; .setPointMarker((marker) => marker.setFillStyle((f => f.setColor(ColorHEX('#f00')))))&nbsp; &nbsp; .setMouseInteractions(false)const markerB = chart.addChartMarkerXY()&nbsp; &nbsp; .setPointMarker((marker) => marker.setFillStyle((f => f.setColor(ColorHEX('#0f0')))))&nbsp; &nbsp; .setMouseInteractions(false)function getClickInSeriesScale(point, scale) {&nbsp; &nbsp; const m = chart.engine.clientLocation2Engine(point.x, point.y)&nbsp; &nbsp; return translatePoint(m, chart.engine.scale, scale)}chart.onSeriesBackgroundMouseDragStop((obj, ev, b, start) => {&nbsp; &nbsp; let pointA = getClickInSeriesScale(start, lineSeries.scale)&nbsp; &nbsp; let pointB = getClickInSeriesScale({x:ev.clientX,y:ev.clientY}, lineSeries.scale)&nbsp; &nbsp; // move markes to start and end points&nbsp; &nbsp; markerA.setPosition(pointA)&nbsp; &nbsp; markerB.setPosition(pointB)})<script src="https://unpkg.com/@arction/lcjs@1.3.1/dist/lcjs.iife.js"></script>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答