【问题标题】:Parsing Array for a Stacked Area Chart in D3在 D3 中解析堆积面积图的数组
【发布时间】:2017-11-30 20:51:54
【问题描述】:

我正在尝试修改示例堆积面积图here

目前它给了我这样的错误:

Error: <path> attribute d: Expected number, "MNaN,22770LNaN,21…".

在这一行:.attr('d', area);

这是我目前的代码:

let margin = {top: 20, right: 60, bottom: 30, left: 60},
        width = 960 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

    let parseDate = d3.timeParse('%m/%d/%Y');

    let x = d3.scaleTime().range([0, width]),
        y = d3.scaleLinear()
            .range([height, 0]),
        z = d3.scaleOrdinal()
        .domain(['Detractors', 'Passives' , 'Promoters'])
        .range(['#e81123', '#a0a1a2', '#7fba00']);

    let stack = d3.stack();

    let area = d3.area()
        .x(function(d) { return x(d.data.date); })
        .y0(function(d) { return y(d.y0); })
        .y1(function(d) { return y(d.y0 + d.y); });

    let g = this._svg.append('g')
        .attr('transform', 'translate(' + 0 + ',' + margin.top + ')');

    let data = [
                { 'date': '04/23/12' , 'Detractors': 20 , 'Passives': 30 , 'Promoters': 50 },
                { 'date': '04/24/12' , 'Detractors': 32 , 'Passives': 19 , 'Promoters': 42 },
                { 'date': '04/25/12' , 'Detractors': 45 , 'Passives': 11 , 'Promoters': 44 },
                { 'date': '04/26/12' , 'Detractors': 20 , 'Passives': 13 , 'Promoters': 64 }];

    // console.log(myData.map(function (d) { return d.key; }));

    // console.log('KEYS: '+ keys);

    x.domain(d3.extent(data, function(d) {
        console.log(parseDate(d.date));
        return parseDate(d.date); }));
    // let keys = (d3.keys(data[0]).filter(function (key) { return key !== 'date'; }));
    z.domain(d3.keys(data[0]).filter(function (key) { return key !== 'date'; }));
    // z.domain(keys);
    stack.keys(d3.keys(data[0]).filter(function (key) { return key !== 'date'; }));

    let layer = g.selectAll('.layer')
        .data(stack(data))
        .enter().append('g')
        .attr('class', 'layer');

    layer.append('path')
        .attr('class', 'area')
        .style('fill', function(d) {
            // console.log('d.key: ' + d.key);
            console.log('d.key: ' + d.key +  ' color: ' + z(d.key));
            return z(d.key); })
        .attr('d', area);

    layer.filter(function(d) { return d[d.length - 1][1] - d[d.length - 1][0] > 0.01; })
        .append('text')
        .attr('x', width - 6)
        .attr('y', function(d) { return y((d[d.length - 1][0] + d[d.length - 1][1]) / 2); })
        .attr('dy', '.35em')
        .style('font', '10px sans-serif')
        .style('text-anchor', 'end')
        .text(function(d) {
            // console.log('key label: ' + d.key);
            return d.key; });


    g.append('g')
        .attr('class', 'axis axis--x')
        .attr('transform', 'translate(0,' + height + ')')
        .call(d3.axisBottom(x));

        // console.log(height);
    g.append('g')
        .attr('class', 'axis axis--y')
        .call(d3.axisLeft(y).ticks(5, '%'));

    }

我只是想解析数组,以便正确解析日期、键和值。我只是似乎无法正确。

非常感谢您的帮助!

【问题讨论】:

  • 修复是我的数据应该是这样的:let data = [ { 'date': '04/23/12' , 'Detractors': 0.20 , 'Passives': 0.30 , 'Promoters': 0.50 }, { 'date': '04/24/12' , 'Detractors': 0.32 , 'Passives': 0.19 , 'Promoters': 0.42 }, { 'date': '04/25/12' , 'Detractors': 0.45 , 'Passives': 0.11 , 'Promoters': 0.44 }, { 'date': '04/26/12' , 'Detractors': 0.20 , 'Passives': 0.13 , 'Promoters': 0.64 }];

标签: javascript arrays parsing typescript d3.js


【解决方案1】:

您遇到的具体错误是由于数据中的日期格式造成的。您的代码中有一行注释掉:let parseDate = d3.timeParse("%m/%d/%Y"); 您确实想使用 d3.timeParse 函数将数据转换为日期格式:

let tp = d3.timeParse("%m/%d/%y")
for(var i=0; i<data.length;i++){
  data[i].date = tp(data[i].date) 
}

【讨论】:

  • 我已将行更新为 x.domain(d3.extent(data, function(d) { console.log(parseDate(d.date)); return parseDate(d.date); })); 我什至已经尝试过使用 for 循环的方式,但这仍然给我与 area 函数在同一行的错误
  • 这是我为测试它而创建的小提琴。似乎我对格式做了很多清理,但我认为我没有更改代码的其他部分 - 可能添加了缺少的括号或其他内容。如果你注释掉我的 for 循环 - 你会得到一个错误......循环,没有错误(但图表没有完全呈现)jsfiddle.net/genestd/p7yabxup
  • 我已将 for 循环放置在您放置它的位置,它只是将整个图表显示为红色。就像你说的图表的其余部分没有呈现或者其余的数据不是我不太确定我相信的括号都匹配。
猜你喜欢
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多