【问题标题】:Getting my csv and d3 to work让我的 csv 和 d3 工作
【发布时间】:2014-03-18 01:06:31
【问题描述】:

大家好,他们还在努力适应 D3,听起来可能很基础。我使用了 wiki 和其他一些帖子,但我认为我的一些东西可能已经过时了。我试图从我的 csv 文件中获取年份和 MPG,并用 x 作为年份来制作散点图。我也不想快捷方式和修改 csv 文件,因为无论有多少属性,我都想学习如何操作。我认为我遇到的部分问题是我的代码的这一部分:

d3.csv("data.csv", function(data) {
  dataset = data.map(function(d) { return [ +d["Year"], +d["MPG"] ]; });
});

  x.domain(d3.extent(data, function(d) { return d.Year; })).nice();
  y.domain(d3.extent(data, function(d) { return d.MPG; })).nice();

但我会让你们判断这一点。这是我目前所拥有的:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
}

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

.dot {
  stroke: #000;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

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

var x = d3.scale.linear()
    .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 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("data.csv", function(data) {
  dataset = data.map(function(d) { return [ +d["Year"], +d["MPG"] ]; });
});

  x.domain(d3.extent(data, function(d) { return d.Year; })).nice();
  y.domain(d3.extent(data, function(d) { return d.MPG; })).nice();

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

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

  svg.selectAll(".dot")
      .data(data)
    .enter().append("circle")
      .attr("class", "dot")
      .attr("r", 3.5)
      .attr("cx", function(d) { return x(d.Year); })
      .attr("cy", function(d) { return y(d.MPG); })
      .style("fill", function(d) { return color(d.species); });

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

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

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

});

</script>

我的 csv 文件如下所示:

汽车、制造商、MPG、气缸、排量、马力、重量、加速度、型号年份、产地 chevelle malibu,雪佛兰,18,8,307,130,3504,12,70,美国 云雀 320,别克,15,8,350,165,3693,11.5,70,美国 卫星,普利茅斯,18,8,318,150,3436,11,70,美国 rebel sst,amc,16,8,304,150,3433,12,70,美国 都灵,福特,17,8,302,140,​​3449,10.5,70,美国

【问题讨论】:

  • d3.csv 是异步的。您需要将所有使用dataset 的代码放入回调中。

标签: csv d3.js


【解决方案1】:

必须添加适当的函数以使其从服务器读取 csv 文件并输入我希望它获取的变量

【讨论】:

    猜你喜欢
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多