简体   繁体   English

使用2级嵌套JSON创建带有点的D3线图

[英]Creating a D3 Line Graph with Points using 2 Level Nested JSON

For the life of me, I can't seem to find a clear answer on how to handle this. 对于我的生活,我似乎无法找到如何处理这个问题的明确答案。 Granted, I'm still pretty new to javascript and D3, but I can't find any good examples. 当然,我仍然是javascript和D3的新手,但我找不到任何好的例子。

I am currently trying to create a D3 multi-line graph with points. 我目前正在尝试使用点创建D3多线图。 I've had some pretty good success with a flat JSON file, but when my JSON data was modified to add an additional layer of nesting, I really began to struggle. 我使用平坦的JSON文件取得了相当不错的成功,但是当我的JSON数据被修改为添加额外的嵌套层时,我真的开始挣扎。

My JSON data file now looks something like this: 我的JSON数据文件现在看起来像这样:

{
    "Model": "Model A", 
    "Data": [
        {
            "Metric": "Metric 1", 
            "Data": [
                    {"Date":"2017-2-25", "Value": "34"}, 
                    {"Date":"2017-2-26", "Value": "52"},
                    {"Date":"2017-2-27", "Value": "47"},
                    {"Date":"2017-2-28", "Value": "50"}
             ]
        }, 
        {
            "Metric": "Metric 2", 
            "Data": [
                    {"Date":"2017-2-25", "Value": "22"}, 
                    {"Date":"2017-2-26", "Value": "27"},
                    {"Date":"2017-2-27", "Value": "25"},
                    {"Date":"2017-2-28", "Value": "21"}
             ]
        }, 
        {
            "Metric": "Metric 3", 
            "Data": [
                    {"Date":"2017-2-25", "Value": "27"}, 
                    {"Date":"2017-2-26", "Value": "28"},
                    {"Date":"2017-2-27", "Value": "25"},
                    {"Date":"2017-2-28", "Value": "22"}
             ]
        }
    ]
} 

This extra nested layer was added so I could filter by model and metric. 添加了这个额外的嵌套层,因此我可以按模型和指标进行过滤。

Assuming I do not have multiple "Models" in my JSON file (so taking the JSON exactly as it is from the above snippet). 假设我的JSON文件中没有多个“模型”(因此从上面的代码片段中获取完全相同的JSON)。 How do I effectively plot Value and Date per metric (multi-line chart)? 如何有效地绘制每个度量的ValueDate (多线图)? If anyone could point me to some working examples as well, that would be an incredible help! 如果有人能指出我的一些工作实例,那将是一个不可思议的帮助!

Given your data structure, a possible solution is binding the data (specifically the outer Data array) to groups: 根据您的数据结构,可能的解决方案是将数据(特别是外部Data阵列)绑定到组:

var groups = svg.selectAll("foo")
    .data(data.Data)
    .enter()
    .append("g");

And using each inner Data array as the data to each line: 并使用每个内部Data数组作为每行的数据:

var lines = groups.append("path")
    .attr("d", d => lineGen(d.Data))
    .attr("fill", "none")
    .attr("stroke", (d, i) => colors(i));

Here is a demo using your data object: 这是一个使用您的数据对象的演示:

 var data = { "Model": "Model A", "Data": [{ "Metric": "Metric 1", "Data": [{ "Date": "2017-2-25", "Value": "34" }, { "Date": "2017-2-26", "Value": "52" }, { "Date": "2017-2-27", "Value": "47" }, { "Date": "2017-2-28", "Value": "50" }] }, { "Metric": "Metric 2", "Data": [{ "Date": "2017-2-25", "Value": "22" }, { "Date": "2017-2-26", "Value": "27" }, { "Date": "2017-2-27", "Value": "25" }, { "Date": "2017-2-28", "Value": "21" }] }, { "Metric": "Metric 3", "Data": [{ "Date": "2017-2-25", "Value": "27" }, { "Date": "2017-2-26", "Value": "28" }, { "Date": "2017-2-27", "Value": "25" }, { "Date": "2017-2-28", "Value": "22" }] }] }; var width = 500, height = 300; var colors = d3.scaleOrdinal(d3.schemeCategory10); var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height); var parse = d3.timeParse("%Y-%m-%d"); var xScale = d3.scaleTime() .range([30, width - 20]) .domain(d3.extent(data.Data[0].Data, d => parse(d.Date))); var yScale = d3.scaleLinear() .range([height - 20, 20]) .domain([0, 60]); var lineGen = d3.line() .x(d => xScale(parse(d.Date))) .y(d => yScale(d.Value)); var groups = svg.selectAll("foo") .data(data.Data) .enter() .append("g"); var lines = groups.append("path") .attr("d", d => lineGen(d.Data)) .attr("fill", "none") .attr("stroke", (d, i) => colors(i)); var gX = svg.append("g").attr("transform", "translate(0," + (height - 20) + ")").call(d3.axisBottom(xScale)); var gY = svg.append("g").attr("transform", "translate(30,0)").call(d3.axisLeft(yScale)); 
 <script src="https://d3js.org/d3.v4.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM