From 5358c5fe6b0a966dcfe0495b47b06d18155ed647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 27 May 2019 12:13:08 +0200 Subject: [PATCH] Gauge/BarGauge: font size improvements (#17292) --- .../src/components/BarGauge/BarGauge.tsx | 24 +++++++++++++------ .../__snapshots__/BarGauge.test.tsx.snap | 3 ++- .../grafana-ui/src/components/Gauge/Gauge.tsx | 5 ++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx b/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx index 4351b6671d6..cb08d1b15ab 100644 --- a/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx +++ b/packages/grafana-ui/src/components/BarGauge/BarGauge.tsx @@ -11,8 +11,9 @@ import { DisplayValue, Themeable, TimeSeriesValue, Threshold, VizOrientation } f const MIN_VALUE_HEIGHT = 18; const MAX_VALUE_HEIGHT = 50; const MIN_VALUE_WIDTH = 50; -const MAX_VALUE_WIDTH = 100; -const LINE_HEIGHT = 1.5; +const MAX_VALUE_WIDTH = 150; +const TITLE_LINE_HEIGHT = 1.5; +const VALUE_LINE_HEIGHT = 1; export interface Props extends Themeable { height: number; @@ -227,7 +228,7 @@ function calculateTitleDimensions(props: Props): TitleDimensions { return { fontSize: 14, width: width, - height: 14 * LINE_HEIGHT, + height: 14 * TITLE_LINE_HEIGHT, placement: 'below', }; } @@ -238,7 +239,7 @@ function calculateTitleDimensions(props: Props): TitleDimensions { const titleHeight = Math.max(Math.min(height * maxTitleHeightRatio, MAX_VALUE_HEIGHT), 17); return { - fontSize: titleHeight / LINE_HEIGHT, + fontSize: titleHeight / TITLE_LINE_HEIGHT, width: 0, height: titleHeight, placement: 'above', @@ -251,7 +252,7 @@ function calculateTitleDimensions(props: Props): TitleDimensions { const titleHeight = Math.max(height * maxTitleHeightRatio, MIN_VALUE_HEIGHT); return { - fontSize: titleHeight / LINE_HEIGHT, + fontSize: titleHeight / TITLE_LINE_HEIGHT, height: 0, width: Math.min(Math.max(width * maxTitleWidthRatio, 50), 200), placement: 'left', @@ -485,7 +486,7 @@ export function getValueColor(props: Props): string { * Only exported to for unit test */ function getValueStyles(value: string, color: string, width: number, height: number): CSSProperties { - const heightFont = height / LINE_HEIGHT; + const heightFont = height / VALUE_LINE_HEIGHT; const guess = width / (value.length * 1.1); const fontSize = Math.min(Math.max(guess, 14), heightFont); @@ -495,6 +496,15 @@ function getValueStyles(value: string, color: string, width: number, height: num width: `${width}px`, display: 'flex', alignItems: 'center', - fontSize: fontSize.toFixed(2) + 'px', + lineHeight: VALUE_LINE_HEIGHT, + fontSize: fontSize.toFixed(4) + 'px', }; } + +// function getTextWidth(text: string): number { +// const canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas")); +// var context = canvas.getContext("2d"); +// context.font = "'Roboto', 'Helvetica Neue', Arial, sans-serif"; +// var metrics = context.measureText(text); +// return metrics.width; +// } diff --git a/packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap b/packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap index 4bb9395dd96..1d341a9b0d4 100644 --- a/packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap +++ b/packages/grafana-ui/src/components/BarGauge/__snapshots__/BarGauge.test.tsx.snap @@ -18,8 +18,9 @@ exports[`BarGauge Render with basic options should render 1`] = ` "alignItems": "center", "color": "#73BF69", "display": "flex", - "fontSize": "27.27px", + "fontSize": "27.2727px", "height": "300px", + "lineHeight": 1, "paddingLeft": "10px", "width": "60px", } diff --git a/packages/grafana-ui/src/components/Gauge/Gauge.tsx b/packages/grafana-ui/src/components/Gauge/Gauge.tsx index eb49891d298..0a0495c4848 100644 --- a/packages/grafana-ui/src/components/Gauge/Gauge.tsx +++ b/packages/grafana-ui/src/components/Gauge/Gauge.tsx @@ -58,7 +58,7 @@ export class Gauge extends PureComponent { if (length > 12) { return FONT_SCALE - (length * 5) / 110; } - return FONT_SCALE - (length * 5) / 100; + return FONT_SCALE - (length * 5) / 101; } draw() { @@ -78,7 +78,8 @@ export class Gauge extends PureComponent { const gaugeWidthReduceRatio = showThresholdLabels ? 1.5 : 1; const gaugeWidth = Math.min(dimension / 5.5, 40) / gaugeWidthReduceRatio; const thresholdMarkersWidth = gaugeWidth / 5; - const fontSize = Math.min(dimension / 5.5, 100) * (value.text !== null ? this.getFontScale(value.text.length) : 1); + const fontSize = Math.min(dimension / 4, 100) * (value.text !== null ? this.getFontScale(value.text.length) : 1); + const thresholdLabelFontSize = fontSize / 2.5; const options: any = {