【问题标题】:get Y value given X value in AM Charts JS在 AM Charts JS 中获取给定 X 值的 Y 值
【发布时间】:2021-07-24 15:16:29
【问题描述】:

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

给定1。数据点:

const data = [{x: 23, y: 0},{x: 24, y: 0},{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))

【问题讨论】:

    标签: javascript reactjs amcharts amcharts4


    【解决方案1】:

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

    const data = [{x: 23, y: 0},{x: 24, y: 0},{x: 25, y: 23},{x: 26, y: 24}];
    
    console.log(data.find(item => item.x === 23).y);

    【讨论】:

    • 问题是,数据不完整,例如我的数据集只有整数,而 am 图表在两者之间插入值。所以如果 x = 23.5,那么 y 是什么?我需要它从图表中。
    • 如果你只打算步进 0.5 x 单位,你可以把 x = 23.5 左右两边的点的 y 值相加,然后除以 2。
    • 这只是一个例子,这条线可以是任何二维多项式,我需要介于两者之间的任何点......
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    相关资源
    最近更新 更多