简体   繁体   中英

D3 show values as label on right axis

I am doing a dot plot in D3 and I want to add the values along a right Y axis. I have done this before in many charts, adding labels is straightforward, but for some reason this particular chart is giving a lot of problems.

I cant get the values of the dots to show on the right axis.

jsfiddle: The chart appears on click.

The relevant code for the value labels attached to the right axis:

svg.append("g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + (width + 10) + " ,0)")    
    .call(yAxis1)
    .selectAll('text')
    .text(function(d){ return xScale(d.value); });

With an ordinal axis you are binding your domain values to the axis ticks. So, the scale domain should be:

var yScale1 = d3.scale.ordinal()
    .domain(data.map(function(d) { return d.value; })) //<-- use '.value'
    .rangeRoundPoints([0, height]);

Then your y-axis call just becomes:

svg.append("g")
  .attr("class", "y axis")
  .attr("transform", "translate(" + (width + 10) + " ,0)")  
  .call(yAxis1);

Updated fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM