简体   繁体   中英

D3 left justify legend text

I have a bar graph with a legend see below

在此处输入图片说明

this works fine however i can't seem to get the text to left justify

<script>

var margin = {top: 20, right: 55, bottom: 30, left: 40},
    width = 350 - margin.left - margin.right,
    height = 350 - margin.top - margin.bottom;

var x0 = d3.scale.ordinal()
    .rangeRoundBands([0, width], .1);

var x1 = d3.scale.ordinal();

var y = d3.scale.linear()
    .range([height, 0]);

var color = d3.scale.ordinal()
    .range(["#7CA2C8", "#ECAD6F", "#C3D1DC", "#7CC8A2","#C49AC4", "#F6D587", "#AAC1B4", "#DBB087", "#E7CBC0", "#A2C87C", "#BAA187","#8BBFDA"]);

var xAxis = d3.svg.axis()
    .scale(x0)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(d3.format(".2s"));

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var dataFile = ".\\temp\\data.csv";
d3.csv(dataFile, function(error, data) {
  if (error) throw error;

data.sort(function(a, b){ return d3.ascending(a.Year, b.Year); })

  var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "Year"; });

  data.forEach(function(d) {
    d.etypes = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
  });

  x0.domain(data.map(function(d) { return d.Year; }));
  x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
  y.domain([0, d3.max(data, function(d) { return d3.max(d.etypes, function(d) { return d.value; }); })]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", -35)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Turnover Ratio");

  var year = svg.selectAll(".year")
      .data(data)
      .enter().append("g")
      .attr("class", "year")
      .attr("transform", function(d) { return "translate(" + x0(d.Year) + ",0)"; });

  year.selectAll("rect")
      .data(function(d) { return d.etypes; })
      .enter().append("rect")
      .attr("width", x1.rangeBand())
      .attr("x", function(d) { return x1(d.name); })
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); })
      .style("fill", function(d) { return color(d.name); });

  var legend = svg.selectAll(".legend")
      .data(ageNames.slice().reverse())
    .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });


  legend.append("rect")
      .attr("x", width - 5)
      .attr("y", 5)
      .attr("width", 5)
      .attr("height", 5)
      .style("fill", color);

  legend.append("text")
      .attr("x", width + 53)
      .attr("y", 5)
      .attr("dy", ".35em")
      .style("text-anchor", "end")
      .text(function(d) { return d; });



});

</script>

.style("text-anchor", "end") is what i have ...changing it to start gves this however

在此处输入图片说明

which is even worse...how would i get Term to left justify over to where Permanent is?

You have to adjust the position of the texts, not just the text-anchor attribute:

legend.append("rect")
    .attr("x", width - 5)
    .attr("y", 5)
    .attr("width", 5)
    .attr("height", 5)
    .style("fill", color);

legend.append("text")
    .attr("x", width -8)
    .attr("y", 5)
    .attr("dy", ".35em")
    .style("text-anchor", "end")
    .text(function(d) { return d; });

Check the demo:

 var margin = {top: 20, right: 55, bottom: 30, left: 40}, width = 350 - margin.left - margin.right, height = 350 - margin.top - margin.bottom; var color = d3.scale.ordinal() .range(["#7CA2C8", "#ECAD6F", "#C3D1DC", "#7CC8A2","#C49AC4", "#F6D587", "#AAC1B4", "#DBB087", "#E7CBC0", "#A2C87C", "#BAA187","#8BBFDA"]); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var ageNames = ["Team", "Permanent"]; var legend = svg.selectAll(".legend") .data(ageNames.slice().reverse()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width - 5) .attr("y", 5) .attr("width", 5) .attr("height", 5) .style("fill", color); legend.append("text") .attr("x", width -8) .attr("y", 5) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

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