我正在为数据写一张条形图,它显示了多个攻击战术和其他信息.战术显示在X轴上,并进行了排序.我希望当他们有多个相同的战术堆叠在一起的时候.堆叠条形图的所有其他示例都使用.STACK,我非常肯定它并不真正适用于这种情况.

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>



<script>


// set the dimensions and margins of the graph
var margin = {top: 30, right: 30, bottom: 70, left: 60},
    width = 1000 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;
    barheight = 60;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

// Parse the Data
d3.csv("https://raw.githubusercontent.com/BaileyHCUDenver/KPCUDTEST/main/test-data-short.csv", function(data) {

// X axis
var x = d3.scaleBand()
  .range([ 0, width ])
  .domain(data.map(function(d) { return d.tactic; }))
  .padding(0.2);
svg.append("g")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x))
  .selectAll("text")
    .attr("transform", "translate(-10,0)rotate(-)")
    .style("text-anchor", "end");

// Add Y axis //Not needed to show the Y axis because their is no need for it 
//var y = d3.scaleLinear()
  //.domain([0, 20])
  //.range([ height, 0]);
//svg.append("g")
  //.call(d3.axisLeft(y));

// Bars
svg.selectAll("mybar")
  .data(data)
  .enter()
  .append("rect")
    .attr("x", function(d) { return x(d.tactic); })
    .attr("y", function(d) { 
        return height - barheight;
        })
    .attr("width", x.bandwidth())
    .attr("height", function(d) { return barheight; })
    .attr("fill", "#FFFFFF") //Fill in with white
    .attr("stroke", "black") // Black outline color
    .attr("stroke-width", ); // Outline width (adjust as needed)


svg.selectAll("mytext")
      .data(data)
      .enter()
      .append("text")
      .text(function (d) { return d.title; }) // Use the 'value' field from your data
      .attr("x", function (d) { return x(d.tactic) + x.bandwidth() / 2; }) // Center the text
      .attr("y", function (d) { return height - barheight + 10; }) // Adjust the vertical position
      .style("text-anchor", "middle") // Center-align the text
      .style("font-size", "9px") // Set font size
      .style("fill", "black"); // Set text color

svg.selectAll("mytext")
      .data(data)
      .enter()
      .append("text")
      .text(function (d) { return d.tactic; }) // Use the 'value' field from your data
      .attr("x", function (d) { return x(d.tactic) + x.bandwidth() / 2; }) // Center the text
      .attr("y", function (d) { return height - barheight + 20; }) // Adjust the vertical position
      .style("text-anchor", "middle") // Center-align the text
      .style("font-size", "9px") // Set font size
      .style("fill", "black"); // Set text color
})

</script>

任何帮助都将不胜感激,因为我不确定如何才能让这些盒子堆叠在一起

推荐答案

使用tacticCount保存战术数量.

把横条(rect)和文本放在<g>中,将<g>‘S翻译的基础改成taticCount.

// set the dimensions and margins of the graph
var margin = {
    top: 30,
    right: 30,
    bottom: 70,
    left: 60
  },
  width = 1000 - margin.left - margin.right,
  height = 500 - margin.top - margin.bottom;
barheight = 60;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

// Parse the Data
d3.csv("https://raw.githubusercontent.com/BaileyHCUDenver/KPCUDTEST/main/test-data-short.csv", function(data) {
  // X axis
  var x = d3.scaleBand()
    .range([0, width])
    .domain(data.map(function(d) {
      return d.tactic;
    }))
    .padding(0.2);
  svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x))
    .selectAll("text")
    .attr("transform", "translate(-10,0)rotate(-)")
    .style("text-anchor", "end");

  // Add Y axis //Not needed to show the Y axis because their is no need for it 
  //var y = d3.scaleLinear()
  //.domain([0, 20])
  //.range([ height, 0]);
  //svg.append("g")
  //.call(d3.axisLeft(y));

  // Bars
  let tacticCount = {}
  const barItemContainers = svg.selectAll("mybar")
    .data(data)
    .enter()
    .append('g')
    .attr('transform', (d) => {
      tacticCount[d.tactic] = (tacticCount[d.tactic] || 0) + 1
      return `translate(${x(d.tactic)},${height - barheight * tacticCount[d.tactic]})`
    });
  barItemContainers.append("rect")
    .attr("width", x.bandwidth())
    .attr("height", barheight)
    .attr("fill", "#FFFFFF") //Fill in with white
    .attr("stroke", "black") // Black outline color
    .attr("stroke-width", ); // Outline width (adjust as needed)
  barItemContainers.append("text")
    .text(d => d.title) // Use the 'value' field from your data
    .attr("x", x.bandwidth() / 2) // Center the text
    .attr("y", 10) // Adjust the vertical position
    .style("text-anchor", "middle") // Center-align the text
    .style("font-size", "9px") // Set font size
    .style("fill", "black"); // Set text color
  barItemContainers.append("text")
    .text(d => d.tactic) // Use the 'value' field from your data
    .attr("x", x.bandwidth() / 2) // Center the text
    .attr("y", 20) // Adjust the vertical position
    .style("text-anchor", "middle") // Center-align the text
    .style("font-size", "9px") // Set font size
    .style("fill", "black"); // Set text color

})
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

Javascript相关问答推荐

在JavaScript中,是否有一种方法可以创建自定义thable,在等待某些代码后自动触发它?

从连接字符串创建客户端时,NodeJS连接到CosmosDB失败

如何通过在提交时工作的函数显示dom元素?

RxJS setTimeout操作符等效

如何判断属于多个元素的属性是否具有多个值之一

Klaro与Angular的集成

通过使用100%间隔时间来代表我们还剩多少时间来倒计时

v—自动完成不显示 Select 列表中的所有项目

在286之后恢复轮询

Ember.js 5.4更新会话存储时如何更新组件变量

如何在coCos2d-x中更正此错误

我怎么在JS里连续加2个骰子的和呢?

这个值总是返回未定义的-Reaction

如何使用JavaScript拆分带空格的单词

第二次更新文本输入字段后,Reaction崩溃

确保函数签名中的类型安全:匹配值

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

为什么我的按钮没有从&q;1更改为&q;X&q;?

如何为仅有数据可用的点显示X轴标签?

JAVASCRIPT SWITCH CASE语句:当表达式为';ALL';