在 AM Charts JS 中获取给定 X 值的 Y 值

问题描述

我在我的项目中使用 AMCharts,我想要的是完成这个:

给定1。数据点:

const data = [{x: 23,y: 0},{x: 24,{x: 25,y: 23},...,{x: 26,y: 24}]

我想从系列中提取给定 X 值的任何 Y 值...

我用来创建图表的部分代码

        this.chart = am4core.create(this.chartDiv,am4charts.XYChart);

        this.title = this.chart.titles.create();

        // chartData is just an array of x,y values
        this.chart.data = this.props.chartData;

        const X_AXIS = this.chart.xAxes.push(new am4charts.ValueAxis());
        X_AXIS.title.text = "X VALUES";

        const Y_AXIS = this.chart.yAxes.push(new am4charts.ValueAxis());
        Y_AXIS.title.text = "Y VALUES";

        this.series = this.chart.series.push(new am4charts.Lineseries());
        this.series.datafields.valueX = "xValue";
        this.series.datafields.valueY = "yValue";

        // cursor
        this.chart.cursor = new am4charts.XYCursor();
        this.chart.cursor.xAxis = X_AXIS;
        this.chart.cursor.yAxis = Y_AXIS;
        this.chart.cursor.snapToSeries = this.series;

我怎样才能做到这一点?在 JS 中说 X = 24 的 Y 值(类似 this.series.get(25))

解决方法

为什么不直接使用 Array.prototype.find 方法搜索数据,因为无论如何您都是基于数据数组生成点的。

const data = [{x: 23,y: 0},{x: 24,{x: 25,y: 23},{x: 26,y: 24}];

console.log(data.find(item => item.x === 23).y);