When several series sit side by side under each category, a clustered column chart places them in parallel: readers have to add the column heights themselves to get the total, and it is hard to see how much of the total each part takes up. A stacked chart piles the segments of one category into a single column, where the full height is the category total and each segment is one series' share, so total and composition are read at once. Absolute values versus percentages is the most common way a stacked chart changes, and each reading suits a different situation. This article uses Spire.Presentation for JavaScript to create PowerPoint presentations with stacked charts 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 a Stacked Column Chart

To put a category's total and the make-up of its series into a single column, start the base chart as a stacked column chart: the series of one category are no longer placed side by side but piled up in turn, where the top of the column corresponds to the sum of all series and each segment to the value of one series. The chart keeps the height comparison between totals while making clear which series add up to each total.

Writing Result
slide.Shapes.AppendChart({ type: ChartType.ColumnStacked, rectangle, init }) Inserts a stacked column chart at the specified position
chart.ChartTitle.TextProperties.Text and chart.HasTitle = true Sets and shows the chart title
chart.ChartData.get_Item({ row, column }).Text / NumberValue Writes the category names, series names and the value of each category
chart.Series.SeriesLabel and chart.Categories.CategoryLabels Binds the series names and the category labels
chart.Series.get_Item(i).Values Binds the value range of the i-th series

The steps are:

  1. Get the Spire.Presentation WASM module, confirm it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Use RectangleF.FromLTRB() to set the chart rectangle, then call Shapes.AppendChart() with ChartType.ColumnStacked to insert a stacked column chart.
  3. Set ChartTitle.TextProperties.Text and set HasTitle to true so the chart shows its title.
  4. Write the category and series names through Text of ChartData.get_Item(), and write the value of each series in each category through NumberValue.
  5. Bind the series names and category labels with Series.SeriesLabel and Categories.CategoryLabels, then bind each series' value range with Series.get_Item(i).Values; the series stack from bottom to top in the order they are bound.
  6. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from VFS and trigger the download.

The code for creating a stacked column chart is as follows:

function App() {
  // create a stacked column chart
  const createStackedColumn = 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 stacked column chart on the slide
    const rect = pptModule.RectangleF.FromLTRB(80, 100, 660, 430);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.ColumnStacked, rectangle: rect, init: false });

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

    // prepare the data of the three series
    const quarters = ['Q1', 'Q2', 'Q3', 'Q4'];
    const laptops = [210, 240, 225, 260];
    const tablets = [140, 150, 170, 160];
    const phones = [180, 200, 190, 230];

    // write the category and series names
    chart.ChartData.get_Item({ row: 0, column: 0 }).Text = 'Quarter';
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Laptops';
    chart.ChartData.get_Item({ row: 0, column: 2 }).Text = 'Tablets';
    chart.ChartData.get_Item({ row: 0, column: 3 }).Text = 'Smartphones';

    // write the values of each category
    for (let i = 0; i < quarters.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = quarters[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = laptops[i];
      chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = tablets[i];
      chart.ChartData.get_Item({ row: i + 1, column: 3 }).NumberValue = phones[i];
    }

    // bind the series names, category labels and value ranges
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'D1' });
    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' });
    chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C5' });
    chart.Series.get_Item(2).Values = chart.ChartData.get_Item({ name: 'D2', endCellName: 'D5' });

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

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

  // read the generated file from 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 a Stacked Chart in PowerPoint</h1>
      <button onClick={createStackedColumn}>Create Stacked Column Chart</button>
    </div>
  );
}

export default App;

The presentation after creating a stacked column chart:

The presentation after creating a stacked column chart


Create a 100% Stacked Column Chart

When the totals of different categories vary widely, a stacked column chart gives most of the vertical space to the largest category while the smallest category is left with a very short column, making the proportions between segments hard to compare. A 100% stacked column chart normalizes every column to 100%, so each segment keeps only its share of the category total and categories with different totals can be compared by composition side by side. Change the chart type to ChartType.Column100PercentStacked and set the value axis scale to percentages to get such a chart.

