diff --git a/ui/src/shared/components/Dygraph.js b/ui/src/shared/components/Dygraph.js index 8fb92c6a76..c5b85d2fbb 100644 --- a/ui/src/shared/components/Dygraph.js +++ b/ui/src/shared/components/Dygraph.js @@ -19,10 +19,13 @@ export default class Dygraph extends Component { x: null, series: [], }, + sortType: null, + legendOrder: 'asc', } this.getTimeSeries = ::this.getTimeSeries this.sync = ::this.sync + this.handleSortLegend = ::this.handleSortLegend } static defaultProps = { @@ -38,6 +41,35 @@ export default class Dygraph extends Component { 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() { const timeSeries = this.getTimeSeries() // 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 (