Articles in this section
Category / Section

How to create clustered bar chart in the PowerPoint using the dynamic data from database?

11 mins read

Syncfusion PowerPoint Presentation is a .NET PowerPoint class library that can be used by developers to create, read, and write PowerPoint files by using C#, VB.NET, and managed C++ code without Microsoft PowerPoint Presentation or Interop dependencies. Using this library, you can create a clustered bar chart in the PowerPoint Presentation using dynamic data from the database.

Steps to create clustered bar chart in the PowerPoint Presentation using the dynamic data:

  1. Create a new C# console application (.NET Framework) project.
    Create .NET Framework console application in Visual Studio
  2. Install the Syncfusion.Presentation.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.
    Add Presentation.WinForms NuGet packages
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.Presentation;
using Syncfusion.OfficeChart;

VB.NET

Imports Syncfusion.Presentation
Imports Syncfusion.OfficeChart
  1. Use the following code example to create clustered bar chart in the PowerPoint Presentation using the dynamic data from database.

C#

           //Creates a Presentation instance
           IPresentation pptxDoc = Presentation.Create();
           //Adds a blank slide to the Presentation
           ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
           //Adds chart to the slide with position and size
           IPresentationChart chart = slide.Charts.AddChart(100, 10, 700, 500);
           //Sets chart type.
           chart.ChartType = OfficeChartType.Bar_Clustered;

           //Assign data range.
           chart.DataRange = chart.ChartData[1, 1, 6, 4];
           chart.IsSeriesInRows = false;

           //Gets the data table from the database.
           DataTable dataTable = GetDataTable();
           //Sets data to the chart - RowIndex, columnIndex and data.
           SetChartData(chart, dataTable);

           //Apply chart elements.
           //Set chart title.
           chart.ChartTitle = "Clustered Bar Chart";

           //Sets Datalabels.
           IOfficeChartSerie serie1 = chart.Series[0];
           IOfficeChartSerie serie2 = chart.Series[1];
           IOfficeChartSerie serie3 = chart.Series[2];

           serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
           serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
           serie3.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
           serie1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
           serie2.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
           serie3.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;

           //Sets legend.
           chart.HasLegend = true;
           chart.Legend.Position = OfficeLegendPosition.Bottom;
           //Creates file stream.
           using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../Result.pptx"), FileMode.Create, FileAccess.ReadWrite))
           {
               //Saves the PowerPoint Presentation to file stream.
               pptxDoc.Save(outputFileStream);
           }

VB.NET

           'Creates a Presentation instance
          Dim pptxDoc As IPresentation =  Presentation.Create() 
          'Adds a blank slide to the Presentation
          Dim slide As ISlide =  pptxDoc.Slides.Add(SlideLayoutType.Blank) 
          'Adds chart to the slide with position and size
          Dim chart As IPresentationChart =  slide.Charts.AddChart(100,10,700,500) 
          'Sets chart type.
          chart.ChartType = OfficeChartType.Bar_Clustered

          'Assign data range.
          chart.DataRange = chart.ChartData(1, 1, 6, 4)
          chart.IsSeriesInRows = False

          'Gets the data table from the database.
          Dim dataTable As DataTable =  GetDataTable() 
          'Sets data to the chart - RowIndex, columnIndex and data.
          SetChartData(chart, dataTable)

          'Apply chart elements.
          'Set chart title.
          chart.ChartTitle = "Clustered Bar Chart"

          'Sets Datalabels.
          Dim serie1 As IOfficeChartSerie =  chart.Series(0) 
          Dim serie2 As IOfficeChartSerie =  chart.Series(1) 
          Dim serie3 As IOfficeChartSerie =  chart.Series(2) 

          serie1.DataPoints.DefaultDataPoInteger.DataLabels.IsValue = True
          serie2.DataPoints.DefaultDataPoInteger.DataLabels.IsValue = True
          serie3.DataPoints.DefaultDataPoInteger.DataLabels.IsValue = True
          serie1.DataPoints.DefaultDataPoInteger.DataLabels.Position = OfficeDataLabelPosition.Center
          serie2.DataPoints.DefaultDataPoInteger.DataLabels.Position = OfficeDataLabelPosition.Center
          serie3.DataPoints.DefaultDataPoInteger.DataLabels.Position = OfficeDataLabelPosition.Center

          'Sets legend.
          chart.HasLegend = True
          chart.Legend.Position = OfficeLegendPosition.Bottom
          'Creates file stream.
          Imports (FileStream outputFileStream = New FileStream(Path.GetFullPath("../../Result.pptx"), FileMode.Create, FileAccess.ReadWrite))
          {
              'Saves the PowerPoint Presentation to file stream.
              pptxDoc.Save(outputFileStream)
          }
  1. Add the below helper methods to get the data from database.

