Javascript D3-读取.json并尝试绘制点-未显示任何内容

Javascript D3-读取.json并尝试绘制点-未显示任何内容,javascript,d3.js,Javascript,D3.js,在过去的几年里,我一直在学习各种教程(似乎D3已经经历了一系列的变化),但似乎无法完成这项基本任务。从MonthySales.json加载数据并尝试绘制点。空白屏幕,但控制台中没有任何错误 var h=100; var w=400; d3.json("MonthlySales.json", function(error, data) { var dataset = data var line =

在过去的几年里,我一直在学习各种教程(似乎D3已经经历了一系列的变化),但似乎无法完成这项基本任务。从MonthySales.json加载数据并尝试绘制点。空白屏幕,但控制台中没有任何错误

    var h=100;
    var w=400;
    
    d3.json("MonthlySales.json", function(error, data) { 

        var dataset = data
    
        var line = d3.line()
        .x(function (d) {return ((d.month-20130001)/3.25)})
        .y(function (d) {return h-d.sales; })
    
        var svg = d3.select("body").append("svg").attr("width",w).attr("height",h)
    
        svg.append("path").datum(dataset).attr("class","line").attr("d",line)
           .attr("fill","none").attr("stroke","purple").attr("stroke-width","2")

        });
JSON的设置形式如下:

[
{"month":20130101, "sales":38},
{"month":20130201, "sales":35},
{"month":20130301, "sales":24},
{"month":20130401, "sales":21},
{"month":20130501, "sales":34},
{"month":20130601, "sales":45},
{"month":20130701, "sales":67},
{"month":20130801, "sales":1},
{"month":20130901, "sales":54},
{"month":20131001, "sales":10},
{"month":20131101, "sales":20},
{"month":20131201, "sales":30}
]
在V5/v6上是JS fetch的包装器,fetch是一种承诺

function responseJson(response) {
  if (!response.ok) throw new Error(response.status + " " + response.statusText);
  if (response.status === 204 || response.status === 205) return;
  return response.json();
}

export default function(input, init) {
  return fetch(input, init).then(responseJson);
}
输入参数仍然是指向JSON的路径,但init是fetch()的options对象

responseJson()处理承诺的响应,并将一个JSON对象传递给下一个then()

现在正确的方法是

常数h=100;
常数w=400;
d3.json(“https://gist.githubusercontent.com/jkutianski/57d1d392068b6c5a9216466f9e5d6459/raw/f93d5c296c3b6b69c920c573e010a29cc4339f2a/MonthlySales.json")
.然后(数据集=>{
常量行=d3.line()
.x(d=>(d.month-20130001)/3.25)
.y(d=>h-d.sales);
const svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,w)
.attr(“高度”,h);
追加(“路径”)
.数据集(数据集)
.attr(“类”、“行”)
.attr(“d”,行)
.attr(“填充”、“无”)
.attr(“笔划”、“紫色”)
.attr(“笔划宽度”、“2”);
})
.catch(控制台错误)


d3.选择(“身体”)
应该是
d3.选择(“身体”)
你好,安德鲁-感谢您的发现。修正了,但仍然是没有显示或没有错误的相同结果。当我修改它时,它对我来说显示得很好-但是您使用的是d3的哪个版本?这里是v6:
d3.json(“MonthlySales.json”),function(error,data){
应该是
d3.json(“MonthlySales.json”)。然后(function(data){
从d3v5开始
{
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  }