Skip to content

Commit 4a38a00

Browse files
authored
Enabling titles for x and y axes for Declarative charts (#33533)
1 parent c53044a commit 4a38a00

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Enabling titles for x and y axes for Declarative charts",
4+
"packageName": "@fluentui/react-charting",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charting/src/components/DeclarativeChart/PlotlySchemaAdapter.ts

+63-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ export const isMonthArray = (array: any[]): boolean => {
4343
return false;
4444
};
4545

46+
function getTitles(layout: any) {
47+
const titles = {
48+
chartTitle:
49+
typeof layout.title === 'string' ? layout.title : typeof layout.title?.text === 'string' ? layout.title.text : '',
50+
xAxisTitle:
51+
typeof layout?.xaxis?.title === 'string'
52+
? layout?.xaxis?.title
53+
: typeof layout?.xaxis?.title?.text === 'string'
54+
? layout?.xaxis?.title?.text
55+
: '',
56+
yAxisTitle:
57+
typeof layout?.yaxis?.title === 'string'
58+
? layout?.yaxis?.title
59+
: typeof layout?.yaxis?.title?.text === 'string'
60+
? layout?.yaxis?.title?.text
61+
: '',
62+
};
63+
return titles;
64+
}
65+
4666
export const updateXValues = (xValues: any[]): any[] => {
4767
const presentYear = new Date().getFullYear();
4868
const dates = xValues.map(possiblyMonthValue => {
@@ -115,9 +135,11 @@ export const transformPlotlyJsonToDonutProps = (
115135
},
116136
};
117137

138+
const { chartTitle } = getTitles(layout);
139+
118140
return {
119141
data: {
120-
chartTitle: layout?.title,
142+
chartTitle,
121143
chartData: donutData,
122144
},
123145
hideLegend: layout?.showlegend === false ? true : false,
@@ -165,13 +187,17 @@ export const transformPlotlyJsonToVSBCProps = (
165187
});
166188
});
167189

190+
const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
191+
168192
return {
169193
data: Object.values(mapXToDataPoints),
170-
chartTitle: layout?.title,
171194
// width: layout?.width,
172195
// height: layout?.height,
173196
barWidth: 'auto',
174197
yMaxValue,
198+
chartTitle,
199+
xAxisTitle,
200+
yAxisTitle,
175201
};
176202
};
177203

@@ -203,12 +229,16 @@ export const transformPlotlyJsonToGVBCProps = (
203229
});
204230
});
205231

232+
const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
233+
206234
return {
207235
data: Object.values(mapXToDataPoints),
208-
chartTitle: layout?.title,
209236
// width: layout?.width,
210237
// height: layout?.height,
211238
barwidth: 'auto',
239+
chartTitle,
240+
xAxisTitle,
241+
yAxisTitle,
212242
};
213243
};
214244

@@ -288,13 +318,17 @@ export const transformPlotlyJsonToVBCProps = (
288318
});
289319
});
290320

321+
const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
322+
291323
return {
292324
data: vbcData,
293-
chartTitle: typeof layout?.title === 'string' ? layout?.title : '',
294325
// width: layout?.width,
295326
// height: layout?.height,
296327
barWidth: 24,
297328
supportNegativeData: true,
329+
chartTitle,
330+
xAxisTitle,
331+
yAxisTitle,
298332
};
299333
};
300334

@@ -324,20 +358,26 @@ export const transformPlotlyJsonToScatterChartProps = (
324358
};
325359
});
326360

361+
const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
362+
327363
const chartProps: IChartProps = {
328-
chartTitle: typeof layout.title === 'string' ? layout.title : '',
364+
chartTitle,
329365
lineChartData: chartData,
330366
};
331367

332368
if (isAreaChart) {
333369
return {
334370
data: chartProps,
335371
supportNegativeData: true,
372+
xAxisTitle,
373+
yAxisTitle,
336374
} as IAreaChartProps;
337375
} else {
338376
return {
339377
data: chartProps,
340378
supportNegativeData: true,
379+
xAxisTitle,
380+
yAxisTitle,
341381
} as ILineChartProps;
342382
}
343383
};
@@ -372,9 +412,15 @@ export const transformPlotlyJsonToHorizontalBarWithAxisProps = (
372412
const gapFactor = 1 / (1 + scalingFactor * numberOfBars);
373413
const barHeight = availableHeight / (numberOfBars * (1 + gapFactor));
374414

415+
const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
416+
375417
return {
376418
data: chartData,
377-
chartTitle: typeof layout.title === 'string' ? layout.title : '',
419+
chartTitle,
420+
xAxisTitle,
421+
yAxisTitle,
422+
secondaryYAxistitle:
423+
typeof layout?.yaxis2?.title === 'string' ? layout?.yaxis2?.title : layout?.yaxis2?.title?.text || '',
378424
barHeight,
379425
showYAxisLables: true,
380426
styles: {
@@ -419,13 +465,17 @@ export const transformPlotlyJsonToHeatmapProps = (jsonObj: any): IHeatMapChartPr
419465
? firstData.colorscale.map((arr: any) => arr[0] * (zMax - zMin) + zMin)
420466
: [];
421467
const rangeValuesForColorScale: string[] = firstData.colorscale ? firstData.colorscale.map((arr: any) => arr[1]) : [];
468+
const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
422469

423470
return {
424471
data: [heatmapData],
425472
domainValuesForColorScale,
426473
rangeValuesForColorScale,
427474
hideLegend: true,
428475
showYAxisLables: true,
476+
chartTitle,
477+
xAxisTitle,
478+
yAxisTitle,
429479
sortOrder: 'none',
430480
};
431481
};
@@ -473,9 +523,12 @@ export const transformPlotlyJsonToSankeyProps = (
473523
},
474524
};
475525
const shouldResize: number = width + height;
526+
527+
const { chartTitle } = getTitles(layout);
528+
476529
return {
477530
data: {
478-
chartTitle: typeof layout?.title === 'string' ? layout?.title : '',
531+
chartTitle,
479532
SankeyChartData: sankeyChartData,
480533
},
481534
width,
@@ -538,10 +591,12 @@ export const transformPlotlyJsonToGaugeProps = (
538591
},
539592
};
540593

594+
const { chartTitle } = getTitles(layout);
595+
541596
return {
542597
segments,
543598
chartValue: typeof firstData.value === 'number' ? firstData.value : 0,
544-
chartTitle: typeof firstData.title?.text === 'string' ? firstData.title?.text : '',
599+
chartTitle,
545600
sublabel,
546601
// range values can be null
547602
minValue: typeof firstData.gauge?.axis?.range?.[0] === 'number' ? firstData.gauge?.axis?.range?.[0] : undefined,

0 commit comments

Comments
 (0)