Where should I listen for my events? #90
Replies: 7 comments 14 replies
-
Also are better docs with more examples under construction? |
Beta Was this translation helpful? Give feedback.
-
Sorry for the late response. This is different from the examples we have in the docs. There is no ref,chart.dispose etc. Should i go with that method? |
Beta Was this translation helpful? Give feedback.
-
https://apache.github.io/echarts-handbook/en/concepts/event/ example code: function SvgComponent({ option }: any) {
const [value, setValue] = React.useState(0);
const svgRef = useRef<any>(null);
useEffect(() => {
let chart: any;
if (svgRef.current) {
// @ts-ignore
chart = echarts.init(svgRef.current, 'light', {
renderer: 'svg',
width: E_WIDTH,
height: E_HEIGHT,
});
chart.setOption(option);
chart.on('click', function (params: any) {
setValue(params.value);
});
}
return () => chart?.dispose();
}, [option]);
return (
<>
<Text>{value}</Text>
<SvgChart ref={svgRef} useRNGH />
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
Here is my latest code I commented out my chart.on("click") const speedOptions = {
title: {
text: "Speed",
textStyle: {
color: "#FFFFFF",
},
},
xAxis: {
type: "category",
data: categories,
axisLine: {
show: false,
},
axisTick: {
show: false,
},
axisLabel: {
interval: 0,
},
},
yAxis: {
show: false,
},
series: {
name: "Speed",
type: "line",
smooth: true,
data: speedData,
lineStyle: { color: "#BC6D29", width: 3 },
itemStyle: { color: "#BC6D29" },
symbol: "circle",
symbolSize: 7,
},
};
useEffect(() => {
let chart: any;
if (speedRef.current) {
chart = echarts.init(speedRef.current, "light", {
renderer: "svg",
width: E_WIDTH * 0.95,
height: E_HEIGHT * 0.1,
});
chart.setOption(speedOptions);
// chart.on("click", function (params: any) {
// console.log({ params });
// console.log(params);
// chart.setOption({
// title: {
// text: "CLICKED",
// textStyle: {
// color: "#FFFFFF",
// },
// },
// });
// });
}
return () => chart?.dispose();
}, [chartSeries]);```` |
Beta Was this translation helpful? Give feedback.
-
don't |
Beta Was this translation helpful? Give feedback.
-
After some time. I managed to do it in another way but I want to ask if performance-wise is okay. useEffect(() => {
console.log({ expanded });
let chart: any;
const notExpandedOptions = {...}
const expandedOptions = {...}
if (speedRef.current) {
chart = echarts.init(speedRef.current, "light", {
renderer: "svg",
width: E_WIDTH * 0.9,
height: expanded ? E_HEIGHT * 0.4 : E_HEIGHT * 0.1,
});
expanded
? chart.setOption(expandedOptions)
: chart.setOption(speedOptions);
return () => chart?.dispose();
}, [chartSeries, expanded]); Whenever I press my button based on the state of my expanded state I give different options. |
Beta Was this translation helpful? Give feedback.
-
Screen.Recording.2023-08-03.at.9.33.01.AM.movHere is a small demostration what I managed to do with the two different options. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Im trying to listen when a user press on my pie charts. When he press I want to take the value and label and show it somewhere else in a specific container.
This is the event I found from the docs. Inside paramas I found both name,value.
chart.on("click", function (params) { console.log(params); });
My problem is where should I call this?
Should I create a useEffect that takes as dependency my chart?
This is what I tried but its 100% wrong and breaks the app too.
Beta Was this translation helpful? Give feedback.
All reactions