Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

declaring variable react hooks

<script>

// set the dimensions and margins of the graph
const margin = {top: 10, right: 30, bottom: 20, left: 50},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
const 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/holtzy/D3-graph-gallery/master/DATA/data_stacked.csv").then( function(data) {

  // List of subgroups = header of the csv files = soil condition here
  const subgroups = data.columns.slice(1)

  // List of groups = species here = value of the first column called group -> I show them on the X axis
  const groups = data.map(d => d.group)

  console.log(data)

  // Add X axis
  const x = d3.scaleBand()
      .domain(groups)
      .range([0, width])
      .padding([0.2])
  svg.append("g")
    .attr("transform", `translate(0, ${height})`)
    .call(d3.axisBottom(x).tickSize(0));

  // Add Y axis
  const y = d3.scaleLinear()
    .domain([0, 40])
    .range([ height, 0 ]);
  svg.append("g")
    .call(d3.axisLeft(y));

  // Another scale for subgroup position?
  const xSubgroup = d3.scaleBand()
    .domain(subgroups)
    .range([0, x.bandwidth()])
    .padding([0.05])

  // color palette = one color per subgroup
  const color = d3.scaleOrdinal()
    .domain(subgroups)
    .range(['#e41a1c','#377eb8','#4daf4a'])

  // Show the bars
  svg.append("g")
    .selectAll("g")
    // Enter in data = loop group per group
    .data(data)
    .join("g")
      .attr("transform", d => `translate(${x(d.group)}, 0)`)
    .selectAll("rect")
    .data(function(d) { return subgroups.map(function(key) { return {key: key, value: d[key]}; }); })
    .join("rect")
      .attr("x", d => xSubgroup(d.key))
      .attr("y", d => y(d.value))
      .attr("width", xSubgroup.bandwidth())
      .attr("height", d => height - y(d.value))
      .attr("fill", d => color(d.key));

})

</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to tell this x = 12 + 30 when i read it in js 
Javascript :: circle collision javascript 
Javascript :: var s= 
Javascript :: mock anonymous function jest 
Javascript :: react native run android common error 
Javascript :: what i sminify javascript 
Javascript :: loop number in react 
Javascript :: bigint type js 
Python :: pandas merge all csv in a folder 
Python :: install matplotlib conda 
Python :: suppres tensorflow warnings 
Python :: rotate axis labels matplotlib 
Python :: python wait 1 sec 
Python :: pygame scale image python 
Python :: check python 32 or 64 
Python :: pandas read csv no index 
Python :: how to get the calendar of current month in python 
Python :: Colorcodes Discord.py 
Python :: install spotipy 
Python :: combine path python 
Python :: Pandas: How to Drop Rows that Contain a Specific String 
Python :: conda install dash 
Python :: python read file to variable 
Python :: python upload video to youtube 
Python :: how to make a custom icon for pygame 
Python :: sort by two columns in pandas 
Python :: how to check if python has been added to path 
Python :: how to change windows icon tkinter 
Python :: get page source code selenium python 
Python :: turn list to string with commas python 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =