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:
JavaScript
x
50
50
1
<template>
2
<chart :options="chartOptionsBar"
3
:autoresize="true"
4
ref="barChart"
5
@click="mergeOptions(chartOptionsBar)"></chart>
6
<template>
7
8
<script>
9
10
export default {
11
data() {
12
return {
13
manualOptions: '',
14
chartOptionsBar: {
15
xAxis: {
16
data: ['Q1', 'Q2', 'Q3', 'Q4'],
17
},
18
yAxis: {
19
type: 'value',
20
},
21
series: [
22
{
23
type: 'bar',
24
data: [
25
{ value: 335, name: '1' },
26
{ value: 310, name: '2' },
27
{ value: 234, name: '3' },
28
{ value: 135, name: '4' },
29
],
30
},
31
],
32
title: {
33
text: 'Quarterly Sales Results',
34
x: 'center',
35
textStyle: {
36
fontSize: 24,
37
},
38
},
39
color: ['#127ac2'],
40
},
41
};
42
},
43
methods: {
44
mergeOptions (options) {
45
console.log(options.series[0]);
46
},
47
},
48
};
49
</script>
50
Advertisement
Answer
I managed to detect the clicked subset by adding a refs attribute and the click event to the component and passing a function. Then I put this function in methods:
JavaScript
1
8
1
methods: {
2
getDataSubset() {
3
this.$refs.barChart.chart.on('click', (params) => {
4
this.subset = params.data;
5
});
6
},
7
},
8