Question

I am making a Waterfall chart in a PowerPoint add-in using VSTO.

PowerPoint.Slide slide = null;
PowerPoint.Shape shape = null;
PowerPoint.Chart chart = null;

This lines tells what kind of chart to make.

shape = slide.Shapes.AddChart(Office.XlChartType.xlColumnStacked, 200, 200, 300, 200);

This line here opens an Excel workbook which makes the chart when data is added into it.

chart = shape.Chart;

Here is an image of the chart that I create.My Waterfall chart

Now the problem is, I want the 2nd bar to be plotted form the end of the 1st bar i.e from 4 instead of 0 on the x-axis.

Like this.

The Real Waterfall Chart

Can someone tell me is there any way I can draw my series from the value of the previous series instead of 0 value of x-axis?

Was it helpful?

Solution

I don't think Excel or PowerPoint charts supports waterfalls out of the box. However, you can do this by keeping the chart type as stacked and adding a separate helper chart series that is invisible and pushes the other series up. Then it is all about calculating the different values for this helper chart series based on the previous values of the actual chart series.

By invisible, I imply making both the line and the fill none. The chart series will still be there, but you just won't see it unless you start editing the chart.

OTHER TIPS

I created waterfall chart from Stacked column chart and used the Workbook object from the Chartdata of the Selected shape. You can follow this link to check how to create waterfall chart manually..and then manipulate the Worksheet/datasheet of the chart through code to create Waterfall chart. Here is a snippet of my code..

 private void calculationAndFormatting(bool excelEvent,Excel.Worksheet Sheet)
    {
        //unregister from Excel Change Event

        Sheet.Application.EnableEvents = false;

                int lRow = 1;
                lRow = iRowCount;

                Sheet.Range["A1", "A" + lRow].Copy(Type.Missing);
                Sheet.Range["B" + (lRow + 5)].PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
                Sheet.Range["B1", "B" + lRow].Copy(Type.Missing);
                Sheet.Range["A" + (lRow + 5)].PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
                Sheet.Range["B1", "D1"].Copy(Type.Missing);
                Sheet.Range["C" + (lRow + 5)].PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
                int ulRow = Sheet.UsedRange.Rows.Count;
                int k=ulRow;
                if (!excelEvent)
                {
                    Sheet.Range["C" + (lRow + 6)].Value = "0";
                    Sheet.Range["C" + (ulRow + 1)].Value = "0";

                    Sheet.Range["A" + (ulRow + 1)].Value = "=SUM(A" + (lRow + 5) + ":" + "A" + ulRow + ")";
                    Sheet.Range["B" + (ulRow + 1)].Value = "Total Value";
                    k = ulRow + 1;
                }              
                for (int i = lRow + 6; i <= k; i++)
                {
                    if (Sheet.Range["A" + i].Value < 0)
                    {
                        if (i <= k-1 && i != lRow + 6)
                        {
                            Sheet.Range["C" + i].Formula = "=E" + (i - 1) + "+" + "A" + i;
                        }
                        Sheet.Range["D" + i].Formula = "=-A" + i;
                        Sheet.Range["E" + i].Formula = "=C" + i;
                    }
                    else
                    {
                        if (i <= k-1 && i != lRow + 6)
                        {
                            Sheet.Range["C" + i].Formula = "=E" + (i - 1);
                        }
                        Sheet.Range["D" + i].Formula = "=A" + i;
                        Sheet.Range["E" + i].Formula = "=C" + i + "+" + "D" + i;
                    }

            }
                string sourceCol = "='" + Sheet.Name + "'!$C$" + (lRow + 5) + ":$E$" + (k);
                pChart.SetSourceData(sourceCol, PowerPoint.XlRowCol.xlColumns);


                PowerPoint.Axis axis = pChart.Axes(PowerPoint.XlAxisType.xlValue);
                axis.MaximumScale = 1.25 * (System.Double)Sheet.Range["D" + (k)].Value;
                axis.MinimumScale = 0.0;
                PowerPoint.Axis catAxis = pChart.Axes(PowerPoint.XlAxisType.xlCategory);

                catAxis.CategoryNames = Sheet.Range["B" + (lRow + 6), "B" + (k)].Value;
                                applyFormatting();
                drawLeaderLines(Sheet);
            Sheet.Application.EnableEvents = true;
            Sheet = null;

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top