score:5
If you just want straight-line error bars (without top and bottom horizontal lines), yes, you could use an area or line generator (only area generators have y0/y1 methods).
However, you wouldn't call the path generator on all the data at once, you would have to create a selection of paths joined to the data, and then pass the data object as a one (area generator) or two-point (line generator) array to the generator function.
This should work:
var errorBarArea = d3.svg.area()
.x(function(d) {return x(d.state); })
.y0(function(d) {return y(d.value - d.moe); })
.y1(function(d) {return y(d.value + d.moe); })
.interpolate("linear");
var errorBarSVG = d3.select("body").append("svg")
var errorBars = errorBarSVG.selectAll("path")
.data(data);
errorBars.enter().append("path");
errorBars.attr("d", function(d){return errorBarArea([d]);})
//turn the data into a one-element array
//and pass it to the area function
.attr("stroke", "red")
.attr("stroke-width", 1.5);
This will create a zero-width area path for each bar, connecting the +/- points in a straight line.
If you used a line generator function, you would have to calculate the top and bottom Y-values when you turned the data into an array:
errorBars.attr("d", function(d){
return errorBarLine([{x:d.x, y:d.value - d.moe},
{x:d.x, y:d.value + d.moe}]);
})
Alternately, you could use d3.svg.diagonal
to create straight-line error bars. A diagonal function is specifically designed to take a single data object and extract start and end line points from it; you would then have to customize the source
and target
accessor functions to create objects like the ones in the code sample above.
However, if you want a custom-shaped error bar with horizontal top and bottom lines, you'll need to write your own function to create the path "d"
attribute based on a data object. I discuss doing that in this answer, for an error bar the shape would actually be much simpler, since it's all just straight lines. You might find this tutorial on the path directions syntax a useful reference.
Finally, you might also want to re-write your bar chart code so that each bar is represented by a <g>
element joined to the data, and then you append both the rectangle and the error bar path within it without calculating separate data joins.
Source: stackoverflow.com
Related Query
- Adding Error Bars to Grouped Bar Chart with D3.js
- Grouped bar chart with zoom and updateable data error
- d3.js bar chart sorting: can't figure out how to sort x-axis labels along with bars
- How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?
- Need help lining up x axis ticks with bars in D3.js bar chart
- Sorting the bars in a bar chart with dc.js
- NVD3.js: Stacked and grouped bar chart with two y-axis
- D3 Grouped Bar Chart with JSON nested data
- d3.js bar chart with pos & neg bars (win/loss) for each record
- Create a bar chart with local storage data (drawing the bars doesn't work)
- Sorting the bars and getting the group data in a grouped bar chart
- How to draw multiple bars in a row in a bar chart with spaces in between with d3v5?
- Sorting function issue with grouped bar chart
- DC.js bar chart x-axis labels not aligned with bars
- grouped category bar chart with different groups in d3?
- D3.js grouped bar chart text on bars
- Grouped Bar Chart with different y-axis/scales
- Error setting the domain for a grouped bar chart
- How to create a grouped bars brushable chart with D3 js
- d3.js, grouped bar chart with two Y axes, series related to Y1 or Y2
- Problems with grouped bar chart
- unresponsive bars in otherwise responsive grouped d3 bar chart
- Issue with Grouped Bar Chart using D3.js
- Grouped bar chart "in one line" with D3.js
- Grouped Bar Chart with negative value d3
- D3.js Grouped bar chart with JSON data
- How to align ticks with bars in horizontal bar chart / row chart
- Is it possible to select multiple bars on bar chart in d3.js with mouse
- stacked bar chart with overlapping bars C3js
- Issue with Bar Height on D3 Grouped Chart Implementation
More Query from same tag
- D3 updating brush's scale doesn't update brush
- D3.js - Donut charts with multiple rings
- Replicating Mike Bostock's collapsible tree
- d3js not formatting text value
- How to get a D3.js pie chart to render data dynamically from the DOM
- Separate json data from html?
- d3js force layout dynamically adding image or circle for each node
- React application breaks after downloading SVG through d3_save_svg
- How can we create an onclick event for a d3 data map?
- Use D3 to Display SVG image within Angular Component
- How to round top and bottom corner of stacked bar chart?
- Does D3 support multiple brushes on the same line chart?
- dc.js: include all bars when count is zero
- D3.js Hierarchical Edge Bundling, how do i change the color of a text group?
- d3.js v5 - Promise.all replaced d3.queue
- How does `Import Native` work in Elm
- Howto set custom ticks and labels on Y axis in nvd3
- d3js cluster layout dendogram node links to custom depths
- How to add multiple divs in mouseover (jquery)
- D3 force directed layout distribute across X axis
- d3.js Attributes, Transform, Translate when using polylines
- How to make bar chart with additional vertical lines below the x axis
- d3 line graph using csv
- D3.js: Multiple line charts mouseover with same x (time) showing different y (price)
- D3JS Sunburst not updating text
- How to display names next to bubbles in d3js motion chart
- want to fixed origin in d3 chart
- Dragging a globe in D3
- d3 pattern to add an element if missing
- how to get d3.event.Y as location in a <g> element