score:1
Accepted answer
You are parsing your values using only the abbreviated month name. If you don't specify a year, it defaults to 1900.
Let's proof that:
console.log("A date without year: " + d3.timeParse("%b")("Jan"))
<script src="https://d3js.org/d3.v4.min.js"></script>
So, that 1900
is expected.
Solution: if you want to make sure that the axis only shows the full month name, use tickFormat
with the correct specifier:
.tickFormat(function(d){
return d3.timeFormat("%B")(d)
})
Here is your updated code:
text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke-width: 1.5px;
}
.label {
text-anchor: middle;
}
.label rect {
fill: white;
}
.label-key {
font-weight: bold;
}
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var parseTime = d3.timeParse("%b");
var svg = d3.select("svg");
var margin = {
top: 30,
right: 50,
bottom: 30,
left: 30
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
labelPadding = 3;
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = [{
date: 'Jan',
2017: 130,
2016: 40,
2015: 140
}, {
date: 'Feb',
2017: 137,
2016: 58,
2015: 120
}, {
date: 'Mar',
2017: 166,
2016: 97,
2015: 140
}, {
date: 'Apr',
2017: 154,
2016: 117,
2015: 40
}, {
date: 'May',
2017: 179,
2016: 98,
2015: 110
}, {
date: 'Jun',
2017: 187,
2016: 120,
2015: 140
}, {
date: 'Jul',
2017: 189,
2016: 84,
2015: 30
}, {
date: 'Aug',
2017: 137,
2016: 58,
2015: 50
}, {
date: 'Sep',
2017: 166,
2016: 97,
2015: 55
}, {
date: 'Oct',
2017: 154,
2016: 117,
2015: 80
}, {
date: 'Nov',
2017: 179,
2016: 98,
2015: 40
}, {
date: 'Dec',
2017: 187,
2016: 120,
2015: 140
}];
data.columns = ["date", "2017", "2016", "2015"]
data.forEach(d => {
d.date = parseTime(d.date);
for (var k in d)
if (k !== "da") d[k] = +d[k];
});
var series = data.columns.slice(1).map(function(key) {
return data.map(function(d) {
return {
key: key,
date: d.date,
value: d[key]
};
});
});
var x = d3.scaleTime()
.domain([data[0].date, data[data.length - 1].date])
.range([0, width]);
var y = d3.scaleLinear()
.domain([0, d3.max(series, function(s) {
return d3.max(s, function(d) {
return d.value;
});
})])
.range([height, 0]);
var z = d3.scaleOrdinal(d3.schemeCategory10);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x)
.tickFormat(function(d) {
return d3.timeFormat("%B")(d)
}));
var serie = g.selectAll(".serie")
.data(series)
.enter().append("g")
.attr("class", "serie");
serie.append("path")
.attr("class", "line")
.style("stroke", function(d) {
return z(d[0].key);
})
.attr("d", d3.line()
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.value);
}));
var label = serie.selectAll(".label")
.data(function(d) {
return d;
})
.enter().append("g")
.attr("class", "label")
.attr("transform", function(d, i) {
return "translate(" + x(d.date) + "," + y(d.value) + ")";
});
label.append("text")
.attr("dy", ".35em")
.text(function(d) {
return d.value;
})
.filter(function(d, i) {
return i === data.length - 1;
})
.append("tspan")
.attr("class", "label-key")
.text(function(d) {
return " " + d.key;
});
label.insert("rect", "text")
.datum(function() {
return this.nextSibling.getBBox();
})
.attr("x", function(d) {
return d.x - labelPadding;
})
.attr("y", function(d) {
return d.y - labelPadding;
})
.attr("width", function(d) {
return d.width + 2 * labelPadding;
})
.attr("height", function(d) {
return d.height + 2 * labelPadding;
});
</script>
Source: stackoverflow.com
Related Query
- How do you change the default axis labels in D3
- dimple.js How can I change the labels of a chart axis without changing the data?
- How do you change the last tick value in D3.js?
- in Dimple how do you change the order of the series ina legend?
- How to make the NVD3 discreteBarChart labels on the X axis to adapt to the width or to the number of labels?
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- How do you move number labels further away from the x-axis in d3.js?
- How do you change the interpolation for a zoom behavior in d3.js
- How do you find the distance while drawing line on mouse vertical axis or horizontal?
- How to change the color of a d3.js axis line
- How to change labels on the control button in NVD3?
- How to color the text labels individually on an axis in d3?
- How do you change the width of a Y-axis in a D3 chart?
- How to change the data that appear on the axis using D3.js
- How can you change the height of an x-axis with d3.js
- How can you change the element drawing order depending on the datapoint index?
- How to rotate the text labels for the x Axis of a nvd3.js line chart
- How can i show the labels on the x axis when there is data overlap in d3js?
- How to change the label of x axis in D3
- how do I labels to the axis in this d3 example
- How to setup D3.js axis labels to show rounded numbers with the $ sign?
- How to update the axis scale on change events in cubism/d3
- Change the amount of tick labels on the x axis shown?
- How do you rotate the x and y axis in D3 bar chart
- d3.js: Align text labels between ticks on the axis
- How can I get the D3.js axis ticks and positions as an array?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- D3 how to change the width and length of SVG
- How do you remove the background gridlines in nvd3.js?
- How do I change the legend position in a NVD3 chart?
More Query from same tag
- dc.js - queries related to axis
- Extending paths in D3 with transition
- Class not being applied by JQuery
- D3JS different image for each circle/node
- read updated data on client from file created by server JSP
- Using d3 to visualize rapid fire 'clicks'
- how to reference d3js object on hover and click
- when creating another instance of an infinite animation, it disappears
- Array elements representation in data table
- Creating Multiplie Pie Charts in a same page using d3 with JSON data
- dc scatter plot binding onClick event
- d3 Pie chart won't rerender
- Using d3 SVG Scale without translate
- d3- Find max key/value in nested arrays
- Drag multiple elements that aren't grouped in a `g` tag?
- D3.js linechart - controlling x-axis with dates
- Using JSON data to create treemap using google charts or d3.js
- CSS override isn't working (d3 object)
- Can't append simple svg to body element
- Make the element appear at the end of the arc
- d3 Bar Chart append text to bar
- Style fill for d3.js topojson map not completely filling
- d3js Circle Pack diagram with nested single child arrays
- dc.js render labels with thousand seperator
- How to correctly draw the text over my pie chart?
- Adding a dynamic attribute name to a selection
- Dimple JS multiple y axis - change default sorting
- Plotly.js, show tooltips outside of chart container
- Utilize two objects for a single topojson merge. Multiple datum in d3.select("svg")
- D3.js scale returning wrong values from dataset