Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript D3折线图-错误:<;路径>;属性d:预期数量_Javascript_D3.js - Fatal编程技术网

Javascript D3折线图-错误:<;路径>;属性d:预期数量

Javascript D3折线图-错误:<;路径>;属性d:预期数量,javascript,d3.js,Javascript,D3.js,我得到了这个CSV数据(只是一个示例-它更大): 我找不到任何显示没有日期函数的折线图的例子,所以我修改了这个例子来制作线性刻度,但它不起作用。我试图用这段代码制作折线图,但我得到了错误:属性d:预期的数字 // set the dimensions and margins of the graph var margin = {top: 20, right: 20, bottom: 30, left: 50}, width = 960 - margin.left - margin.rig

我得到了这个CSV数据(只是一个示例-它更大):

我找不到任何显示没有日期函数的折线图的例子,所以我修改了这个例子来制作线性刻度,但它不起作用。我试图用这段代码制作折线图,但我得到了错误:属性d:预期的数字

// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;


// set the ranges
var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);

// define the line
var valueline = d3.line()
    .x(function(d) { return x(d.value); })
    .y(function(d) { return y(d.count); });

// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
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 + ")");

// Get the data
d3.csv("exp2.csv", function(error, data) {
  if (error) throw error;

  // format the data
  data.forEach(function(d) {
      d.value = +d.value;
      d.count = +d.count;
  });

  // Scale the range of the data
  x.domain([0, d3.max(data, function(d) { return d.value; })]);
  y.domain([0, d3.max(data, function(d) { return d.count; })]);

  // Add the valueline path.
  svg.append("path")
      .data([data])
      .attr("class", "line")
      .attr("d", valueline);

  // Add the X Axis
  svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  // Add the Y Axis
  svg.append("g")
      .call(d3.axisLeft(y));

});

正如@GerardoFurtado所指出的,您的数据不是以“
csv
”分隔的(即“逗号分隔的),而是以分号分隔的。我最初的想法是将其作为的副本关闭,但看起来在版本4中语法已经发生了足够多的变化,因此我需要一个新的答案

这样做:

// set up a parser
var parser = d3.dsvFormat(';').parse;

// get the data as text
d3.text("exp2.csv", function(error, data) {

  if (error) throw error;

  // now parse the data
  data = parser(data);

  // format the data
  data.forEach(function(d) {
    d.value = +d.value;
    d.count = +d.count;
  });

  ...

这是一份完整的报告。

对吗?CSV是逗号分隔的。。。无论如何,代码没有问题:我用完整的数据集进行了尝试,但失败了-我删除了解析器并使数据成为真实的csv。数据集太大了吗?@darude,你把文件改成了逗号。你的问题有分号,这就是我回答的全部原因。如果你把你的文件改成逗号,我看到了-d3.csv(“exp2.csv”,function(error,data)x d3.text(“exp2.csv”,function(error,data)。非常感谢!
// set up a parser
var parser = d3.dsvFormat(';').parse;

// get the data as text
d3.text("exp2.csv", function(error, data) {

  if (error) throw error;

  // now parse the data
  data = parser(data);

  // format the data
  data.forEach(function(d) {
    d.value = +d.value;
    d.count = +d.count;
  });

  ...