0

I am creating a (non-ribbon) chord diagram using amcharts library, and was wondering if there is a way to sort nodes given a specific value stored in the dataset.

What I am looking for is if we have for example some dataset like this :

data = [
    {"from": A, "number": 3},
    {"from": B, "number": 1},
    {"from": C, "number": 2},
    {"from": A, "to": C, "value": 1},
    {"from": B, "to": A, "value": 1},
    {"from": B, "to": C, "value": 1},
]

Then the nodes could be sorted depending on number so that we have in this example B > C > A.

From what I have seen in the API and documentation, I found that we can do it with chart.sortBy with either default (will sort given the order that a node appears in the dataset, here A>B>C), name (sort with the FromName datafield property of the node in alphabetic order, here A>B>C as well) or value (sort with the total value datafield property of the node, ie the sum of all weighted-link going out of this nod, here B>A>C). However I couldn't find anything else than those 3 properties...

Would you know if there is a way to have a personalized sort on the nodes ?

Sanimys
  • 101
  • 1
  • 10
  • 1
    You can use [`.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) and implement a custom one for the scenario. From the docs: `compareFunction` - *Specifies a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.* – norbitrial May 23 '20 at 16:46
  • @norbitrial Do you mean that I use `sort()` on my dataset so that it is sorted by `number` and then I use the default property of `sortBy` from chart ? – Sanimys May 23 '20 at 16:54
  • 1
    I actually did that and it works wonder, thank you a lot for this idea ! – Sanimys May 23 '20 at 17:08

1 Answers1

0

The solution, as suggested by @norbitrial is to sort the dataset on number before using the default option for the sortBy on dataset. Here is the code I used to sort my dataset (it may be non-optimal but at least it works !) :

data.sort(function(x, y) {
  if ("number" in x && !("number" in y)) {
    return -1;
  } else if ("number" in y && !("number" in x)) {
    return 1;
  }
  if (x["number"] < y["number"]) {
    return -1;
  }else if (x["number"] > y["number"]) {
    return 1;
  }
  return 0;
})
Sanimys
  • 101
  • 1
  • 10