Create Combination Charts in PowerPoint with JavaScript in React

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.