'Drawing horizontal stacked bar chart using d3(v4) in react

I have a question for the first time.

I want to draw horizontal stacked bar chart like this.... enter image description here

My data structure like this

{"code":200,"message":"success","responseTime":"2022-03-24T20:05:47+09:00","response":{"results":[{"age":"0 - 9세","population":30895,"year":2013},{"age":"10 - 19세","population":31843,"year":2013},{"age":"20 - 29세","population":28386,"year":2013},{"age":"30 - 49세","population":91273,"year":2013},{"age":"50 - 64세","population":41210,"year":2013},{"age":"65세 이상","population":18470,"year":2013},{"age":"0 - 9세","population":31650,"year":2014},{"age":"10 - 19세","population":31368,"year":2014},{"age":"20 - 29세","population":28977,"year":2014},{"age":"30 - 49세","population":93869,"year":2014},{"age":"50 - 64세","population":43057,"year":2014},{"age":"65세 이상","population":19366,"year":2014},{"age":"0 - 9세","population":33205,"year":2015},{"age":"10 - 19세","population":30595,"year":2015},{"age":"20 - 29세","population":29713,"year":2015},{"age":"30 - 49세","population":96613,"year":2015},{"age":"50 - 64세","population":45629,"year":2015},{"age":"65세 이상","population":20073,"year":2015},{"age":"0 - 9세","population":33408,"year":2016},{"age":"10 - 19세","population":29864,"year":2016},{"age":"20 - 29세","population":28596,"year":2016},{"age":"30 - 49세","population":96494,"year":2016},{"age":"50 - 64세","population":47889,"year":2016},{"age":"65세 이상","population":20932,"year":2016},{"age":"0 - 9세","population":32328,"year":2017},{"age":"10 - 19세","population":29490,"year":2017},{"age":"20 - 29세","population":26965,"year":2017},{"age":"30 - 49세","population":93739,"year":2017},{"age":"50 - 64세","population":49311,"year":2017},{"age":"65세 이상","population":22240,"year":2017},{"age":"0 - 9세","population":30656,"year":2018},{"age":"10 - 19세","population":28905,"year":2018},{"age":"20 - 29세","population":25861,"year":2018},{"age":"30 - 49세","population":90117,"year":2018},{"age":"50 - 64세","population":51521,"year":2018},{"age":"65세 이상","population":23456,"year":2018},{"age":"0 - 9세","population":28840,"year":2019},{"age":"10 - 19세","population":28309,"year":2019},{"age":"20 - 29세","population":25190,"year":2019},{"age":"30 - 49세","population":87340,"year":2019},{"age":"50 - 64세","population":53705,"year":2019},{"age":"65세 이상","population":24892,"year":2019},{"age":"0 - 9세","population":26716,"year":2020},{"age":"10 - 19세","population":27963,"year":2020},{"age":"20 - 29세","population":24350,"year":2020},{"age":"30 - 49세","population":84275,"year":2020},{"age":"50 - 64세","population":55581,"year":2020},{"age":"65세 이상","population":26869,"year":2020},{"age":"0 - 9세","population":25335,"year":2021},{"age":"10 - 19세","population":27837,"year":2021},{"age":"20 - 29세","population":23629,"year":2021},{"age":"30 - 49세","population":82130,"year":2021},{"age":"50 - 64세","population":56436,"year":2021},{"age":"65세 이상","population":27833,"year":2021}],"totalCount":54}}

I just found similar code I was looking for, but it was d3 v3 so I couldn't use it. (d3.js highlighting stacked bar and getting selected values) For example, an error occurred when using d3.layout.stack()

and I don't understand this Answer (d3 v4 nested data and stacked bar chart)

So I want to know how to draw a horizontal stackedbar graph with that data. Or I want to know the data structure for drawing graphs. I would like to know if there is a site that I can refer to. ardently..

I have to give a presentation next week, so I was worried about that
My current progress is like this.....

 //x
  const xArra = [0, 60000, 120000, 180000, 240000, 300000];
  const yaX = d3.scaleLinear().domain([0, 300000]).range([0, dimension.width]);
  const xaAxis = d3.axisBottom(yaX).tickValues(xArra);
  //y
  let yaY: any;
  let layers;

  if (ya) {
    yaY = d3
      .scaleBand()
      .domain(ya.map((d) => d.year))
      .range([0, dimension.height])
      .paddingInner(5);
  }
  const yaAxis = d3.axisLeft(yaY).ticks(ya?.length);

  const yaZ = d3
    .scaleOrdinal()
    .range(['#98abc5', '#8a89a6', '#7b6888', '#6b486b', '#a05d56', '#d0743c']);



  useEffect(() => {
    if (ya) {
      const xAxisGroup = d3
        .select('#ya')
        .append('g')
        .attr(
          'transform',
          `translate(${dimension.marginLeft}, ${dimension.chartHeight + 5})`,
        )
        .style('font-family', 'Noto Sans KR')
        .style('font-size', '15')
        .call(xaAxis);

      const yAxisGroup = d3
        .select('#ya')
        .append('g')
        .style('font-family', 'Noto Sans KR')
        .style('font-size', '15')
        .attr('transform', `translate(${dimension.marginLeft},5)`)
        .call(yaAxis);

}, [ya]);

In the end, I could only draw axes like this.... enter image description here

ya data is this.. enter image description here

Please help, thank you everyone.!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source