score:4
Accepted answer
Instead of Math.min
in the y
position, use Math.max
:
.attr('y', function (d) {
return y2(Math.max(0, d["MacdHistogram"]))
});
The reason is that, regardless the orientation of your range, a SVG rectangle always (unless you mess with the transform
) has the width growing from left to right, and the height growing from top to bottom.
Here is a demo with that change, using bogus data:
var margin2 = {
top: 20,
right: 20,
bottom: 20,
left: 20
};
var height2 = 400 - margin2.top - margin2.bottom;
var width2 = 500 - margin2.left - margin2.right;
// Add svg to
var svg = d3.select('body').
append('svg').
attr('width', width2 + margin2.left + margin2.right).
attr('height', height2 + margin2.top + margin2.bottom).
append('g').
attr('transform', 'translate(' + margin2.left + ',' + margin2.top + ')');
//title
svg.append("text")
.attr("x", (width2 / 2))
.attr("y", 0 - (margin2.top / 3))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Title");
// X scale
var x2 = d3.scaleBand().
range([width2, 0])
.padding(0.1);
//y scale
var y2 = d3.scaleLinear()
.range([height2, 0]);
var xAxis = d3.axisBottom(x2);
var yAxis = d3.axisLeft(y2).
tickSize(6, 0);
// text label for the x axis
svg.append("text")
.attr("transform", "translate(" + (width2 / 2) + " ," + (height2 + margin2.top + 20) + ")")
.style("text-anchor", "middle")
.text("X Label");
function render(data) {
x2.domain(data.map(function(d) {
return d["date"];
}));
y2.domain(d3.extent(data, function(d) {
return d["MacdHistogram"];
})).nice();
svg.selectAll('.bar').
data(data).
enter().append('rect').
attr('class', function(d) {
return "bar bar--" + (d["MacdHistogram"] < 0 ? "negative" : "positive");
}).
attr('x', function(d) {
return x2(d["date"]);
}).
attr('y', function(d) {
return y2(Math.max(0, d["MacdHistogram"]));
}).
attr('height', function(d) {
return Math.abs(y2(d["MacdHistogram"]) - y2(0));
}).
attr('width', x2.bandwidth());
svg.append('g').
attr('class', 'y axis').
attr('transform', 'translate(' + width2 + ',0)').
call(yAxis);
var tickNegative = svg.append('g').
attr('class', 'x axis').
attr('transform', 'translate(0,' + y2(0) + ')').
call(xAxis).
selectAll('.tick').
filter(function(d, i) {
return data[i].value < 0;
});
}
var data = [{
date: 1,
MacdHistogram: 1
}, {
date: 2,
MacdHistogram: -2
}, {
date: 3,
MacdHistogram: 8
}, {
date: 4,
MacdHistogram: 3
}, {
date: 5,
MacdHistogram: 12
}, {
date: 6,
MacdHistogram: -5
}, {
date: 7,
MacdHistogram: 1
}, {
date: 8,
MacdHistogram: 9
}, {
date: 9,
MacdHistogram: -1
}, {
date: 10,
MacdHistogram: 10
}, {
date: 11,
MacdHistogram: 7
}, {
date: 12,
MacdHistogram: -8
}, {
date: 13,
MacdHistogram: 7
}, {
date: 14,
MacdHistogram: -4
}, {
date: 15,
MacdHistogram: 1
}];
render(data)
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- d3.js v4 bar chart with negative values on y axis
- D3 v4 bar chart X axis with negative values
- Bar chart with negative values
- d3.js stacked bar chart with positive and negative values
- Transitioning a bar chart with negative values for the width
- D3 bar chart not working properly with all negative and positive values
- D3.js: Adding a time axis to a bar graph with negative values
- d3.js horizontal bar graph with positive and negative values - move y axis labels to far left side of graph
- d3,js setting X and Y domains on Horizontal Bar Chart with all negative values
- Make simple bar chart using C3 with separate columns on the x axis
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- Special donut chart with different rings/arcs for positive and negative values
- Build D3js Area Chart with negative values
- d3.js Bar chart supporting negative values
- DC.js bar chart with date axis
- Make a chart with area charts having both positive and negative y axis in dc or d3.js
- Need help lining up x axis ticks with bars in D3.js bar chart
- dc.js bar chart with ordinal x axis scale doesn't render correctly
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- d3 bar chart with fixed bar width and fixed spacing to align in center axis line
- D3 Bar and Linear Chart With Multiple Axis
- Unable to handle negative values on d3 bar chart
- Prevent negative values in NVD3 chart axis
- d3.js bar which starts with negative values
- d3 bar chart with custom x axis and bar width
- d3 redraw bar chart with new values
- Draw Line chart with positive & negative values
- D3 js bar chart with 24 hour x axis
- Values are not reflecting Dynamically in D3 Bar Chart using. Overlapping occur with the existing Graph.
- Avoid negative axes values for dc powered d3 bubble chart with crossfilter
More Query from same tag
- What is the different between d3.max() vs. math.max() in d3.js?
- Create dynamic vertical rule in d3
- Difference Chart using Dimple.js
- Can't add labels to d3js force layout network visualization
- D3: Microlibraries for creating curves
- How do I execute a php script from javascript from D3.js?
- Add my Dataset to circle graph
- embedding json in sankey d3 javascript file
- D3 zoom is not properly reseting zoom state and translated location
- Using D3 To Parse My CSV
- D3 graph with svg nodes - How to move nodes to arbitrary positions
- Cannot plot chart using D3.js
- D3 nodes overlapping
- Updating the coordinates in d3js for a tree layout
- How to add reference line to nvd3 discreteBarChart
- Accessing data in Tooltip on D3 Multi-line Chart
- Existing elements in D3 SVG chart aren't updated - all is regarded as exit() and enter() sets
- d3js timeline adding current month renders multiple rectangles and doesn't clear
- The difference between select using merge and selectAll in D3 update mode
- D3, how to finish this graph, data format
- Displaying String values on Y-axis using d3.js
- D3 visualization to create building / campus map
- Transform flat table to hierarchy
- D3.js array of objects not creating elements
- Making a d3 chart update with new data
- d3 Change colour of tick on axis with function
- How to pass libraries through Google CAJA?
- d3 multiple line graphs
- Bar chart show values on top
- D3js Ordinal Axis does not show all the ticks