C#

      private static DataTable GetDataTable()
      {
          string path = Path.GetFullPath(@"../../Data/DataBase.mdb");
          //Create a new instance of OleDbConnection
          OleDbConnection connection = new OleDbConnection();
          //Sets the string to open a Database
          connection.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Password=\"\";User ID=Admin;Data Source=" + path;
          //Opens the Database connection
          connection.Open();
          //Get all the data from the Database
          OleDbCommand query = new OleDbCommand("select * from Fruits", connection);
          //Create a new instance of OleDbDataAdapter
          OleDbDataAdapter adapter = new OleDbDataAdapter(query);
          //Create a new instance of DataSet
          DataSet dataSet = new DataSet();
          //Adds rows in the Dataset
          adapter.Fill(dataSet);
          //Create a DataTable from the Dataset
          DataTable table = dataSet.Tables[0];
          return table;
      }

      private static void SetChartData(IPresentationChart chart, DataTable dataTable)
      {
          //Sets the heading for chart data.
          chart.ChartData.SetValue(1, 1, "Fruits");
          chart.ChartData.SetValue(1, 2, "Joey");
          chart.ChartData.SetValue(1, 3, "Mathew");
          chart.ChartData.SetValue(1, 4, "Peter");

          int rowIndex = 2;
          int colIndex = 1;
          //Get the values from the DataTable and set the value for chart data.
          foreach (DataRow row in dataTable.Rows)
          {
              foreach (object val in row.ItemArray)
              {
                  string value = val.ToString();
                  //Sets data to the chart - RowIndex, columnIndex and data.
                  chart.ChartData.SetValue(rowIndex, colIndex, value);
                  colIndex++;
                  if (colIndex == (row.ItemArray.Length + 1))
                      break;
              }
              colIndex = 1;
              rowIndex++;
          }
      }

VB.NET

      Private Shared Function GetDataTable() As DataTable
          Dim path As String =  Path.GetFullPath("../../Data/DataBase.mdb") 
          'Create a new instance of OleDbConnection
          Dim connection As OleDbConnection =  New OleDbConnection() 
          'Sets the string to open a Database
          connection.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Password=\"\";User ID=Admin;Data Source=" + path
          'Opens the Database connection
          connection.Open()
          'Get all the data from the Database
          Dim query As OleDbCommand =  New OleDbCommand("select * from Fruits",connection) 
          'Create a new instance of OleDbDataAdapter
          Dim adapter As OleDbDataAdapter =  New OleDbDataAdapter(query) 
          'Create a new instance of DataSet
          Dim dataSet As DataSet =  New DataSet() 
          'Adds rows in the Dataset
          adapter.Fill(dataSet)
          'Create a DataTable from the Dataset
          Dim table As DataTable =  dataSet.Tables(0) 
          Return table
      End Function

      Private Shared  Sub SetChartData(ByVal chart As IPresentationChart, ByVal dataTable As DataTable)
          'Sets the heading for chart data.
          chart.ChartData.SetValue(1, 1, "Fruits")
          chart.ChartData.SetValue(1, 2, "Joey")
          chart.ChartData.SetValue(1, 3, "Mathew")
          chart.ChartData.SetValue(1, 4, "Peter")

          Dim rowIndex As Integer =  2 
          Dim colIndex As Integer =  1 
          'Get the values from the DataTable and set the value for chart data.
          Dim row As DataRow
          For Each row In dataTable.Rows
              Dim val As Object
              For Each val In row.ItemArray
                  Dim value As String =  val.ToString() 
                  'Sets data to the chart - RowIndex, columnIndex and data.
                  chart.ChartData.SetValue(rowIndex, colIndex, value)
                  colIndex = colIndex + 1
                  If colIndex =(row.ItemArray.Length + 1) Then
                      Exit For
                  End If
              Next
              colIndex = 1
              rowIndex = rowIndex + 1
          Next
      End Sub

A complete working sample to create clustered bar chart in the PowerPoint Presentation using the dynamic data from database using C# can be downloaded from GitHub.

By executing the program, you will get the PowerPoint Presentation as follows.

Output document generated

Take a moment to peruse the documentation, where you can find basic PowerPoint Presentation processing options along with features like, find and replace text, working with charts, animation, smart arts and many more in the PowerPoint Presentation, and most importantly PDF conversion with code examples.

Explore more about the rich set of Syncfusion PowerPoint Framework features.

Conclusion

I hope you enjoyed learning about how to create clustered bar chart in the PowerPoint using the dynamic data from database.

You can refer to our WinForms Presentation feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Presentation documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied