簡體   English   中英

使用D3js在線圖上顯示標簽時出錯

[英]Error on label displaying on line plot with D3js

我有一個兩線圖,其點被D3js從tsv文件淹沒。 我想將標簽顯示在圓點附近。 tsv文件不完整,缺少一些值,如下所示:

year    value1    value2
....
2000    8956355 
2001    8924704 
2002    8865723 
2003    8747717 
2004    8701521 
2004    8701521 
2005        11380809
2006        10672554
2007    8513818 10394369
2008    8462607 10297716
2009    8448535 9998783
2010    8411177 9988697
2011    8024205 9491908
2012    7920080 8725402
2013    7911208 8668111
2014    7807984 8274928
2015    7747598 8027083
2016    7575879 7779103

我用於顯示第一行,點和標簽的代碼如下(第二行相同):

// line1
fw1.append("path")
    .datum(data)
        .attr("class", "line")
        .attr("fill", "none")
        .attr("stroke", "#004494")
        .attr("stroke-linejoin", "round")
        .attr("stroke-linecap", "round")
        .attr("stroke-width", 1.5)
        .attr("d", line1);
// dots for line1
fw1.selectAll(".dot")
        .data(data)
            .enter()
            .append("circle")
                .attr("class", "dot")
                .style("stroke", "#004494")
                .attr("cx", function(d) { return x1(d.year); })
                .attr("cy", function(d) { return y1(d.value1); })
                .attr("r", 3.5)
fw1.append("g")
    .classed('labels-group', true)
    .selectAll('text')
    .data(data)
    .enter()
    .append('text')
        .classed('textlbl', true)
        .attr({ 'x': function(d) { return x1(d.year);},
                    'y': function(d) { return y1(d.value1);}
        })
    .text(function(d) { return d.value1 = (d.value1==="") ? null : +d.value1; });

正確顯示了兩行和點,但是代碼為標簽“未捕獲的TypeError:無法讀取屬性'text'為null”返回了以下錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM