多线图标签 - D3.js

时间:2014-07-09 16:37:26

标签: javascript d3.js label linegraph

我正在尝试复制Multi-Series Line Chart example,但我无法将标签显示在该行的末尾或完全没有。代码基本上与示例完全相同,但更改了一些单词。数据集属于县人口,其格式与示例中的相同,最早的日期位于列表顶部,最近的日期位于列表底部。

任何人都能看到我失踪的东西吗?

var margin = {top: 20, right: 80, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y%m%d").parse;

var x = d3.time.scale()
    .range([0, width]);

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

var color = d3.scale.category10();

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

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var line = d3.svg.line()
    .interpolate("basis")
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.population); });

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 + ")");

d3.csv("historicalpopulationTest.csv", function(error, data) {
  color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));

  data.forEach(function(d) {
    d.date = parseDate(d.date);
  });

  var counties = color.domain().map(function(name) {
    return {
      name: name,
      values: data.map(function(d) {
        return {date: d.date, population: +d[name]};
      })
    };
  });

  x.domain(d3.extent(data, function(d) { return d.date; }));

  y.domain([
    d3.min(counties, function(c) { return d3.min(c.values, function(v) { return v.population; }); }),
    d3.max(counties, function(c) { return d3.max(c.values, function(v) { return v.population; }); })
  ]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
     .append("text")
      .attr("x", width)
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Year");

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

  var county = svg.selectAll(".county")
      .data(counties)
    .enter().append("g")
      .attr("class", "county");

  county.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("stroke", function(d) { return color(d.name); });

  county.append("text")
      .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
      .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.population) + ")"; })
      .attr("x", width)
      .attr("dy", ".35em")
      .text(function(d) { return d.name; });

});

图表看起来像这样

enter image description here

1 个答案:

答案 0 :(得分:3)

我认为您的问题出现在以下代码中

county.append("text")
  .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
  .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.population) + ")"; })
  .attr("x", width)
  .attr("dy", ".35em")
  .text(function(d) { return d.name; });

我没有运行你的代码,但看起来你正在将文本翻译到行尾,然后将x位置移动一个额外的“宽度”像素数。

尝试将.attr("x", width)更改为。attr("x", 3)