Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(charts): Update V9 Charts to use DataVizGradientPalette #33323

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e5e79fc
add gradient declarations
shawn-grant Nov 20, 2024
b362372
replace color with gradient in ChartDataPoint and add gradient utilit…
shawn-grant Nov 20, 2024
758f388
refactor: update test-data.ts dataPoints to use gradient property
shawn-grant Nov 20, 2024
09b1252
feat: implement gradient support in DonutChart components
shawn-grant Nov 21, 2024
eef1cf2
test: update DonutChart tests to use gradient instead of color
shawn-grant Nov 21, 2024
0c562b5
feat: add gradient support to HorizontalBarChart and update rendering…
shawn-grant Nov 21, 2024
2eb7d67
refactor: replace color with gradient in HorizontalBarChart test cases
shawn-grant Nov 21, 2024
942d9a3
build: update charts api readme
shawn-grant Nov 21, 2024
d3cc0db
add update-snapshots script to package.json
shawn-grant Nov 21, 2024
5d90039
update tests and test snapshot
shawn-grant Nov 21, 2024
f64134a
Merge branch 'master' into chart-gradients
shawn-grant Nov 21, 2024
d9aa34d
refactor: update all DonutChart stories to use gradient pallete
shawn-grant Nov 21, 2024
40f628c
refactor: update HorizontalBarChart stories to use gradient palette
shawn-grant Nov 21, 2024
355faa6
Merge branch 'master' into chart-gradients
shawn-grant Nov 21, 2024
18988c5
fix HBC test snapshots
shawn-grant Nov 21, 2024
61e5ad7
update change files
shawn-grant Nov 21, 2024
fc6d1c4
update change files
shawn-grant Nov 22, 2024
e10f630
Merge branch 'chart-gradients' of https://github.com/shawn-grant/flue…
shawn-grant Nov 22, 2024
8a3d7bf
update change type to patch
shawn-grant Nov 22, 2024
751e2e0
Merge branch 'master' into chart-gradients
shawn-grant Nov 22, 2024
c58d7af
build: fix formatting issues
shawn-grant Nov 22, 2024
0cc5413
build: fix ci fail (vr-tests-react-components)
shawn-grant Nov 22, 2024
f75812e
add change file
shawn-grant Nov 22, 2024
78f2709
Merge branch 'master' into chart-gradients
shawn-grant Nov 25, 2024
0138bed
pr-fix(HorizontalBarChart): use tokens.colorBackgroundOverlay
shawn-grant Nov 25, 2024
610b397
feat(DonutChart): update gradient calculation and add custom gradient…
shawn-grant Nov 25, 2024
f01a57a
feat(HorizontalBarChart): add custom gradient story
shawn-grant Nov 25, 2024
7961be6
pr-fix(DonutChart): check both gradient colors are defined before app…
shawn-grant Nov 25, 2024
904d04f
update test snapshots
shawn-grant Nov 26, 2024
8a6ce72
pr-fix(DonutChart): rename gradient prop to color for Arc
shawn-grant Nov 26, 2024
969f516
pr-fix(DonutChart): rename gradient prop to color and update related …
shawn-grant Nov 26, 2024
dcf5e3d
pr-fix(HorizontalBarChart): rename gradient prop to color and update …
shawn-grant Nov 26, 2024
026000b
revert changes in vr-tests-react-components
shawn-grant Nov 26, 2024
25c7e0f
update tests
shawn-grant Nov 26, 2024
1f7ca56
Merge branch 'master' into chart-gradients
shawn-grant Nov 27, 2024
40ee893
pr-cleanup: revert changes in @fluentui/react-virtualizer
shawn-grant Nov 27, 2024
e15353b
fix tests
shawn-grant Nov 27, 2024
6c15c0d
minor change
shawn-grant Nov 27, 2024
6ecf234
pr-fix(charts): improve missing color handling
shawn-grant Nov 28, 2024
3d9a7e2
Merge branch 'master' into chart-gradients
shawn-grant Nov 28, 2024
81890dc
Merge branch 'master' into chart-gradients
shawn-grant Dec 2, 2024
5057918
Merge branch 'master' into chart-gradients
shawn-grant Dec 3, 2024
1168c3b
Merge branch 'master' into chart-gradients
shawn-grant Dec 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Introduce gradients and rounded corners to v9 charts",
"packageName": "@fluentui/react-charts-preview",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export type ChartDataMode = 'default' | 'fraction' | 'percentage';
// @public (undocumented)
export interface ChartDataPoint {
callOutAccessibilityData?: AccessibilityProps;
color?: string;
color?: string | [string, string];
data?: number;
horizontalBarChartdata?: HorizontalDataPoint;
legend?: string;
Expand Down Expand Up @@ -304,6 +304,36 @@ export interface DataPoint {
y: number;
}

// @public (undocumented)
export const DataVizGradientPalette: {
gradient1: string;
gradient2: string;
gradient3: string;
gradient4: string;
gradient5: string;
gradient6: string;
gradient7: string;
gradient8: string;
gradient9: string;
gradient10: string;
gradient1Ext: string;
gradient2Ext: string;
gradient3Ext: string;
gradient4Ext: string;
gradient5Ext: string;
gradient6Ext: string;
gradient7Ext: string;
gradient8Ext: string;
gradient9Ext: string;
gradient10Ext: string;
success: string;
highSuccess: string;
warning: string;
error: string;
highError: string;
disabled: string;
};

// @public (undocumented)
export const DataVizPalette: {
color1: string;
Expand Down Expand Up @@ -403,9 +433,15 @@ export interface EventsAnnotationProps {
// @public (undocumented)
export const getColorFromToken: (token: string, isDarkTheme?: boolean) => string;

// @public (undocumented)
export const getGradientFromToken: (token: string, isDarkTheme?: boolean) => [string, string];

// @public (undocumented)
export const getNextColor: (index: number, offset?: number, isDarkTheme?: boolean) => string;

// @public (undocumented)
export const getNextGradient: (index: number, offset?: number, isDarkTheme?: boolean) => [string, string];

// @public (undocumented)
export interface GroupedVerticalBarChartData {
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"start": "yarn storybook",
"storybook": "yarn --cwd ../stories storybook",
"test": "jest --passWithNoTests",
"update-snapshots": "jest -u",
"type-check": "just-scripts type-check"
},
"syncpack": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChartDataPoint } from '../index';
import { ArcProps } from './index';
import { format as d3Format } from 'd3-format';
import { formatValueWithSIPrefix, useRtl } from '../../../utilities/index';
import { useId } from '@fluentui/react-utilities';

// Create a Arc within Donut Chart variant which uses these default styles and this styled subcomponent.
/**
Expand All @@ -13,7 +14,7 @@ import { formatValueWithSIPrefix, useRtl } from '../../../utilities/index';
*/
export const Arc: React.FunctionComponent<ArcProps> = React.forwardRef<HTMLDivElement, ArcProps>(
(props, forwardedRef) => {
const arc = d3Arc();
const arc = d3Arc().cornerRadius(5);
const currentRef = React.createRef<SVGPathElement>();
const _isRTL: boolean = useRtl();
const classes = useArcStyles_unstable(props);
Expand Down Expand Up @@ -90,22 +91,33 @@ export const Arc: React.FunctionComponent<ArcProps> = React.forwardRef<HTMLDivEl
//TO DO 'replace' is throwing error
const id = props.uniqText! + props.data!.data.legend!.replace(/\s+/, '') + props.data!.data.data;
const opacity: number = props.activeArc === props.data!.data.legend || props.activeArc === '' ? 1 : 0.1;

// check if gradient ([string, string]) or color (string) is provided
const useGradient = Array.isArray(props.color);
const clipId = useId('Arc_clip') + (useGradient ? `${props.color[0]}_${props.color[1]}` : (props.color as string));

const fill = useGradient
? `conic-gradient(
from ${props.data?.startAngle}rad,
${props.color[0]},
${props.color[1]} ${props.data!.endAngle - props.data!.startAngle}rad
)`
: (props.color as string);

const pathData = arc({ ...props.data!, innerRadius: props.innerRadius, outerRadius: props.outerRadius })!;
const focusPathData = arc({ ...props.focusData!, innerRadius: props.innerRadius, outerRadius: props.outerRadius })!;

return (
<g ref={currentRef}>
{!!focusedArcId && focusedArcId === id && (
// TODO innerradius and outerradius were absent
<path
id={id + 'focusRing'}
d={arc({ ...props.focusData!, innerRadius: props.innerRadius, outerRadius: props.outerRadius })!}
className={classes.focusRing}
/>
<path id={id + 'focusRing'} d={focusPathData} className={classes.focusRing} />
)}
<path
// TODO innerradius and outerradius were absent
id={id}
d={arc({ ...props.data!, innerRadius: props.innerRadius, outerRadius: props.outerRadius })!}
className={classes.root}
style={{ fill: props.color, cursor: href ? 'pointer' : 'default' }}
d={pathData}
style={{ fill: 'transparent', cursor: href ? 'pointer' : 'default' }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removed the classname?

Copy link
Contributor Author

@shawn-grant shawn-grant Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the classname to the <div> that renders the gradients. The <path> element won't be visible, so the class and its styles aren't needed.
Test also break if both elements have the class.

onFocus={_onFocus.bind(this, props.data!.data, id)}
data-is-focusable={props.activeArc === props.data!.data.legend || props.activeArc === ''}
onMouseOver={_hoverOn.bind(this, props.data!.data)}
Expand All @@ -117,6 +129,22 @@ export const Arc: React.FunctionComponent<ArcProps> = React.forwardRef<HTMLDivEl
aria-label={_getAriaLabel()}
role="img"
/>
{/* clipping mask */}
<clipPath id={clipId}>
<path d={pathData} />
</clipPath>
{/* div to attach conic-gradient fill to */}
<foreignObject x="-50%" y="-50%" width="100%" height="100%" clipPath={`url(#${clipId})`}>
<div
className={classes.root}
style={{
width: '100%',
height: '100%',
background: fill,
opacity,
}}
/>
</foreignObject>
{_renderArcLabel(classes.arcLabel)}
</g>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export interface ArcProps {
outerRadius: number;

/**
* Color for the Arc.
* Color or gradient for the legend in the chart. If not provided, it will fallback on the default color palette.
*/
color: string;
color: string | [string, string];

/**
* Defines the function that is executed upon hovering over the legend
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function sharedAfterEach() {

// Do this after unmounting the wrapper to make sure if any timers cleaned up on unmount are
// cleaned up in fake timers world
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((global.setTimeout as any).mock) {
jest.useRealTimers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DonutChartProps } from './DonutChart.types';
import { useDonutChartStyles_unstable } from './useDonutChartStyles.styles';
import { ChartDataPoint } from '../../DonutChart';
import { convertToLocaleString } from '../../utilities/locale-util';
import { getColorFromToken, getNextColor } from '../../utilities/index';
import { getNextGradient } from '../../utilities/index';
import { Legend, Legends } from '../../index';
import { useId } from '@fluentui/react-utilities';
import { useFocusableGroup } from '@fluentui/react-tabster';
Expand All @@ -23,7 +23,6 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
(props, forwardedRef) => {
const _rootElem = React.useRef<HTMLDivElement | null>(null);
const _uniqText: string = useId('_Pie_');
/* eslint-disable @typescript-eslint/no-explicit-any */
let _calloutAnchorPoint: ChartDataPoint | null;
let _emptyChartId: string | null;
const legendContainer = React.useRef<HTMLDivElement | null>(null);
Expand All @@ -34,7 +33,7 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
const [_width, setWidth] = React.useState<number | undefined>(props.width || 200);
const [_height, setHeight] = React.useState<number | undefined>(props.height || 200);
const [activeLegend, setActiveLegend] = React.useState<string>('');
const [color, setColor] = React.useState<string | undefined>('');
const [color, setColor] = React.useState<string | [string, string] | undefined>('');
const [xCalloutValue, setXCalloutValue] = React.useState<string>('');
const [yCalloutValue, setYCalloutValue] = React.useState<string>('');
const [selectedLegend, setSelectedLegend] = React.useState<string>('');
Expand Down Expand Up @@ -76,13 +75,14 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
});
return elevatedData;
}

function _createLegends(chartData: ChartDataPoint[]): JSX.Element {
const legendDataItems = chartData.map((point: ChartDataPoint, index: number) => {
const color: string = point.color!;
const useGradient = Array.isArray(point.color);
// mapping data to the format Legends component needs
const legend: Legend = {
const pointLegend: Legend = {
title: point.legend!,
color,
color: useGradient ? point.color![0] : (point.color as string),
action: () => {
if (selectedLegend === point.legend) {
setSelectedLegend('');
Expand All @@ -98,7 +98,7 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
setActiveLegend('');
},
};
return legend;
return pointLegend;
});
const legends = (
<Legends
Expand All @@ -115,7 +115,7 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
setPopoverOpen(selectedLegend === '' || selectedLegend === data.legend);
setValue(data.data!.toString());
setLegend(data.legend);
setColor(data.color!);
setColor(data.color);
setXCalloutValue(data.xAxisCalloutData!);
setYCalloutValue(data.yAxisCalloutData!);
setFocusedArcId(id);
Expand All @@ -128,7 +128,7 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
setPopoverOpen(selectedLegend === '' || selectedLegend === data.legend);
setValue(data.data!.toString());
setLegend(data.legend);
setColor(data.color!);
setColor(data.color);
setXCalloutValue(data.xAxisCalloutData!);
setYCalloutValue(data.yAxisCalloutData!);
setDataPointCalloutProps(data);
Expand Down Expand Up @@ -193,13 +193,30 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
function _addDefaultColors(donutChartDataPoint?: ChartDataPoint[]): ChartDataPoint[] {
return donutChartDataPoint
? donutChartDataPoint.map((item, index) => {
let defaultColor: string;
if (typeof item.color === 'undefined') {
defaultColor = getNextColor(index, 0);
} else {
defaultColor = getColorFromToken(item.color);
let itemColor = item.color;

// if color is not defined, assign a default color
if (!itemColor) {
itemColor = getNextGradient(index);
}
return { ...item, defaultColor };
// if color is a string, check if it is undefined or blank assign a default color
if (typeof itemColor === 'string' && itemColor.trim() === '') {
itemColor = getNextGradient(index);
}
// if color is an array, check if either colors are undefined or blank
// if startColor is undefined or blank, assign a default color
// if endColor is undefined or blank, assign the startColor to endColor
if (Array.isArray(itemColor)) {
const [startColor, endColor] = itemColor;
if (!startColor || startColor.trim() === '') {
itemColor[0] = getNextGradient(index)[0];
}
if (!endColor || endColor.trim() === '') {
itemColor[1] = itemColor[0];
}
}

return { ...item, color: itemColor };
})
: [];
}
Expand Down Expand Up @@ -263,6 +280,7 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
const chartData = _elevateToMinimums(points.filter((d: ChartDataPoint) => d.data! >= 0));
const valueInsideDonut = _valueInsideDonut(props.valueInsideDonut!, chartData!);
const focusAttributes = useFocusableGroup();

return !_isChartEmpty() ? (
<div
className={classes.root}
Expand Down Expand Up @@ -299,7 +317,7 @@ const DonutChartBase: React.FunctionComponent<DonutChartProps> = React.forwardRe
isPopoverOpen={isPopoverOpen}
legend={legend!}
YValue={value!}
color={color}
color={Array.isArray(color) ? color[0] : color}
isCalloutForStack={false}
customCallout={{
customizedCallout: props.onRenderCalloutPerDataPoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable react/jsx-no-bind */
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as React from 'react';
import { pie as d3Pie } from 'd3-shape';
import { pie as d3Pie, PieArcDatum } from 'd3-shape';
import { PieProps } from './index';
import { Arc } from '../Arc/index';
import { ChartDataPoint } from '../index';
Expand All @@ -25,7 +25,6 @@ export const Pie: React.FunctionComponent<PieProps> = React.forwardRef<HTMLDivEl
const classes = usePieStyles_unstable(props);
const pieForFocusRing = d3Pie()
.sort(null)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was showing as an unused eslint directive

.value((d: any) => d.data)
.padAngle(0);

Expand All @@ -45,17 +44,15 @@ export const Pie: React.FunctionComponent<PieProps> = React.forwardRef<HTMLDivEl
return totalValue;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function arcGenerator(d: any, i: number, focusData: any, href?: string): JSX.Element {
const color = d && d.data && d.data.color;
function arcGenerator(d: PieArcDatum<ChartDataPoint>, i: number, focusData: any, href?: string): JSX.Element {
return (
<Arc
key={i}
data={d}
focusData={focusData}
innerRadius={props.innerRadius}
outerRadius={props.outerRadius}
color={color!}
color={d.data.color!}
onFocusCallback={_focusCallback}
hoverOnCallback={_hoverCallback}
onBlurCallback={props.onBlurCallback}
Expand All @@ -78,7 +75,6 @@ export const Pie: React.FunctionComponent<PieProps> = React.forwardRef<HTMLDivEl

const piechart = d3Pie<ChartDataPoint>()
.sort(null)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.value((d: any) => d.data)
.padAngle(0.02)(data);
const translate = `translate(${props.width / 2}, ${props.height / 2})`;
Expand All @@ -87,7 +83,7 @@ export const Pie: React.FunctionComponent<PieProps> = React.forwardRef<HTMLDivEl

return (
<g transform={translate}>
{piechart.map((d: any, i: number) => arcGenerator(d, i, focusData[i], props.href))}
{piechart.map((d: PieArcDatum<ChartDataPoint>, i: number) => arcGenerator(d, i, focusData[i], props.href))}
{props.valueInsideDonut && (
<text y={5} textAnchor="middle" dominantBaseline="middle" className={classes.insideDonutString}>
{props.valueInsideDonut}
Expand Down
Loading
Loading