-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix hiding chart elements when components are removed #2529
Changes from 4 commits
473b558
9d82b66
9611398
3a3f230
d6bc99e
06ba916
8c2dc53
f05f364
e08d2f2
436c01f
d78a0d5
90888aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lg-charts/core': patch | ||
--- | ||
|
||
Fixes hiding chart elements when components are removed. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,24 @@ export function Grid({ horizontal = true, vertical = true }: GridProps) { | |
updatedOptions.xAxis = getUpdatedLineOptions(!!vertical); | ||
updatedOptions.yAxis = getUpdatedLineOptions(!!horizontal); | ||
updateChartOptions(updatedOptions); | ||
|
||
return () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not get unmounted by default? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This issue is that this isn't a real component, in a sense, given that it doesn't actually render anything to the DOM. Instead, this ends up basically being "config via JSX". So on unmount, essentially nothing happens. The component itself, which renders nothing, IS removed from the DOM. But this does nothing to actually update the chart that's rendered, since the chart is config based. |
||
/** | ||
* Hides the grid lines when the component is unmounted. | ||
*/ | ||
updateChartOptions({ | ||
xAxis: { | ||
splitLine: { | ||
show: false, | ||
}, | ||
}, | ||
yAxis: { | ||
splitLine: { | ||
show: false, | ||
}, | ||
}, | ||
}); | ||
}; | ||
}, [horizontal, vertical, theme]); | ||
|
||
return null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,22 @@ import { defaultLineOptions } from './config'; | |
import { LineProps } from './Line.types'; | ||
|
||
export function Line({ name, data }: LineProps) { | ||
const { addChartSeries } = useChartContext(); | ||
const { addChartSeries, removeChartSeries } = useChartContext(); | ||
|
||
useEffect(() => { | ||
addChartSeries({ | ||
...defaultLineOptions, | ||
name, | ||
data, | ||
}); | ||
|
||
return () => { | ||
/** | ||
* Remove the series when the component unmounts to make sure the series | ||
* is removed when a `Line` is hidden. | ||
*/ | ||
removeChartSeries(name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only place I see a potential issue is if the consumer has rendered another line with the same name before this one unmounts, and we then remove the wrong line from the chart. But at that point, I'd almost call that "user error" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is an interesting point. I've discovered that when lines are rendered with the same names, they end up having the same colors. I wonder if there's actually any good way to restrict this so that names have to be unique There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a check within There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah and actually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So oddly I'm finding that |
||
}; | ||
}, [name, data]); | ||
|
||
return null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,22 @@ export function YAxis({ type, label, unit }: YAxisProps) { | |
|
||
useEffect(() => { | ||
updateChartOptions(getOptions({ type, label, unit, theme })); | ||
|
||
return () => { | ||
/** | ||
* Hides the axis when the component is unmounted. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a cleaner way to handle unmount? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure. Definitely open to ideas! The problem is as noted in this comment. So the goal here is to essentially manually trigger a chart config update any time the component unmounts. I'll actually chuck this out to the team async and see if anyone has any cleaner ideas for this! |
||
*/ | ||
updateChartOptions({ | ||
yAxis: { | ||
axisLine: { | ||
show: false, | ||
}, | ||
axisLabel: { | ||
show: false, | ||
}, | ||
}, | ||
}); | ||
}; | ||
}, [type, label, unit, theme, updateChartOptions]); | ||
|
||
return null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added just to be on the safe side. I don't believe echarts actually does an object reference equality check to control repaints, but thought it might as well be a new object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