|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +import * as React from 'react'; |
| 4 | +import { DonutChart } from '../DonutChart/index'; |
| 5 | +import { VerticalStackedBarChart } from '../VerticalStackedBarChart/index'; |
| 6 | +import { |
| 7 | + transformPlotlyJsonToDonutProps, |
| 8 | + transformPlotlyJsonToColumnProps, |
| 9 | + transformPlotlyJsonToScatterChartProps, |
| 10 | + transformPlotlyJsonToHorizontalBarWithAxisProps, |
| 11 | + isDateArray, |
| 12 | + isNumberArray, |
| 13 | + transformPlotlyJsonToHeatmapProps, |
| 14 | + transformPlotlyJsonToSankeyProps, |
| 15 | + transformPlotlyJsonToGaugeProps, |
| 16 | +} from './PlotlySchemaAdapter'; |
| 17 | +import { LineChart } from '../LineChart/index'; |
| 18 | +import { HorizontalBarChartWithAxis } from '../HorizontalBarChartWithAxis/index'; |
| 19 | +import { AreaChart } from '../AreaChart/index'; |
| 20 | +import { HeatMapChart } from '../HeatMapChart/index'; |
| 21 | +import { SankeyChart } from '../SankeyChart/SankeyChart'; |
| 22 | +import { GaugeChart } from '../GaugeChart/index'; |
| 23 | + |
| 24 | +export interface Schema { |
| 25 | + /** |
| 26 | + * Plotly schema represented as JSON object |
| 27 | + */ |
| 28 | + plotlySchema: any; |
| 29 | + |
| 30 | + /** |
| 31 | + * The legends selected by the user to persist in the chart |
| 32 | + */ |
| 33 | + selectedLegends?: string[]; |
| 34 | + |
| 35 | + /** |
| 36 | + * Dictionary for localizing the accessibility labels |
| 37 | + */ |
| 38 | + accesibilityLabels?: { [key: string]: string }; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * DeclarativeChart props. |
| 43 | + * {@docCategory DeclarativeChart} |
| 44 | + */ |
| 45 | +export interface DeclarativeChartProps extends React.RefAttributes<HTMLDivElement> { |
| 46 | + /** |
| 47 | + * The schema representing the chart data, layout and configuration |
| 48 | + */ |
| 49 | + chartSchema: Schema; |
| 50 | + |
| 51 | + /** |
| 52 | + * Callback when an event occurs |
| 53 | + */ |
| 54 | + onSchemaChange?: (eventData: Schema) => void; |
| 55 | +} |
| 56 | + |
| 57 | +const useColorMapping = () => { |
| 58 | + const colorMap = React.useRef(new Map<string, string>()); |
| 59 | + return colorMap; |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * DeclarativeChart component. |
| 64 | + * {@docCategory DeclarativeChart} |
| 65 | + */ |
| 66 | +export const DeclarativeChart: React.FunctionComponent<DeclarativeChartProps> = React.forwardRef< |
| 67 | + HTMLDivElement, |
| 68 | + DeclarativeChartProps |
| 69 | +>((props, forwardedRef) => { |
| 70 | + const { plotlySchema } = props.chartSchema; |
| 71 | + const xValues = plotlySchema.data[0].x; |
| 72 | + const isXDate = isDateArray(xValues); |
| 73 | + const isXNumber = isNumberArray(xValues); |
| 74 | + const colorMap = useColorMapping(); |
| 75 | + |
| 76 | + switch (plotlySchema.data[0].type) { |
| 77 | + case 'pie': |
| 78 | + return <DonutChart {...transformPlotlyJsonToDonutProps(plotlySchema, colorMap)} />; |
| 79 | + case 'bar': |
| 80 | + const orientation = plotlySchema.data[0].orientation; |
| 81 | + if (orientation === 'h') { |
| 82 | + return ( |
| 83 | + <HorizontalBarChartWithAxis {...transformPlotlyJsonToHorizontalBarWithAxisProps(plotlySchema, colorMap)} /> |
| 84 | + ); |
| 85 | + } else { |
| 86 | + return <VerticalStackedBarChart {...transformPlotlyJsonToColumnProps(plotlySchema, colorMap)} />; |
| 87 | + } |
| 88 | + case 'scatter': |
| 89 | + const isAreaChart = plotlySchema.data.some((series: any) => series.fill === 'tonexty'); |
| 90 | + if (isXDate || isXNumber) { |
| 91 | + if (isAreaChart) { |
| 92 | + return <AreaChart {...transformPlotlyJsonToScatterChartProps(plotlySchema, true, colorMap)} />; |
| 93 | + } |
| 94 | + return <LineChart {...transformPlotlyJsonToScatterChartProps(plotlySchema, false, colorMap)} />; |
| 95 | + } |
| 96 | + return <VerticalStackedBarChart {...transformPlotlyJsonToColumnProps(plotlySchema, colorMap)} />; |
| 97 | + case 'heatmap': |
| 98 | + return <HeatMapChart {...transformPlotlyJsonToHeatmapProps(plotlySchema)} />; |
| 99 | + case 'sankey': |
| 100 | + return <SankeyChart {...transformPlotlyJsonToSankeyProps(plotlySchema, colorMap)} />; |
| 101 | + case 'indicator': |
| 102 | + if (plotlySchema?.data?.[0]?.mode?.includes('gauge')) { |
| 103 | + return <GaugeChart {...transformPlotlyJsonToGaugeProps(plotlySchema, colorMap)} />; |
| 104 | + } |
| 105 | + return <div>Unsupported Schema</div>; |
| 106 | + default: |
| 107 | + return <div>Unsupported Schema</div>; |
| 108 | + } |
| 109 | +}); |
| 110 | +DeclarativeChart.displayName = 'DeclarativeChart'; |
0 commit comments