Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

点击echarts图像上的点时获得x轴数据

  1. User

    点击echarts图像上的点时获得x轴数据

  2. Assistant

    要在echarts图像上点击点时获取x轴数据,可以使用echarts的click事件,并结合echarts的convertFromPixel方法将点击的坐标转换为数据值。

    以下是一个示例代码:

    // 定义echarts实例
    var myChart = echarts.init(document.getElementById('chart'));
    
    // 点击事件
    myChart.on('click', function(params) {
      // 将点击坐标转换为数据值
      var xValue = myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0];
      console.log('点击的x轴数据:', xValue);
    });
    
    // 配置项和数据
    var option = {
      xAxis: {
        type: 'category',
        data: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        type: 'line',
        data: [12, 34, 23, 45, 67, 54, 33]
      }]
    };
    
    // 使用配置项和数据显示图表
    myChart.setOption(option);
    

    在上面的代码中,通过监听echarts的click事件,在点击时获取到点击的坐标,并使用convertFromPixel方法将点击的坐标转换为对应的x轴数据值。最后通过console.log将x轴数据值打印到控制台。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.