diff options
| author | Haishan <[email protected]> | 2019-04-21 00:05:44 +0800 |
|---|---|---|
| committer | Haishan <[email protected]> | 2019-04-21 21:58:33 +0800 |
| commit | 882b168082ddbcbe7991a71a09944f1a60084fc3 (patch) | |
| tree | d12345be635943537042a929aed8376ae4480324 /src/hooks/useLineChart.js | |
| parent | eda2501b1d68c6f82a4a824ffe12caf5be7b33f2 (diff) | |
squash: feat(config): add options to select traffic chart style
Diffstat (limited to 'src/hooks/useLineChart.js')
| -rw-r--r-- | src/hooks/useLineChart.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/hooks/useLineChart.js b/src/hooks/useLineChart.js new file mode 100644 index 0000000..2ab23a0 --- /dev/null +++ b/src/hooks/useLineChart.js @@ -0,0 +1,28 @@ +import React from 'react'; +import { commonChartOptions } from 'm/chart'; + +const { useEffect } = React; +const options = commonChartOptions; + +export default function useLineChart( + Chart, + elementId, + data, + subscription, + extraChartOptions = {} +) { + useEffect(() => { + 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]); +} |
