Add ability to sort numerically and alphabetically
parent
45ee3f7253
commit
32ecf1b796
|
@ -19,10 +19,13 @@ export default class Dygraph extends Component {
|
||||||
x: null,
|
x: null,
|
||||||
series: [],
|
series: [],
|
||||||
},
|
},
|
||||||
|
sortType: null,
|
||||||
|
legendOrder: 'asc',
|
||||||
}
|
}
|
||||||
|
|
||||||
this.getTimeSeries = ::this.getTimeSeries
|
this.getTimeSeries = ::this.getTimeSeries
|
||||||
this.sync = ::this.sync
|
this.sync = ::this.sync
|
||||||
|
this.handleSortLegend = ::this.handleSortLegend
|
||||||
}
|
}
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
|
@ -38,6 +41,35 @@ export default class Dygraph extends Component {
|
||||||
return timeSeries.length ? timeSeries : [[0]]
|
return timeSeries.length ? timeSeries : [[0]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleSortLegend(sortType) {
|
||||||
|
const {legend, legendOrder} = this.state
|
||||||
|
const ascending = this.sortByType(legend.series, sortType)
|
||||||
|
|
||||||
|
if (legendOrder === 'asc') {
|
||||||
|
return this.setState({
|
||||||
|
legend: {...legend, series: ascending},
|
||||||
|
sortType,
|
||||||
|
legendOrder: 'desc',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
legend: {...legend, series: ascending.reverse()},
|
||||||
|
sortType,
|
||||||
|
legendOrder: 'asc',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
sortByType(list, sortType) {
|
||||||
|
return _.sortBy(list, ({y, label}) => {
|
||||||
|
if (sortType === 'numeric') {
|
||||||
|
return y
|
||||||
|
}
|
||||||
|
|
||||||
|
return label
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
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'};
|
||||||
|
@ -201,7 +233,7 @@ export default class Dygraph extends Component {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="dygraph-child">
|
<div className="dygraph-child">
|
||||||
<DygraphLegend {...legend} />
|
<DygraphLegend {...legend} onSort={this.handleSortLegend} />
|
||||||
<div
|
<div
|
||||||
ref={r => {
|
ref={r => {
|
||||||
this.graphContainer = r
|
this.graphContainer = r
|
||||||
|
|
|
@ -1,18 +1,24 @@
|
||||||
import React, {PropTypes} from 'react'
|
import React, {PropTypes} from 'react'
|
||||||
|
|
||||||
const DygraphLegend = ({series}) => (
|
const DygraphLegend = ({series, onSort}) => (
|
||||||
<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" />
|
||||||
<button className="btn btn-primary btn-xs">
|
<button
|
||||||
|
className="btn btn-primary btn-xs"
|
||||||
|
onClick={() => onSort('alphabetic')}
|
||||||
|
>
|
||||||
A-Z
|
A-Z
|
||||||
</button>
|
</button>
|
||||||
<button className="btn btn-primary btn-xs">
|
<button
|
||||||
|
className="btn btn-primary btn-xs"
|
||||||
|
onClick={() => onSort('numeric')}
|
||||||
|
>
|
||||||
0-9
|
0-9
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div className="dygraph-legend--contents">
|
<div className="dygraph-legend--contents">
|
||||||
{series.map(({label, color, yHTML}) => {
|
{series.map(({label, color, yHTML, isHighlighted}) => {
|
||||||
return (
|
return (
|
||||||
<span key={label + color}>
|
<span key={label + color}>
|
||||||
<b>
|
<b>
|
||||||
|
@ -27,7 +33,7 @@ const DygraphLegend = ({series}) => (
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
||||||
const {arrayOf, bool, number, shape, string} = PropTypes
|
const {arrayOf, bool, func, number, shape, string} = PropTypes
|
||||||
|
|
||||||
DygraphLegend.propTypes = {
|
DygraphLegend.propTypes = {
|
||||||
x: number,
|
x: number,
|
||||||
|
@ -43,6 +49,7 @@ DygraphLegend.propTypes = {
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
dygraph: shape(),
|
dygraph: shape(),
|
||||||
|
onSort: func.isRequired,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default DygraphLegend
|
export default DygraphLegend
|
||||||
|
|
Loading…
Reference in New Issue