You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取LCJS图表中X轴常量线对应曲线Y1的数值

获取LCJS图表中X轴常量线与曲线的交点Y值

要获取X轴常量线(值为100)对应Y1曲线的数值,可根据数据情况选择以下实现方式:

1. 直接查找匹配数据点

如果曲线数据中存在X值恰好等于100的点,可直接从系列数据中提取对应Y值:

const chart = lightningChart()
    .ChartXY({
        theme: Themes.darkGold,
    })
const series = chart.addLineSeries().setName('Y1')
createProgressiveTraceGenerator()
    .setNumberOfPoints(100)
    .generate()
    .toPromise()
    .then((data) => {
        series.add(data)
        // 查找X=100的数据点
        const targetPoint = data.find(point => point.x === 100)
        if (targetPoint) {
            console.log('X=100对应的Y值:', targetPoint.y)
        } else {
            console.log('数据中无X=100的点,需插值计算')
        }
    })
const xAxis = chart.getDefaultAxisX()
const xAxisConstantline = xAxis.addConstantLine()
xAxisConstantline.setValue(100)
xAxisConstantline.setName('X Axis Constantline')

2. 插值计算交点Y值

如果数据中没有刚好X=100的点,可通过线性插值计算交点Y值:

const chart = lightningChart()
    .ChartXY({
        theme: Themes.darkGold,
    })
const series = chart.addLineSeries().setName('Y1')
createProgressiveTraceGenerator()
    .setNumberOfPoints(100)
    .generate()
    .toPromise()
    .then((data) => {
        series.add(data)
        const targetX = 100
        // 确保数据按X轴有序排列
        const sortedData = [...data].sort((a, b) => a.x - b.x)
        // 找到包含targetX的相邻数据点
        let leftPoint, rightPoint
        for (let i = 0; i < sortedData.length - 1; i++) {
            if (sortedData[i].x <= targetX && sortedData[i+1].x >= targetX) {
                leftPoint = sortedData[i]
                rightPoint = sortedData[i+1]
                break
            }
        }
        if (leftPoint && rightPoint) {
            // 线性插值计算Y值
            const interpolatedY = leftPoint.y + (rightPoint.y - leftPoint.y) * (targetX - leftPoint.x) / (rightPoint.x - leftPoint.x)
            console.log('X=100对应的插值Y值:', interpolatedY)
        } else {
            console.log('X=100超出当前数据的X范围')
        }
    })
const xAxis = chart.getDefaultAxisX()
const xAxisConstantline = xAxis.addConstantLine()
xAxisConstantline.setValue(100)
xAxisConstantline.setName('X Axis Constantline')

3. 利用LCJS内置方法简化计算

部分LCJS版本的LineSeries提供了getInterpolatedValue方法,可直接调用获取插值结果:

// 在数据添加到series后调用
const interpolatedY = series.getInterpolatedValue(100)
console.log('X=100对应的Y值:', interpolatedY)

内容的提问来源于stack exchange,提问作者冷汐柠

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:02:19