2017-04-26 80 views
-4

我正試圖從json將我的數據激活到d3可視化。 我有一個JSON結果返回給我日期和數量。如何使用json格式而不是csv使用d3?

如何用json替換這個函數? 原始鏈接Got my sample of d3 from here

d3.csv('@Server.MapPath("Views/file/sp500.csv")', type, function (error, data) { 

JSON文件

[{Month: "February 2014", Count: 50}, {Month: "March 2014", Count: 66},…] 
這個它沒有火JSON目前

,但有一個錯誤,說cannot read the property of null

d3.json("/JuraServicing/GetStatistic", function (data) { 
+0

您是否修改了您的代碼版本以在示例數據中引用'Month'和'Count'屬性而不是'date'和'price'? – Mark

+0

嗨,是啊,我做了修改,並相應地改變了所有的變量json列 –

回答

1

你只需要擁有雙JSON文件中引用的鍵值。它應該工作得很好。

這是工作的罰款:

<!DOCTYPE html> 
<html> 
<head> 
    <script src="https://d3js.org/d3.v4.min.js"></script> 
</head> 
<body> 
    <script> 
     d3.json("/data.json", function (data) { 
       document.write(JSON.stringify(data)) 
     }); 
    </script> 
</body> 
</html> 

,這裏是data.json

[{"Month": "February 2014", "Count": 50}, {"Month": "March 2014", "Count": 6}] 

希望它可以幫助!

+0

您好我的json是從這裏生成的。所以我不能添加雙引號文件.'公共JsonResult GetStatistic(字符串年){ var machinelist = service.GetAllServicMachine(); var q = from machinelist group i by Convert.ToDateTime(i.receive_datum).ToString(「MMM yyyy」)into grp select new {Count = grp.Count(),Month = grp.Key,}; return Json(q,JsonRequestBehavior.AllowGet); }' –

+0

但它不是一個JSON,它實際上是一個JavaScript對象,它不能被d3.json讀取 –

+0

返回的結果是一個json。我嘗試與其他有JSON例子的D3,我只是通過我的網址和工作正常。這是給出的示例代碼是CSV,所以我把它轉換成JSON認爲它不會工作。 –

相關問題