1 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
這個(gè)...
let u2 = chart2.selectAll()
……和……一樣
let u2 = chart2.selectAll(null)
...這是保證您的輸入選擇始終包含數(shù)據(jù)數(shù)組中的所有元素的最佳方法。您可以在此處閱讀:選擇 null:D3.js 中“selectAll(null)”背后的原因是什么?
顯然,這不是你想要的。所以,簡(jiǎn)單的修復(fù)是:
let u2 = chart2.selectAll("rect")
另外,請(qǐng)考慮使用 key 函數(shù),否則您將通過索引連接數(shù)據(jù)。
這是帶有該更改的代碼:
<body>
<button onclick="updateData()">Update</button>
<!-- Bar Chart for SVG-->
<svg width="500" height="500" id="svg2">
</svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
//Bar Chart
//Creating data
let data2 = [{
name: 'A',
value: Math.floor(Math.random() * 100)
},
{
name: 'B',
value: Math.floor(Math.random() * 100)
},
{
name: 'C',
value: Math.floor(Math.random() * 100)
},
{
name: 'D',
value: Math.floor(Math.random() * 100)
},
{
name: 'E',
value: Math.floor(Math.random() * 100)
},
{
name: 'F',
value: Math.floor(Math.random() * 100)
},
{
name: 'G',
value: Math.floor(Math.random() * 100)
},
{
name: 'H',
value: Math.floor(Math.random() * 100)
},
{
name: 'I',
value: Math.floor(Math.random() * 100)
},
{
name: 'J',
value: Math.floor(Math.random() * 100)
}
]
//Creating svg, margin, width, height
let svg2 = d3.select("#svg2")
let margin = 30;
let width = 500 - 2 * margin;
let height = 500 - 2 * margin;
//Creating chart by using g in svg2.
let chart2 = svg2.append('g')
.attr('transform', 'translate(30, 30)');
//yScale
let yScale2 = d3.scaleLinear()
.range([height, 0])
.domain([0, 100]);
//append g to create y axis
chart2.append('g')
.call(d3.axisLeft(yScale2));
//xScale
let xScale2 = d3.scaleBand()
.range([0, width])
.domain(data2.map((d) => d.name))
.padding(0.2);
//append g to create x axis
chart2.append('g')
.attr('transform', 'translate(0, 440)')
.call(d3.axisBottom(xScale2));
//Creating color scale using scaleOrdinal and schemeCategory10
let colorScale = d3.scaleOrdinal(d3.schemeCategory10)
//Selecting all in chart2 to set/append rectangular, and axis.
chart2.selectAll()
.data(data2)
.enter()
.append('rect')
.attr('x', (d) => xScale2(d.name))
.attr('y', (d) => yScale2(d.value))
.attr('height', (d) => height - yScale2(d.value))
.attr('width', xScale2.bandwidth())
//Putting colors on the bar
chart2.selectAll('rect')
.style('fill', d => colorScale(d.name));
function updateData() {
let data2 = [{
name: 'A',
value: Math.floor(Math.random() * 100)
},
{
name: 'B',
value: Math.floor(Math.random() * 100)
},
{
name: 'C',
value: Math.floor(Math.random() * 100)
},
{
name: 'D',
value: Math.floor(Math.random() * 100)
},
{
name: 'E',
value: Math.floor(Math.random() * 100)
},
{
name: 'F',
value: Math.floor(Math.random() * 100)
},
{
name: 'G',
value: Math.floor(Math.random() * 100)
},
{
name: 'H',
value: Math.floor(Math.random() * 100)
},
{
name: 'I',
value: Math.floor(Math.random() * 100)
},
{
name: 'J',
value: Math.floor(Math.random() * 100)
}
]
let u2 = chart2.selectAll("rect")
.data(data2)
u2
.enter()
.append('rect')
.merge(u2)
.transition()
.duration(1000)
.attr('x', (d) => xScale2(d.name))
.attr('y', (d) => yScale2(d.value))
.attr('height', (d) => height - yScale2(d.value))
.attr('width', xScale2.bandwidth())
chart2.selectAll('rect')
.style('fill', d => colorScale(d.name));
}
</script>
添加回答
舉報(bào)