简体   繁体   English

带有D3的JSON中的嵌套数组

[英]Nested arrays in JSON with D3

I'm new with d3js and am trying to plot a line from an array nested in a JSON. 我是d3js的新手,正在尝试从嵌套在JSON中的数组中绘制一条线。 Few questions on the topic have already been asked but their solutions don't work for my specific problem (maybe it's just me being a total novice though). 关于该主题的问题很少有人提出,但是他们的解决方案对我的特定问题不起作用(也许只是我是一个新手而已)。

The JSON file (json_data.json) in question is this: 有问题的JSON文件(json_data.json)是这样的:

{"Test": 1, 
 "Name": "SampleA", 
 "Voltage": [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1], 
 "Current": [-0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5]}

"Voltage" and "Current" are the x and y values I want to plot with this js: “电压”和“电流”是我想用此js绘制的x和y值:

var margin = {top: 50, right: 50, bottom: 50, left: 50},
    width = 800 - margin.left - margin.right,
    height = 600 - margin.top - margin.bottom;

var xScale = d3.scaleLinear().range([0, width]);
var yScale = d3.scaleLinear().range([height, 0]);

var xAxis = d3.axisBottom().scale(xScale);
var yAxis = d3.axisLeft().scale(yScale);

var line = d3.line()
    .x(function(d,i){return d.Voltage[i]; } )
    .y(function(d,i){return d.Current[i]; } );

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 + ")");

d3.json("json_data.json", function(data) {

xScale.domain(d3.extent(data.Voltage));
yScale.domain(d3.extent(data.Current));

svg.append("path")     
    .attr("d", line(data))

svg.append("g")       
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

svg.append("g")        
    .attr("class", "y axis")
    .call(yAxis);

});

The problem is that for some reason the line chart is not displayed. 问题是由于某种原因未显示折线图。 The axes are shown with the correct scale which makes me think there must be an issue with the line() generator. 以正确的比例显示轴,这使我认为line()生成器一定存在问题。 The JSON and the nested arrays show up when I run console.log. 当我运行console.log时,将显示JSON和嵌套数组。

I'd really appreciate your help! 非常感谢您的帮助! And if you have any suggestions on how to improve my code or JSON structure I'm listening :) Thanks a lot! 如果您对如何改善代码或JSON结构有任何建议,我在听:)非常感谢!

d3.line "generates a line for the given array of data " ( https://github.com/d3/d3-shape#_line ). d3.line“为给定的数据数组生成一行”( https://github.com/d3/d3-shape#_line )。

So I would create an array of points, such as: 因此,我将创建一个点数组,例如:

let lineData = []
let lineDataLength = data.Voltage.length;

for (var i = 0; i < lineDataLength; i++) {
      let point = {};
      point.x = data.Voltage[i];
      point.y = data.Current[i];
      lineData.push(point);
};

Then for the line generator simplify to: 然后将线路生成器简化为:

var line = d3.line()
    .x(function(d,i){return xScale(d.x); } )
    .y(function(d,i){return yScale(d.y); } );

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

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