Skip to content

Commit 8c7c11a

Browse files
authored
feat(react-charting): Adding Annotation only chart (#35390)
1 parent 5863b2a commit 8c7c11a

19 files changed

+2000
-26
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "feat(chart-utilities): Adding annotation only chart",
4+
"packageName": "@fluentui/chart-utilities",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "feat(react-charting): Adding annotation only chart",
4+
"packageName": "@fluentui/react-charting",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/chart-utilities/etc/chart-utilities.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export interface ErrorOptions {
387387
}
388388

389389
// @public (undocumented)
390-
export type FluentChart = 'area' | 'composite' | 'donut' | 'fallback' | 'gauge' | 'groupedverticalbar' | 'heatmap' | 'horizontalbar' | 'line' | 'scatter' | 'scatterpolar' | 'sankey' | 'table' | 'verticalstackedbar' | 'gantt';
390+
export type FluentChart = 'annotation' | 'area' | 'composite' | 'donut' | 'fallback' | 'gauge' | 'groupedverticalbar' | 'heatmap' | 'horizontalbar' | 'line' | 'scatter' | 'scatterpolar' | 'sankey' | 'table' | 'verticalstackedbar' | 'gantt';
391391

392392
// @public (undocumented)
393393
export interface Font {

packages/charts/chart-utilities/src/PlotlySchemaConverter.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Datum, TypedArray, PlotData, PlotlySchema, Data, Layout, SankeyDat
22
import { decodeBase64Fields } from './DecodeBase64Data';
33

44
export type FluentChart =
5+
| 'annotation'
56
| 'area'
67
| 'composite'
78
| 'donut'
@@ -201,6 +202,16 @@ export function isArrayOrTypedArray(a: any): boolean {
201202
return Array.isArray(a) || isTypedArray(a);
202203
}
203204

205+
type PlotlyAnnotations = PlotlySchema extends { layout?: { annotations?: infer T } } ? T : unknown;
206+
207+
const hasAnnotationContent = (annotations: PlotlyAnnotations | undefined): boolean => {
208+
if (!annotations) {
209+
return false;
210+
}
211+
212+
return !isArrayOrTypedArray(annotations) || (annotations as { length: number }).length > 0;
213+
};
214+
204215
export const getValidSchema = (input: any): PlotlySchema => {
205216
try {
206217
const validatedSchema = input as PlotlySchema;
@@ -210,10 +221,24 @@ export const getValidSchema = (input: any): PlotlySchema => {
210221
if (typeof validatedSchema !== 'object') {
211222
throw new Error(`Plotly input is not an object. Input type: ${typeof validatedSchema}`);
212223
}
224+
const hasAnnotations = hasAnnotationContent(validatedSchema?.layout?.annotations);
225+
213226
if (!isArrayOrTypedArray(validatedSchema.data)) {
227+
if (hasAnnotations) {
228+
return {
229+
...validatedSchema,
230+
data: [],
231+
};
232+
}
214233
throw new Error('Plotly input data is not a valid array or typed array');
215234
}
216235
if (validatedSchema.data.length === 0) {
236+
if (hasAnnotations) {
237+
return {
238+
...validatedSchema,
239+
data: [],
240+
};
241+
}
217242
throw new Error('Plotly input data is empty');
218243
}
219244
return validatedSchema;
@@ -465,7 +490,14 @@ export const mapFluentChart = (input: any): OutputChartType => {
465490
return { isValid: false, errorMessage: `Failed to decode plotly schema: ${error}` };
466491
}
467492

468-
const validTraces = getValidTraces(validSchema.data, validSchema.layout);
493+
const hasAnnotations = hasAnnotationContent(validSchema?.layout?.annotations);
494+
const hasTraces = isArrayOrTypedArray(validSchema.data) && validSchema.data.length > 0;
495+
496+
const validTraces = hasTraces ? getValidTraces(validSchema.data, validSchema.layout) : [];
497+
498+
if (!hasTraces && hasAnnotations) {
499+
return { isValid: true, type: 'annotation', validTracesInfo: [] };
500+
}
469501
let foundScatterGantt = false;
470502
let mappedTraces = validTraces.map(trace => {
471503
const traceIndex = trace[0];

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ import { IStyleFunctionOrObject as IStyleFunctionOrObject_2 } from '@fluentui/re
2222
import { ITheme } from '@fluentui/react/lib/Styling';
2323
import { ITheme as ITheme_2 } from '@fluentui/react';
2424
import type { JSXElement } from '@fluentui/utilities';
25+
import type { Margin } from '@fluentui/chart-utilities';
2526
import * as React_2 from 'react';
2627
import { SankeyLink } from 'd3-sankey';
2728
import { SankeyNode } from 'd3-sankey';
2829
import { ScaleBand } from 'd3-scale';
2930
import { ScaleLinear } from 'd3-scale';
3031
import { TimeLocaleDefinition } from 'd3-time-format';
3132

33+
// @public (undocumented)
34+
export const AnnotationOnlyChart: React_2.FC<IAnnotationOnlyChartProps>;
35+
3236
// @public
3337
export const AreaChart: React_2.FunctionComponent<IAreaChartProps>;
3438

@@ -114,6 +118,7 @@ export interface ChartAnnotationStyleProps {
114118
fontWeight?: React_2.CSSProperties['fontWeight'];
115119
opacity?: number;
116120
padding?: string;
121+
rotation?: number;
117122
textColor?: string;
118123
}
119124

@@ -289,6 +294,21 @@ export interface IAccessibilityProps {
289294
ariaLabelledBy?: string;
290295
}
291296

297+
// @public
298+
export interface IAnnotationOnlyChartProps {
299+
annotations: ChartAnnotation[];
300+
chartTitle?: string;
301+
componentRef?: React_2.RefObject<IChart>;
302+
description?: string;
303+
fontColor?: string;
304+
fontFamily?: string;
305+
height?: number;
306+
margin?: Partial<Margin>;
307+
paperBackgroundColor?: string;
308+
plotBackgroundColor?: string;
309+
width?: number;
310+
}
311+
292312
// @public
293313
export interface IAreaChartProps extends ICartesianChartProps {
294314
culture?: string;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './components/AnnotationOnlyChart/index';

0 commit comments

Comments
 (0)