Skip to content

Commit

Permalink
Add more feautre
Browse files Browse the repository at this point in the history
Add following features:
Customize the no. of ticks
Add axis label for all the chart
Add color legend for interval chart
Customize the color of the values
Add suffix and prefix on the tick values
Add min and max date for all line  charts
  • Loading branch information
mustafasaifee42 committed Jan 3, 2025
1 parent 77e9b04 commit 555bbbb
Show file tree
Hide file tree
Showing 61 changed files with 1,261 additions and 436 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": "1.0.29",
"version": "1.0.30",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"browser": "./dist/index.umd.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ interface Props {
resetSelectionOnDoubleClick: boolean;
tooltipBackgroundStyle: CSSObject;
detailsOnClick?: string;
xAxisTitle?: string;
noOfTicks: number;
valueColor?: string;
}

export function Graph(props: Props) {
Expand Down Expand Up @@ -95,13 +98,16 @@ export function Graph(props: Props) {
resetSelectionOnDoubleClick,
tooltipBackgroundStyle,
detailsOnClick,
xAxisTitle,
valueColor,
noOfTicks,
} = props;
const [mouseOverData, setMouseOverData] = useState<any>(undefined);
const [eventX, setEventX] = useState<number | undefined>(undefined);
const [eventY, setEventY] = useState<number | undefined>(undefined);
const [mouseClickData, setMouseClickData] = useState<any>(undefined);
const margin = {
top: topMargin,
top: xAxisTitle ? topMargin + 25 : topMargin,
bottom: bottomMargin,
left: leftMargin,
right: rightMargin,
Expand Down Expand Up @@ -176,7 +182,7 @@ export function Graph(props: Props) {
: graphHeight,
])
.paddingInner(barPadding);
const xTicks = x.ticks(5);
const xTicks = x.ticks(noOfTicks);

return (
<>
Expand All @@ -192,7 +198,7 @@ export function Graph(props: Props) {
<line
x1={x(d)}
x2={x(d)}
y1={0 - margin.top}
y1={0 - topMargin}
y2={graphHeight + margin.bottom + margin.top}
style={{
stroke:
Expand All @@ -204,7 +210,7 @@ export function Graph(props: Props) {
/>
<text
x={x(d)}
y={0 - margin.top}
y={0 - topMargin}
textAnchor='start'
fontSize={12}
dy={10}
Expand All @@ -219,11 +225,29 @@ export function Graph(props: Props) {
fill: UNDPColorModule[mode || 'light'].grays['gray-500'],
}}
>
{numberFormattingFunction(d, '', '')}
{numberFormattingFunction(d, prefix, suffix)}
</text>
</g>
))
: null}
{xAxisTitle ? (
<text
transform={`translate(${graphWidth / 2}, ${0 - margin.top})`}
style={{
fill: UNDPColorModule[mode || 'light'].grays['gray-700'],
fontFamily: rtl
? language === 'he'
? 'Noto Sans Hebrew, sans-serif'
: 'Noto Sans Arabic, sans-serif'
: 'ProximaNova, proxima-nova, Helvetica Neue, Roboto, sans-serif',
}}
textAnchor='middle'
dy={15}
fontSize={12}
>
{xAxisTitle}
</text>
) : null}
<AnimatePresence>
{groupedData[indx].values.map(d => (
<motion.g
Expand Down Expand Up @@ -342,9 +366,10 @@ export function Graph(props: Props) {
y={0}
style={{
fill:
barColor.length > 1
valueColor ||
(barColor.length > 1
? UNDPColorModule[mode || 'light'].grays['gray-600']
: barColor[0],
: barColor[0]),
fontSize: '0.875rem',
textAnchor: d.size
? d.size < 0
Expand All @@ -365,11 +390,7 @@ export function Graph(props: Props) {
dy={5}
transition={{ duration: 0.5 }}
>
{numberFormattingFunction(
d.size,
prefix || '',
suffix || '',
)}
{numberFormattingFunction(d.size, prefix, suffix)}
</motion.text>
) : null}
</motion.g>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ interface Props {
resetSelectionOnDoubleClick?: boolean;
tooltipBackgroundStyle?: CSSObject;
detailsOnClick?: string;
xAxisTitle?: string;
noOfTicks?: number;
valueColor?: string;
}

export function AnimatedHorizontalBarChart(props: Props) {
Expand Down Expand Up @@ -130,6 +133,9 @@ export function AnimatedHorizontalBarChart(props: Props) {
padding: '0.5rem',
},
detailsOnClick,
xAxisTitle,
noOfTicks = 5,
valueColor,
} = props;

const [svgWidth, setSvgWidth] = useState(0);
Expand Down Expand Up @@ -384,6 +390,9 @@ export function AnimatedHorizontalBarChart(props: Props) {
resetSelectionOnDoubleClick={resetSelectionOnDoubleClick}
tooltipBackgroundStyle={tooltipBackgroundStyle}
detailsOnClick={detailsOnClick}
xAxisTitle={xAxisTitle}
noOfTicks={noOfTicks}
valueColor={valueColor}
/>
) : null}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ interface Props {
resetSelectionOnDoubleClick: boolean;
tooltipBackgroundStyle: CSSObject;
detailsOnClick?: string;
xAxisTitle?: string;
noOfTicks: number;
valueColor?: string;
}

export function Graph(props: Props) {
Expand Down Expand Up @@ -88,6 +91,9 @@ export function Graph(props: Props) {
resetSelectionOnDoubleClick,
tooltipBackgroundStyle,
detailsOnClick,
xAxisTitle,
valueColor,
noOfTicks,
} = props;

const dataFormatted = sortBy(
Expand Down Expand Up @@ -117,7 +123,7 @@ export function Graph(props: Props) {
);

const margin = {
top: topMargin,
top: xAxisTitle ? topMargin + 25 : topMargin,
bottom: bottomMargin,
left: leftMargin,
right: rightMargin,
Expand Down Expand Up @@ -161,7 +167,7 @@ export function Graph(props: Props) {
.domain(data[0].size.map((_d, i) => `${i}`))
.range([0, y.bandwidth()])
.paddingInner(0.1);
const xTicks = x.ticks(5);
const xTicks = x.ticks(noOfTicks);
return (
<>
<svg
Expand All @@ -173,27 +179,11 @@ export function Graph(props: Props) {
{showTicks
? xTicks.map((d, i) => (
<g key={i}>
<text
x={x(d)}
y={-12.5}
style={{
fill: UNDPColorModule[mode || 'light'].grays['gray-500'],
fontFamily: rtl
? language === 'he'
? 'Noto Sans Hebrew, sans-serif'
: 'Noto Sans Arabic, sans-serif'
: 'ProximaNova, proxima-nova, Helvetica Neue, Roboto, sans-serif',
}}
textAnchor='middle'
fontSize={12}
>
{numberFormattingFunction(d, '', '')}
</text>
<line
x1={x(d)}
x2={x(d)}
y1={-2.5}
y2={graphHeight + margin.bottom}
y1={0 - topMargin}
y2={graphHeight + margin.bottom + margin.top}
style={{
stroke:
UNDPColorModule[mode || 'light'].grays['gray-500'],
Expand All @@ -202,9 +192,46 @@ export function Graph(props: Props) {
strokeDasharray='4,8'
opacity={d === 0 ? 0 : 1}
/>
<text
x={x(d)}
y={0 - topMargin}
textAnchor='start'
fontSize={12}
dy={10}
dx={3}
opacity={d === 0 ? 0 : 1}
style={{
fontFamily: rtl
? language === 'he'
? 'Noto Sans Hebrew, sans-serif'
: 'Noto Sans Arabic, sans-serif'
: 'ProximaNova, proxima-nova, Helvetica Neue, Roboto, sans-serif',
fill: UNDPColorModule[mode || 'light'].grays['gray-500'],
}}
>
{numberFormattingFunction(d, prefix, suffix)}
</text>
</g>
))
: null}
{xAxisTitle ? (
<text
transform={`translate(${graphWidth / 2}, ${0 - margin.top})`}
style={{
fill: UNDPColorModule[mode || 'light'].grays['gray-700'],
fontFamily: rtl
? language === 'he'
? 'Noto Sans Hebrew, sans-serif'
: 'Noto Sans Arabic, sans-serif'
: 'ProximaNova, proxima-nova, Helvetica Neue, Roboto, sans-serif',
}}
textAnchor='middle'
dy={15}
fontSize={12}
>
{xAxisTitle}
</text>
) : null}
<AnimatePresence>
{groupedData[indx].values.map((d, i) => {
return (
Expand Down Expand Up @@ -282,7 +309,7 @@ export function Graph(props: Props) {
subBarScale.bandwidth() / 2
}
style={{
fill: barColors[j],
fill: valueColor || barColors[j],
fontSize: '0.875rem',
textAnchor: el
? el < 0
Expand All @@ -302,11 +329,7 @@ export function Graph(props: Props) {
}}
transition={{ duration: 0.5 }}
>
{numberFormattingFunction(
el,
prefix || '',
suffix || '',
)}
{numberFormattingFunction(el, prefix, suffix)}
</motion.text>
) : null}
</motion.g>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ interface Props {
resetSelectionOnDoubleClick?: boolean;
tooltipBackgroundStyle?: CSSObject;
detailsOnClick?: string;
xAxisTitle?: string;
noOfTicks?: number;
valueColor?: string;
}

export function AnimatedHorizontalGroupedBarGraph(props: Props) {
Expand Down Expand Up @@ -120,6 +123,9 @@ export function AnimatedHorizontalGroupedBarGraph(props: Props) {
padding: '0.5rem',
},
detailsOnClick,
xAxisTitle,
noOfTicks = 5,
valueColor,
} = props;
const [svgWidth, setSvgWidth] = useState(0);
const [svgHeight, setSvgHeight] = useState(0);
Expand Down Expand Up @@ -338,6 +344,9 @@ export function AnimatedHorizontalGroupedBarGraph(props: Props) {
resetSelectionOnDoubleClick={resetSelectionOnDoubleClick}
tooltipBackgroundStyle={tooltipBackgroundStyle}
detailsOnClick={detailsOnClick}
xAxisTitle={xAxisTitle}
noOfTicks={noOfTicks}
valueColor={valueColor}
/>
) : null}
</div>
Expand Down
Loading

0 comments on commit 555bbbb

Please sign in to comment.