d3折线图,折线未绘制 [英] d3 line chart, line not drawing

查看:95
本文介绍了d3折线图,折线未绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模仿简单的示例此处,但是我似乎只获取要绘制的x和y轴,该线没有符号.尽管我已经梳理了几次,但我通过数组而不是通过tsv文件加载数据的方式有所不同,我认为所有内容都以相同的方式馈入秤和行中.无效的JSBin 此处:

I'm trying to mimic the simple blocks example here, however I only seem to get the x and y axis to draw, no sign of the line. I'm loading data differently, via an array rather than a tsv file, though I've combed through a few times and I think everything is feeding into the scales and the line the same way. Non-working JSBin here:

<!DOCTYPE html>
<html>
<head>
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<svg width="960" height="500"></svg>

<script>
var data = [["2014-12-31", 0.31999999999999],["2014-11-30", 2.71],["2014-10-31", -4.05],["2014-09-30", 4.22],["2014-08-31", 2.17],["2014-07-31", 5.36],["2014-06-30", 3.99],["2014-05-31", 3.52],["2014-04-30", -.46],["2014-03-31", -8.22],["2014-02-28", 5.89]]

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");



var parseTime = d3.timeParse("%Y-%m-%d");

var x = d3.scaleTime().rangeRound([0, width]);
var y = d3.scaleLinear().rangeRound([height, 0]);

var data = data.map(function(d){
  return [parseTime(d[0]), +d[1]]
});
var line = d3.line()
  .x(function(d){
    return d[0];
  })
  .y(function(d){
    return d[1];
  });

x.domain(d3.extent(data, function(d) { return d[0]; }));
y.domain(d3.extent(data, function(d) { return d[1]; }));

g.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x))
    .select(".domain")
    .remove();

g.append("g")
    .call(d3.axisLeft(y))
    .append("text")
    .attr("fill", "#000")
    .attr("transform", "rotate(-90)")
    .attr("y", 50)
    .attr("dy", "0.9em")
    .attr("text-anchor", "end")
    .text("Price ($)");

g.append("path")
    .datum(data)
    .attr("fill", "none")
    .attr("stroke", "steelblue")
    .attr("stroke-linejoin", "round")
    .attr("stroke-linecap", "round")
    .attr("stroke-width", 3)
    .attr("d", line);
</script>

</body>
</html>

我明显在做错什么吗?

anything obvious that I'm doing wrong?

推荐答案

问题是您没有缩放数据以使其正确地适合svg坐标空间:

The issue is that you are not scaling your data to fit properly within your svg coordinate space:

var line = d3.line()
  .x(function(d){
    return d[0];
  })
  .y(function(d){
    return d[1];
  });

这部分代码设置数据的绘制坐标,并且当前您将数据值用作x和y svg值,而没有任何缩放比例.您需要根据自己的规模进行缩放:

This section of code sets the plotted coordinates of your data, and you are currently using your data values as your x and y svg values without any scaling. You need to scale it according to your scale:

var line = d3.line()
  .x(function(d){
    console.log(x(d[0])); return x(d[0]);
  })
  .y(function(d){
    return y(d[1]);
  });

这将使您的图形能够按预期绘制.

This will allow your graph to draw as intended.

这篇关于d3折线图,折线未绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