Render a pie chart using SVG

To render a pie chart in React using SVG, you can create a reusable component that takes data as input and generates the SVG elements accordingly. Here’s an example code snippet that demonstrates how to do this:

import React from 'react';

const PieChart = ({ data, width, height }) => {
  const total = data.reduce((sum, { value }) => sum + value, 0);
  const center = width / 2;
  const radius = Math.min(width, height) / 2;

  let startAngle = 0;
  let endAngle = 0;

  const renderSlice = (value, index) => {
    const sliceAngle = (value / total) * 360;
    const largeArcFlag = sliceAngle > 180 ? 1 : 0;

    startAngle = endAngle;
    endAngle = startAngle + sliceAngle;

    const startX = center + radius * Math.cos((startAngle * Math.PI) / 180);
    const startY = center + radius * Math.sin((startAngle * Math.PI) / 180);
    const endX = center + radius * Math.cos((endAngle * Math.PI) / 180);
    const endY = center + radius * Math.sin((endAngle * Math.PI) / 180);

    const pathData = [
      `M ${center},${center}`,
      `L ${startX},${startY}`,
      `A ${radius},${radius} 0 ${largeArcFlag},1 ${endX},${endY}`,
      'Z'
    ].join(' ');

    return <path key={index} d={pathData} fill={data[index].color} />;
  };

  return (
    <svg width={width} height={height}>
      {data.map(renderSlice)}
    </svg>
  );
};

export default PieChart;

In this code, the PieChart component receives the following props:

  • data: An array of objects containing the value and color for each slice of the pie chart.
  • width: The width of the chart in pixels.
  • height: The height of the chart in pixels.

The component calculates the necessary dimensions for the pie chart based on the provided props. It then iterates over the data array and generates an SVG path element for each slice of the pie.

The renderSlice function calculates the start and end angles for each slice based on the value and total of the data. It then uses trigonometry to determine the coordinates for the start and end points of the arc. The pathData variable constructs an SVG path string for each slice using the generated coordinates.

Finally, the component renders an SVG path element for each slice with the d attribute set to pathData. The fill attribute defines the color of each slice based on the color property in the data array.

You can use the PieChart component in your application like this:

import React from 'react';
import PieChart from './PieChart';

const App = () => {
  const data = [
    { value: 30, color: 'red' },
    { value: 50, color: 'blue' },
    { value: 20, color: 'green' }
  ];
  
  return (
    <div>
      <h1>Pie Chart Example</h1>
      <PieChart data={data} width={400} height={300} />
    </div>
  );
};

export default App;

In this example, we pass an array of objects (data) containing the value and color for each slice, along with the desired width and height, to the Pie

Related Posts

Leave a Reply

Your email address will not be published.