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

Feature - customize pie chart legend #762

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions docs/modules/ROOT/pages/user-guide/reports/pie-chart.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ will be almost completely visualized as a thin disc.
slice reserved for white space. For example, when set to 3.6, there will
be 1/100th of the total circle space reserved between each slice.

|Legend Width (px) |number |128 |The width of the legend (if enabled).

|Legend Position |number |Horizontal | The type of legend positioning to use, horizontal or vertical.

|Horizontal Legend Translation (px) |number |0 | The amount in pixels the legend is shifted horizontally.

|Slice border with (px) |number |0 |The width of the border of each
slice.

Expand Down
108 changes: 73 additions & 35 deletions src/chart/pie/PieChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,89 @@ const NeoPieChart = (props: ChartProps) => {
const arcLinkLabelsSkipAngle = settings.arcLinkLabelsSkipAngle ? settings.arcLinkLabelsSkipAngle : 1;
const cornerRadius = settings.cornerRadius ? settings.cornerRadius : 1;
const arcLabelsSkipAngle = settings.arcLabelsSkipAngle ? settings.arcLabelsSkipAngle : 10;
const legendWidth = settings.legendWidth ? settings.legendWidth : 128;
const legendPosition = settings.legendPosition ? settings.legendPosition : 'Horizontal';
const legendTranslate = settings.legendTranslate ? settings.legendTranslate : 0;

const arcLabelsFontSize = settings.arcLabelsFontSize ? settings.arcLabelsFontSize : 13;

const legend = settings.legend ? settings.legend : false;
const colorScheme = settings.colors ? settings.colors : 'set2';
const styleRules = useStyleRules(
extensionEnabled(props.extensions, 'styling'),
props.settings.styleRules,
settings.styleRules,
props.getGlobalParameter
);

const chartColorsByScheme = getD3ColorsByScheme(colorScheme);

const calculateMargin = () => {
return {
top: marginTop,
right: legend ? (legendPosition === 'horizontal' ? marginRight : marginRight + legendWidth) : marginRight,
bottom: legend ? (legendPosition === 'horizontal' ? legendHeight + marginBottom : marginBottom) : marginBottom,
left: marginLeft,
};
};

const calculateLegendConfig = () => {
if (!legend) {
return []; // No legend required
}

if (legendPosition === 'Horizontal') {
return [
{
dataFrom: 'keys',
anchor: 'bottom',
direction: 'row',
justify: false,
translateX: legendTranslate,
translateY: legendWidth,
itemsSpacing: 2,
itemWidth: legendWidth,
itemHeight: 20,
itemDirection: 'left-to-right',
itemOpacity: 0.85,
symbolSize: 20,
effects: [
{
on: 'hover',
style: {
itemOpacity: 1,
},
},
],
},
];
}
// Vertical legend
return [
{
dataFrom: 'keys',
anchor: 'bottom-right',
direction: 'column',
justify: false,
translateX: legendWidth + 10 + legendTranslate,
translateY: 0,
itemsSpacing: 1,
itemWidth: legendWidth,
itemHeight: 20,
itemDirection: 'left-to-right',
itemOpacity: 0.85,
symbolSize: 15,
effects: [
{
on: 'hover',
style: {
itemOpacity: 1,
},
},
],
},
];
};

// Compute chart colors, based on default scheme and on styling rules
const computedChartColors = data.map((value, index) => {
let colorIndex = index;
Expand Down Expand Up @@ -155,41 +225,9 @@ const NeoPieChart = (props: ChartProps) => {
arcLinkLabelsSkipAngle={arcLinkLabelsSkipAngle}
arcLinkLabelsOffset={arcLinkLabelsOffset}
arcLabelsSkipAngle={arcLabelsSkipAngle}
margin={{
top: marginTop,
right: marginRight,
bottom: legend ? legendHeight + marginBottom : marginBottom,
left: marginLeft,
}}
margin={calculateMargin()}
colors={computedChartColors}
legends={
legend
? [
{
anchor: 'bottom',
direction: 'row',
justify: false,
translateX: 0,
translateY: 50,
itemsSpacing: 0,
itemWidth: 100,
itemHeight: 18,
itemDirection: 'left-to-right',
itemOpacity: 1,
symbolSize: 18,
symbolShape: 'circle',
effects: [
{
on: 'hover',
style: {
itemTextColor: '#000',
},
},
],
},
]
: []
}
legends={calculateLegendConfig()}
animate={true}
// TODO : Needs to be set dynamic (default true on percentage)
arcLabel={getArcLabel}
Expand Down
28 changes: 22 additions & 6 deletions src/config/ReportConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,6 @@ const _REPORT_TYPES = {
},
maxRecords: 250,
settings: {
legend: {
label: 'Show Legend',
type: SELECTION_TYPES.LIST,
values: [true, false],
default: false,
},
sortByValue: {
label: 'Auto-sort slices by value',
type: SELECTION_TYPES.LIST,
Expand Down Expand Up @@ -567,6 +561,28 @@ const _REPORT_TYPES = {
type: SELECTION_TYPES.NUMBER,
default: 50,
},
legend: {
label: 'Show Legend',
type: SELECTION_TYPES.LIST,
values: [true, false],
default: false,
},
legendWidth: {
label: 'Legend Width (px)',
type: SELECTION_TYPES.NUMBER,
default: 128,
},
legendPosition: {
label: 'Legend Position',
type: SELECTION_TYPES.LIST,
values: ['Horizontal', 'Vertical'],
default: 'Horizontal',
},
legendTranslate: {
label: 'Horizontal Legend Translation (px)',
type: SELECTION_TYPES.NUMBER,
default: 0,
},
},
},
line: {
Expand Down
Loading