svg - How to label my axis using words in d3.js -
i make bar chart, every bar represents frequency of word.
my data array of objects, each having property word , freq.
i declared axis :
let echellex = d3.scaleband() .domain(d3.extent(data, d => d.word)) .range([marges.left, width - marges.right]) .padding(0.1); and called :
svg.append("g") .attr("class","axex") .attr("transform", `translate(0,${height})`) .call(axex) .selectall("text") .attr("font-weight", "normal") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", ".5em") .attr("transform", "rotate(-65)"); i add bars svg :
svg.append("g").selectall(".barre").data(data).enter() .append("rect") .attr("class", "barre") .attr("x", (d,i) => echellex(d.word)) .attr("y", (d) => echelley(d.freq)) .attr("width", 2.5) .attr("height", d => height - echelley(d.freq)) .attr("fill","steelblue"); but result :
when change line
.attr("x", (d,i) => echellex(d.word)) to
.attr("x", (d,i) => marges.left + i*3) i this, better, labels of axis still don't match.
i checked documentation of d3.scaleband , tried few tweaks can't seem make work way want to. missing? every bar (d.freq) have corresponding label (d.word) underneath on axis.
band scales ordinal instead of linear, means need provide every possible value domain instead of min/max. d3 cannot interpolate ordinal values.
try this
.domain(data.map(d => d.word)) you have revert change made x attr, band scale should take care of that.
edit: made mistake of thinking d3.map did same thing array map.


Comments
Post a Comment