【问题标题】:How to use json data instead of tsv file in d3 multi line charts?如何在 d3 多折线图中使用 json 数据而不是 tsv 文件?
【发布时间】:2017-02-04 15:15:17
【问题描述】:

jsfiddle 链接https://jsfiddle.net/8vhsLor6/

基本上我想用 json 对象替换 tsv 文件数据。我收到错误。类型错误

var cities = data.columns.slice(1).map(function(id) {

我有这样的数据

var data = [
{date:"1-May-12","New York":"58.13", "San Francisco":"58.13", "Austin": "43"},
{date:"30-Apr-12","New York":"53.98" , "San Francisco":"48.13", "Austin": "53"},
{date:"27-Apr-12","New York":"67.00", "San Francisco":"38.13", "Austin": "63"},
{date:"26-Apr-12","New York":"89.70", "San Francisco":"28.13", "Austin": "73"},
{date:"25-Apr-12","New York":"99.00", "San Francisco":"18.13", "Austin": "83"}
];

【问题讨论】:

标签: javascript jquery json d3.js


【解决方案1】:

第 4 版中的 d3.tsv:

将数据从 Bostock's code 从 TSV 更改为 JSON(或更准确地说,更改为变量)时,您忘记了一些重要的事情:在新的 D3 v4.x 中,d3.tsv 函数创建了一个 数组属性称为columns

此属性包含 TSV 文件的所有 headers 作为数组。在原始代码中,如果你console.log(data.columns),你会得到这个:

["date", "New York", "San Francisco", "Austin"];

所以,基本上,为了让您的代码正常工作,我所做的只是添加这个属性:

data.columns = ["date", "New York", "San Francisco", "Austin"];

这是你的小提琴:https://jsfiddle.net/uz9rtcwd/

PS:您的日期格式错误。另外,还要解析data数组中的日期(这一步对应d3.tsv中的存取函数,但要记住d3.json没有存取函数)。

【讨论】:

  • 如何让它动态而不是你写的静态?
  • 你想让什么动态化?
【解决方案2】:

要使多线图有几个步骤需要添加:-

var svg = d3.select("svg"),
    margin = {top: 20, right: 80, 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("%e-%b-%y")

var x = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    z = d3.scaleOrdinal(d3.schemeCategory10);

var line = d3.line()
    .curve(d3.curveBasis)
    .x(function(d) { return x(parseTime(d.date)); })
    .y(function(d) { return y(d.temperature); });


var data = [
{date:"1-May-12","New York":"58.13", "San Francisco":"58.13", "Austin": "43"},
{date:"30-Apr-12","New York":"53.98" , "San Francisco":"48.13", "Austin": "53"},
{date:"27-Apr-12","New York":"67.00", "San Francisco":"38.13", "Austin": "63"},
{date:"26-Apr-12","New York":"89.70", "San Francisco":"28.13", "Austin": "73"},
{date:"25-Apr-12","New York":"99.00", "San Francisco":"18.13", "Austin": "83"}
];

	var keys =d3.keys(data[0]);
  var i = keys.indexOf('date')
  if(i != -1) {
    keys.splice(i, 1);
  }
  var cities = keys.map(function(d) { 
    return {
      id:d,
      values: data.map( function(e) {
        return {
          date: e.date,
          temperature: e[d]
        };
      })
  } });

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

  y.domain([
    d3.min(cities, function(c) { return d3.min(c.values, function(d) { return d.temperature; }); }),
    d3.max(cities, function(c) { return d3.max(c.values, function(d) { return d.temperature; }); })
  ]);

  z.domain(cities.map(function(c) { return c.id; }));

  g.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  g.append("g")
      .attr("class", "axis axis--y")
      .call(d3.axisLeft(y))
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", "0.71em")
      .attr("fill", "#000")
      .text("Temperature, ºF");

  var city = g.selectAll(".city")
    .data(cities)
    .enter().append("g")
      .attr("class", "city");

  city.append("path")
      .attr("class", "line")
      .attr("d", function(d) {  return line(d.values); })
      .style("stroke", function(d) { return z(d.id); });

  city.append("text")
      .datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; })
      .attr("transform", function(d) {  return "translate(" + x(parseTime(d.value.date)) + "," + y(d.value.temperature) + ")"; })
      .attr("x", 3)
      .attr("dy", "0.35em")
      .style("font", "10px sans-serif")
      .text(function(d) { return d.id; });
.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}


.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="960" height="500"></svg>

var keys =d3.keys(data[0]); var i = keys.indexOf('date') if(i != -1) { keys.splice(i, 1); } var cities = keys.map(function(d) { return { id:d, values: data.map( function(e) { return { date: e.date, temperature: e[d] }; }) } });

【讨论】:

  • 我真的可以知道这个答案的问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 2016-09-13
  • 2012-12-27
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
相关资源
最近更新 更多