Writing Result
slide.Shapes.AppendChart({ type: ChartType.Column100PercentStacked, rectangle, init }) Inserts a 100% stacked column chart that normalizes each category to 100%
chart.PrimaryValueAxis.NumberFormat = '0%' Shows the value axis scale as percentages

The steps are:

  1. Get the Spire.Presentation WASM module, confirm it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Use RectangleF.FromLTRB() to set the chart rectangle, then call Shapes.AppendChart() with ChartType.Column100PercentStacked to insert a 100% stacked column chart.
  3. Set the chart title, then write the category names, series names and the value of each category through ChartData.get_Item().
  4. Bind the series names, category labels and value ranges with Series.SeriesLabel, Categories.CategoryLabels and Series.get_Item(i).Values.
  5. Set PrimaryValueAxis.NumberFormat to '0%', so the value axis scale shows percentages that match the normalized columns.
  6. Call Presentation.SaveToFile() to save the result file, release the presentation with Dispose(), then read the file from VFS and trigger the download.

The code for creating a 100% stacked column chart is as follows:

function App() {
  // create a 100% stacked column chart
  const createPercentStackedColumn = 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 100% stacked column chart on the slide
    const rect = pptModule.RectangleF.FromLTRB(80, 100, 660, 430);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.Column100PercentStacked, rectangle: rect, init: false });

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

    // prepare the data of the three series
    const quarters = ['Q1', 'Q2', 'Q3', 'Q4'];
    const laptops = [210, 240, 225, 260];
    const tablets = [140, 150, 170, 160];
    const phones = [180, 200, 190, 230];

    // write the category and series names
    chart.ChartData.get_Item({ row: 0, column: 0 }).Text = 'Quarter';
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Laptops';
    chart.ChartData.get_Item({ row: 0, column: 2 }).Text = 'Tablets';
    chart.ChartData.get_Item({ row: 0, column: 3 }).Text = 'Smartphones';

    // write the values of each category
    for (let i = 0; i < quarters.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = quarters[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = laptops[i];
      chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = tablets[i];
      chart.ChartData.get_Item({ row: i + 1, column: 3 }).NumberValue = phones[i];
    }

    // bind the series names, category labels and value ranges
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'D1' });
    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' });
    chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C5' });
    chart.Series.get_Item(2).Values = chart.ChartData.get_Item({ name: 'D2', endCellName: 'D5' });

    // show the value axis scale as percentages
    chart.PrimaryValueAxis.NumberFormat = '0%';

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

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

  // read the generated file from 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 a Stacked Chart in PowerPoint</h1>
      <button onClick={createPercentStackedColumn}>Create 100% Stacked Column Chart</button>
    </div>
  );
}

export default App;

The presentation after creating a 100% stacked column chart:

The presentation after creating a 100% stacked column chart


Frequently Asked Questions

Stacked column chart or clustered column chart: which should I choose

Reason: Both chart types draw the series of one category in the same place; the difference is how the series are arranged. A clustered column chart places the series side by side, which suits comparing the height of each series; a stacked column chart piles them up, which suits reading the total and the make-up of that total.

Solution: When you need to compare the values of each series, insert a clustered column chart with ChartType.ColumnClustered; when you need to highlight the total and its composition, insert a stacked column chart with ChartType.ColumnStacked. Both use the same data, so switching the view only takes a change of chart type:

const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.ColumnStacked, rectangle: rect, init: false });

How do I hide the gridlines of a stacked chart's value axis

Reason: The horizontal gridlines that are on by default cross the stacked columns, and the boundaries between segments overlap the gridlines, which looks cluttered.

Solution: Set the fill type of the value axis major gridlines to FillFormatType.None to hide the gridlines without affecting the columns themselves:

chart.PrimaryValueAxis.MajorGridTextLines.FillType = pptModule.FillFormatType.None;

How do I show the value of each segment on a stacked chart

Reason: A stacked chart only gives the height of each segment; the exact value still has to be estimated against the value axis, which is hard to read when there are many segments.

Solution: Turn on the data labels of the series explicitly, and the value of that series in each category appears next to each segment:

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

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.

Published in Chart

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.

Published in Chart

Charts turn a set of numbers into a shape you can read at a glance, making proportions, trends, and comparisons in a presentation far easier to grasp. Inserting a chart by hand in PowerPoint means picking a type, pasting the data, and adjusting titles and labels each time; generating a presentation that already contains charts in the browser folds that step into an automated document workflow.

This article uses Spire.Presentation for JavaScript to build PowerPoint presentations and add charts 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 product supports many chart types, including column, line, pie, bar, scatter, and bubble charts; this tutorial selects three common types—column, pie, and line—as examples.

The article covers three 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 a Column Chart

A column chart is a good fit for comparing several series across multiple categories: each category gets a group of columns of different heights, so the differences stand out at once. To build one, write the values for each category into the chart's data table, bind a data range to each series, and Spire.Presentation draws the columns on the slide.

Syntax What it does
slide.Shapes.AppendChart({ type: ChartType.ColumnClustered, rectangle, init }) Inserts a clustered column chart at the given position on the slide
chart.ChartTitle.TextProperties.Text and chart.HasTitle = true Sets and shows the chart title
chart.ChartData.get_Item({ row, column }).Text Writes the series names and category names
chart.ChartData.get_Item({ row, column }).NumberValue Writes the value for each category
chart.Series.SeriesLabel Specifies the range that holds the series names
chart.Categories.CategoryLabels Specifies the range that holds the category labels
chart.Series.get_Item(i).Values Binds the value range to the i-th series

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Define the chart's rectangle with RectangleF.FromLTRB(), then call Shapes.AppendChart() with ChartType.ColumnClustered to insert a clustered column chart.
  3. Set ChartTitle.TextProperties.Text and set HasTitle to true so the chart shows a title.
  4. Write the series names and category names through ChartData.get_Item() with Text, and the values for each category with NumberValue.
  5. Bind the series names and category labels with Series.SeriesLabel and Categories.CategoryLabels, then bind a value range to each series with Series.get_Item(i).Values.
  6. Save the result file with Presentation.SaveToFile(), release the presentation with Dispose(), then read the file back from the VFS and trigger the download.

The code for creating a column chart is as follows:

function App() {
  // create a column chart
  const createColumnChart = 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 clustered column chart on the slide
    const rect = pptModule.RectangleF.FromLTRB(90, 100, 640, 420);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.ColumnClustered, rectangle: rect, init: false });

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

    // prepare the data for two series
    const quarters = ['Q1', 'Q2', 'Q3', 'Q4'];
    const online = [128, 156, 142, 180];
    const inStore = [96, 88, 110, 124];

    // write the series names
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Online';
    chart.ChartData.get_Item({ row: 0, column: 2 }).Text = 'In-store';

    // write the category names and the value for each category
    for (let i = 0; i < quarters.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = quarters[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = online[i];
      chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = inStore[i];
    }

    // bind the series names, category labels, and value ranges
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'C1' });
    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' });
    chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C5' });

    // save the result file
    const outputFileName = 'ColumnChart_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 Charts in PowerPoint</h1>
      <button onClick={createColumnChart}>Create a column chart</button>
    </div>
  );
}

export default App;

The presentation after creating a column chart:

The presentation after creating a column chart


Create a Pie Chart

A pie chart uses the size of each slice to show how each part contributes to the whole, which suits category data with only one set of values. Beyond inserting the shape, you can give every slice its own color and let the labels show both the value and the percentage, making the composition clearer.

Syntax What it does
slide.Shapes.AppendChart({ type: ChartType.Pie, rectangle, init }) Inserts a pie chart at the given position on the slide
chart.ChartData.get_Item({ row, column }).Text / NumberValue Writes the categories and values of the single series
chart.Series.get_Item(i).DataPoints.Add(cdp) Creates a data point for each slice
point.Fill.FillType = FillFormatType.Solid and point.Fill.SolidColor.Color Fills each slice with a specific solid color
chart.Series.get_Item(i).DataLabels.LabelValueVisible Shows the value on each slice
chart.Series.get_Item(i).DataLabels.PercentValueVisible Shows the percentage on each slice

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Define the chart's rectangle with RectangleF.FromLTRB(), then call Shapes.AppendChart() with ChartType.Pie to insert a pie chart.
  3. Set the chart title, then write the category names, series name, and values through ChartData.get_Item().
  4. 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, create a data point for each slice with new ChartDataPoint() and DataPoints.Add(), then set Fill.FillType and Fill.SolidColor.Color so the slices differ in color.
  6. Set DataLabels.LabelValueVisible and DataLabels.PercentValueVisible to true to show the value and the percentage.
  7. Save the result file with Presentation.SaveToFile(), release the presentation with Dispose(), then read the file back from the VFS and trigger the download.

The code for creating a pie chart is as follows:

function App() {
  // create a pie chart
  const createPieChart = 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 Region';
    chart.ChartTitle.TextProperties.IsCentered = true;
    chart.ChartTitle.Height = 30;
    chart.HasTitle = true;

    // prepare the categories and values
    const regions = ['Northeast', 'South', 'West', 'Midwest'];
    const shares = [35, 25, 22, 18];

    // write the category names and values
    chart.ChartData.get_Item({ row: 0, column: 0 }).Text = 'Region';
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Share';
    for (let i = 0; i < regions.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = regions[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];
    }

    // show the value and the percentage on each slice
    chart.Series.get_Item(0).DataLabels.LabelValueVisible = true;
    chart.Series.get_Item(0).DataLabels.PercentValueVisible = true;

    // save the result file
    const outputFileName = 'PieChart_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 Charts in PowerPoint</h1>
      <button onClick={createPieChart}>Create a pie chart</button>
    </div>
  );
}

export default App;

The presentation after creating a pie chart:

The presentation after creating a pie chart


Create a Line Chart

A line chart connects the values of one series across categories into a single line, which makes the direction of change easy to follow. A line chart with markers also draws a point at every value, so the turning points stand out and each value can be read one by one.

Syntax What it does
slide.Shapes.AppendChart({ type: ChartType.LineMarkers, rectangle, init }) Inserts a line chart with data markers at the given position on the slide
chart.ChartData.get_Item({ row, column }).Text / NumberValue Writes the series names, category names, and values
chart.Series.SeriesLabel and chart.Categories.CategoryLabels Binds the series names and category labels
chart.Series.get_Item(i).Values Binds the value range to the i-th series

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Define the chart's rectangle with RectangleF.FromLTRB(), then call Shapes.AppendChart() with ChartType.LineMarkers to insert a line chart with data markers.
  3. Set the chart title, then write the series names, category names, and values through ChartData.get_Item().
  4. Bind the series names and category labels with Series.SeriesLabel and Categories.CategoryLabels, then bind a value range to each series with Series.get_Item(i).Values.
  5. Save the result file with Presentation.SaveToFile(), release the presentation with Dispose(), then read the file back from the VFS and trigger the download.

The code for creating a line chart is as follows:

function App() {
  // create a line chart
  const createLineChart = 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 line chart with data markers on the slide
    const rect = pptModule.RectangleF.FromLTRB(90, 100, 640, 420);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.LineMarkers, rectangle: rect, init: false });

    // set the chart title
    chart.ChartTitle.TextProperties.Text = 'Quarterly Sales Trend';
    chart.ChartTitle.TextProperties.IsCentered = true;
    chart.ChartTitle.Height = 30;
    chart.HasTitle = true;

    // prepare the data for two series
    const quarters = ['Q1', 'Q2', 'Q3', 'Q4'];
    const online = [128, 156, 142, 180];
    const inStore = [96, 88, 110, 124];

    // write the series names
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Online';
    chart.ChartData.get_Item({ row: 0, column: 2 }).Text = 'In-store';

    // write the category names and the value for each category
    for (let i = 0; i < quarters.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = quarters[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = online[i];
      chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = inStore[i];
    }

    // bind the series names, category labels, and value ranges
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'C1' });
    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' });
    chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C5' });

    // save the result file
    const outputFileName = 'LineChart_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 Charts in PowerPoint</h1>
      <button onClick={createLineChart}>Create a line chart</button>
    </div>
  );
}

export default App;

The presentation after creating a line chart:

The presentation after creating a line chart


FAQ

Why is the chart title not shown after I set it

Cause: Assigning ChartTitle.TextProperties.Text alone is not enough; when the title switch is off, the title is not drawn.

Solution: Set chart.HasTitle to true at the same time as the text, and the title appears above the chart:

chart.ChartTitle.TextProperties.Text = 'Quarterly Sales';
chart.HasTitle = true;

Why do the category labels or values show up empty

Cause: After the chart data is written, the chart has no categories or values to use until the category label range and each series' value range are bound to it.

Solution: Write the cells with ChartData.get_Item() first, then bind the matching ranges with Categories.CategoryLabels and Series.get_Item(i).Values; both use ChartData.get_Item({ name, endCellName }) to give the start and end cells.

How do I create a line chart with data markers

Cause: There are several line chart types; a plain line chart only draws the line, so the marker type has to be chosen explicitly.

Solution: When calling Shapes.AppendChart(), set the type to ChartType.LineMarkers, and a data marker is drawn at every value.


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.

Published in Chart

A chart that carries only one chart type forces data with different magnitudes or different readings onto separate slides, and readers have to compare across two charts. A combination chart merges two or more chart types into a single chart: each series gets its own type while the shared category axis keeps the series aligned, so highs and trends can be read together. Which types to pair is decided by the questions the chart has to answer at once; there is no fixed pairing, and columns plus a line are simply the most common in practice. This article uses Spire.Presentation for JavaScript to create PowerPoint presentations with combination charts 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 a Column and Line Combination Chart

A chart with a single type gives data of different magnitudes or readings one slide each, and comparing them first requires aligning two category axes. A combination chart starts from a base chart and changes the type of one series to another chart type, so both readings land in a single chart whose category axis is already aligned. This feature is the foundation of a combination chart: once a column and a line coexist, the same data is expressed in two chart types, and how the scales are assigned can be adjusted in the next step.

Writing Result
slide.Shapes.AppendChart({ type: ChartType.ColumnClustered, rectangle, init }) Inserts a clustered column chart at the specified position on the slide
chart.ChartTitle.TextProperties.Text and chart.HasTitle = true Sets and shows the chart title
chart.ChartData.get_Item({ row, column }).Text / NumberValue Writes the category names, series names, and the values of each category
chart.Series.SeriesLabel and chart.Categories.CategoryLabels Binds the ranges that hold the series names and category labels
chart.Series.get_Item(i).Values Binds the value range of the i-th series
chart.Series.get_Item(i).Type = ChartType.LineMarkers Changes the type of the specified series to a line with data markers

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Define the chart's rectangle with RectangleF.FromLTRB(), then call Shapes.AppendChart() with ChartType.ColumnClustered to insert a clustered column chart.
  3. Set ChartTitle.TextProperties.Text and set HasTitle to true so the chart shows a title.
  4. Write the category names and series names through ChartData.get_Item() with Text, and the values for each category with NumberValue.
  5. Bind the series names and category labels with Series.SeriesLabel and Categories.CategoryLabels, then bind a value range to each series with Series.get_Item(i).Values.
  6. Set the Type of the second series to ChartType.LineMarkers, so that series turns from a column into a line with data markers and forms a combination chart with the first series.
  7. Save the result file with Presentation.SaveToFile(), release the presentation with Dispose(), then read the file back from the VFS and trigger the download.

The code for creating a combination chart is as follows:

function App() {
  // create a column and line combination chart
  const createCombinationChart = 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 clustered column chart on the slide
    const rect = pptModule.RectangleF.FromLTRB(60, 90, 700, 430);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.ColumnClustered, rectangle: rect, init: false });

    // set the chart title
    chart.ChartTitle.TextProperties.Text = 'Monthly Sales and Growth Rate';
    chart.ChartTitle.TextProperties.IsCentered = true;
    chart.ChartTitle.Height = 30;
    chart.HasTitle = true;

    // prepare the data of the two series
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
    const sales = [200, 250, 300, 150, 200, 400];
    const growth = [0.6, 0.8, 0.6, 0.2, 0.5, 0.9];

    // write the category and series names
    chart.ChartData.get_Item({ row: 0, column: 0 }).Text = 'Month';
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Sales';
    chart.ChartData.get_Item({ row: 0, column: 2 }).Text = 'Growth';

    // write the values of each category
    for (let i = 0; i < months.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = months[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = sales[i];
      chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = growth[i];
    }

    // bind the series names, category labels and value ranges
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'C1' });
    chart.Categories.CategoryLabels = chart.ChartData.get_Item({ name: 'A2', endCellName: 'A7' });
    chart.Series.get_Item(0).Values = chart.ChartData.get_Item({ name: 'B2', endCellName: 'B7' });
    chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C7' });

    // change the type of the second series to a line with markers, forming a combination chart with the column
    chart.Series.get_Item(1).Type = pptModule.ChartType.LineMarkers;

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

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

  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 a Combination Chart in PowerPoint</h1>
      <button onClick={createCombinationChart}>Create Combination Chart</button>
    </div>
  );
}

export default App;

The presentation after creating a column and line combination chart:

The presentation after creating a column and line combination chart


Plot a Series on the Secondary Axis

In a combination chart, when two series share one value axis and their magnitudes differ widely, the smaller series is pressed into a line hugging the bottom of the chart, and its movement cannot be read. The secondary axis gives such a series its own value scale on the right: the column still follows the left scale while the line follows the right one, so both magnitudes can fill the vertical space of the chart. The right axis can also use its own number format, so the scale shows the unit it needs directly.

Writing Result
chart.Series.get_Item(i).UseSecondAxis = true Plots the specified series on the secondary axis
chart.SecondaryValueAxis.NumberFormat Sets the number format of the secondary axis scale
chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None Hides the gridlines of the secondary axis

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready, load the font file, then create a blank presentation with new Presentation() and get the first slide.
  2. Define the chart's rectangle with RectangleF.FromLTRB(), then call Shapes.AppendChart() with ChartType.ColumnClustered to insert a clustered column chart.
  3. Set the chart title, then write the category names, series names, and the values of each category through ChartData.get_Item().
  4. Bind the series names, category labels, and value ranges with Series.SeriesLabel, Categories.CategoryLabels, and Series.get_Item(i).Values, and set the Type of the second series to ChartType.LineMarkers.
  5. Set the UseSecondAxis of that series to true, so the line takes its scale from the secondary axis.
  6. Set SecondaryValueAxis.NumberFormat to the number format you need, and set SecondaryValueAxis.MajorGridTextLines.FillType to FillFormatType.None to hide the gridlines of the secondary axis.
  7. Save the result file with Presentation.SaveToFile(), release the presentation with Dispose(), then read the file back from the VFS and trigger the download.

The code for plotting a series on the secondary axis is as follows:

function App() {
  // plot a series on the secondary axis
  const plotOnSecondaryAxis = 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 clustered column chart on the slide
    const rect = pptModule.RectangleF.FromLTRB(60, 90, 700, 430);
    const chart = slide.Shapes.AppendChart({ type: pptModule.ChartType.ColumnClustered, rectangle: rect, init: false });

    // set the chart title
    chart.ChartTitle.TextProperties.Text = 'Monthly Sales and Growth Rate';
    chart.ChartTitle.TextProperties.IsCentered = true;
    chart.ChartTitle.Height = 30;
    chart.HasTitle = true;

    // prepare the data of the two series
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
    const sales = [200, 250, 300, 150, 200, 400];
    const growth = [0.6, 0.8, 0.6, 0.2, 0.5, 0.9];

    // write the category and series names
    chart.ChartData.get_Item({ row: 0, column: 0 }).Text = 'Month';
    chart.ChartData.get_Item({ row: 0, column: 1 }).Text = 'Sales';
    chart.ChartData.get_Item({ row: 0, column: 2 }).Text = 'Growth';

    // write the values of each category
    for (let i = 0; i < months.length; i++) {
      chart.ChartData.get_Item({ row: i + 1, column: 0 }).Text = months[i];
      chart.ChartData.get_Item({ row: i + 1, column: 1 }).NumberValue = sales[i];
      chart.ChartData.get_Item({ row: i + 1, column: 2 }).NumberValue = growth[i];
    }

    // bind the series names, category labels and value ranges
    chart.Series.SeriesLabel = chart.ChartData.get_Item({ name: 'B1', endCellName: 'C1' });
    chart.Categories.CategoryLabels = chart.ChartData.get_Item({ name: 'A2', endCellName: 'A7' });
    chart.Series.get_Item(0).Values = chart.ChartData.get_Item({ name: 'B2', endCellName: 'B7' });
    chart.Series.get_Item(1).Values = chart.ChartData.get_Item({ name: 'C2', endCellName: 'C7' });

    // change the second series to a line with markers
    chart.Series.get_Item(1).Type = pptModule.ChartType.LineMarkers;

    // plot the line series on the secondary axis
    chart.Series.get_Item(1).UseSecondAxis = true;

    // show the secondary axis scale as percentages and hide its gridlines
    chart.SecondaryValueAxis.NumberFormat = '0%';
    chart.SecondaryValueAxis.MajorGridTextLines.FillType = pptModule.FillFormatType.None;

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

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

  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 a Combination Chart in PowerPoint</h1>
      <button onClick={plotOnSecondaryAxis}>Use Secondary Axis</button>
    </div>
  );
}

export default App;

The presentation after plotting a series on the secondary axis:

The presentation after plotting a series on the secondary axis


FAQ

How to fix the scale range of the secondary axis

Cause: The upper and lower limits of the secondary axis are calculated automatically by default, so they change whenever the data changes. In a report that contains several charts, inconsistent right-hand scales make the charts hard to compare side by side.

Solution: Set IsAutoMax and IsAutoMin to false to turn off the automatic calculation, then give MinValue and MaxValue fixed limits:

chart.SecondaryValueAxis.IsAutoMax = false;
chart.SecondaryValueAxis.IsAutoMin = false;
chart.SecondaryValueAxis.MinValue = 0;
chart.SecondaryValueAxis.MaxValue = 1;

How to hide the legend or move it below the chart

Cause: The legend is shown on the right of the chart by default, and with many categories or series it takes width away from the plot area; with only one or two series, the legend is often redundant.

Solution: Set HasLegend to false to hide the legend. To keep the legend but change its position, use ChartLegend.Position, for example to place it below the chart:

chart.HasLegend = false;

chart.HasLegend = true;
chart.ChartLegend.Position = pptModule.ChartLegendPositionType.Bottom;

How to show the value of each point on the line

Cause: A line only shows the trend, so individual values have to be estimated against the right-hand scale; in a combination chart the two series use axes with different scales, which makes the readings easier to mix up.

Solution: Set DataLabels.LabelValueVisible of the line series to true, and the value of each data point is shown next to the point:

chart.Series.get_Item(1).DataLabels.LabelValueVisible = true;

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.

Published in Chart