javascript - D3 line chart axis text labels in multi line -
i have line chart built in d3.js. needed some customisation. looking split x-axis text labels in 2 lines. want date in 1 line , month in another.
the present chart has "14 dec" in 1 line.
the present chart:

the x-axis labels split 2 lines here. date , month in 2 different lines.
expected x-axis:

var xscale = d3.time.scale().domain([data[0][xkeyval], data[data.length - 1][xkeyval]]).range([margin.left, width]); var yscale = d3.scale.linear().domain([0, d3.max(data, function(d) { return d[ykeyval]; })]).range([height, margin.left]); var xaxisgen = d3.svg.axis() .scale(xscale) .orient("bottom") .ticks(_config.keys.xaxis.ticks) .tickformat(d3.time.format("%d %b")) .ticksize(0); var yaxisgen = d3.svg.axis() .scale(yscale) .orient("left") .tickvalues(_config.keys.yaxis.tickvalues.length > 0 ? _config.keys.yaxis.tickvalues : 1) .ticksize(0);
i'd after generating axis:
svg.append("svg:g") .attr("class", "x axis") .attr("transform", "translate(0," +height + ")") .call(_config.xaxisgen) .selectall('.x .tick text') // select x tick texts .call(function(t){ t.each(function(d){ // each 1 var self = d3.select(this); var s = self.text().split(' '); // text , split self.text(''); // clear out self.append("tspan") // insert 2 tspans .attr("x", 0) .attr("dy",".8em") .text(s[0]); self.append("tspan") .attr("x", 0) .attr("dy",".8em") .text(s[1]); }) }); updated example.
Comments
Post a Comment