Skip to content

Commit 759b04d

Browse files
Add feature to add year on multiline chart
1 parent c177821 commit 759b04d

5 files changed

Lines changed: 45 additions & 20 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@undp/data-viz",
3-
"version": "2.3.16",
3+
"version": "2.3.17",
44
"main": "./dist/index.cjs",
55
"module": "./dist/index.js",
66
"browser": "./dist/index.umd.js",

src/Components/Graphs/LineCharts/MultiLineAltChart/Graph.tsx

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ interface Props {
9090
customLayers: CustomLayerDataType[];
9191
naLabel: string;
9292
showHighlightedLinesLabels: boolean;
93+
showDateOnHover: boolean;
9394
}
9495

9596
interface FormattedDataType {
@@ -140,6 +141,7 @@ export function Graph(props: Props) {
140141
naLabel,
141142
customLayers,
142143
showHighlightedLinesLabels,
144+
showDateOnHover,
143145
} = props;
144146
const svgRef = useRef(null);
145147
const isInView = useInView(svgRef, {
@@ -575,23 +577,41 @@ export function Graph(props: Props) {
575577
</motion.g>
576578
))}
577579
{mouseOverData ? (
578-
<text
579-
y={y(mouseOverData.y) - 8}
580-
x={x(mouseOverData.date)}
581-
className={cn('graph-value text-sm font-bold', classNames?.graphObjectValues)}
582-
style={{
583-
fill:
584-
data.filter(el => el.color).length === 0
585-
? lineColors[0]
586-
: !mouseOverData.color
587-
? Colors.gray
588-
: lineColors[colorDomain.indexOf(mouseOverData.color)],
589-
textAnchor: 'middle',
590-
...(styles?.graphObjectValues || {}),
591-
}}
592-
>
593-
{numberFormattingFunction(mouseOverData.y, naLabel, precision, prefix, suffix)}
594-
</text>
580+
<>
581+
<text
582+
y={y(mouseOverData.y) - 8}
583+
x={x(mouseOverData.date)}
584+
className={cn('graph-value text-sm font-bold', classNames?.graphObjectValues)}
585+
style={{
586+
fill:
587+
data.filter(el => el.color).length === 0
588+
? lineColors[0]
589+
: !mouseOverData.color
590+
? Colors.gray
591+
: lineColors[colorDomain.indexOf(mouseOverData.color)],
592+
textAnchor: 'middle',
593+
...(styles?.graphObjectValues || {}),
594+
}}
595+
>
596+
{numberFormattingFunction(mouseOverData.y, naLabel, precision, prefix, suffix)}
597+
</text>
598+
{showDateOnHover && (
599+
<text
600+
y={y(mouseOverData.y) + 20}
601+
x={x(mouseOverData.date)}
602+
className={cn(
603+
'graph-value text-sm fill-primary-gray-500 dark:fill-primary-gray-400',
604+
classNames?.graphObjectValues,
605+
)}
606+
style={{
607+
textAnchor: 'middle',
608+
...(styles?.graphObjectValues || {}),
609+
}}
610+
>
611+
{format(new Date(mouseOverData.date), dateFormat || 'yyyy')}
612+
</text>
613+
)}
614+
</>
595615
) : null}
596616
</motion.g>
597617
{dataFormatted

src/Components/Graphs/LineCharts/MultiLineAltChart/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ interface Props {
113113
showColorScale?: boolean;
114114
/** Toggle visibility of NA color in the color scale. This is only applicable if the data props hae color parameter and showColorScale prop is true */
115115
showNAColor?: boolean;
116+
/** Toggle visibility of year labels on hover. */
117+
showDateOnHover?: boolean;
116118
/** Defines how “NA” values should be displayed/labelled in the graph */
117119
naLabel?: string;
118120
/** Data points to highlight. Use the label value from data to highlight the data point */
@@ -212,6 +214,7 @@ export function MultiLineAltChart(props: Props) {
212214
precision = 2,
213215
customLayers = [],
214216
naLabel = 'NA',
217+
showDateOnHover = false,
215218
} = props;
216219

217220
const [svgWidth, setSvgWidth] = useState(0);
@@ -339,6 +342,7 @@ export function MultiLineAltChart(props: Props) {
339342
precision={precision}
340343
customLayers={customLayers}
341344
naLabel={naLabel}
345+
showDateOnHover={showDateOnHover}
342346
/>
343347
) : null}
344348
</GraphArea>

src/stories/Graph+Maps+Charts/LineCharts/MultiLineAltChart.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ const meta: Meta<PagePropsAndCustomArgs> = {
185185
graphDownload: { table: { defaultValue: { summary: 'false' } } },
186186
highlightedLines: { control: 'text' },
187187
dataDownload: { table: { defaultValue: { summary: 'false' } } },
188+
showDateOnHover: { control: 'boolean', table: { defaultValue: { summary: 'false' } } },
188189

189190
// Interactions and Callbacks
190191
onSeriesMouseOver: { action: 'seriesMouseOver' },

0 commit comments

Comments
 (0)