From c61bd2f8660e43905f1cc6bd99c25fe19cbc233d Mon Sep 17 00:00:00 2001 From: Julien Portalier Date: Thu, 6 Jul 2023 16:42:01 +0200 Subject: [PATCH] Fix: format value of survey charts tooltips (#2295) --- assets/js/components/charts/Forecasts.jsx | 3 ++- assets/js/components/charts/SuccessRate.jsx | 22 ++++++------------- .../js/components/charts/SuccessRateLine.jsx | 7 +++--- assets/js/components/charts/utils.js | 14 ++++++++++++ 4 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 assets/js/components/charts/utils.js diff --git a/assets/js/components/charts/Forecasts.jsx b/assets/js/components/charts/Forecasts.jsx index ccc21611b..7fbb39dab 100644 --- a/assets/js/components/charts/Forecasts.jsx +++ b/assets/js/components/charts/Forecasts.jsx @@ -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 } @@ -110,7 +111,7 @@ export default class Forecasts extends Component { .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") diff --git a/assets/js/components/charts/SuccessRate.jsx b/assets/js/components/charts/SuccessRate.jsx index ec7c480f8..72afee763 100644 --- a/assets/js/components/charts/SuccessRate.jsx +++ b/assets/js/components/charts/SuccessRate.jsx @@ -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 } @@ -45,14 +45,6 @@ class SuccessRate extends Component { 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 @@ -86,14 +78,14 @@ class SuccessRate extends Component { className="initial label" transform={`translate(${-arcRadius - offset}, 0) rotate(-90)`} > - {this.labelFormat(1 - progress)} + {labelFormat(1 - progress)} - {this.labelFormat(progress)} + {labelFormat(progress)} @@ -128,7 +120,7 @@ class SuccessRate extends Component { {t("estimated success rate")} - {this.numberFormat(estimated)} + {numberFormat(estimated)} { {t("Progress")} - {this.numberFormat(progress)} + {numberFormat(progress)} { {t("initial success rate")} - {this.numberFormat(initial)} + {numberFormat(initial)} { "zero-exhausted": zeroExhausted, })} > - {this.numberFormat(actual)} + {numberFormat(actual)} diff --git a/assets/js/components/charts/SuccessRateLine.jsx b/assets/js/components/charts/SuccessRateLine.jsx index 69c43fefc..89cef9455 100644 --- a/assets/js/components/charts/SuccessRateLine.jsx +++ b/assets/js/components/charts/SuccessRateLine.jsx @@ -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 } @@ -105,13 +106,13 @@ export default class SuccessRateLine extends Component { .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")) } } diff --git a/assets/js/components/charts/utils.js b/assets/js/components/charts/utils.js new file mode 100644 index 000000000..369981443 --- /dev/null +++ b/assets/js/components/charts/utils.js @@ -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) +} +