Skip to content

Commit c8680db

Browse files
committed
Format percentages in area and bar charts nicely
1 parent 1f9d5c2 commit c8680db

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Types of changes include:
1212

1313
## Upcoming/Unreleased
1414

15+
- FIXED: Percentages in area and bar charts are now limited to 2 decimal places. (h/t Starlight)
16+
1517
## 1.3.3
1618

1719
- NEW: You can now view any chart as a bar chart, so you can see your total progress each day.

src/components/chart/BarChart.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { utcFormat } from 'd3-time-format';
77
import { useChartColors } from './chart-colors';
88
import { getChartDomain, orderSeries } from './chart-functions';
99
10-
import { kify } from 'src/lib/number';
10+
import { formatPercent, kify } from 'src/lib/number';
1111
import { formatDate, formatDuration, parseDateString } from 'src/lib/date';
1212
import { TallyMeasure, TALLY_MEASURE } from 'server/lib/models/tally/consts';
1313
import { TALLY_MEASURE_INFO, formatCount } from 'src/lib/tally';
@@ -41,7 +41,11 @@ const props = withDefaults(defineProps<{
4141
}));
4242
4343
function formatCountWithMeasureHint(value) {
44-
return formatCount(value, props.measureHint);
44+
if(props.measureHint === 'percent') {
45+
return formatPercent(value, 100) + '%';
46+
} else {
47+
return formatCount(value, props.measureHint);
48+
}
4549
}
4650
4751
const chartColors = useChartColors();
@@ -136,7 +140,7 @@ function renderChart() {
136140
format: {
137141
date: formatDate,
138142
series: seriesNames.size > 1,
139-
value: props.valueFormatFn ?? (props.measureHint === 'percent' ? d => `${d}%` : formatCountWithMeasureHint),
143+
value: props.valueFormatFn ?? formatCountWithMeasureHint,
140144
x: false,
141145
y: false,
142146
z: false,

src/components/chart/LineChart.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { utcFormat } from 'd3-time-format';
77
import { useChartColors } from './chart-colors';
88
import { getChartDomain, orderSeries } from './chart-functions';
99
10-
import { kify } from 'src/lib/number';
10+
import { formatPercent, kify } from 'src/lib/number';
1111
import { formatDate, formatDuration, parseDateString } from 'src/lib/date';
1212
import { TallyMeasure, TALLY_MEASURE } from 'server/lib/models/tally/consts';
1313
import { TALLY_MEASURE_INFO, formatCount } from 'src/lib/tally';
@@ -39,7 +39,11 @@ const props = withDefaults(defineProps<{
3939
}));
4040
4141
function formatCountWithMeasureHint(value) {
42-
return formatCount(value, props.measureHint);
42+
if(props.measureHint === 'percent') {
43+
return formatPercent(value, 100) + '%';
44+
} else {
45+
return formatCount(value, props.measureHint);
46+
}
4347
}
4448
4549
const chartColors = useChartColors();
@@ -136,7 +140,7 @@ function renderChart() {
136140
format: {
137141
date: formatDate,
138142
series: seriesNames.size > 1,
139-
value: props.valueFormatFn ?? (props.measureHint === 'percent' ? d => `${d}%` : formatCountWithMeasureHint),
143+
value: props.valueFormatFn ?? formatCountWithMeasureHint,
140144
x: false,
141145
y: false,
142146
z: false,

0 commit comments

Comments
 (0)