简体   繁体   English

Echarts.js:检测对数据子集的点击

[英]Echarts.js: Detect a click on a subset of data

I want to detect a click on a particular subset in a bar chart. 我想检测条形图中特定子集的单击。 As I want to init a new chart after the click, with the data from the clicked subset. 因为我想在单击后使用来自被单击子集的数据来初始化新图表。 Currently when I click on chart bars, I get data of the whole chart and can't retrieve data of just one bar. 当前,当我单击图表条时,我会获得整个图表的数据,而无法仅检索一个条的数据。

Here is what I have: 这是我所拥有的:

<template>
 <chart :options="chartOptionsBar"
           :autoresize="true"
           ref="barChart"
           @click="mergeOptions(chartOptionsBar)"></chart>
<template>

<script>
...
export default {
  data() {
    return {
      manualOptions: '',
      chartOptionsBar: {
        xAxis: {
          data: ['Q1', 'Q2', 'Q3', 'Q4'],
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            type: 'bar',
            data: [
              { value: 335, name: '1' },
              { value: 310, name: '2' },
              { value: 234, name: '3' },
              { value: 135, name: '4' },
            ],
          },
        ],
        title: {
          text: 'Quarterly Sales Results',
          x: 'center',
          textStyle: {
            fontSize: 24,
          },
        },
        color: ['#127ac2'],
      },
    };
  },
  methods: {
    mergeOptions (options) {
      console.log(options.series[0]);
    },
  },
};
</script>

In your click handler you are passing in a reference to the original config object for the chart, so this is what will be available to you in your mergeOptions function 在您的点击处理程序中,您正在传递对图表原始配置对象的引用,因此可以在mergeOptions函数中使用该对象

If you remove the parenthesis and argument your function will get the event information for the click event @click="mergeOptions" 如果您删除括号和参数,则您的函数将获取click事件@click="mergeOptions"的事件信息。

mergeOptions (eventInfo) {
  console.log(eventInfo);
  // hopefully this information will be a bit more use
}

According to the documentation echarts.baidu.com/tutorial.html#Events%20and%20actions%20in%20ECharts%0D this information object will allow you to determine which bar was clicked. 根据echarts.baidu.com/tutorial.html#Events%20and%20actions%20in%20ECharts%0D文档,此信息对象将允许您确定单击了哪个栏。

I managed to detect the clicked subset by adding a refs attribute and the click event to the component and passing a function. 我设法通过向组件添加refs属性和click事件并传递一个函数来检测单击的子集。 Then I put this function in methods: 然后我将此函数放入方法中:

methods: {
    getDataSubset() {
      this.$refs.barChart.chart.on('click', (params) => {
        this.subset = params.data;
      });
    },
  },

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

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