Refactor sorting and ordering

pull/10616/head
Andrew Watkins 2017-06-28 15:07:26 -07:00
parent f0728264db
commit 617f62da52
2 changed files with 64 additions and 89 deletions

View File

@ -19,7 +19,7 @@ export default class Dygraph extends Component {
x: null, x: null,
series: [], series: [],
}, },
sortType: null, sortType: '',
legendOrder: 'asc', legendOrder: 'asc',
filterText: '', filterText: '',
} }
@ -44,19 +44,14 @@ export default class Dygraph extends Component {
} }
handleSortLegend(sortType) { handleSortLegend(sortType) {
const {legend, legendOrder} = this.state if (this.state.legendOrder === 'asc') {
const series = this.sortByType(legend.series, sortType)
if (legendOrder === 'asc') {
return this.setState({ return this.setState({
legend: {...legend, series: series.reverse()},
sortType, sortType,
legendOrder: 'desc', legendOrder: 'desc',
}) })
} }
this.setState({ this.setState({
legend: {...legend, series},
sortType, sortType,
legendOrder: 'asc', legendOrder: 'asc',
}) })
@ -66,21 +61,6 @@ export default class Dygraph extends Component {
this.setState({filterText: e.target.value}) this.setState({filterText: e.target.value})
} }
sortByType(list, sortType, order) {
if (!sortType) {
return list
}
const orderFunc = ({y, label}) => (sortType === 'numeric' ? y : label)
const orderedList = _.sortBy(list, orderFunc)
if (order === 'desc') {
return orderedList.reverse()
}
return orderedList
}
componentDidMount() { componentDidMount() {
const timeSeries = this.getTimeSeries() const timeSeries = this.getTimeSeries()
// dygraphSeries is a legend label and its corresponding y-axis e.g. {legendLabel1: 'y', legendLabel2: 'y2'}; // dygraphSeries is a legend label and its corresponding y-axis e.g. {legendLabel1: 'y', legendLabel2: 'y2'};
@ -139,14 +119,11 @@ export default class Dygraph extends Component {
return '' return ''
} }
const {state, sortByType} = dygraphComponent if (legend.x === dygraphComponent.state.legend.x) {
const {legendOrder, sortType} = state
if (legend.x === state.legend.x) {
return '' return ''
} }
const orderedSeries = sortByType(legend.series, sortType, legendOrder) dygraphComponent.setState({legend})
dygraphComponent.setState({legend: {...legend, series: orderedSeries}})
return '' return ''
}, },
} }
@ -208,7 +185,6 @@ export default class Dygraph extends Component {
} }
const timeSeries = this.getTimeSeries() const timeSeries = this.getTimeSeries()
const dygraphComponent = this // eslint-disable-line consistent-this
dygraph.updateOptions({ dygraph.updateOptions({
labels, labels,
@ -226,23 +202,6 @@ export default class Dygraph extends Component {
underlayCallback: options.underlayCallback, underlayCallback: options.underlayCallback,
series: dygraphSeries, series: dygraphSeries,
plotter: isBarGraph ? multiColumnBarPlotter : null, plotter: isBarGraph ? multiColumnBarPlotter : null,
legendFormatter: legend => {
if (!legend.x) {
return ''
}
const {state, sortByType} = dygraphComponent
const {sortType, legendOrder} = state
if (legend.x === state.legend.x) {
return ''
}
const orderedSeries = sortByType(legend.series, sortType, legendOrder)
dygraphComponent.setState({
legend: {...legend, series: orderedSeries},
})
return ''
},
}) })
// part of optional workaround for preventing updateOptions from breaking legend // part of optional workaround for preventing updateOptions from breaking legend
@ -262,7 +221,7 @@ export default class Dygraph extends Component {
} }
render() { render() {
const {legend, filterText} = this.state const {legend, filterText, legendOrder, sortType} = this.state
return ( return (
<div className="dygraph-child"> <div className="dygraph-child">
@ -271,6 +230,8 @@ export default class Dygraph extends Component {
onSort={this.handleSortLegend} onSort={this.handleSortLegend}
onInputChange={this.handleLegendInputChange} onInputChange={this.handleLegendInputChange}
filterText={filterText} filterText={filterText}
sortOrder={legendOrder}
sortType={sortType}
/> />
<div <div
ref={r => { ref={r => {

View File

@ -1,49 +1,61 @@
import React, {PropTypes} from 'react' import React, {PropTypes} from 'react'
import _ from 'lodash'
const DygraphLegend = ({series, onSort, filterText, onInputChange}) => ( const DygraphLegend = ({
<div style={{userSelect: 'text'}} className="container--dygraph-legend"> series,
<div className="dygraph-legend--header"> onSort,
<input filterText,
className="form-control input-xs" onInputChange,
type="text" sortOrder,
value={filterText} sortType,
onChange={onInputChange} }) => {
/> const sorted = _.sortBy(
<button series,
className="btn btn-primary btn-xs" ({y, label}) => (sortType === 'numeric' ? y : label)
onClick={() => onSort('alphabetic')} )
> const ordered = sortOrder === 'desc' ? sorted.reverse() : sorted
A-Z const filtered = ordered.filter(s => s.label.match(filterText))
</button>
<button return (
className="btn btn-primary btn-xs" <div style={{userSelect: 'text'}} className="container--dygraph-legend">
onClick={() => onSort('numeric')} <div className="dygraph-legend--header">
> <input
0-9 className="form-control input-xs"
</button> type="text"
value={filterText}
onChange={onInputChange}
/>
<button
className="btn btn-primary btn-xs"
onClick={() => onSort('alphabetic')}
>
A-Z
</button>
<button
className="btn btn-primary btn-xs"
onClick={() => onSort('numeric')}
>
0-9
</button>
</div>
<div className="dygraph-legend--contents">
{filtered.map(({label, color, yHTML, isHighlighted}) => {
return (
<span key={label + color}>
<b>
<span
style={{color, fontWeight: isHighlighted ? 'bold' : 'normal'}}
>
{label}: {yHTML || 'no value'}
</span>
</b>
</span>
)
})}
</div>
</div> </div>
<div className="dygraph-legend--contents"> )
{series.filter(s => s.label.match(filterText)).map(({ }
label,
color,
yHTML,
isHighlighted,
}) => {
return (
<span key={label + color}>
<b>
<span
style={{color, fontWeight: isHighlighted ? 'bold' : 'normal'}}
>
{label}: {yHTML}
</span>
</b>
</span>
)
})}
</div>
</div>
)
const {arrayOf, bool, func, number, shape, string} = PropTypes const {arrayOf, bool, func, number, shape, string} = PropTypes
@ -64,6 +76,8 @@ DygraphLegend.propTypes = {
onSort: func.isRequired, onSort: func.isRequired,
onInputChange: func.isRequired, onInputChange: func.isRequired,
filterText: string.isRequired, filterText: string.isRequired,
sortOrder: string.isRequired,
sortType: string.isRequired,
} }
export default DygraphLegend export default DygraphLegend