From b0f00c074ec4804897362d2e14394d3a3980dd8b Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Thu, 12 Apr 2018 15:54:07 -0700 Subject: [PATCH] WIP legened wont render on initial render --- ui/src/shared/components/Dygraph.js | 25 ++--- ui/src/shared/components/DygraphLegend.js | 123 +++++++++++----------- ui/src/shared/components/LineGraph.js | 4 - 3 files changed, 74 insertions(+), 78 deletions(-) diff --git a/ui/src/shared/components/Dygraph.js b/ui/src/shared/components/Dygraph.js index 13716e737c..f5e972444f 100644 --- a/ui/src/shared/components/Dygraph.js +++ b/ui/src/shared/components/Dygraph.js @@ -97,24 +97,12 @@ class Dygraph extends Component { } shouldComponentUpdate(nextProps, nextState) { - const timeRangeChanged = !_.isEqual( - nextProps.timeRange, - this.props.timeRange - ) - - if (this.dygraph.isZoomed() && timeRangeChanged) { - this.dygraph.resetZoom() - } - const arePropsEqual = _.isEqual(this.props, nextProps) const areStatesEqual = _.isEqual(this.state, nextState) - - const shouldUpdate = !arePropsEqual || !areStatesEqual - - return shouldUpdate + return !arePropsEqual || !areStatesEqual } - componentDidUpdate() { + componentDidUpdate(prevProps) { const {labels, axes: {y, y2}, options, isBarGraph} = this.props const dygraph = this.dygraph @@ -127,6 +115,15 @@ class Dygraph extends Component { const timeSeries = this.timeSeries + const timeRangeChanged = !_.isEqual( + prevProps.timeRange, + this.props.timeRange + ) + + if (this.dygraph.isZoomed() && timeRangeChanged) { + this.dygraph.resetZoom() + } + const updateOptions = { ...options, labels, diff --git a/ui/src/shared/components/DygraphLegend.js b/ui/src/shared/components/DygraphLegend.js index 74665da282..33db5adee9 100644 --- a/ui/src/shared/components/DygraphLegend.js +++ b/ui/src/shared/components/DygraphLegend.js @@ -9,26 +9,28 @@ import {ErrorHandling} from 'src/shared/decorators/errors' @ErrorHandling class DygraphLegend extends Component { - state = { - legend: { - x: null, - series: [], - }, - sortType: '', - isAscending: true, - filterText: '', - isSnipped: false, - isFilterVisible: false, - legendStyles: {}, - pageX: null, - } + constructor(props) { + super(props) - componentDidMount() { this.props.dygraph.updateOptions({ legendFormatter: this.legendFormatter, highlightCallback: this.highlightCallback, unhighlightCallback: this.unhighlightCallback, }) + + this.state = { + legend: { + x: null, + series: [], + }, + sortType: '', + isAscending: true, + filterText: '', + isSnipped: false, + isFilterVisible: false, + legendStyles: {}, + pageX: null, + } } componentWillUnmount() { @@ -40,6 +42,53 @@ class DygraphLegend extends Component { } } + highlightCallback = e => { + console.log('callback firing: ', this, e, e.target) + this.setState({pageX: e.pageX}) + this.props.onShow(e) + } + + legendFormatter = legend => { + if (!legend.x) { + return '' + } + + const {legend: prevLegend} = this.state + const highlighted = legend.series.find(s => s.isHighlighted) + const prevHighlighted = prevLegend.series.find(s => s.isHighlighted) + + const yVal = highlighted && highlighted.y + const prevY = prevHighlighted && prevHighlighted.y + + if (legend.x === prevLegend.x && yVal === prevY) { + return '' + } + + this.legend = this.setState({legend}) + return '' + } + + unhighlightCallback = e => { + const { + top, + bottom, + left, + right, + } = this.legendNodeRef.getBoundingClientRect() + + const mouseY = e.clientY + const mouseX = e.clientX + + const mouseBuffer = 5 + const mouseInLegendY = mouseY <= bottom && mouseY >= top - mouseBuffer + const mouseInLegendX = mouseX <= right && mouseX >= left + const isMouseHoveringLegend = mouseInLegendY && mouseInLegendX + + if (!isMouseHoveringLegend) { + this.props.onHide() + } + } + handleToggleFilter = () => { this.setState({ isFilterVisible: !this.state.isFilterVisible, @@ -71,52 +120,6 @@ class DygraphLegend extends Component { this.setState({sortType, isAscending: !this.state.isAscending}) } - unhighlightCallback = e => { - const { - top, - bottom, - left, - right, - } = this.legendNodeRef.getBoundingClientRect() - - const mouseY = e.clientY - const mouseX = e.clientX - - const mouseBuffer = 5 - const mouseInLegendY = mouseY <= bottom && mouseY >= top - mouseBuffer - const mouseInLegendX = mouseX <= right && mouseX >= left - const isMouseHoveringLegend = mouseInLegendY && mouseInLegendX - - if (!isMouseHoveringLegend) { - this.props.onHide() - } - } - - highlightCallback = e => { - this.setState({pageX: e.pageX}) - this.props.onShow(e) - } - - legendFormatter = legend => { - if (!legend.x) { - return '' - } - - const {legend: prevLegend} = this.state - const highlighted = legend.series.find(s => s.isHighlighted) - const prevHighlighted = prevLegend.series.find(s => s.isHighlighted) - - const yVal = highlighted && highlighted.y - const prevY = prevHighlighted && prevHighlighted.y - - if (legend.x === prevLegend.x && yVal === prevY) { - return '' - } - - this.legend = this.setState({legend}) - return '' - } - render() { const {dygraph, onHide, isHidden} = this.props diff --git a/ui/src/shared/components/LineGraph.js b/ui/src/shared/components/LineGraph.js index 17183c5016..2a1c508f5e 100644 --- a/ui/src/shared/components/LineGraph.js +++ b/ui/src/shared/components/LineGraph.js @@ -15,10 +15,6 @@ class LineGraph extends Component { super(props) } - shouldComponentUpdate(nextProps, nextState) { - return shallowCompare(this, nextProps, nextState) - } - componentWillMount() { const {data, isInDataExplorer} = this.props this._timeSeries = timeSeriesToDygraph(data, isInDataExplorer)