I want to create a DualAxisBarchart
but kind of stuck. Tried many way but actually am able to create Two yAxis
but instead to create two separate bar its creating two bar at the same place please suggest me some approach am creating this using multichart()
function
Asked
Active
Viewed 67 times
0

Konrad Krakowiak
- 12,285
- 11
- 58
- 45

Vikas Kumar
- 11
- 5
-
Post your code in a [Plunker](http://plnkr.co/edit/Q4ohxfLaepnOnApDCVQK?p=preview) or Fiddle and it may help us find the issue. – Lucas Apr 21 '15 at 03:01
1 Answers
0
I have not used nvd3, however I have created a dual axis bar chart using d3. In the code below I am assuming the variable height, x, svg are already defined. The only differences are the ranges for the upperY and lowerY functions and how the lowerBars y and height are defined. I really hope this helps
var upperY = d3.scale.linear().range([height / 2, 0]);
var lowerY = d3.scale.linear().range([height / 2, height]);
var upperYAxis = d3.svg.axis()
.scale(upperY)
.orient("left");
var lowerYAxis = d3.svg.axis()
.scale(lowerY)
.orient("left");
svg.append("g")
.attr("class", "y axis")
.call(upperYAxis);
svg.append("g")
.attr("class", "y2 axis")
.call(lowerYAxis);
var upperBars = svg.selectAll(".upperBar")
.data(upperBarData)
.enter()
.append("rect")
.attr("class", "upperBar")
.attr("x", function(d) { return x(d.index); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return upperY(d.value); })
.attr("height", function(d) { return height / 2 - upperY(d.value); });
var lowerBars = svg.selectAll(".lowerBar")
.data(lowerBarData)
.enter()
.append("rect")
.attr("class", "lowerBar")
.attr("x", function(d) { return x(d.index); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return height / 2; })
.attr("height", function(d) { return lowerY(d.value) - height / 2; });

Jon
- 301
- 4
- 9
-
johnnty thanx for this solution. i can use this for my other issue where my small values bars hiding behind the big ones.. but again am saying what i want to create two bars on xAxis with two yAxis check githubnvd3 issue no #973 https://github.com/novus/nvd3/issues – Vikas Kumar Apr 18 '15 at 11:19
-
Sorry Vikas, I thought you meant creating a upper bar chart and a lower one. From your issue am I correct in thinking that you want a y axis on the left and a y axis on the right and one set of bars to use left axis and another to use the right? – Jon Apr 18 '15 at 11:28
-
So I tried out something. If you are using rangeRoundBands and `x.rangeBand()` for the width then try setting one set of bars width to `x.rangeBand() / 2`. Then set the other bars x to `x(d.index) + x.rangeBand() / 2` and its width to `x.rangeBand() / 2` – Jon Apr 18 '15 at 11:56