Add filtering to legend

pull/1679/head
Andrew Watkins 2017-06-28 14:39:09 -07:00
parent 6abed0534a
commit 5894d62f7d
2 changed files with 29 additions and 6 deletions

View File

@ -21,11 +21,13 @@ export default class Dygraph extends Component {
}, },
sortType: null, sortType: null,
legendOrder: 'asc', legendOrder: 'asc',
filterText: '',
} }
this.getTimeSeries = ::this.getTimeSeries this.getTimeSeries = ::this.getTimeSeries
this.sync = ::this.sync this.sync = ::this.sync
this.handleSortLegend = ::this.handleSortLegend this.handleSortLegend = ::this.handleSortLegend
this.handleLegendInputChange = ::this.handleLegendInputChange
} }
static defaultProps = { static defaultProps = {
@ -60,6 +62,10 @@ export default class Dygraph extends Component {
}) })
} }
handleLegendInputChange(e) {
this.setState({filterText: e.target.value})
}
sortByType(list, sortType, order) { sortByType(list, sortType, order) {
if (!sortType) { if (!sortType) {
return list return list
@ -238,11 +244,11 @@ export default class Dygraph extends Component {
return '' return ''
}, },
}) })
// part of optional workaround for preventing updateOptions from breaking legend // part of optional workaround for preventing updateOptions from breaking legend
// if (this.lastMouseMoveEvent) { // if (this.lastMouseMoveEvent) {
// dygraph.mouseMove_(this.lastMouseMoveEvent) // dygraph.mouseMove_(this.lastMouseMoveEvent)
// } // }
dygraph.resize() dygraph.resize()
const {w} = this.dygraph.getArea() const {w} = this.dygraph.getArea()
this.props.setResolution(w) this.props.setResolution(w)
@ -256,11 +262,16 @@ export default class Dygraph extends Component {
} }
render() { render() {
const {legend} = this.state const {legend, filterText} = this.state
return ( return (
<div className="dygraph-child"> <div className="dygraph-child">
<DygraphLegend {...legend} onSort={this.handleSortLegend} /> <DygraphLegend
{...legend}
onSort={this.handleSortLegend}
onInputChange={this.handleLegendInputChange}
filterText={filterText}
/>
<div <div
ref={r => { ref={r => {
this.graphContainer = r this.graphContainer = r

View File

@ -1,9 +1,14 @@
import React, {PropTypes} from 'react' import React, {PropTypes} from 'react'
const DygraphLegend = ({series, onSort}) => ( const DygraphLegend = ({series, onSort, filterText, onInputChange}) => (
<div style={{userSelect: 'text'}} className="container--dygraph-legend"> <div style={{userSelect: 'text'}} className="container--dygraph-legend">
<div className="dygraph-legend--header"> <div className="dygraph-legend--header">
<input className="form-control input-xs" type="text" /> <input
className="form-control input-xs"
type="text"
value={filterText}
onChange={onInputChange}
/>
<button <button
className="btn btn-primary btn-xs" className="btn btn-primary btn-xs"
onClick={() => onSort('alphabetic')} onClick={() => onSort('alphabetic')}
@ -18,7 +23,12 @@ const DygraphLegend = ({series, onSort}) => (
</button> </button>
</div> </div>
<div className="dygraph-legend--contents"> <div className="dygraph-legend--contents">
{series.map(({label, color, yHTML, isHighlighted}) => { {series.filter(s => s.label.match(filterText)).map(({
label,
color,
yHTML,
isHighlighted,
}) => {
return ( return (
<span key={label + color}> <span key={label + color}>
<b> <b>
@ -52,6 +62,8 @@ DygraphLegend.propTypes = {
), ),
dygraph: shape(), dygraph: shape(),
onSort: func.isRequired, onSort: func.isRequired,
onInputChange: func.isRequired,
filterText: string.isRequired,
} }
export default DygraphLegend export default DygraphLegend