From 5e071375a9a4fd6e48e0a8d4de08b66a0e559be5 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 20 Nov 2017 21:51:22 -0800 Subject: [PATCH] WIP Add gauge controls in display options --- .../dashboards/components/DisplayOptions.js | 25 ++- ui/src/dashboards/components/GaugeOptions.js | 183 ++++++++++++++++++ ui/src/dashboards/constants/gaugeColors.js | 1 + .../style/components/ceo-display-options.scss | 49 +++++ 4 files changed, 248 insertions(+), 10 deletions(-) create mode 100644 ui/src/dashboards/components/GaugeOptions.js diff --git a/ui/src/dashboards/components/DisplayOptions.js b/ui/src/dashboards/components/DisplayOptions.js index 57e51a215e..617a36e21e 100644 --- a/ui/src/dashboards/components/DisplayOptions.js +++ b/ui/src/dashboards/components/DisplayOptions.js @@ -1,6 +1,7 @@ import React, {Component, PropTypes} from 'react' import GraphTypeSelector from 'src/dashboards/components/GraphTypeSelector' +import GaugeOptions from 'src/dashboards/components/GaugeOptions' import AxesOptions from 'src/dashboards/components/AxesOptions' import {buildDefaultYLabel} from 'shared/presenters' @@ -44,22 +45,26 @@ class DisplayOptions extends Component { } = this.props const {axes} = this.state + const isGauge = selectedGraphType === 'gauge' + return (
- + {isGauge + ? + : }
) } diff --git a/ui/src/dashboards/components/GaugeOptions.js b/ui/src/dashboards/components/GaugeOptions.js new file mode 100644 index 0000000000..1452d99a82 --- /dev/null +++ b/ui/src/dashboards/components/GaugeOptions.js @@ -0,0 +1,183 @@ +import React, {Component, PropTypes} from 'react' + +import FancyScrollbar from 'shared/components/FancyScrollbar' +import ColorDropdown from 'shared/components/ColorDropdown' +import uuid from 'node-uuid' + +import { + MAX_THRESHOLDS, + GAUGE_COLORS, +} from 'src/dashboards/constants/gaugeColors' + +class GaugeOptions extends Component { + constructor(props) { + super(props) + + this.state = { + minValue: this.props.config.minValue, + minColor: this.props.config.minColor, + maxValue: this.props.config.maxValue, + maxColor: this.props.config.maxColor, + thresholds: this.props.config.thresholds, + } + } + + handleChooseMinColor = color => { + this.setState({minColor: color}) + } + + handleChooseMaxColor = color => { + this.setState({maxColor: color}) + } + + handleAddThreshold = () => { + const {minValue, thresholds} = this.state + const randomColor = Math.floor(Math.random() * GAUGE_COLORS.length) + + const newThreshold = { + id: uuid.v4(), + value: minValue + 1, + hex: GAUGE_COLORS[randomColor].hex, + text: GAUGE_COLORS[randomColor].text, + } + thresholds.push(newThreshold) + + this.setState([thresholds]) + } + + handleDeleteThreshold = threshold => () => { + const {thresholds} = this.state + + const filteredThresholds = thresholds.filter(t => { + return t.id !== threshold.id + }) + + this.setState({thresholds: filteredThresholds}) + } + + handleChooseColor = threshold => newColor => { + threshold.hex = newColor.hex + threshold.text = newColor.text + } + + handleChangeValue = threshold => e => { + threshold.value = Number(e.target.value) + } + + render() { + const {minValue, minColor, maxValue, maxColor, thresholds} = this.state + + const disableMaxColor = thresholds.length > 0 + + const disableAddThreshold = thresholds.length > MAX_THRESHOLDS + + return ( + +
+
Gauge Controls
+
+
+
Minimum
+ + +
+ {thresholds.length > 0 && + thresholds.map(threshold => +
+
+ Threshold +
+ + + +
+ )} +
+
Maximum
+ + +
+ +
+
+
+ ) + } +} + +const {arrayOf, func, number, shape, string} = PropTypes + +GaugeOptions.defaultProps = { + config: { + minValue: 0, + minColor: GAUGE_COLORS[2], + maxValue: 100, + maxColor: GAUGE_COLORS[13], + thresholds: [], + }, +} + +GaugeOptions.propTypes = { + config: shape({ + minValue: number.isRequired, + minColor: shape({hex: string.isRequired, text: string.isRequired}) + .isRequired, + maxValue: number.isRequired, + maxColor: shape({hex: string.isRequired, text: string.isRequired}) + .isRequired, + thresholds: arrayOf( + shape({ + value: number.isRequired, + hex: string.isRequired, + text: string.isRequired, + }) + ), + }), + onUpdateOptions: func, +} + +export default GaugeOptions diff --git a/ui/src/dashboards/constants/gaugeColors.js b/ui/src/dashboards/constants/gaugeColors.js index 9072288602..30018c80ee 100644 --- a/ui/src/dashboards/constants/gaugeColors.js +++ b/ui/src/dashboards/constants/gaugeColors.js @@ -1,3 +1,4 @@ +export const MAX_THRESHOLDS = 3 export const GAUGE_COLORS = [ { hex: '#BF3D5E', diff --git a/ui/src/style/components/ceo-display-options.scss b/ui/src/style/components/ceo-display-options.scss index b05705e328..c2c7ea7f03 100644 --- a/ui/src/style/components/ceo-display-options.scss +++ b/ui/src/style/components/ceo-display-options.scss @@ -195,3 +195,52 @@ $graph-type--gutter: 4px; padding-left: 6px; @include no-user-select(); } + + + +/* + Cell Editor Overlay - Gauge Controls + ------------------------------------------------------ +*/ +.gauge-controls { + width: 100%; +} + +.gauge-controls--section { + width: 100%; + display: flex; + flex-wrap: nowrap; + align-items: center; + height: 30px; + margin-bottom: 8px; +} +button.btn.btn-primary.btn-sm.gauge-controls--add-threshold { + width: 100%; +} + +.gauge-controls--label { + height: 30px; + background-color: $g4-onyx; + font-weight: 600; + color: $g11-sidewalk; + padding: 0 11px; + border-radius: 4px; + line-height: 30px; + @include no-user-select(); + width: 120px; +} +.gauge-controls--label-editable { + height: 30px; + font-weight: 600; + color: $g16-pearl; + padding: 0 11px; + border-radius: 4px; + line-height: 30px; + @include no-user-select(); + width: 90px; +} + +.gauge-controls--input { + flex: 1 0 0; + margin: 0 4px; +}