javascript - d3折线图-对象挖掘

标签 javascript arrays object d3.js

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

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

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

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

但是这对我不起作用。问题出在域上吗? 请帮忙。

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, d =>d))

    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 = svg.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))

最佳答案

小改动/拼写错误:

您将高度宽度分配给body,而不是svg。交换这两行:

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

并向路径添加一些CSS:

path.line {
  fill: none;
  stroke: #000; 
}

这是一个 fork :https://codepen.io/shashank2104/pen/GwqjVK

关于javascript - d3折线图-对象挖掘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53227253/

相关文章:

javascript - 如何更改所选链接的背景?

c++ - 从ppm文件读取RGB值,并使用结构将其存储到称为 “Image”的2d数组中(动态数组)

javascript - 为什么 'this' 不是我期望的那样?

javascript - 如何从数据库中的对象中删除空值?

JavaScript 错误 - 未捕获的语法错误 : Unexpected number

javascript - jQuery 点击功能——选择器的 css 没有被改变

javascript - 如何在 Chromium 中打开 PDF 文件?

java - 如何检查数组中的所有值是否都高于特定数量?

arrays - 如何扩展 Array<Double>?

c++ - 如何通过对象引用传递函数的参数(C++)