简体   繁体   English

如何在 amcharts 中获取 axisRange 的 y 值?

[英]How do I get the y value of an axisRange in amcharts?

I have a line series chart and I want to add a draggable series range label.我有一个线系列图表,我想添加一个可拖动的系列范围标签。 On drag event of the label, I want to print the Y value of the chart where the range label is.在标签的拖动事件中,我想打印范围标签所在图表的 Y 值。 How can I achieving this?我怎样才能做到这一点?

I've attached an image to make it easier to visualize and understand what I am trying to achieve.我附上了一张图片,以便更容易想象和理解我想要实现的目标。 在此处输入图像描述

This is what I have tried in code but the value i'm getting is not consistently correct.这是我在代码中尝试过的,但我得到的值并不总是正确的。

const cursorPosition = {
  x: 0,
  y: 0,
}

chart.cursor.events.on('cursorpositionchanged', (ev: any) => {
  const xAxis = ev.target.chart.xAxes.getIndex(0)
  const yAxis = ev.target.chart.yAxes.getIndex(0)
  cursorPosition.x = xAxis.positionToDate(xAxis.toAxisPosition(ev.target.xPosition))
  cursorPosition.y = yAxis.positionToValue(yAxis.toAxisPosition(ev.target.yPosition))
})

const maxRange = valueAxis.axisRanges.create()
maxRange.value = props.max
maxRange.grid.stroke = am4core.color('#00695C')
maxRange.grid.strokeWidth = 2
maxRange.grid.strokeOpacity = 1
maxRange.label.text = 'max'
maxRange.label.fontSize = '18px'
maxRange.label.scale = 0.7
maxRange.label.background.fill = maxRange.grid.stroke
maxRange.label.fill = am4core.color('#fff')
maxRange.label.draggable = true
maxRange.label.inside = true
maxRange.label.x = 0
maxRange.label.y = 0
maxRange.label.isMeasured = true

// Position of max label maxRange.label.maxX = 58
maxRange.label.minX = 58

maxRange.label.events.on('dragged', () => {
  maxRange.value = valueAxis.positionToValue(
    valueAxis.renderer.coordinateToPosition(valueAxis.renderer.pixelHeight - maxRange.label.pixelY)
  )

  console.log(cursorPosition.x)
})

To get the y value of the axisRange in the code you provided, you can use the value property of the axisRange object.要在您提供的代码中获取 axisRange 的 y 值,您可以使用 axisRange 对象的 value 属性。

For example:例如:

let yValue = maxRange.value;

This will give you the maximum value of the axisRange, which is set using the value property when the axisRange is created:这将为您提供 axisRange 的最大值,该值是在创建 axisRange 时使用 value 属性设置的:

maxRange.value = props.max;

Keep in mind that the value property may contain any data type, depending on the type of axis and the data being plotted.请记住,值属性可能包含任何数据类型,具体取决于轴的类型和绘制的数据。 For example, if the axis is a date/time axis, the value property may contain a JavaScript Date object.例如,如果轴是日期/时间轴,则 value 属性可能包含一个 JavaScript Date 对象。

Additionally, the code you provided includes an event listener that allows the user to drag the label for the axisRange and update its value.此外,您提供的代码包含一个事件侦听器,允许用户拖动 axisRange 的标签并更新其值。 The new value is calculated by converting the pixel position of the label to a value using the positionToValue method of the valueAxis:新值是通过使用 valueAxis 的 positionToValue 方法将标签的像素位置转换为值来计算的:

maxRange.value = valueAxis.positionToValue(
  valueAxis.renderer.coordinateToPosition(valueAxis.renderer.pixelHeight - maxRange.label.pixelY)
);

This new value is then used to update the value property of the axisRange.这个新值随后用于更新 axisRange 的值属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM