Create Exploded Pie Chart in PowerPoint with JavaScript in React

A pie chart uses the size of each slice to show how parts make up a whole. By default the slices sit tightly together as one full circle; when you want to stress one part or make the figure easier to read, you can pull the slices outward from the center to form an exploded pie chart (also called a separated or exploded pie). Dragging slices one by one in PowerPoint is slow and hard to align; generating a presentation that already contains an exploded pie chart in the browser folds that step into an automated document workflow.

This article uses Spire.Presentation for JavaScript to build a PowerPoint presentation and insert an exploded pie chart in a React project. The whole process runs locally on the user's device through WebAssembly, requires no Office installation and no back-end service, and keeps the presentation from leaving the browser.

The article covers two core features:

For installation and project configuration, see Integrating Spire.Presentation for JavaScript in a React Project. The examples below assume that Spire.Presentation for JavaScript is installed and the WebAssembly module has been initialized.


Create an Exploded Pie Chart

When you create an exploded pie chart, all slices are pulled outward together, which makes each boundary clearer and the whole figure more prominent. The approach is to insert a single-series pie chart, bind the data, and then set a separation distance greater than 0 on the series, so that every slice moves out along its own angle and the pie is separated evenly.

Code Result
slide.Shapes.AppendChart({ type: ChartType.Pie, rectangle, init }) Inserts a pie chart at the specified position on the slide
chart.ChartData.get_Item({ row, column }).Text / NumberValue Writes the series name and the value of each category
chart.Series.SeriesLabel, chart.Categories.CategoryLabels, chart.Series.get_Item(0).Values Binds the series name, category labels, and value range
chart.Series.get_Item(0).DataPoints.Add(cdp) and point.Fill Creates a data point for each slice and fills it with a color
chart.Series.get_Item(0).Distance = 15 Separates every slice of the series outward
chart.Series.get_Item(0).DataLabels.PercentValueVisible = true Shows the percentage on each slice

The steps are:

  1. Get the Spire.Presentation WASM module and confirm it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Specify the chart rectangle with RectangleF.FromLTRB(), then call Shapes.AppendChart() with ChartType.Pie to insert a pie chart.
  3. Set ChartTitle.TextProperties.Text and set HasTitle to true so the chart shows a title.
  4. Use ChartData.get_Item() with Text to write the category names and NumberValue to write the value of each category, then bind the series name, category labels, and value range with Series.SeriesLabel, Categories.CategoryLabels, and Series.get_Item(0).Values.
  5. Loop over the values, use new ChartDataPoint() with DataPoints.Add() to create a data point for each slice, and set Fill.FillType and Fill.SolidColor.Color so the slices have different colors.
  6. Set Series.get_Item(0).Distance to a value greater than 0 (15 in the example) so all slices of the series move outward together.
  7. Set DataLabels.PercentValueVisible to true to show the percentage on each slice.
  8. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from the VFS and trigger the download.

The code to create an exploded pie chart is as follows:

function App() {
  // create an exploded pie chart
  const createExplodedPieChart = async () => {
    // get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // check whether the module is ready
    if (!pptModule) {
      alert('Spire.Presentation is not ready yet');
      return;
    }

    // load the font so that the text in the chart renders correctly
    await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);

    // create a blank presentation and get the first slide
    const ppt = new pptModule.Presentation();
    const slide = ppt.Slides.get_Item(0);

    // insert a pie chart on the slide
    const rect = pptModule.RectangleF.FromLTRB(90, 100, 640, 420);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.Pie, rectangle: rect, init: false });

    // set the chart title
    chart.ChartTitle.TextProperties.Text = 'Sales Share by Product';
    chart.ChartTitle.TextProperties.IsCentered = true;
    chart.ChartTitle.Height = 30;
    chart.HasTitle = true;

    // prepare the categories and values
    const products = ['Laptops', 'Phones', 'Tablets', 'Accessories'];
    const shares = [35, 25, 22, 18];

    // write the category names and values
    chart.ChartData.get_Item({ row: 0, column: 0 }).Text = 'Product';
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Share';
    for (let i = 0; i < products.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = products[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = shares[i];
    }

    // bind the series name, category labels and value range
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'B1' });
    chart.Categories.CategoryLabels = chart.ChartData.get_Item({ name: 'A2', endCellName: 'A5' });
    chart.Series.get_Item(0).Values = chart.ChartData.get_Item({ name: 'B2', endCellName: 'B5' });

    // create a data point for each slice and fill it with a different color
    const colors = [
      pptModule.Color.get_RosyBrown(),
      pptModule.Color.get_LightBlue(),
      pptModule.Color.get_LightPink(),
      pptModule.Color.get_MediumPurple(),
    ];
    for (let i = 0; i < shares.length; i++) {
      const cdp = new pptModule.ChartDataPoint(chart.Series.get_Item(0));
      cdp.Index = i;
      chart.Series.get_Item(0).DataPoints.Add(cdp);

      const point = chart.Series.get_Item(0).DataPoints.get_Item(i);
      point.Fill.FillType = pptModule.FillFormatType.Solid;
      point.Fill.SolidColor.Color = colors[i];
    }

    // set the series distance so that all slices are pulled outward
    chart.Series.get_Item(0).Distance = 15;

    // show percentages on the slices
    chart.Series.get_Item(0).DataLabels.PercentValueVisible = true;

    // save the result file
    const outputFileName = 'ExplodedPieChart_result.pptx';
    ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
    ppt.Dispose();

    // read the generated file from the VFS and trigger the download
    downloadFile(outputFileName);
  };

  // read the generated file from the VFS and trigger the download
  const downloadFile = (outputFileName) => {
    const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
    const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = outputFileName;
    a.click();
    URL.revokeObjectURL(url);
  };

  return (
    <div style={{ textAlign: 'center', height: '300px' }}>
      <h1>Create Exploded Pie Chart in PowerPoint</h1>
      <button onClick={createExplodedPieChart}>Create Exploded Pie Chart</button>
    </div>
  );
}

export default App;

The presentation after creating an exploded pie chart:

The presentation after creating an exploded pie chart


Explode a Single Slice

To explode a single slice, emphasize only one item in the pie chart—for example the largest share—and let it break away from the circle on its own while the other slices stay in place. In this case you do not separate the whole series; instead, create a data point for each slice in the series and then set a separation distance only on the data point of the target slice, so that this slice moves out along its own angle and draws attention.

Code Result
chart.Series.get_Item(0).DataPoints.get_Item(i) Gets the slice data point at index i
chart.Series.get_Item(0).DataPoints.get_Item(i).Distance = 30 Separates only that slice outward
chart.Series.get_Item(0).Distance left at its default The other slices stay joined as a full circle

The steps are:

  1. Get the Spire.Presentation WASM module and confirm it is ready, load the font file, create a blank presentation with new Presentation(), and get the first slide.
  2. Specify the rectangle with RectangleF.FromLTRB(), call Shapes.AppendChart() with ChartType.Pie to insert a pie chart, then write the data with ChartData.get_Item() and bind the series name, category labels, and values.
  3. Loop over the values, use new ChartDataPoint() with DataPoints.Add() to create a data point for each slice, and set Fill so the slices have different colors.
  4. Choose the index of the slice to emphasize, get its data point with DataPoints.get_Item(index), and set its Distance to a value greater than 0 (30 in the example) so only this slice moves outward.
  5. Set DataLabels.PercentValueVisible to true to show the percentage on each slice.
  6. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from the VFS and trigger the download.

The code to explode a single slice is as follows:

function App() {
  // explode a single slice
  const explodeSingleSlice = async () => {
    // get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // check whether the module is ready
    if (!pptModule) {
      alert('Spire.Presentation is not ready yet');
      return;
    }

    // load the font so that the text in the chart renders correctly
    await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);

    // create a blank presentation and get the first slide
    const ppt = new pptModule.Presentation();
    const slide = ppt.Slides.get_Item(0);

    // insert a pie chart on the slide
    const rect = pptModule.RectangleF.FromLTRB(90, 100, 640, 420);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.Pie, rectangle: rect, init: false });

    // set the chart title
    chart.ChartTitle.TextProperties.Text = 'Sales Share by Product';
    chart.ChartTitle.TextProperties.IsCentered = true;
    chart.ChartTitle.Height = 30;
    chart.HasTitle = true;

    // prepare the categories and values
    const products = ['Laptops', 'Phones', 'Tablets', 'Accessories'];
    const shares = [35, 25, 22, 18];

    // write the category names and values
    chart.ChartData.get_Item({ row: 0, column: 0 }).Text = 'Product';
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Share';
    for (let i = 0; i < products.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = products[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = shares[i];
    }

    // bind the series name, category labels and value range
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'B1' });
    chart.Categories.CategoryLabels = chart.ChartData.get_Item({ name: 'A2', endCellName: 'A5' });
    chart.Series.get_Item(0).Values = chart.ChartData.get_Item({ name: 'B2', endCellName: 'B5' });

    // create a data point for each slice and fill it with a different color
    const colors = [
      pptModule.Color.get_RosyBrown(),
      pptModule.Color.get_LightBlue(),
      pptModule.Color.get_LightPink(),
      pptModule.Color.get_MediumPurple(),
    ];
    for (let i = 0; i < shares.length; i++) {
      const cdp = new pptModule.ChartDataPoint(chart.Series.get_Item(0));
      cdp.Index = i;
      chart.Series.get_Item(0).DataPoints.Add(cdp);

      const point = chart.Series.get_Item(0).DataPoints.get_Item(i);
      point.Fill.FillType = pptModule.FillFormatType.Solid;
      point.Fill.SolidColor.Color = colors[i];
    }

    // set the distance on the slice with the largest share only, so it breaks away from the circle
    const highlightIndex = 0;
    chart.Series.get_Item(0).DataPoints.get_Item(highlightIndex).Distance = 30;

    // show percentages on the slices
    chart.Series.get_Item(0).DataLabels.PercentValueVisible = true;

    // save the result file
    const outputFileName = 'ExplodeSingleSlice_result.pptx';
    ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
    ppt.Dispose();

    // read the generated file from the VFS and trigger the download
    downloadFile(outputFileName);
  };

  // read the generated file from the VFS and trigger the download
  const downloadFile = (outputFileName) => {
    const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
    const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = outputFileName;
    a.click();
    URL.revokeObjectURL(url);
  };

  return (
    <div style={{ textAlign: 'center', height: '300px' }}>
      <h1>Create Exploded Pie Chart in PowerPoint</h1>
      <button onClick={explodeSingleSlice}>Explode a Single Slice</button>
    </div>
  );
}

export default App;

The presentation after exploding a single slice:

The presentation after exploding a single slice


FAQ

How to turn the exploded pie back into a normal pie

Cause: The separation depends on the value of Distance; when it is 0, the slices do not move outward.

Solution: Set the series distance back to 0 to restore a tightly joined pie:

chart.Series.get_Item(0).Distance = 0;

Percentages are not shown on the slices

Cause: Data labels do not show percentages by default; you need to turn that on explicitly on the series DataLabels.

Solution: Set PercentValueVisible to true, and a percentage label is drawn on each slice:

chart.Series.get_Item(0).DataLabels.PercentValueVisible = true;

All slices share the same color after exploding

Cause: A pie chart has a single series and uses one fill color by default, so the slices are not colored differently automatically.

Solution: Create a data point for each slice and set Fill per point to give each slice its own color:

const cdp = new pptModule.ChartDataPoint(chart.Series.get_Item(0));
cdp.Index = i;
chart.Series.get_Item(0).DataPoints.Add(cdp);

const point = chart.Series.get_Item(0).DataPoints.get_Item(i);
point.Fill.FillType = pptModule.FillFormatType.Solid;
point.Fill.SolidColor.Color = colors[i];

Get a Free License

If you wish to delete the evaluation message from the resulting documents, or to get rid of function limitations, please contact sales to obtain a valid 30-day temporary license.