【问题标题】:d3 path d = MNaN,NaNLNaN,NaNd3 路径 d = MNaN,NaNLNaN,NaN
【发布时间】:2019-04-12 01:40:45
【问题描述】:

尝试构建折线图(多行)。初始数据一直是一个对象数组,例如:

[{
    2010: 8236.082,
    countryName: "Afghanistan"
}]

每一行都需要一个 x/y 对数组[[x,y],[x,y]]。我的xyyearamount 的排放量。这意味着我必须重组我的数据使其看起来像这样:

[
     {
         country: "Afganistan",
         emissions: [
             { year: 2019, amount: 8236.082 }
         ]
     }
]

数据处理后,我被路径 d = MNaN,NaNLNaN,NaN 困住了。我在这里做错了什么?

Codepen

//Define full width, full height and margins
let fullWidth = 600;
let fullHeight = 700;
let margin = {
    top: 20,
    left: 70,
    bottom: 100,
    right: 10
}

//Define line chart with and height 
let width = fullWidth - margin.left - margin.right;
let height = fullHeight - margin.top - margin.bottom;

//Define x and y scale range
let xScale = d3.scaleLinear()
    .range([0, width])

let yScale = d3.scaleLinear()
    .range([0, height])

//Draw svg
let svg = d3.select("body")
    .attr("width", fullWidth)
    .attr("height", fullHeight)
    .append("svg")
    .append("g")

d3.json("https://api.myjson.com/bins/izmg6").then(data => {
    console.log(data);



    //Structure data so should be an array of arrays  etc [[x,y], [x,y], [x,y]]

    let years = d3.keys(data[0]).slice(0, 50);
    console.log(years);

    let dataset = [];

    data.forEach((d, i) => {

        let myEmissions = [];

        years.forEach(y => {
            if (d[y]) {

                myEmissions.push({
                    year: y,
                    amount: d[y]
                })
            }
        })

        dataset.push({
            country: d.countryName,
            emissions: myEmissions
        });
    })

    console.log(dataset);

    //Define x and y domain
    xScale
        .domain(d3.extent(years))

    yScale
        .domain([d3.max(dataset, d =>
        d3.max(d.emissions, d =>
            +d.amount)), 0])


        //Generate line
    let line = d3.line()
                .x(d => {
                    xScale(d.year);
                })
                .y(d => {
                    yScale(d.amount);
                });


    let groups = d3.selectAll("g")
        .data(dataset)
        .enter()
        .append("g")

    groups.append("title")
        .text(d => d.country)

    groups.selectAll("path")
        .data(d => [d.emissions])
        .enter()
        .append("path")
        .attr("d", line)
        .attr("class", line)

}).catch(error => console.log(error))

【问题讨论】:

  • 你在哪里看到一堆 NaN 值?
  • @Amy 如果你去 codepen 并检查输出窗口,SVG 由带有 NaN 的路径组组成。
  • 一件事,不相关,看起来你的路径组被附加到你的 SVG 之外。
  • @PeterCollingridge 啊,我花了一些时间搜索,但我终于看到了他们。谢谢。 document.querySelectorAll("g > path")
  • @PeterCollingridge 感谢必须是 groups = svg.selectAll("g")

标签: javascript arrays object d3.js


【解决方案1】:

NaN 的主要问题是

let line = d3.line()
   .x(d => xScale(d.year) )
   .y(d => yScale(d.amount) );

但这不是图表的唯一问题。

【讨论】:

  • 感谢 d 路径现在具有值。你能指导我还有什么问题吗?
猜你喜欢
  • 2017-11-24
  • 2017-10-04
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 2021-11-27
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多