Skip to content

Commit

Permalink
Add minBarWidth, sorting in animated graphs, and max no of bars
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafasaifee42 committed Oct 24, 2024
1 parent 44a4239 commit be9809d
Show file tree
Hide file tree
Showing 33 changed files with 404 additions and 70 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@undp-data/undp-visualization-library",
"version": "0.1.14",
"version": "0.1.15",
"main": "./dist/index.umd.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
28 changes: 28 additions & 0 deletions src/Components/Dashboard/GraphEl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ function GraphEl(props: Props) {
minHeight: settings?.minHeight,
showNAColor: settings?.showNAColor,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
maxNumberOfBars: settings?.maxNumberOfBars,
};
case 'horizontalGroupedBarChart':
return {
Expand Down Expand Up @@ -310,6 +312,9 @@ function GraphEl(props: Props) {
language: settings?.language,
minHeight: settings?.minHeight,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
maxNumberOfBars: settings?.maxNumberOfBars,
sorParameter: settings?.sortParameter,
};
case 'verticalBarChart':
return {
Expand Down Expand Up @@ -362,6 +367,8 @@ function GraphEl(props: Props) {
minHeight: settings?.minHeight,
showNAColor: settings?.showNAColor,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
maxNumberOfBars: settings?.maxNumberOfBars,
};
case 'verticalGroupedBarChart':
return {
Expand Down Expand Up @@ -455,6 +462,9 @@ function GraphEl(props: Props) {
language: settings?.language,
minHeight: settings?.minHeight,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
maxNumberOfBars: settings?.maxNumberOfBars,
sorParameter: settings?.sortParameter,
};
case 'animatedHorizontalBarChart':
return {
Expand Down Expand Up @@ -509,6 +519,8 @@ function GraphEl(props: Props) {
autoSort: settings?.autoSort,
showOnlyActiveDate: settings?.showOnlyActiveDate,
dateFormat: settings?.dateFormat,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
};
case 'animatedHorizontalGroupedBarChart':
return {
Expand Down Expand Up @@ -604,6 +616,9 @@ function GraphEl(props: Props) {
autoPlay: settings?.autoPlay,
showOnlyActiveDate: settings?.showOnlyActiveDate,
dateFormat: settings?.dateFormat,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
sorParameter: settings?.sortParameter,
};
case 'animatedVerticalBarChart':
return {
Expand Down Expand Up @@ -658,6 +673,8 @@ function GraphEl(props: Props) {
autoSort: settings?.autoSort,
showOnlyActiveDate: settings?.showOnlyActiveDate,
dateFormat: settings?.dateFormat,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
};
case 'animatedVerticalGroupedBarChart':
return {
Expand Down Expand Up @@ -753,6 +770,9 @@ function GraphEl(props: Props) {
autoPlay: settings?.autoPlay,
showOnlyActiveDate: settings?.showOnlyActiveDate,
dateFormat: settings?.dateFormat,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
sorParameter: settings?.sortParameter,
};
case 'lineChart':
return {
Expand Down Expand Up @@ -1307,6 +1327,8 @@ function GraphEl(props: Props) {
arrowConnector: settings?.arrowConnector,
connectorStrokeWidth: settings?.connectorStrokeWidth,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
maxNumberOfBars: settings?.maxNumberOfBars,
};
case 'verticalDumbbellChart':
return {
Expand Down Expand Up @@ -1356,6 +1378,8 @@ function GraphEl(props: Props) {
arrowConnector: settings?.arrowConnector,
connectorStrokeWidth: settings?.connectorStrokeWidth,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
maxNumberOfBars: settings?.maxNumberOfBars,
};
case 'treeMap':
return {
Expand Down Expand Up @@ -2218,6 +2242,8 @@ function GraphEl(props: Props) {
autoPlay: settings?.autoPlay,
showOnlyActiveDate: settings?.showOnlyActiveDate,
dateFormat: settings?.dateFormat,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
};
case 'animatedVerticalDumbbellChart':
return {
Expand Down Expand Up @@ -2269,6 +2295,8 @@ function GraphEl(props: Props) {
autoPlay: settings?.autoPlay,
showOnlyActiveDate: settings?.showOnlyActiveDate,
dateFormat: settings?.dateFormat,
maxBarThickness: settings?.maxBarThickness,
minBarThickness: settings?.minBarThickness,
};
case 'animatedScatterPlot':
return {
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Dashboard/GriddedGraphs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function GriddedGraphs(props: Props) {
defaultValue: el.defaultValue,
availableValues: getUniqValue(d, el.column)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down Expand Up @@ -191,7 +191,7 @@ export function GriddedGraphs(props: Props) {
defaultValue: el.defaultValue,
availableValues: getUniqValue(d, el.column)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down Expand Up @@ -222,7 +222,7 @@ export function GriddedGraphs(props: Props) {
defaultValue: el.defaultValue,
availableValues: getUniqValue(tempData, el.column)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Dashboard/MultiGraphDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function MultiGraphDashboard(props: Props) {
defaultValue: el.defaultValue,
availableValues: getUniqValue(d, el.column)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down Expand Up @@ -150,7 +150,7 @@ export function MultiGraphDashboard(props: Props) {
defaultValue: el.defaultValue,
availableValues: getUniqValue(d, el.column)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand All @@ -176,7 +176,7 @@ export function MultiGraphDashboard(props: Props) {
defaultValue: el.defaultValue,
availableValues: getUniqValue(tempData, el.column)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down
18 changes: 17 additions & 1 deletion src/Components/Dashboard/MultiGraphDashboardWideToLongFormat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,23 @@ export function MultiGraphDashboardWideToLongFormat(props: Props) {
dataSettings={{
data,
}}
graphDataConfiguration={el.graphDataConfiguration}
graphDataConfiguration={
el.graphType === 'unitChart'
? [
{
columnId: 'indicator',
chartConfigId: 'label',
},
{ columnId: 'value', chartConfigId: 'value' },
]
: [
{
columnId: 'indicator',
chartConfigId: 'label',
},
{ columnId: 'value', chartConfigId: 'size' },
]
}
debugMode={debugMode}
readableHeader={readableHeader || []}
/>
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Dashboard/SingleGraphDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function SingleGraphDashboard(props: Props) {
el.column,
)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down Expand Up @@ -171,7 +171,7 @@ export function SingleGraphDashboard(props: Props) {
defaultValue: el.defaultValue,
availableValues: getUniqValue(d, el.column)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down Expand Up @@ -200,7 +200,7 @@ export function SingleGraphDashboard(props: Props) {
el.column,
)
.filter(v =>
el.excludeValues ? el.excludeValues.indexOf(v) !== -1 : true,
el.excludeValues ? el.excludeValues.indexOf(v) === -1 : true,
)
.map(v => ({
value: v,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ interface Props {
rtl: boolean;
language: 'en' | 'he' | 'ar';
mode: 'light' | 'dark';
maxBarThickness?: number;
minBarThickness?: number;
}

export function Graph(props: Props) {
Expand Down Expand Up @@ -81,6 +83,8 @@ export function Graph(props: Props) {
rtl,
language,
mode,
maxBarThickness,
minBarThickness,
} = props;
const [mouseOverData, setMouseOverData] = useState<any>(undefined);
const [eventX, setEventX] = useState<number | undefined>(undefined);
Expand Down Expand Up @@ -152,7 +156,14 @@ export function Graph(props: Props) {
.nice();
const y = scaleBand()
.domain(uniqLabels.map((_d, i) => `${i}`))
.range([0, graphHeight])
.range([
0,
minBarThickness
? Math.max(graphHeight, minBarThickness * uniqLabels.length)
: maxBarThickness
? Math.min(graphHeight, maxBarThickness * uniqLabels.length)
: graphHeight,
])
.paddingInner(barPadding);
const xTicks = x.ticks(5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ interface Props {
showNAColor?: boolean;
minHeight?: number;
mode?: 'light' | 'dark';
maxBarThickness?: number;
minBarThickness?: number;
}

export function AnimatedHorizontalBarChart(props: Props) {
Expand Down Expand Up @@ -113,6 +115,8 @@ export function AnimatedHorizontalBarChart(props: Props) {
showNAColor,
minHeight,
mode,
maxBarThickness,
minBarThickness,
} = props;

const [svgWidth, setSvgWidth] = useState(0);
Expand Down Expand Up @@ -400,6 +404,8 @@ export function AnimatedHorizontalBarChart(props: Props) {
rtl={checkIfNullOrUndefined(rtl) ? false : (rtl as boolean)}
language={language || (rtl ? 'ar' : 'en')}
mode={mode || 'light'}
maxBarThickness={maxBarThickness}
minBarThickness={minBarThickness}
/>
) : null}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ interface Props {
rtl: boolean;
language: 'en' | 'he' | 'ar';
mode: 'light' | 'dark';
sortParameter?: number | 'total';
maxBarThickness?: number;
minBarThickness?: number;
}

export function Graph(props: Props) {
Expand Down Expand Up @@ -75,6 +78,9 @@ export function Graph(props: Props) {
rtl,
language,
mode,
sortParameter,
maxBarThickness,
minBarThickness,
} = props;

const dataFormatted = sortBy(
Expand All @@ -92,21 +98,33 @@ export function Graph(props: Props) {
),
([date, values]) => ({
date,
values: autoSort
? sortBy(values, d => sum(d.size.map(el => el || 0)))
.reverse()
.map((el, i) => ({
values:
sortParameter !== undefined || autoSort
? sortParameter === 'total' || sortParameter === undefined
? sortBy(data, d => sum(d.size.filter(el => el !== undefined)))
.reverse()
.map((el, i) => ({
...el,
id: `${i}`,
}))
: sortBy(data, d =>
checkIfNullOrUndefined(d.size[sortParameter])
? -Infinity
: d.size[sortParameter],
)
.reverse()
.map((el, i) => ({
...el,
id: `${i}`,
}))
: (
uniqLabels.map(label =>
values.find(o => o.label === label),
) as GroupedBarGraphWithDateDataType[]
).map((el, i) => ({
...el,
id: `${i}`,
}))
: (
uniqLabels.map(label =>
values.find(o => o.label === label),
) as GroupedBarGraphWithDateDataType[]
).map((el, i) => ({
...el,
id: `${i}`,
})),
})),
}),
);

Expand All @@ -129,7 +147,14 @@ export function Graph(props: Props) {
const x = scaleLinear().domain([0, xMaxValue]).range([0, graphWidth]).nice();
const y = scaleBand()
.domain(uniqLabels.map((_d, i) => `${i}`))
.range([0, graphHeight])
.range([
0,
minBarThickness
? Math.max(graphHeight, minBarThickness * uniqLabels.length)
: maxBarThickness
? Math.min(graphHeight, maxBarThickness * uniqLabels.length)
: graphHeight,
])
.paddingInner(barPadding);
const xTicks = x.ticks(5);

Expand Down
Loading

0 comments on commit be9809d

Please sign in to comment.