|
| 1 | +import * as React from 'react'; |
| 2 | +import { useCallback, useReducer, useRef } from 'react'; |
| 3 | +import cloneDeep from 'lodash/cloneDeep'; |
| 4 | +import defaultsDeep from 'lodash/defaultsDeep'; |
| 5 | +import { getMutationObserver } from '../utils/observe'; |
| 6 | + |
| 7 | +import * as echarts from 'echarts/core'; |
| 8 | +import { EChartsOption } from 'echarts/types/dist/option'; |
| 9 | +import { LineChart } from 'echarts/charts'; |
| 10 | +import { GridComponent, TitleComponent, TooltipComponent } from 'echarts/components'; |
| 11 | +import { SVGRenderer } from 'echarts/renderers'; |
| 12 | + |
| 13 | +// Register minimal required components |
| 14 | +echarts.use([GridComponent, LineChart, SVGRenderer, TitleComponent, TooltipComponent]); |
| 15 | + |
| 16 | +import { EChartsInitOpts } from 'echarts/types/dist/echarts'; |
| 17 | +import { ThemeDefinition } from '../themes/Theme'; |
| 18 | +import { getClassName } from '../utils/styles'; |
| 19 | +import { getTheme } from '../utils/theme'; |
| 20 | +import { ThemeColor } from '../themes/ThemeColor'; |
| 21 | + |
| 22 | +/** |
| 23 | + * The Line chart relates all the data point symbols by broken lines, which is used to show the trend of data changing |
| 24 | + * |
| 25 | + * Note: Only the minimum requirements are imported from echarts. This includes components to support the grid, series, |
| 26 | + * title, and tooltip properties. To include a toolbox; for example, you must include the component (in addition to |
| 27 | + * toolbox props) like so: |
| 28 | + * |
| 29 | + * import * as echarts from 'echarts/core'; |
| 30 | + * import { ToolboxComponent } from 'echarts/components'; |
| 31 | + * echarts.use([ToolboxComponent]); |
| 32 | + * |
| 33 | + * @beta |
| 34 | + */ |
| 35 | +export interface LineProps { |
| 36 | + /** |
| 37 | + * The className prop specifies a class name that will be applied to outermost element |
| 38 | + */ |
| 39 | + className?: string; |
| 40 | + /** |
| 41 | + * Specify height explicitly, in pixels |
| 42 | + */ |
| 43 | + height?: number; |
| 44 | + /** |
| 45 | + * The id prop specifies an ID that will be applied to outermost element. |
| 46 | + */ |
| 47 | + id?: string; |
| 48 | + /** |
| 49 | + * Flag indicating to use the legend tooltip (default). This may be overridden by the `option.tooltip` property. |
| 50 | + */ |
| 51 | + isLegendTooltip?: boolean; |
| 52 | + /** |
| 53 | + * Flag indicating to use the SVG renderer (default). This may be overridden by the `opts.renderer` property. |
| 54 | + */ |
| 55 | + isSvgRenderer?: boolean; |
| 56 | + /** |
| 57 | + * This creates a Mutation Observer to watch the given DOM selector. |
| 58 | + * |
| 59 | + * When the pf-v6-theme-dark selector is added or removed, this component will be notified to update its computed |
| 60 | + * theme styles. However, if the dark theme is not updated dynamically (e.g., via a toggle), there is no need to add |
| 61 | + * this Mutation Observer. |
| 62 | + * |
| 63 | + * Note: Don't provide ".pf-v6-theme-dark" as the node selector as it won't exist in the page for light theme. |
| 64 | + * The underlying querySelectorAll() function needs to find the element the dark theme selector will be added to. |
| 65 | + * |
| 66 | + * See https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Locating_DOM_elements_using_selectors |
| 67 | + * |
| 68 | + * @propType string |
| 69 | + * @example <Line nodeSelector="html" /> |
| 70 | + * @example <Line nodeSelector="#main" /> |
| 71 | + * @example <Line nodeSelector=".chr-scope__default-layout" /> |
| 72 | + */ |
| 73 | + nodeSelector?: string; |
| 74 | + /** |
| 75 | + * ECharts uses this object to configure its properties; for example, series, title, and tooltip |
| 76 | + * |
| 77 | + * See https://echarts.apache.org/en/option.html |
| 78 | + */ |
| 79 | + option?: EChartsOption; |
| 80 | + /** |
| 81 | + * Optional chart configuration |
| 82 | + * |
| 83 | + * See https://echarts.apache.org/en/api.html#echarts.init |
| 84 | + */ |
| 85 | + opts?: EChartsInitOpts; |
| 86 | + /** |
| 87 | + * The theme prop specifies a theme to use for determining styles and layout properties for a component. Any styles or |
| 88 | + * props defined in theme may be overwritten by props specified on the component instance. |
| 89 | + * |
| 90 | + * See https://echarts.apache.org/handbook/en/concepts/style/#theme |
| 91 | + */ |
| 92 | + theme?: ThemeDefinition; |
| 93 | + /** |
| 94 | + * Specifies the theme color. Valid values are 'blue', 'green', 'multi', etc. |
| 95 | + * |
| 96 | + * Note: Not compatible with theme prop |
| 97 | + * |
| 98 | + * @example themeColor={ChartThemeColor.blue} |
| 99 | + */ |
| 100 | + themeColor?: string; |
| 101 | + /** |
| 102 | + * Specify width explicitly, in pixels |
| 103 | + */ |
| 104 | + width?: number; |
| 105 | +} |
| 106 | + |
| 107 | +export const Line: React.FunctionComponent<LineProps> = ({ |
| 108 | + className, |
| 109 | + height, |
| 110 | + id, |
| 111 | + isLegendTooltip = true, |
| 112 | + isSvgRenderer = true, |
| 113 | + nodeSelector, |
| 114 | + option, |
| 115 | + opts, |
| 116 | + theme, |
| 117 | + themeColor, |
| 118 | + width, |
| 119 | + ...rest |
| 120 | +}: LineProps) => { |
| 121 | + const containerRef = useRef<HTMLDivElement>(); |
| 122 | + const echart = useRef<echarts.ECharts>(); |
| 123 | + const [update, forceUpdate] = useReducer((x) => x + 1, 0); |
| 124 | + |
| 125 | + const series: any = cloneDeep(option?.series); |
| 126 | + const tooltip: any = cloneDeep(option?.tooltip); |
| 127 | + |
| 128 | + const getSize = () => ({ |
| 129 | + ...(height && { height: `${height}px` }), |
| 130 | + ...(width && { width: `${width}px` }) |
| 131 | + }); |
| 132 | + |
| 133 | + const getLegendTooltip = useCallback(() => { |
| 134 | + const symbolSize = '10px'; |
| 135 | + const valueFormatter = tooltip?.valueFormatter ? tooltip.valueFormatter : (value: number | string) => value; |
| 136 | + |
| 137 | + const defaults = { |
| 138 | + confine: true, |
| 139 | + formatter: (params: any) => { |
| 140 | + let result = ''; |
| 141 | + params?.map((param, index) => { |
| 142 | + if (index === 0) { |
| 143 | + result += `<p style="text-align:left;">${param.name}</p>`; |
| 144 | + } |
| 145 | + // Todo: Replace param.marker with custom icon -- see https://github.com/apache/echarts/issues/19826 |
| 146 | + result += ` |
| 147 | + <p style="text-align:left;"> |
| 148 | + <div style="display: inline-block; background-color: ${param.color}; height: ${symbolSize}; width: ${symbolSize};"></div> |
| 149 | + ${param.seriesName} |
| 150 | + <strong style="float:right; margin-left: 20px"> |
| 151 | + ${valueFormatter(param.value, param.dataIndex)} |
| 152 | + </strong> |
| 153 | + </p> |
| 154 | + `; |
| 155 | + }); |
| 156 | + return result; |
| 157 | + }, |
| 158 | + trigger: 'axis' |
| 159 | + }; |
| 160 | + return defaultsDeep(tooltip, defaults); |
| 161 | + }, [tooltip]); |
| 162 | + |
| 163 | + React.useEffect(() => { |
| 164 | + const isSkeleton = themeColor === ThemeColor.skeleton; |
| 165 | + const chartTheme = theme ? theme : getTheme(themeColor); |
| 166 | + const renderer = isSvgRenderer ? 'svg' : 'canvas'; |
| 167 | + |
| 168 | + echart.current = echarts.init( |
| 169 | + containerRef.current, |
| 170 | + chartTheme, |
| 171 | + defaultsDeep(opts, { height, renderer, width }) // height and width are necessary here for unit tests |
| 172 | + ); |
| 173 | + |
| 174 | + const getSeries = () => |
| 175 | + series.map((serie: any) => { |
| 176 | + const defaults = { |
| 177 | + emphasis: { |
| 178 | + ...(isSkeleton ? { disabled: true } : { focus: 'adjacency' }) |
| 179 | + }, |
| 180 | + type: 'line' |
| 181 | + }; |
| 182 | + return defaultsDeep(serie, defaults); |
| 183 | + }); |
| 184 | + |
| 185 | + // Todo: Tooltip symbol workaround -- see https://github.com/apache/echarts/issues/18016 |
| 186 | + echart.current?.setOption({ |
| 187 | + ...option, |
| 188 | + ...(!isSkeleton && isLegendTooltip && { tooltip: getLegendTooltip() }), |
| 189 | + ...(isSkeleton && { tooltip: undefined }), // Skeleton should not have any interactions |
| 190 | + series: getSeries() |
| 191 | + }); |
| 192 | + |
| 193 | + return () => { |
| 194 | + echart.current?.dispose(); |
| 195 | + }; |
| 196 | + }, [containerRef, getLegendTooltip, option, opts, series, theme, themeColor, update]); |
| 197 | + |
| 198 | + // Resize observer |
| 199 | + React.useEffect(() => { |
| 200 | + echart.current?.resize(); |
| 201 | + }, [height, width]); |
| 202 | + |
| 203 | + // Dark theme observer |
| 204 | + React.useEffect(() => { |
| 205 | + let observer = () => {}; |
| 206 | + observer = getMutationObserver(nodeSelector, () => { |
| 207 | + forceUpdate(); |
| 208 | + }); |
| 209 | + return () => { |
| 210 | + observer(); |
| 211 | + }; |
| 212 | + }, [nodeSelector]); |
| 213 | + |
| 214 | + return <div className={getClassName(className)} id={id} ref={containerRef} style={getSize()} {...rest} />; |
| 215 | +}; |
| 216 | +Line.displayName = 'Line'; |
0 commit comments