按下按钮更新条形图

我是 D3-v5 的新手,正在尝试使用更新按钮(随机值)创建条形图。我按照下面的代码创建了所有东西,


  <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)


但是,当我按下 HTML 中的更新按钮时,它会转换并重叠图表,如下图所示,

http://img2.mukewang.com/61839dfd0001168304480479.jpg

唯一需要更改的是条形图,但所有条形图都在随值转换。所以我试图从网上谷歌它找出,但找不到它..

我需要编辑或添加哪些内容..?

有人可以帮我吗?


炎炎设计
浏览 220回答 1
1回答

波斯汪

这个...let&nbsp;u2&nbsp;=&nbsp;chart2.selectAll()……和……一样let&nbsp;u2&nbsp;=&nbsp;chart2.selectAll(null)...这是保证您的输入选择始终包含数据数组中的所有元素的最佳方法。您可以在此处阅读:选择 null:D3.js 中“selectAll(null)”背后的原因是什么?显然,这不是你想要的。所以,简单的修复是:let&nbsp;u2&nbsp;=&nbsp;chart2.selectAll("rect")另外,请考虑使用 key 函数,否则您将通过索引连接数据。这是带有该更改的代码:<body>&nbsp; <button onclick="updateData()">Update</button>&nbsp; <!-- Bar Chart for SVG-->&nbsp; <svg width="500" height="500" id="svg2">&nbsp; &nbsp; </svg>&nbsp; <script src="https://d3js.org/d3.v5.min.js"></script>&nbsp; <script>&nbsp; &nbsp; //Bar Chart&nbsp; &nbsp; //Creating data&nbsp; &nbsp; let data2 = [{&nbsp; &nbsp; &nbsp; &nbsp; name: 'A',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'B',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'C',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'D',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'E',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'F',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'G',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'H',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'I',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'J',&nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; &nbsp; //Creating svg, margin, width, height&nbsp; &nbsp; let svg2 = d3.select("#svg2")&nbsp; &nbsp; let margin = 30;&nbsp; &nbsp; let width = 500 - 2 * margin;&nbsp; &nbsp; let height = 500 - 2 * margin;&nbsp; &nbsp; //Creating chart by using g in svg2.&nbsp; &nbsp; let chart2 = svg2.append('g')&nbsp; &nbsp; &nbsp; .attr('transform', 'translate(30, 30)');&nbsp; &nbsp; //yScale&nbsp; &nbsp; let yScale2 = d3.scaleLinear()&nbsp; &nbsp; &nbsp; .range([height, 0])&nbsp; &nbsp; &nbsp; .domain([0, 100]);&nbsp; &nbsp; //append g to create y axis&nbsp; &nbsp; chart2.append('g')&nbsp; &nbsp; &nbsp; .call(d3.axisLeft(yScale2));&nbsp; &nbsp; //xScale&nbsp; &nbsp; let xScale2 = d3.scaleBand()&nbsp; &nbsp; &nbsp; .range([0, width])&nbsp; &nbsp; &nbsp; .domain(data2.map((d) => d.name))&nbsp; &nbsp; &nbsp; .padding(0.2);&nbsp; &nbsp; //append g to create x axis&nbsp; &nbsp; chart2.append('g')&nbsp; &nbsp; &nbsp; .attr('transform', 'translate(0, 440)')&nbsp; &nbsp; &nbsp; .call(d3.axisBottom(xScale2));&nbsp; &nbsp; //Creating color scale using scaleOrdinal and schemeCategory10&nbsp; &nbsp; let colorScale = d3.scaleOrdinal(d3.schemeCategory10)&nbsp; &nbsp; //Selecting all in chart2 to set/append rectangular, and axis.&nbsp; &nbsp; chart2.selectAll()&nbsp; &nbsp; &nbsp; .data(data2)&nbsp; &nbsp; &nbsp; .enter()&nbsp; &nbsp; &nbsp; .append('rect')&nbsp; &nbsp; &nbsp; .attr('x', (d) => xScale2(d.name))&nbsp; &nbsp; &nbsp; .attr('y', (d) => yScale2(d.value))&nbsp; &nbsp; &nbsp; .attr('height', (d) => height - yScale2(d.value))&nbsp; &nbsp; &nbsp; .attr('width', xScale2.bandwidth())&nbsp; &nbsp; //Putting colors on the bar&nbsp; &nbsp; chart2.selectAll('rect')&nbsp; &nbsp; &nbsp; .style('fill', d => colorScale(d.name));&nbsp; &nbsp; function updateData() {&nbsp; &nbsp; &nbsp; let data2 = [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'A',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'B',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'C',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'D',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'E',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'F',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'G',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'H',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'I',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: 'J',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: Math.floor(Math.random() * 100)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; let u2 = chart2.selectAll("rect")&nbsp; &nbsp; &nbsp; &nbsp; .data(data2)&nbsp; &nbsp; &nbsp; u2&nbsp; &nbsp; &nbsp; &nbsp; .enter()&nbsp; &nbsp; &nbsp; &nbsp; .append('rect')&nbsp; &nbsp; &nbsp; &nbsp; .merge(u2)&nbsp; &nbsp; &nbsp; &nbsp; .transition()&nbsp; &nbsp; &nbsp; &nbsp; .duration(1000)&nbsp; &nbsp; &nbsp; &nbsp; .attr('x', (d) => xScale2(d.name))&nbsp; &nbsp; &nbsp; &nbsp; .attr('y', (d) => yScale2(d.value))&nbsp; &nbsp; &nbsp; &nbsp; .attr('height', (d) => height - yScale2(d.value))&nbsp; &nbsp; &nbsp; &nbsp; .attr('width', xScale2.bandwidth())&nbsp; &nbsp; &nbsp; chart2.selectAll('rect')&nbsp; &nbsp; &nbsp; &nbsp; .style('fill', d => colorScale(d.name));&nbsp; &nbsp; }&nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript