Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions src/BarChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,40 @@ class BarChart extends React.Component {
}
});
}

sortAxis = (i, descending) => {
if(descending === undefined) descending = true;
let toSort = Object.keys(this.props.data).map(name => {
return {
name: name,
val: this.props.data[name][i]
};
});
toSort.sort((left, right) => descending ? left.val < right.val : left.val > right.val);
toSort = toSort.slice(0, this.maxItems);
const maxVal = Math.max.apply(Math, toSort.map(item => item.val));
return [toSort.reduce((ret, item, idx) => ({
...ret, ...{[item.name]: idx}
}), {}), maxVal];

/**
* sortAxis
* Handles sorting the results
* @param {*} i is the item to start sorting from
* @param {number} maxItems is the maximum number of items to allow in the list
* @param {*} descending is the direction to sort
*/
sortAxis = (i, descending) => {
if(descending === undefined) descending = true;
// Build a new array to sort e.x. { name: 'some name', val: 1 }
let toSort = Object.keys(this.props.data).map(name => {
return {
name,
val: this.props.data[name][i]
};
});
// Handle the sorting based on the values
toSort.sort((left, right) => left.val - right.val)
if (descending) {
toSort.reverse()
}
// Slice based on the maximum items allowed
const fItems = Object.keys(this.props.data).length
if (this.maxItems && this.maxItems <= fItems) {
toSort = toSort.slice(0, this.maxItems)
}
const maxVal = Math.max.apply(Math, toSort.map(item => item.val))
const minVal = Math.min.apply(Math, toSort.map(item => item.val))
// Sorted list of results based on the axis
return [toSort.reduce((ret, item, idx) => ({
...ret, ...{ [item.name]: idx }
}), {}), minVal, maxVal]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In lines 22 and 56 the return value at index 1 is interpreted as maxVal, not minVal. So you should either change the order here or in the mentioned lines (or not return minVal at all, as it does not seem to be needed).

}

getInfoFromRank = name => {
const currIdx = this.state.idx;
Expand Down Expand Up @@ -131,4 +149,4 @@ class BarChart extends React.Component {
}
}

export default BarChart;
export default BarChart;