Skip to content

Commit 3b50446

Browse files
Merge pull request #1921 from Syncfusion-Content/hotfix/hotfix-v24.2.3
DOCINFRA-2341_merged_using_automation
2 parents 4e01c6d + aa8846b commit 3b50446

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

File-Formats/DocIO/Working-with-Charts.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,170 @@ document.Close()
467467

468468
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Charts/Create-custom-chart).
469469

470+
## Creating a Chart from a Database
471+
472+
Create a chart in a Word document using the .NET Word Library by utilizing the values retrieved from the database.
473+
474+
The following code example illustrates how to create a chart in a Word document from a database.
475+
476+
{% tabs %}
477+
{% highlight c# tabtitle="C# [Windows-specific]" %}
478+
479+
//Create a new instance of WordDocument.
480+
using (WordDocument document = new WordDocument())
481+
{
482+
document.EnsureMinimal();
483+
//Get the data table.
484+
DataTable dataTable = GetDataTable();
485+
//Create and append the chart to the paragraph.
486+
WChart chart = document.LastParagraph.AppendChart(446, 270);
487+
chart.ChartType = OfficeChartType.Pie;
488+
//Assign the data.
489+
AddChartData(chart, dataTable);
490+
//Set a chart title.
491+
chart.ChartTitle = "Best Selling Products";
492+
IOfficeChartSerie pieSeries = chart.Series.Add("Sales");
493+
pieSeries.Values = chart.ChartData[2, 2, 11, 2];
494+
//Set the data label.
495+
pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
496+
pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside;
497+
//Set the category labels.
498+
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 11, 1];
499+
//Set the legend.
500+
chart.HasLegend = true;
501+
//Save a Word document.
502+
document.Save(Path.GetFullPath(@"../../Result.docx"));
503+
}
504+
505+
// Get the data to create a pie chart.
506+
private static DataTable GetDataTable()
507+
{
508+
string path = Path.GetFullPath(@"../../Data/DataBase.mdb");
509+
//Create a new instance of OleDbConnection.
510+
OleDbConnection connection = new OleDbConnection();
511+
//Set the string to open a Database.
512+
connection.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Password=\"\";User ID=Admin;Data Source=" + path;
513+
//Open the Database connection.
514+
connection.Open();
515+
//Get all the data from the Database.
516+
OleDbCommand query = new OleDbCommand("select * from Products", connection);
517+
//Create a new instance of OleDbDataAdapter.
518+
OleDbDataAdapter adapter = new OleDbDataAdapter(query);
519+
//Create a new instance of DataSet.
520+
DataSet dataSet = new DataSet();
521+
//Add rows in the Dataset.
522+
adapter.Fill(dataSet);
523+
//Create a DataTable from the Dataset.
524+
DataTable table = dataSet.Tables[0];
525+
table.TableName = "Products";
526+
return table;
527+
}
528+
529+
// Set the value for the chart.
530+
private static void AddChartData(WChart chart, DataTable dataTable)
531+
{
532+
//Set the value for the chart data.
533+
chart.ChartData.SetValue(1, 1, "Names");
534+
chart.ChartData.SetValue(1, 2, "Product");
535+
536+
int rowIndex = 2;
537+
int colIndex = 1;
538+
//Get the value from the DataTable and set the value for the chart data
539+
foreach (DataRow row in dataTable.Rows)
540+
{
541+
foreach (object val in row.ItemArray)
542+
{
543+
string value = val.ToString();
544+
chart.ChartData.SetValue(rowIndex, colIndex, value);
545+
colIndex++;
546+
if (colIndex == 3)
547+
break;
548+
}
549+
colIndex = 1;
550+
rowIndex++;
551+
}
552+
}
553+
554+
{% endhighlight %}
555+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
556+
557+
' Create a new instance of WordDocument.
558+
Using document As New WordDocument()
559+
document.EnsureMinimal()
560+
' Get the data table.
561+
Dim dataTable As DataTable = GetDataTable()
562+
' Create and append the chart to the paragraph.
563+
Dim chart As WChart = document.LastParagraph.AppendChart(446, 270)
564+
chart.ChartType = OfficeChartType.Pie
565+
' Assign the data.
566+
AddChartData(chart, dataTable)
567+
' Set a chart title.
568+
chart.ChartTitle = "Best Selling Products"
569+
Dim pieSeries As IOfficeChartSerie = chart.Series.Add("Sales")
570+
pieSeries.Values = chart.ChartData(2, 2, 11, 2)
571+
' Set the data label.
572+
pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsValue = True
573+
pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside
574+
' Set the category labels.
575+
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData(2, 1, 11, 1)
576+
' Set the legend.
577+
chart.HasLegend = True
578+
' Save a Word document.
579+
document.Save(Path.GetFullPath("..\..\Result.docx"))
580+
End Using
581+
582+
' Get the data to create a pie chart.
583+
Private Function GetDataTable() As DataTable
584+
585+
Dim path As String = System.IO.Path.GetFullPath("..\..\Data\DataBase.mdb")
586+
' Create a new instance of OleDbConnection.
587+
Dim connection As New OleDbConnection()
588+
' Set the string to open a Database.
589+
connection.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" & path
590+
' Open the Database connection.
591+
connection.Open()
592+
' Get all the data from the Database.
593+
Dim query As New OleDbCommand("select * from Products", connection)
594+
' Create a new instance of OleDbDataAdapter.
595+
Dim adapter As New OleDbDataAdapter(query)
596+
' Create a new instance of DataSet.
597+
Dim dataSet As New DataSet()
598+
' Add rows in the Dataset.
599+
adapter.Fill(dataSet)
600+
' Create a DataTable from the Dataset.
601+
Dim table As DataTable = dataSet.Tables(0)
602+
table.TableName = "Products"
603+
Return table
604+
End Function
605+
606+
' Set the value for the chart.
607+
Private Sub AddChartData(ByVal chart As WChart, ByVal dataTable As DataTable)
608+
' Set the value for the chart data.
609+
chart.ChartData.SetValue(1, 1, "Names")
610+
chart.ChartData.SetValue(1, 2, "Product")
611+
612+
Dim rowIndex As Integer = 2
613+
Dim colIndex As Integer = 1
614+
' Get the value from the DataTable and set the value for the chart data
615+
For Each row As DataRow In dataTable.Rows
616+
For Each val As Object In row.ItemArray
617+
Dim value As String = val.ToString()
618+
chart.ChartData.SetValue(rowIndex, colIndex, value)
619+
colIndex += 1
620+
If colIndex = 3 Then
621+
Exit For
622+
End If
623+
Next
624+
colIndex = 1
625+
rowIndex += 1
626+
Next
627+
End Sub
628+
629+
{% endhighlight %}
630+
{% endtabs %}
631+
632+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Charts/Create-Pie-chart-from-database).
633+
470634
## Refreshing the Chart
471635

472636
The chart may not have the data in the referred excel file instead it may represent some other data. For those charts to have the excel data, it should be refreshed.

0 commit comments

Comments
 (0)