D3响应折线图显示无行

时间:2016-03-18 16:17:21

标签: d3.js

之前我总是使用Angular-nvd3包装器库,那是相对的straith forward。因为我觉得使用这个包装库,构建多个图表变得非常慢。我正试图用D3来完成同样的事情。

我希望有一个完全响应的折线图,它将放入一个指令并在我的项目中需要重用。

我在互联网上找到了一些例子,阅读了一些教程,并试图将这些例子合并到一个工作中。

出于某种原因,我的专线没有显示。

我修改了我的代码,我构建了这一行:

var line = d3.svg.line()
    .x(function(d, i) { return xScale(d); })
    .y(function(d, i) { return yScale(d); });

...

graph.append("path")
    .attr("class", "line")
    .attr("d", function(d) { return line(d); })

我做了一个小提琴,因为显示完整的例子

要容易得多

你去:

http://jsfiddle.net/purewebdesign/2N2rt/217/

1 个答案:

答案 0 :(得分:1)

首先,我们来谈谈您的输入数据。您有一个具有数组属性的单个对象的数组。但是,d3通常喜欢使用对象数组,其中内部对象代表“点”。像这样:

var data = [
    {
    year: "2009",
    value: 5
  },{
    year: "2010",
    value: 10
  },{
    year: "2011",
    value: 15
  },{
    year: "2012",
    value: 20
  },{
    year: "2013",
    value: 25
  }
];

其次,让我们来谈谈你的尺度。您的xScale被创建为d3.time.scale,然后您给它一年的整数表示。这不是date objectd3理解为日期对象的东西。有了这么简单的数据,我认为你应该使用ordinal scale代替。

第三,我们来谈谈你的数据绑定。 d3行函数需要一个数据数组。但是,你的绑定方式,你会一次给出一个观点。相反,您应该使用.datum

将所有这些放在一起并清理它们:

var data = [{
  year: "2009",
  value: 5
}, {
  year: "2010",
  value: 10
}, {
  year: "2011",
  value: 15
}, {
  year: "2012",
  value: 20
}, {
  year: "2013",
  value: 25
}];

var margin = 20,
  width = parseInt(d3.select("#graph").style("width")) - margin * 4.5,
  height = parseInt(d3.select("#graph").style("height")) - margin * 4.5;

var xScale = d3.scale.ordinal()
  .domain(data.map(function(d) {
    return d.year;
  }))
  .rangeBands([0, width], .1);

var yScale = d3.scale.linear()
  .domain(d3.extent(data, function(d) {
    return d.value;
  }))
  .range([height, 0]);

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

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

var line = d3.svg.line()
  .x(function(d, i) {
    return xScale(d.year);
  })
  .y(function(d, i) {
    return yScale(d.value);
  });

var graph = d3.select("#graph")
  .datum(data)
  .attr("width", width + margin * 2)
  .attr("height", height + margin * 2)
  .append("g")
  .attr("transform", "translate(" + margin + "," + margin + ")");

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

graph.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("Price ($)");

graph.append("path")
  .attr("class", "line")
  .attr("d", function(d) {
    return line(d);
  })


var firstRecordVal = data[0].value,
  lastRecordVal = data[data.length - 1].value,
  firstRecordYear = data[0].year,
  lastRecordYear = data[data.length - 1].year;

var first = graph.append("g")
  .attr("class", "first")
  .style("display", "none");

first.append("text")
  .attr("x", 0)
  .attr("y", -10)
  .attr("text-anchor", "end")
  .text("€ " + firstRecordVal);
first.append("circle")
  .attr("r", 3);


var last = graph.append("g")
  .attr("class", "last")
  .style("display", "none");

last.append("text")
  .attr("x", 0)
  .attr("y", -10)
  .text("€ " + lastRecordVal);
last.append("circle")
  .attr("r", 3);

function resize() {
  var width = parseInt(d3.select("#graph").style("width")) - margin * 2,
    height = parseInt(d3.select("#graph").style("height")) - margin * 2;

  xScale.rangeBands([0, width], .1);
  yScale.range([height, 0])

  if (width < 500 && height < 180) {
    graph.select('.x.axis').style("display", "none");
    graph.select('.y.axis').style("display", "none");

    graph.select(".first")
      .attr("transform", "translate(" + xScale(firstRecordYear) + "," + yScale(firstRecordVal) + ")")
      .style("display", "initial");

    graph.select(".last")
      .attr("transform", "translate(" + xScale(lastRecordYear) + "," + yScale(lastRecordVal) + ")")
      .style("display", "initial");
  } else {
    graph.select('.x.axis').style("display", "initial");
    graph.select('.y.axis').style("display", "initial");
    graph.select(".last")
      .style("display", "none");
    graph.select(".first")
      .style("display", "none");
  }

  yAxis.ticks(Math.max(height / 50, 2));
  xAxis.ticks(Math.max(width / 50, 2));

  graph
    .attr("width", width + margin * 2)
    .attr("height", height + margin * 2)

  graph.select('.x.axis')
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

  graph.select('.y.axis')
    .call(yAxis);


  graph.selectAll('.line')
    .attr("d", line);
}

d3.select(window).on('resize', resize);

resize();
body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg id="graph"></svg>

相关问题