2017-08-22 2 views
0

私はd3jsグラフを使用して折れ線グラフを取得しようとしています。以下はコードです。これは、次のエラーです。「属性d:期待値」、「M49、NaNZ」。「x軸を名前として、yをカウントとして解析しています。データを数値として取得していない行のパスをプロットする際に、コードに何か問題があるようです。D3js折れ線グラフエラー "属性d:予想される数値" M49、NaNZ "。"

var margin = { 
 
    top: 30, 
 
    right: 20, 
 
    bottom: 30, 
 
    left: 50 
 
}; 
 
var width = 600 - margin.left - margin.right; 
 
var height = 270 - margin.top - margin.bottom; 
 

 

 

 
var x = d3.scale.ordinal() 
 
    .rangeRoundBands([0, width], .1); 
 

 
var y = d3.scale.linear().range([height, 0]); 
 

 
var xAxis = d3.svg.axis().scale(x) 
 
    .orient("bottom").ticks(5); 
 

 
var yAxis = d3.svg.axis().scale(y) 
 
    .orient("left").ticks(5); 
 

 
var valueline = d3.svg.line() 
 
    .x(function (d) { 
 
     return x(d.name); 
 
    }) 
 
    .y(function (d) { 
 
     return y(d.count); 
 
    }); 
 

 
var svg = d3.select("body") 
 
    .append("svg") 
 
    .attr("width", width + margin.left + margin.right) 
 
    .attr("height", height + margin.top + margin.bottom) 
 
    .append("g") 
 
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 
 

 
// Get the data 
 
var data = [{ 
 
       "name": "Seasonal Pop", 
 
       "code": "SEASONAL_POP", 
 
       "children": [{ 
 
        "name": "SEASONAL_LYQ1", 
 
        "code": "SEASONAL_LYQ1", 
 
        "count": 1200 
 
       }, { 
 
        "name": "SEASONAL_LYQ2", 
 
        "code": "SEASONAL_LYQ2", 
 
        "count": 2000 
 
       }, { 
 
        "name": "SEASONAL_LYQ3", 
 
        "code": "SEASONAL_LYQ3", 
 
        "count": 1060 
 
       }, { 
 
        "name": "SEASONAL_LYQ4", 
 
        "code": "SEASONAL_LYQ4", 
 
        "count": 2300 
 
       }, { 
 
        "name": "SEASONAL_CYQ1", 
 
        "code": "SEASONAL_CYQ1", 
 
        "count": 1300 
 
       }, { 
 
        "name": "SEASONAL_CYQ2", 
 
        "code": "SEASONAL_CYQ2", 
 
        "count": 3400 
 
       }, { 
 
        "name": "SEASONAL_CYQ3", 
 
        "code": "SEASONAL_CYQ3", 
 
        "count": 4500 
 
       }, { 
 
        "name": "SEASONAL_CYQ4", 
 
        "code": "SEASONAL_CYQ4", 
 
        "count": 5500 
 
       }] 
 
      }]; 
 

 
data.forEach(function (d) { 
 
    d.name = +d.date ; 
 
    d.count = +d.count; 
 
}); 
 

 
// Scale the range of the data 
 
x.domain(d3.extent(data, function (d) { 
 
    return d.name; 
 
    })); 
 
y.domain([0, d3.max(data, function (d) { 
 
    return d.count; 
 
    })]); 
 

 
svg.append("path") // Add the valueline path. 
 
.attr("d", valueline(data)); 
 

 
svg.append("g") // Add the X Axis 
 
.attr("class", "x axis") 
 
    .attr("transform", "translate(0," + height + ")") 
 
    .call(xAxis); 
 

 
svg.append("g") // Add the Y Axis 
 
.attr("class", "y axis") 
 
    .call(yAxis);
body { 
 
    font: 12px Arial; 
 
} 
 
path { 
 
    stroke: steelblue; 
 
    stroke-width: 2; 
 
    fill: none; 
 
} 
 
