From 4727c192539c0f7b29092a786ca270e3e0540e8f Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Thu, 27 Sep 2018 13:27:10 +0300 Subject: [PATCH] getredash/redash#2796 Improve counter text scaling (#2840) --- .../inc/visualizations/counter-render.less | 73 ++++++++----------- .../counter/counter-editor.html | 10 ++- .../app/visualizations/counter/counter.html | 27 +++---- client/app/visualizations/counter/index.js | 34 ++------- 4 files changed, 60 insertions(+), 84 deletions(-) diff --git a/client/app/assets/less/inc/visualizations/counter-render.less b/client/app/assets/less/inc/visualizations/counter-render.less index f123093e9..c57c297a9 100755 --- a/client/app/assets/less/inc/visualizations/counter-render.less +++ b/client/app/assets/less/inc/visualizations/counter-render.less @@ -3,52 +3,43 @@ counter-renderer { text-align: center; padding: 15px 10px; overflow: hidden; -} -counter-renderer counter { - margin: 0; - padding: 0; - display: block; - font-size: 80px; - overflow: hidden; - height: 200px; -} + counter { + margin: 0; + padding: 0; + font-size: 80px; + line-height: normal; + overflow: hidden; + display: flex; + align-items: center; + justify-content: center; -counter-renderer value, -counter-renderer counter-name, -counter-renderer counter-target { - font-size: 1em; - line-height: 1em; - display: block; -} + value, + counter-target { + font-size: 1em; + display: block; + } -counter-renderer value .ruler, -counter-renderer counter-name .ruler, -counter-renderer counter-target .ruler { - display: block; - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; - font: inherit; - line-height: inherit; - margin: 0; - padding: 0; -} + counter-name { + font-size: 0.5em; + display: block; + } -counter-renderer counter-target { - color: #ccc; -} + &.positive value { + color: #5cb85c; + } -counter-renderer counter.positive value { - color: #5cb85c; -} + &.negative value { + color: #d9534f; + } + } -counter-renderer counter.negative value { - color: #d9534f; - margin-right: 15px; -} + counter-target { + color: #ccc; + } -counter-renderer counter-name { - font-size: 0.5em; - display: block; + counter-name { + font-size: 0.5em; + display: block; + } } diff --git a/client/app/visualizations/counter/counter-editor.html b/client/app/visualizations/counter/counter-editor.html index 5d08136d4..d9b49ce73 100644 --- a/client/app/visualizations/counter/counter-editor.html +++ b/client/app/visualizations/counter/counter-editor.html @@ -7,7 +7,7 @@ Format -
+
@@ -36,13 +36,15 @@
- - Count Rows +
-
+
diff --git a/client/app/visualizations/counter/counter.html b/client/app/visualizations/counter/counter.html index dc8a5001c..dd7f56d45 100644 --- a/client/app/visualizations/counter/counter.html +++ b/client/app/visualizations/counter/counter.html @@ -1,16 +1,17 @@ - {{stringPrefix}}{{counterValue|number}}{{stringSuffix}} - {{stringPrefix}}{{counterValue}}{{stringSuffix}} - ({{targetValue|number}}) - {{visualization.name}} + resize-event="handleResize()" +> +
+ {{stringPrefix}}{{counterValue | number}}{{stringSuffix}} + {{stringPrefix}}{{counterValue}}{{stringSuffix}} + ({{targetValue | number}}) + {{visualization.name}} +
diff --git a/client/app/visualizations/counter/index.js b/client/app/visualizations/counter/index.js index 11a760f7f..a386c9e93 100644 --- a/client/app/visualizations/counter/index.js +++ b/client/app/visualizations/counter/index.js @@ -1,5 +1,5 @@ import numberFormat from 'underscore.string/numberFormat'; -import { isNumber, chain } from 'lodash'; +import { isNumber } from 'lodash'; import counterTemplate from './counter.html'; import counterEditorTemplate from './counter-editor.html'; @@ -23,33 +23,15 @@ function CounterRenderer($timeout) { link($scope, $element) { $scope.fontSize = '1em'; - const rootNode = $element[0].querySelector('counter'); + $scope.scale = 1; + const root = $element[0].querySelector('counter'); + const container = $element[0].querySelector('counter > div'); $scope.handleResize = () => { - const rootMeasures = { - height: Math.floor(rootNode.offsetHeight), - fontSize: parseFloat(window.getComputedStyle(rootNode).fontSize), - }; - const rulers = rootNode.querySelectorAll('.ruler'); - const rulerMeasures = chain(rulers) - .map(ruler => ({ - height: ruler.offsetHeight, - fontSize: parseFloat(window.getComputedStyle(ruler).fontSize), - })) - .reduce((result, value) => ({ - height: result.height + value.height, - fontSize: result.fontSize + value.fontSize, - })) - .value(); - - /* eslint-disable function-paren-newline */ - const fontSize = Math.floor( - rootMeasures.height / - rulerMeasures.height * - rulerMeasures.fontSize / - (rulerMeasures.fontSize / rootMeasures.fontSize), + const scale = Math.min( + root.offsetWidth / container.offsetWidth, + root.offsetHeight / container.offsetHeight, ); - /* eslint-enable function-paren-newline */ - $scope.fontSize = fontSize + 'px'; + $scope.scale = Math.floor(scale * 100) / 100; // keep only two decimal places }; const refreshData = () => {