Skip to content

Commit 99e5e6a

Browse files
AtishayMsftkrkshitijsrmukherAnush2303
authored
feat(charts): Declarative chart component to render chart using plotly json schema (#33348)
Co-authored-by: krkshitij <110246001+krkshitij@users.noreply.github.com> Co-authored-by: srmukher <120183316+srmukher@users.noreply.github.com> Co-authored-by: Anush Gupta <74965306+Anush2303@users.noreply.github.com>
1 parent d66b302 commit 99e5e6a

25 files changed

Lines changed: 2569 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Create declarative chart component",
4+
"packageName": "@fluentui/react-charting",
5+
"email": "atisjai@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charting/etc/react-charting.api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ export const DataVizPalette: {
119119
highSuccess: string;
120120
};
121121

122+
// @public
123+
export const DeclarativeChart: React_2.FunctionComponent<DeclarativeChartProps>;
124+
125+
// @public
126+
export interface DeclarativeChartProps extends React_2.RefAttributes<HTMLDivElement> {
127+
chartSchema: Schema;
128+
onSchemaChange?: (eventData: Schema) => void;
129+
}
130+
122131
// @public
123132
export const DonutChart: React_2.FunctionComponent<IDonutChartProps>;
124133

@@ -1591,6 +1600,15 @@ export const PieChart: React_2.FunctionComponent<IPieChartProps>;
15911600
// @public
15921601
export const SankeyChart: React_2.FunctionComponent<ISankeyChartProps>;
15931602

1603+
// @public (undocumented)
1604+
export interface Schema {
1605+
accesibilityLabels?: {
1606+
[key: string]: string;
1607+
};
1608+
plotlySchema: any;
1609+
selectedLegends?: string[];
1610+
}
1611+
15941612
// @public (undocumented)
15951613
export const Shape: React_2.FC<IShapeProps>;
15961614

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './components/DeclarativeChart/DeclarativeChart';
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)