.axis path, .axis line { 
 
    fill: none; 
 
    stroke: grey; 
 
    stroke-width: 1; 
 
    shape-rendering: crispEdges; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<body></body>

答えて

1

ここでの主な問題は、あなたが誤ってデータにアクセスしているということです。あなたのデータは1つの要素を持つ配列です。その1つの要素には、name,code、およびchildrenのオブジェクトが含まれています。したがって、オブジェクト全体をvalueline()に渡すと、valuelineは存在しない値を検索するため、機能しません。あなたが本当にプロットしたいデータがdata[0].childrenであるd.name = +d.date

に存在しないdate属性にアクセスしようとしているように

またそれが見えます。私はデータからx軸ドメインを設定しましたが、順序尺度の順序を制御したい場合は、ドメインを明示的に明示的に設定する方が良い場合があります。x.domain(['SEASONAL_CYQ3', 'SEASONAL_CYQ4',...])

ここでは動作例を示します。それは少しスタイルが必要ですが、私は軸を開始するためにいくつかのテキストスタイルを開始しました。

さらに、d3 v4を使用するようにアップグレードすることをお勧めします。

var margin = { 
 
    top: 30, 
 
    right: 20, 
 
    bottom: 80, 
 
    left: 50 
 
}; 
 
var width = 600 - margin.left - margin.right; 
 
var height = 270 - margin.top - margin.bottom; 
 

 

 

 
var x = d3.scale.ordinal() 
 
    .rangeRoundBands([0, width], .1); 
 

 
var y = d3.scale.linear().range([height, 0]); 
 

 
var xAxis = d3.svg.axis().scale(x) 
 
    .orient("bottom").ticks(5); 
 

 
var yAxis = d3.svg.axis().scale(y) 
 
    .orient("left").ticks(5); 
 

 
var valueline = d3.svg.line() 
 
    .x(function (d) { 
 
     return x(d.name); 
 
    }) 
 
    .y(function (d) { 
 
     return y(d.count); 
 
    }); 
 

 
var svg = d3.select("body") 
 
    .append("svg") 
 
    .attr("width", width + margin.left + margin.right) 
 
    .attr("height", height + margin.top + margin.bottom) 
 
    .append("g") 
 
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 
 

 
// Get the data 
 
var data = [{ 
 
       "name": "Seasonal Pop", 
 
       "code": "SEASONAL_POP", 
 
       "children": [{ 
 
        "name": "SEASONAL_LYQ1", 
 
        "code": "SEASONAL_LYQ1", 
 
        "count": 1200 
 
       }, { 
 
        "name": "SEASONAL_LYQ2", 
 
        "code": "SEASONAL_LYQ2", 
 
        "count": 2000 
 
       }, { 
 
        "name": "SEASONAL_LYQ3", 
 
        "code": "SEASONAL_LYQ3", 
 
        "count": 1060 
 
       }, { 
 
        "name": "SEASONAL_LYQ4", 
 
        "code": "SEASONAL_LYQ4", 
 
        "count": 2300 
 
       }, { 
 
        "name": "SEASONAL_CYQ1", 
 
        "code": "SEASONAL_CYQ1", 
 
        "count": 1300 
 
       }, { 
 
        "name": "SEASONAL_CYQ2", 
 
        "code": "SEASONAL_CYQ2", 
 
        "count": 3400 
 
       }, { 
 
        "name": "SEASONAL_CYQ3", 
 
        "code": "SEASONAL_CYQ3", 
 
        "count": 4500 
 
       }, { 
 
        "name": "SEASONAL_CYQ4", 
 
        "code": "SEASONAL_CYQ4", 
 
        "count": 5500 
 
       }] 
 
      }]; 
 

 
x.domain(data[0].children.map(d => d.name)) 
 
y.domain([0, d3.max(data[0].children, function (d) { 
 
    return d.count; 
 
    })]); 
 

 
console.log("xed", x(data[0].children[0].name)) 
 

 

 
svg.append("path") // Add the valueline path. 
 
.attr("d", valueline(data[0].children)) 
 

 
svg.append("g") // Add the X Axis 
 
.attr("class", "x axis") 
 
    .attr("transform", "translate(0," + height + ")") 
 
    .call(xAxis) 
 
    .selectAll("text") 
 
    .attr("y", 0) 
 
    .attr("x", 9) 
 
    .attr('font-size', '10px') 
 
    .attr("dy", ".35em") 
 
    .attr("transform", "rotate(90)") 
 
    .style("text-anchor", "start"); 
 

 
svg.append("g") // Add the Y Axis 
 
.attr("class", "y axis") 
 
    .call(yAxis) 
 
    .selectAll('text') 
 
    .attr('font-size', '10px')
body { 
 
    font: 12px Arial; 
 
} 
 
path { 
 
    stroke: steelblue; 
 
    stroke-width: 2; 
 
    fill: none; 
 
} 
 
.axis path, .axis line { 
 
    fill: none; 
 
    stroke: grey; 
 
    stroke-width: 1; 
 
    shape-rendering: crispEdges; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<body></body>

+0

あなたにマークをありがとう:) – SNT

関連する問題