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

Adding secondary y-axis for Declarative charts #33594

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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 @@
{
srmukher marked this conversation as resolved.
Show resolved Hide resolved
"type": "patch",
"comment": "Adding secondary y-axis for Declarative charts",
"packageName": "@fluentui/react-charting",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ export const getColor = (
return colorMap.current.get(legendLabel) as string;
};

const getSecondaryYAxisValues = (series: any, layout: any) => {
let secondaryYAxistitle: string = '';
let secondaryYScaleOptions: { yMinValue?: number; yMaxValue?: number } = {};
secondaryYAxistitle = layout.yaxis2.title;
if (layout.yaxis2.range) {
secondaryYScaleOptions = {
yMinValue: layout.yaxis2.range[0],
yMaxValue: layout.yaxis2.range[1],
};
} else {
const yValues = series.y;
if (yValues) {
secondaryYScaleOptions = {
yMinValue: Math.min(...yValues),
yMaxValue: Math.max(...yValues),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

we dont need secondary scale if yaxis2 is not defined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is for the case in which 'range' is not defined layout.yaxis2.range

Copy link
Contributor

Choose a reason for hiding this comment

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

that means yaxis2 is there with the same range as yaxis1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not necessarily, then we need to find out the range ourselves.

}
}
return { secondaryYAxistitle, secondaryYScaleOptions };
};

export const transformPlotlyJsonToDonutProps = (
jsonObj: any,
colorMap: React.MutableRefObject<Map<string, string>>,
Expand Down Expand Up @@ -160,6 +181,10 @@ export const transformPlotlyJsonToVSBCProps = (
const { data, layout } = jsonObj;
const mapXToDataPoints: { [key: string]: IVerticalStackedChartProps } = {};
let yMaxValue = 0;
let secondaryYAxisValues: {
secondaryYAxistitle: string;
secondaryYScaleOptions: { yMinValue?: number; yMaxValue?: number };
} = { secondaryYAxistitle: '', secondaryYScaleOptions: {} };
Copy link
Contributor

Choose a reason for hiding this comment

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

both these can move to the getSecondaryYAxisValues function


data.forEach((series: any, index1: number) => {
series.x?.forEach((x: string | number, index2: number) => {
Expand All @@ -185,6 +210,9 @@ export const transformPlotlyJsonToVSBCProps = (

yMaxValue = Math.max(yMaxValue, series.y?.[index2]);
});
if (layout.yaxis2 && series.name === layout.yaxis2.title) {
secondaryYAxisValues = getSecondaryYAxisValues(series, layout);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

both these can move to the getSecondaryYAxisValues function

});

const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
Expand All @@ -198,6 +226,8 @@ export const transformPlotlyJsonToVSBCProps = (
chartTitle,
xAxisTitle,
yAxisTitle,
secondaryYAxistitle: secondaryYAxisValues.secondaryYAxistitle,
secondaryYScaleOptions: secondaryYAxisValues.secondaryYScaleOptions,
};
};

Expand All @@ -208,6 +238,10 @@ export const transformPlotlyJsonToGVBCProps = (
): IGroupedVerticalBarChartProps => {
const { data, layout } = jsonObj;
const mapXToDataPoints: Record<string, IGroupedVerticalBarChartData> = {};
let secondaryYAxisValues: {
secondaryYAxistitle: string;
secondaryYScaleOptions: { yMinValue?: number; yMaxValue?: number };
} = { secondaryYAxistitle: '', secondaryYScaleOptions: {} };

data.forEach((series: any, index1: number) => {
series.x?.forEach((x: string | number, index2: number) => {
Expand All @@ -227,6 +261,9 @@ export const transformPlotlyJsonToGVBCProps = (
});
}
});
if (layout.yaxis2 && series.name === layout.yaxis2.title) {
secondaryYAxisValues = getSecondaryYAxisValues(series, layout);
}
});

const { chartTitle, xAxisTitle, yAxisTitle } = getTitles(layout);
Expand All @@ -239,6 +276,8 @@ export const transformPlotlyJsonToGVBCProps = (
chartTitle,
xAxisTitle,
yAxisTitle,
secondaryYAxistitle: secondaryYAxisValues.secondaryYAxistitle,
secondaryYScaleOptions: secondaryYAxisValues.secondaryYScaleOptions,
};
};

Expand Down
Loading