import React, { useState, useEffect } from 'react'; import ReactDOM from 'react-dom'; import { csv, scaleBand, scaleLinear, max, axisBottom, axisLeft, } from 'd3'; const csvUrl = 'https://gist.githubusercontent.com/chaturyaakhilapallem/c398182c49c4e2e841a2133a9b5161a1/raw/b287287225553c952f5986e89f10bb50e40d423f/BMI.csv'; const width = 960; const height = 500; const margin = { top: 50, right: 50, bottom: 80, left: 80 }; const barColor = '#69b3a2'; const scatterColor = '#ff00ff'; const App = () => { const [data, setData] = useState(null); useEffect(() => { const row = (d) => { d.BMI = +d.BMI; d.Height = +d['Height (cm)']; d.Weight = +d['Weight (kg)']; return d; }; csv(csvUrl, row).then((data) => { setData(data); }); }, []); if (!data) { return
Loading...
; } const halfWidth = width / 2 - margin.left - margin.right; const xScale = scaleBand() .domain(data.map((d) => d.Name)) .range([margin.left, halfWidth]) .padding(0.4); // Increased padding between bars const yScaleBMI = scaleLinear() .domain([0, max(data, (d) => d.BMI)]) .nice() .range([height - margin.bottom, margin.top]); const yScaleHeightWeight = scaleLinear() .domain([ 0, max(data, (d) => Math.max(d.Height, d.Weight)), ]) .nice() .range([height - margin.bottom, margin.top]); return ( {data.map((d) => ( ))} { const xAxis = scaleBand() .domain(data.map((d) => d.Name)) .range([margin.left, halfWidth]) .padding(0.3); // Increased padding between bars const xAxisGroup = d3.select(node); xAxisGroup .call(axisBottom(xAxis)) .selectAll('text') .attr('transform', 'rotate(-45)') .attr('x', -10) .attr('y', 10) .style('font-size', '14px') .style('fill', '#666') .style('text-anchor', 'end'); xAxisGroup .append('text') .attr('x', halfWidth / 2) .attr('y', 30) // Adjusted y position .attr('fill', '#444') .attr('text-anchor', 'middle') .style('font-size', '20px') .style('font-weight', 'bold') .style('text-transform', 'uppercase') .text('Name'); }} /> { const yAxis = scaleLinear() .domain([0, max(data, (d) => d.BMI)]) .nice() .range([height - margin.bottom, margin.top]); const yAxisGroup = d3.select(node); yAxisGroup.call(axisLeft(yAxis)); yAxisGroup .append('text') .attr('transform', 'rotate(-90)') .attr('y', -margin.left + 20) .attr('x', -halfWidth / 2) .attr('dy', '1em') .attr('fill', '#444') .attr('text-anchor', 'middle') .style('font-size', '20px') .style('font-weight', 'bold') .style('text-transform', 'uppercase') .text('BMI'); }} /> {/* Second graph */} {data.map((d) => ( <> ))} { const xAxis = scaleBand() .domain(data.map((d) => d.Name)) .range([0, halfWidth]) .padding(0.3); // Increased padding between bars const xAxisGroup = d3.select(node); xAxisGroup .call(axisBottom(xAxis)) .selectAll('text') .attr('transform', 'rotate(-45)') .attr('x', -10) .attr('y', 10) .style('font-size', '14px') .style('fill', '#666') .style('text-anchor', 'end'); xAxisGroup .append('text') .attr('x', halfWidth / 2) .attr('y', 30) // Adjusted y position .attr('fill', '#444') .attr('text-anchor', 'middle') .style('font-size', '20px') .style('font-weight', 'bold') .style('text-transform', 'uppercase') .text('Name'); }} /> { const yAxis = scaleLinear() .domain([ 0, max(data, (d) => Math.max(d.Height, d.Weight), ), ]) .nice() .range([height - margin.bottom, margin.top]); const yAxisGroup = d3.select(node); yAxisGroup.call(axisLeft(yAxis)); yAxisGroup .append('text') .attr('transform', 'rotate(-90)') .attr('y', -margin.left + 20) .attr('x', -halfWidth / 2) .attr('dy', '1em') .attr('fill', '#444') .attr('text-anchor', 'middle') .style('font-size', '20px') .style('font-weight', 'bold') .style('text-transform', 'uppercase') .text('Height & Weight'); }} /> {/* Divider line */} {/* Scatter plot */} {data.map((d) => ( ))} ); }; const rootElement = document.getElementById('root'); ReactDOM.render(, rootElement);