none
Create a PivotChart (based on PivotTable) using C# RRS feed

  • Question

  • Hi guys,

    How do I create a PivotChart (based on PivotTable) using C#?

    Thanks in advance.

    Tuesday, August 26, 2008 9:32 AM

Answers

  • Hello,

     

    We can just create a pivot table firstly, and then create a common chart and call Chart.SetSourceData() method to set the chart's data to pivot table's range. The code looks like the following:

    Code Snippet
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Sheet1!R1C1:R15C2", Version:=xlPivotTableVersion12).CreatePivotTable _
            TableDestination:="Sheet4!R1C1", TableName:="PivotTable1", DefaultVersion _
            :=xlPivotTableVersion12
        Sheets("Sheet4").Select
        Cells(1, 1).Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=Range("Sheet4!$A$1:$C$18")
        ActiveWorkbook.ShowPivotChartActiveFields = True
        ActiveChart.ChartType = xlColumnClustered

     

     

     

    Thanks,

    Ji

    Monday, September 1, 2008 8:30 AM
    Moderator

All replies

  • Hello,

     

    We can just create a pivot table firstly, and then create a common chart and call Chart.SetSourceData() method to set the chart's data to pivot table's range. The code looks like the following:

    Code Snippet
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "Sheet1!R1C1:R15C2", Version:=xlPivotTableVersion12).CreatePivotTable _
            TableDestination:="Sheet4!R1C1", TableName:="PivotTable1", DefaultVersion _
            :=xlPivotTableVersion12
        Sheets("Sheet4").Select
        Cells(1, 1).Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=Range("Sheet4!$A$1:$C$18")
        ActiveWorkbook.ShowPivotChartActiveFields = True
        ActiveChart.ChartType = xlColumnClustered

     

     

     

    Thanks,

    Ji

    Monday, September 1, 2008 8:30 AM
    Moderator
  • Great! Thanks!

     

    Monday, September 1, 2008 9:28 AM