Skip to content

Commit 7fa34a9

Browse files
committed
format y-scale ticks
1 parent 4c39b0a commit 7fa34a9

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/components/graphs/CountBarChart.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ const CountBarChart = ({ column, realData }: CountBarChartProps) => {
1616
const [containerWidth, setContainerWidth] = useState(800);
1717
const { t } = useTranslation();
1818

19+
const formatTick = (value: number) => {
20+
if (value >= 1000000) {
21+
return `${(value / 1000000).toFixed(1)}M`;
22+
} else if (value >= 1000) {
23+
return `${(value / 1000).toFixed(1)}K`;
24+
}
25+
return value.toString();
26+
};
27+
1928
useEffect(() => {
2029
const plotWidth = containerWidth - margin.left - margin.right;
2130
const plotHeight = height - margin.top - margin.bottom;
@@ -86,7 +95,7 @@ const CountBarChart = ({ column, realData }: CountBarChartProps) => {
8695
.style('fill', 'steelblue')
8796
.style('opacity', 0.5);
8897

89-
// Add axes
98+
// Add axes with formatted ticks
9099
svg.append('g')
91100
.attr('transform', `translate(0,${plotHeight})`)
92101
.call(d3.axisBottom(xScale))
@@ -96,7 +105,12 @@ const CountBarChart = ({ column, realData }: CountBarChartProps) => {
96105
.attr('dx', '-.8em')
97106
.attr('dy', '.15em');
98107

99-
svg.append('g').call(d3.axisLeft(yScale));
108+
svg.append('g').call(
109+
d3
110+
.axisLeft(yScale)
111+
.ticks(5) // Reduce number of ticks
112+
.tickFormat(d => formatTick(d as number))
113+
);
100114

101115
// Add title
102116
svg.append('text')

0 commit comments

Comments
 (0)