Skip to content

Commit 1d24132

Browse files
committed
Fix: format value of survey charts tooltips (#2295)
1 parent 207d4a4 commit 1d24132

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

assets/js/components/charts/Forecasts.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from "react"
22
import * as d3 from "d3"
33
import References from "./References"
44
import TimeAgo from "react-timeago"
5+
import { percentFormat } from "./utils"
56

67
const margin = { left: 36, top: 18, right: 18, bottom: 36 }
78

@@ -110,7 +111,7 @@ export default class Forecasts extends Component<Props> {
110111
.style("stroke", data[i].color)
111112
.on("mouseover", (d) =>
112113
tooltip
113-
.text(d.value)
114+
.text(percentFormat(d.value / 100))
114115
.style("top", d3.event.pageY - 10 + "px")
115116
.style("left", d3.event.pageX + 10 + "px")
116117
.style("visibility", "visible")

assets/js/components/charts/SuccessRate.jsx

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { Component } from "react"
2-
import * as d3 from "d3"
32
import Donut from "./Donut"
43
import classNames from "classnames"
54
import { translate } from "react-i18next"
5+
import { numberFormat, labelFormat } from "./utils"
66

77
const margin = { left: 18, top: 18, right: 18, bottom: 18 }
88

@@ -45,14 +45,6 @@ class SuccessRate extends Component<Props> {
4545
window.removeEventListener("resize", this.recalculate)
4646
}
4747

48-
numberFormat(number) {
49-
return number < 0.1 ? d3.format(".1%")(number) : d3.format(".0%")(number)
50-
}
51-
52-
labelFormat(number) {
53-
return number > 0 && number < 1 ? d3.format(".2f")(number) : d3.format(".0f")(number)
54-
}
55-
5648
render() {
5749
const { progress, weight, initial, actual, estimated, exhausted, t } = this.props
5850
const zeroExhausted = exhausted == 0
@@ -86,14 +78,14 @@ class SuccessRate extends Component<Props> {
8678
className="initial label"
8779
transform={`translate(${-arcRadius - offset}, 0) rotate(-90)`}
8880
>
89-
{this.labelFormat(1 - progress)}
81+
{labelFormat(1 - progress)}
9082
</text>
9183
<text
9284
ref="foregroundLabel"
9385
className="actual label hanging"
9486
transform={`translate(${weight - arcRadius + offset}, 0) rotate(-90)`}
9587
>
96-
{this.labelFormat(progress)}
88+
{labelFormat(progress)}
9789
</text>
9890
</g>
9991
</g>
@@ -128,7 +120,7 @@ class SuccessRate extends Component<Props> {
128120
{t("estimated success rate")}
129121
</text>
130122
<text x={arcRadius} y={arcRadius / 2} className="percent large">
131-
{this.numberFormat(estimated)}
123+
{numberFormat(estimated)}
132124
</text>
133125
<g
134126
ref="progress"
@@ -146,7 +138,7 @@ class SuccessRate extends Component<Props> {
146138
{t("Progress")}
147139
</text>
148140
<text x={donutRadius} y={donutRadius - margin.bottom} className="progress percent">
149-
{this.numberFormat(progress)}
141+
{numberFormat(progress)}
150142
</text>
151143
</g>
152144
<g
@@ -166,7 +158,7 @@ class SuccessRate extends Component<Props> {
166158
{t("initial success rate")}
167159
</text>
168160
<text x={donutRadius / 2} y={donutRadius / 2} className="initial percent middle">
169-
{this.numberFormat(initial)}
161+
{numberFormat(initial)}
170162
</text>
171163
</g>
172164
<g
@@ -198,7 +190,7 @@ class SuccessRate extends Component<Props> {
198190
"zero-exhausted": zeroExhausted,
199191
})}
200192
>
201-
{this.numberFormat(actual)}
193+
{numberFormat(actual)}
202194
</text>
203195
</g>
204196
</g>

assets/js/components/charts/SuccessRateLine.jsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from "react"
22
import * as d3 from "d3"
3+
import { percentFormat } from "./utils"
34

45
const margin = { left: 36, top: 18, right: 18, bottom: 36 }
56

@@ -105,13 +106,13 @@ export default class SuccessRateLine extends Component<Props> {
105106
.style("fill", srData[i].color)
106107
.style("stroke", srData[i].color)
107108
.style("opacity", 0.1)
108-
.on("mouseover", (d) =>
109+
.on("mouseover", (d) => {
109110
tooltip
110-
.text(d.value)
111+
.text(percentFormat(d.value / 100))
111112
.style("top", d3.event.pageY - 10 + "px")
112113
.style("left", d3.event.pageX + 10 + "px")
113114
.style("visibility", "visible")
114-
)
115+
})
115116
.on("mouseout", () => tooltip.style("visibility", "hidden"))
116117
}
117118
}

assets/js/components/charts/utils.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as d3 from "d3"
2+
3+
export function percentFormat(number) {
4+
return d3.format(".1%")(number)
5+
}
6+
7+
export function numberFormat(number) {
8+
return number < 0.1 ? d3.format(".1%")(number) : d3.format(".0%")(number)
9+
}
10+
11+
export function labelFormat(number) {
12+
return number > 0 && number < 1 ? d3.format(".2f")(number) : d3.format(".0f")(number)
13+
}
14+

0 commit comments

Comments
 (0)