Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting not working chrome #1

Open
robksawyer opened this issue Nov 25, 2019 · 5 comments
Open

Sorting not working chrome #1

robksawyer opened this issue Nov 25, 2019 · 5 comments
Labels
bug Something isn't working

Comments

@robksawyer
Copy link

Thanks for the great library. Just wanted to mention that sorting seems to be working great in Safari, but not in Chrome. Any ideas?

@rachidelaid
Copy link

rachidelaid commented Nov 26, 2019

it's not working in chrome for me too.
or do I have to change something first?
but it is working in Firefox out of the box.

@bchao1
Copy link
Owner

bchao1 commented Nov 28, 2019

Hey guys, thanks for the feedback. As I am currently a little busy, I'll get back to this issue ASAP. Thanks again!

@bchao1 bchao1 added the bug Something isn't working label Nov 28, 2019
@SunLeL
Copy link

SunLeL commented Dec 3, 2019

I'm sorry for my stupied. I still don't know how to run it. I try to run it with command "node demo.js", but it shows "SyntaxError:Unexpected token '<'", and "SyntaxError: Cannot use import statement outside a module". Besides, where should the code of "import BarChart from 'chart-race-react'; " and "<div style={{width: "500px"}}>" be written in? How should I start?

@robksawyer
Copy link
Author

@Mckinsey666 it would be cool if you added a demo project to show how to launch it. In the meantime, I've created one over at https://github.com/robksawyer/chart-race-react-demo. @SunLeL maybe that'll help you get started.

@robksawyer
Copy link
Author

robksawyer commented Dec 6, 2019

After some more poking around, I figured out what the issue was inside of sortAxis. See the updated sort method below with the fix. For more info see, https://v8.dev/blog/array-sort

I added a few new features to it.

/**
 * 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
 */
  const sortAxis = (i, maxItems, descending) => {
    if(descending === undefined) descending = true;
    // Build a new array to sort e.x. { name: 'some name', val: 1 }
    let toSort = Object.keys(data).map(name => {
      return {
        name,
        val: 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(data).length
    if (maxItems && maxItems <= fItems) {
      toSort = toSort.slice(0, 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]
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants