score:10
Accepted answer
Adjust the line functions x accessor by half the rangeband:
var valueline = d3.svg.line()
.x(function (d) { return x(d.date) + x.rangeBand()/2; })
.y(function (d) { return y(d.open); });
Full Code:
var data = [{ date: '1-May-12', close: 58.13, open: 7.41 }, { date: '2-May-12', close: 53.98, open: 45.55 }, { date: '3-May-12', close: 67.00, open: 11.78}];
var margin = { top: 30, right: 40, bottom: 30, left: 50 },
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.scale.ordinal().rangeBands([0, width], .09); // <-- to change the width of the columns, change the .09 at the end to whatever
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%d-%b-%y"));
var yAxisLeft = d3.svg.axis().scale(y)
.orient("left");
var valueline = d3.svg.line()
.x(function (d) { return x(d.date) + x.rangeBand()/2; })
.y(function (d) { return y(d.open); });
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Get the data
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
});
// Scale the range of the data
x.domain(data.map(function (d) { return d.date; }));
y.domain([0, d3.max(data, function (d) { return d.close; })]);
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.style("fill", "steelblue")
.call(yAxisLeft);
// Draw the bars
svg.selectAll("bar")
.data(data)
.enter()
.append("rect")
.style("fill", "#99ffcc")
.attr("x", function (d) { return x(d.date); })
.attr("width", x.rangeBand())
.attr("y", function (d) { return y(d.close); })
.attr("height", function (d) { return height - y(d.close); });
// Add the valueline path
svg.append("path")
.attr("d", valueline(data));
body {
font: 12px Arial;
}
path {
stroke: red;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- D3.js combining bar and line chart
- d3 bar chart with fixed bar width and fixed spacing to align in center axis line
- NVD3 Line Plus Bar With Focus Chart only displaying half width of first and last bar
- why d3 line chart and bar chart display in same morment?
- Line And Bar Chart Combo in NVD3
- D3.js Combined Bar and Line Chart x-Axis Misalignement Issue
- Add colors to dimple.js bar chart based on value and add goal line
- Right Axis not displaying on Dual Axis Grouped Bar and Line Chart D3
- D3.js Draw vertical line after every 2 bar and draw x-axis label on the top of bar chart
- D3.JS time-series line chart with real-time data, panning and zooming
- D3.js: Line chart - tooltip and vertical line of hover
- Solid and dashed line in D3 line chart
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- D3 update dataset on click and redraw the bar chart
- Line chart using ordinal x-axis with d3.js and nvd3.js
- d3 bar chart y axis ticks and grid lines above max value
- How to add a line on x-axis on a horizontal bar chart in d3
- Mix bar and line charts with Rickshaw (d3 based graphing library for js)
- d3 line chart and clip-path
- d3 v4 drag line chart with x and y axes
- How to use x and width in a bar chart with scaleTime?
- D3 bar chart needs to add arrow and text in particular bar
- Trouble using DC.js (crossfilter and d3 convenience library) - bar chart not showing values
- d3 v4 nested data and stacked bar chart
- d3.js stacked bar chart with positive and negative values
- Combining stacked area chart and brushing in D3
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
- nvd3 line chart not shown properly. (dots and shaded area)
- D3 Line Chart to display first and last point values
- Bar chart using d3.js , pandas and flask
More Query from same tag
- D3.js dynamically switching between number and dollar formats on tick marks
- D3js multiple line chart from json file
- Set D3 projection in Openlayers 4
- Adding a title to child element of svg:g element in D3.js
- variable declaration conditional syntax
- Animating D3 donut chart on load
- D3.js zoomTo point in a 2D map (projection)
- How to visualize multiple graphs with different input files in D3?
- D3: Sunburst chart with root node on the outside ring
- Interaction with updated/entered elements
- Using d3, how can I create a histogram or bar plot where the last bar is the count of all values greater than some number?
- How to draw clickable line in PIXI.js using PIXI.Graphics?
- D3 text as leaflet label?
- What is the best way to create blocks of items in D3.JS
- d3 move circles on y axis update
- Checking for "undefined" and then sorting in Javascript
- d3.js v4 wacky link transition in collapsible tree example
- Best solution to label nodes in SVG dynamic tree (using D3.js)
- using d3.scaleOrdinal troubleshooting
- Prevent D3 adding duplicates on update
- D3.js Adding links between elements in a radial tree (Hierarchical edge bundling elements)
- How do I make my D3 collapsible chart, collapsible?
- Use multi-tier dropdown (that uses jquery) to filter data and display d3 bar chart
- Line chart customization using D3.js and AngularJS
- Path intersection causes parts of them to disappear [D3 Path Generation]
- d3 v4 map becomes empty unexpectedly
- Creating a line graph from array of data objects
- How to connect the elements with each other by a line Angular
- Making d3 v4 group react to simulation
- D3 - accessing nested data for the purpose of creating a navigation tree