首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >d3线图-对象挖掘

d3线图-对象挖掘
EN

Stack Overflow用户
提问于 2018-11-09 14:06:46
回答 1查看 47关注 0票数 0

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

代码语言:javascript
运行
复制
[{
    2010: 8236.082,
    countryName: "Afghanistan"
}]

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

代码语言:javascript
运行
复制
[
     {
         country: "Afganistan",
         emissions: [
             { year: 2019, amount: 8236.082 }
         ]
     }
]

不过,这对我不起作用。这个领域有问题吗?请帮帮忙。

科德芬

代码语言:javascript
运行
复制
//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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-09 14:42:08

小改动/错误:

您将heightwidth分配给body而不是svg。互换这两行:

代码语言:javascript
运行
复制
let svg = d3.select("body")
  .append("svg")
  .attr("width", fullWidth)
  .attr("height", fullHeight)

将一些CSS添加到路径中。

代码语言:javascript
运行
复制
path.line {
  fill: none;
  stroke: #000; 
}

这里有个叉子:https://codepen.io/shashank2104/pen/GwqjVK

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53227253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档