Skip to content

Commit

Permalink
Fix: format value of survey charts tooltips (#2295)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden authored Jul 6, 2023
1 parent cbe5ecb commit c61bd2f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
3 changes: 2 additions & 1 deletion assets/js/components/charts/Forecasts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from "react"
import * as d3 from "d3"
import References from "./References"
import TimeAgo from "react-timeago"
import { percentFormat } from "./utils"

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

Expand Down Expand Up @@ -110,7 +111,7 @@ export default class Forecasts extends Component<Props> {
.style("stroke", data[i].color)
.on("mouseover", (d) =>
tooltip
.text(d.value)
.text(percentFormat(d.value / 100))
.style("top", d3.event.pageY - 10 + "px")
.style("left", d3.event.pageX + 10 + "px")
.style("visibility", "visible")
Expand Down
22 changes: 7 additions & 15 deletions assets/js/components/charts/SuccessRate.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { Component } from "react"
import * as d3 from "d3"
import Donut from "./Donut"
import classNames from "classnames"
import { translate } from "react-i18next"
import { numberFormat, labelFormat } from "./utils"

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

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

numberFormat(number) {
return number < 0.1 ? d3.format(".1%")(number) : d3.format(".0%")(number)
}

labelFormat(number) {
return number > 0 && number < 1 ? d3.format(".2f")(number) : d3.format(".0f")(number)
}

render() {
const { progress, weight, initial, actual, estimated, exhausted, t } = this.props
const zeroExhausted = exhausted == 0
Expand Down Expand Up @@ -86,14 +78,14 @@ class SuccessRate extends Component<Props> {
className="initial label"
transform={`translate(${-arcRadius - offset}, 0) rotate(-90)`}
>
{this.labelFormat(1 - progress)}
{labelFormat(1 - progress)}
</text>
<text
ref="foregroundLabel"
className="actual label hanging"
transform={`translate(${weight - arcRadius + offset}, 0) rotate(-90)`}
>
{this.labelFormat(progress)}
{labelFormat(progress)}
</text>
</g>
</g>
Expand Down Expand Up @@ -128,7 +120,7 @@ class SuccessRate extends Component<Props> {
{t("estimated success rate")}
</text>
<text x={arcRadius} y={arcRadius / 2} className="percent large">
{this.numberFormat(estimated)}
{numberFormat(estimated)}
</text>
<g
ref="progress"
Expand All @@ -146,7 +138,7 @@ class SuccessRate extends Component<Props> {
{t("Progress")}
</text>
<text x={donutRadius} y={donutRadius - margin.bottom} className="progress percent">
{this.numberFormat(progress)}
{numberFormat(progress)}
</text>
</g>
<g
Expand All @@ -166,7 +158,7 @@ class SuccessRate extends Component<Props> {
{t("initial success rate")}
</text>
<text x={donutRadius / 2} y={donutRadius / 2} className="initial percent middle">
{this.numberFormat(initial)}
{numberFormat(initial)}
</text>
</g>
<g
Expand Down Expand Up @@ -198,7 +190,7 @@ class SuccessRate extends Component<Props> {
"zero-exhausted": zeroExhausted,
})}
>
{this.numberFormat(actual)}
{numberFormat(actual)}
</text>
</g>
</g>
Expand Down
7 changes: 4 additions & 3 deletions assets/js/components/charts/SuccessRateLine.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from "react"
import * as d3 from "d3"
import { percentFormat } from "./utils"

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

Expand Down Expand Up @@ -105,13 +106,13 @@ export default class SuccessRateLine extends Component<Props> {
.style("fill", srData[i].color)
.style("stroke", srData[i].color)
.style("opacity", 0.1)
.on("mouseover", (d) =>
.on("mouseover", (d) => {
tooltip
.text(d.value)
.text(percentFormat(d.value / 100))
.style("top", d3.event.pageY - 10 + "px")
.style("left", d3.event.pageX + 10 + "px")
.style("visibility", "visible")
)
})
.on("mouseout", () => tooltip.style("visibility", "hidden"))
}
}
Expand Down
14 changes: 14 additions & 0 deletions assets/js/components/charts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as d3 from "d3"

export function percentFormat(number) {
return d3.format(".1%")(number)
}

export function numberFormat(number) {
return number < 0.1 ? d3.format(".1%")(number) : d3.format(".0%")(number)
}

export function labelFormat(number) {
return number > 0 && number < 1 ? d3.format(".2f")(number) : d3.format(".0f")(number)
}

0 comments on commit c61bd2f

Please sign in to comment.