score:1
Accepted answer
Use .paddingInner([0.1]);
for just setting the inner padding as in the documents https://github.com/d3/d3-scale#band_paddingInner
Here is the working solution:
var originalData = [
{
date: "Jan",
value: 1507
},
{
date: "Feb",
value: 1600
},
{
date: "Mar",
value: 1281
},
{
date: "Apr",
value: 1898
},
{
date: "May",
value: 1749
},
{
date: "June",
value: 1270
},
{
date: "July",
value: 1712
},
{
date: "Aug",
value: 1270
},
{
date: "Sept",
value: 1257
},
{
date: "Oct",
value: 1257
},
{
date: "Nov",
value: 1277
},
{
date: "Dec",
value: 1057
}
]
///////////////////////////// Update function
const randomizeData = (data) => {
const rand = Math.floor(Math.random() * data.length) + 2
const newData = data.slice(0, rand)
update(newData)
}
///////////////////////////// Create SVG
const w = 400
const h = 250
const margin = {
top: 20,
bottom: 40,
left: 40,
right: 20
}
const width = w - margin.left - margin.right
const height = h - margin.top - margin.bottom
const svg = d3.select(".chart-container").append("svg")
.attr("id", "svg")
.attr("width", w)
.attr("height", h)
const chart = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
const x = d3.scaleBand()
.range([0, width])
.paddingInner([0.1]);
const y = d3.scaleLinear()
.rangeRound([height, 0])
const xAxis = chart.append("g").attr("class", "axis axis-x")
const yAxis = chart.append("g").attr("class", "axis axis-y")
///////////////////////////////// Update function
//Recalc Domains
//Redraw Axis
//Enter
//Update
//Exit
const update = (graphData) => {
// Recalc domains based on new data
// x is scaleBand
x.domain(graphData.map( (d) => d.date) );
y.domain([0, d3.max( graphData, (d) => d.value) ]);
const u = chart.selectAll(".bar")
.data(graphData)
// Enter
u.enter()
.append("rect")
.classed('bar', true)
.attr('x', (d) => x(d.date) )
.attr("y", (d) => y(d.value) )
.attr("width", x.bandwidth() )
.attr("height", (d) => height - y(d.value) )
// Update
u.attr('x', (d) => x(d.date) )
.attr("width", x.bandwidth() )
.attr("y", (d) => y(d.value) )
.attr("height", (d) => height - y(d.value) )
// Exit
u.exit()
.remove()
// Redraw axes based on new data
xAxis.call(d3.axisBottom(x))
.attr("transform", "translate(0," + height + ")");
yAxis.call(d3.axisLeft(y))
}
randomizeData(originalData);
var button = document.getElementById('btn');
button.addEventListener('click', () => randomizeData(originalData) );
h1, h2 {
font-family: Lato;
}
#svg{
background-color: lightgrey;
}
.bar{
shape-rendering: crispEdges;
fill: mediumvioletred;
}
.bar-label{
fill: white;
}
.bar-label{
text-anchor: end;
}
.grid line{
stroke: #bbb;
}
.grid .tick:nth-child(4) line{
stroke-width: 3;
stroke: #999;
}
.divide{
fill: lightgrey;
}
.update-btn{
display: block;
margin-top: 30px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="chart-container"></div>
<button class="update-btn" id="btn">Update</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
</body>
</html>
Source: stackoverflow.com
Related Query
- D3 - Add space between internal bars
- How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?
- How can we add the space between bars for multiple groups of bars in c3js?
- nvd3 space between bars
- How to add space between axis and line in D3 line chart?
- Add space between x-axis and line with time scale
- Vega-lite bar chart space between bars
- Add a space between the axis and text with NVD3 charts
- How to add padding between bars and axis in d3?
- Add space between the stacks in d3v4 stacked bar chart?
- D3 spacing between bars
- D3 Chart- place space between tick values and axis path lines
- How do I add a transition delay between multiple individual transitioning polygons in D3?
- How to add some padding between a path and text on it?
- How do I calculate pairs of Bezier S-curves such that the space between them has constant thickness?
- dc.js month chart uneven spacing between bars
- Equal Space between the legends in d3
- D3: Is it possible to add multiple (more than 2) edges between nodes?
- Add spacing between axis and start point
- D3 - Add event to bars
- How to increase the space between axis and axis label?
- Add some gap in between legend and the chart
- How to add text legends in bars of a bar chart in D3?
- how to start drawing a line with a space between y-axis and it using d3js
- why no gaps between bars D3 js
- Increase space between rings in D3 ring chart
- How to add the fill in between two line in line graph?
- How to add non linear curve between any two points of area chart in d3.js
- tree.nodeSize() not working in D3 tree graph to inc. the space between nodes
- How to draw multiple bars in a row in a bar chart with spaces in between with d3v5?
More Query from same tag
- NVD3 Charts in a Bootstrap carousel
- d3 getting data binding for selection works but when I iterate the selection, it doesn't work
- How to get SVG child element position relative to SVG position on page, D3.js?
- Adding custom axis names to Javascript D3 Parallel Coordinates (CSV has no header line)
- D3.js svg does not draw Map
- d3 world map -- calculate geo bounds on zoom
- Enter/update/exit join pattern Bostock example doesn't seem to work with new data "inserted" at position 0
- putting d3.js elements on the slider
- D3.js collapsible tree - expand/collapse nodes
- render multiple d3 charts without overwriting each other
- Create a step scale in D3
- d3 json - Color a state manually in a map
- Error when drawing a d3.js line with no data
- svg viewbox using d3.js: space above graph is still too large
- d3 linear scale - invalid value
- How to create a custom element in d3
- jshint functions with dots in the name
- AngularJS + d3js: Issues with resizing objects
- Refresh D3 chart everytime on change in textarea data
- D3.js Chord Diagram - Make chords touch the pulled apart arc
- How to prevent edgy drop-shadows for small shapes with D3
- Filtering the data values so as to obtain only necessary values in one of the chart
- Getting dots to render on line chart d3 (Doing this in Meteor)
- d3 table append data according to different data types
- How to save D3 graphs as images in django website?
- d not being set with expected information
- Why are cross-origin Image requests blocked by Cytoscape.js but not by d3.js?
- Loading and viewing data JSON data in d3.js
- dc/d3 line chart crashing down to 0 on the y axis after filtering
- dc.js: Setting Categorical Bar Chart X Axis with a Wide Domain