Clone SingleStat inside of Dygraph to get access to staticLegendHeight

This way the SingleStat doesn’t obscure the static legend if it is
present
pull/2891/head
Alex P 2018-02-28 15:00:48 -08:00
parent fcb15f10d2
commit 2f45dad9e4
4 changed files with 38 additions and 18 deletions

View File

@ -301,8 +301,9 @@ class Dygraph extends Component {
render() { render() {
const {isHidden, staticLegendHeight} = this.state const {isHidden, staticLegendHeight} = this.state
const {staticLegend} = this.props const {staticLegend, children} = this.props
const nestedGraph = (children && children.length && children[0]) || children
let dygraphStyle = {...this.props.containerStyle, zIndex: '2'} let dygraphStyle = {...this.props.containerStyle, zIndex: '2'}
if (staticLegend) { if (staticLegend) {
const cellVerticalPadding = 16 const cellVerticalPadding = 16
@ -344,12 +345,13 @@ class Dygraph extends Component {
this.handleReceiveStaticLegendHeight this.handleReceiveStaticLegendHeight
} }
/>} />}
{nestedGraph && React.cloneElement(nestedGraph, {staticLegendHeight})}
</div> </div>
) )
} }
} }
const {array, arrayOf, bool, func, shape, string} = PropTypes const {array, arrayOf, bool, func, node, shape, string} = PropTypes
Dygraph.defaultProps = { Dygraph.defaultProps = {
axes: { axes: {
@ -408,6 +410,7 @@ Dygraph.propTypes = {
dygraphRef: func, dygraphRef: func,
onZoom: func, onZoom: func,
mode: string, mode: string,
children: node,
} }
const mapStateToProps = ({annotations: {mode}}) => ({ const mapStateToProps = ({annotations: {mode}}) => ({

View File

@ -118,17 +118,18 @@ class LineGraph extends Component {
containerStyle={containerStyle} containerStyle={containerStyle}
staticLegend={staticLegend} staticLegend={staticLegend}
isGraphFilled={showSingleStat ? false : isGraphFilled} isGraphFilled={showSingleStat ? false : isGraphFilled}
/> >
{showSingleStat {showSingleStat
? <SingleStat ? <SingleStat
prefix={prefix} prefix={prefix}
suffix={suffix} suffix={suffix}
data={data} data={data}
lineGraph={true} lineGraph={true}
colors={colors} colors={colors}
cellHeight={cellHeight} cellHeight={cellHeight}
/> />
: null} : null}
</Dygraph>
</div> </div>
) )
} }

View File

@ -3,6 +3,7 @@ import classnames from 'classnames'
import lastValues from 'shared/parsing/lastValues' import lastValues from 'shared/parsing/lastValues'
import {SMALL_CELL_HEIGHT} from 'shared/graphs/helpers' import {SMALL_CELL_HEIGHT} from 'shared/graphs/helpers'
import {DYGRAPH_CONTAINER_V_MARGIN} from 'shared/constants'
import {SINGLE_STAT_TEXT} from 'src/dashboards/constants/gaugeColors' import {SINGLE_STAT_TEXT} from 'src/dashboards/constants/gaugeColors'
import {generateSingleStatHexs} from 'shared/constants/colorOperations' import {generateSingleStatHexs} from 'shared/constants/colorOperations'
@ -16,6 +17,7 @@ class SingleStat extends PureComponent {
prefix, prefix,
suffix, suffix,
lineGraph, lineGraph,
staticLegendHeight,
} = this.props } = this.props
// If data for this graph is being fetched for the first time, show a graph-wide spinner. // If data for this graph is being fetched for the first time, show a graph-wide spinner.
@ -39,11 +41,24 @@ class SingleStat extends PureComponent {
lastValue lastValue
) )
const backgroundColor = bgColor
const color = textColor
const height = `calc(100% - ${staticLegendHeight +
DYGRAPH_CONTAINER_V_MARGIN * 2}px)`
const singleStatStyles = staticLegendHeight
? {
backgroundColor,
color,
height,
}
: {
backgroundColor,
color,
}
return ( return (
<div <div className="single-stat" style={singleStatStyles}>
className="single-stat"
style={{backgroundColor: bgColor, color: textColor}}
>
<span <span
className={classnames('single-stat--value', { className={classnames('single-stat--value', {
'single-stat--small': cellHeight === SMALL_CELL_HEIGHT, 'single-stat--small': cellHeight === SMALL_CELL_HEIGHT,
@ -77,6 +92,7 @@ SingleStat.propTypes = {
prefix: string, prefix: string,
suffix: string, suffix: string,
lineGraph: bool, lineGraph: bool,
staticLegendHeight: number,
} }
export default SingleStat export default SingleStat

View File

@ -93,7 +93,7 @@
height: 100% !important; height: 100% !important;
} }
} }
.dygraph > .single-stat { .dygraph-child > .single-stat {
z-index: 3; z-index: 3;
} }
.single-stat--value { .single-stat--value {