0

I need to invert the X-axis to have it begin from the biggest value and to degrade to lowest. For example beginning from 2015 and ending 1985.

Is it possible to just sort the original data json/array in php? Or will it be sorted back to lowest to highest?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
user1271930
  • 331
  • 1
  • 7
  • 21

1 Answers1

0

Maybe this will help,reversed Y-axis D3. You can use this code to invert your data. Change .range([0, h]); to .range([h, 0]);. This sample will help to sort data sort.

Community
  • 1
  • 1
Gabriel
  • 1,413
  • 1
  • 18
  • 29
  • Seems like the sorting at least in backend didn't make any difference and json data is automatically put on scale from smallest to largest. However, shouldn't there be function for this which could be just duplicated with reverse logic? Or can I have "custom labels" for X-axis? I suppose that I can have line chart with X-axis for dogs,cats and monkeys not just numbers... I also tried to change these values to -2014 so that I would render it as needed but the result was a broken chart. – user1271930 Jun 15 '15 at 13:57
  • If I understand your question correctly, you could use index in your json to sort data or you could sort data alphabetically. there is no limitation in `d3` or `javascript`. If your problem does not solve any more please give us a link of your code in [link](http://www.jsfiddle.net) or edit your post and use your code to help us understand your question better. – Gabriel Jun 16 '15 at 16:55
  • Yes that is what I'm looking for. Just don't know what kind customization it requires for D3 to use custom labels? Or is it somewhere documented that how the JSON should be structured? (In that case if I append "index" into JSON and keep the current value as label) But on the other hand I realized that there isn't any method to update data on D3? Seemed like a script hell to achieve a solution where I could update data from backend with ajax. – user1271930 Jun 16 '15 at 19:22
  • D3 is very flexible, So you don't need to keep specific structure. you can access to any node just by keep the `root` node. The `root` node is the first node in the json files. This [link](http://www.stackoverflow.com/questions/21600858/how-to-update-data-form-file-json-using-d3-js-zoomable-circle-pack) will show you update json files methods. As I mentioned before there are many method to sort data like [this](http://www.stackoverflow.com/questions/21639347/d3-js-sort-table-by-number-then-alphabetically-as-a-tie-breaker). – Gabriel Jun 16 '15 at 19:41