admin管理员组

文章数量:1401963

I have drawn my axes in d3 using the d3.extent method to determine the max and min values for the axis.

However, by default, d3 draws the y axis upside down (d3 bar chart is upside down) and I need to invert it.

How to I tell the extent method to invert the results?

I have drawn my axes in d3 using the d3.extent method to determine the max and min values for the axis.

However, by default, d3 draws the y axis upside down (d3 bar chart is upside down) and I need to invert it.

How to I tell the extent method to invert the results?

Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Aug 14, 2015 at 9:39 Neil PNeil P 3,2107 gold badges41 silver badges76 bronze badges 4
  • show us what you have tried so far, ideally with some fiddle or similar – jarandaf Commented Aug 14, 2015 at 10:11
  • I've managed to find a way to make it work, but was wondering if there was a single function to do it! – Neil P Commented Aug 14, 2015 at 10:22
  • 2 Ok, now I understand... a one-liner: d3.time.scale().domain(d3.extent(data.History, function (d) { return d.ArrivalTime; }).reverse())... – jarandaf Commented Aug 14, 2015 at 10:25
  • reverse! Makes perfect sense. Do you want to drop that into an answer so I can mark it? – Neil P Commented Aug 14, 2015 at 10:26
Add a ment  | 

2 Answers 2

Reset to default 8

You can use d3.extent function in bination with reverse:

var yScale = d3.time.scale()
               .domain(d3.extent(data.History, function (d) { return d.ArrivalTime; }).reverse())
               .range([margin.top, chartHeight - margin.top * 2]);

I've managed to do it manually using the following code. I can't find a neater way than this:

 var yMax = d3.max(data.History, function (d) { return d.ArrivalTime });
 var yMin = d3.min(data.History, function (d) { return d.ArrivalTime });

var yScale = d3.time.scale()
                .domain([yMax, yMin])
                .range([margin.top, chartHeight - margin.top * 2]);

本文标签: javascriptd3 jshow to invert d3extentStack Overflow