diff options
| author | Haishan <[email protected]> | 2020-10-31 18:18:04 +0800 |
|---|---|---|
| committer | Haishan <[email protected]> | 2020-11-01 17:42:52 +0800 |
| commit | ff1a39d04e53b428e34d46c55ecd6689189b5443 (patch) | |
| tree | 94a60abe3d28a1d729b877356bdd38d75ce655a5 /src/hooks/useLineChart.ts | |
| parent | e62c9165481ef12ee2310dee1c32f890b3fe4b78 (diff) | |
chore: run ts-migrate
Diffstat (limited to 'src/hooks/useLineChart.ts')
| -rw-r--r-- | src/hooks/useLineChart.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/hooks/useLineChart.ts b/src/hooks/useLineChart.ts new file mode 100644 index 0000000..3bfd7d7 --- /dev/null +++ b/src/hooks/useLineChart.ts @@ -0,0 +1,30 @@ +import React from 'react'; + +import { commonChartOptions } from '../misc/chart'; + +const { useEffect } = React; +const options = commonChartOptions; + +export default function useLineChart( + Chart, + elementId, + data, + subscription, + extraChartOptions = {} +) { + useEffect(() => { + // @ts-expect-error ts-migrate(2339) FIXME: Property 'getContext' does not exist on type 'HTML... Remove this comment to see the full error message + const ctx = document.getElementById(elementId).getContext('2d'); + const c = new Chart(ctx, { + type: 'line', + data, + options: { ...options, ...extraChartOptions }, + }); + const unsubscribe = + subscription && subscription.subscribe(() => c.update()); + return () => { + unsubscribe && unsubscribe(); + c.destroy(); + }; + }, [Chart, elementId, data, subscription, extraChartOptions]); +} |
