Skip to content

Commit 58c1027

Browse files
Merge pull request #166 from Venkateshmuruganandam/main
Sample added for create a chart in a Word document from database
2 parents cfb4960 + d0874cf commit 58c1027

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34414.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-Pie-chart-from-database", "Create-Pie-chart-from-database\Create-Pie-chart-from-database.csproj", "{C60A4689-D9D8-4B34-81BA-9F155029A99A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C60A4689-D9D8-4B34-81BA-9F155029A99A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C60A4689-D9D8-4B34-81BA-9F155029A99A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C60A4689-D9D8-4B34-81BA-9F155029A99A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C60A4689-D9D8-4B34-81BA-9F155029A99A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {900F6099-3871-495F-BA2C-A368A52C3DAC}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{C60A4689-D9D8-4B34-81BA-9F155029A99A}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Create_Pie_chart_from_database</RootNamespace>
10+
<AssemblyName>Create-Pie-chart-from-database</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Syncfusion.Compression.Base, Version=24.2460.7.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Syncfusion.Compression.Base.24.2.7\lib\net46\Syncfusion.Compression.Base.dll</HintPath>
38+
</Reference>
39+
<Reference Include="Syncfusion.DocIO.Base, Version=24.2460.7.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
40+
<HintPath>..\packages\Syncfusion.DocIO.WinForms.24.2.7\lib\net46\Syncfusion.DocIO.Base.dll</HintPath>
41+
</Reference>
42+
<Reference Include="Syncfusion.Licensing, Version=24.2460.7.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
43+
<HintPath>..\packages\Syncfusion.Licensing.24.2.7\lib\net46\Syncfusion.Licensing.dll</HintPath>
44+
</Reference>
45+
<Reference Include="Syncfusion.OfficeChart.Base, Version=24.2460.7.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
46+
<HintPath>..\packages\Syncfusion.OfficeChart.Base.24.2.7\lib\net46\Syncfusion.OfficeChart.Base.dll</HintPath>
47+
</Reference>
48+
<Reference Include="System" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Data.OleDb, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
51+
<HintPath>..\packages\System.Data.OleDb.8.0.0\lib\net462\System.Data.OleDb.dll</HintPath>
52+
</Reference>
53+
<Reference Include="System.Xml.Linq" />
54+
<Reference Include="System.Data.DataSetExtensions" />
55+
<Reference Include="Microsoft.CSharp" />
56+
<Reference Include="System.Data" />
57+
<Reference Include="System.Net.Http" />
58+
<Reference Include="System.Xml" />
59+
</ItemGroup>
60+
<ItemGroup>
61+
<Compile Include="Program.cs" />
62+
<Compile Include="Properties\AssemblyInfo.cs" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<None Include="App.config" />
66+
<None Include="packages.config" />
67+
</ItemGroup>
68+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
69+
</Project>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.OfficeChart;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Data;
6+
using System.Data.OleDb;
7+
using System.IO;
8+
9+
namespace Create_Pie_chart_from_database
10+
{
11+
class Program
12+
{
13+
static void Main(string[] args)
14+
{
15+
//Create a new instance of WordDocument.
16+
using (WordDocument document = new WordDocument())
17+
{
18+
document.EnsureMinimal();
19+
//Get the data table
20+
DataTable dataTable = GetDataTable();
21+
//Create and append the chart to the paragraph.
22+
WChart chart = document.LastParagraph.AppendChart(446, 270);
23+
chart.ChartType = OfficeChartType.Pie;
24+
//Assign the data.
25+
AddChartData(chart, dataTable);
26+
//Set a chart title.
27+
chart.ChartTitle = "Best Selling Products";
28+
IOfficeChartSerie pieSeries = chart.Series.Add("Sales");
29+
pieSeries.Values = chart.ChartData[2, 2, 11, 2];
30+
//Set the data label.
31+
pieSeries.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
32+
pieSeries.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Outside;
33+
//Set the category labels.
34+
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 11, 1];
35+
//Set the legend.
36+
chart.HasLegend = true;
37+
//Save the Word document.
38+
document.Save(Path.GetFullPath(@"../../Result.docx"));
39+
}
40+
}
41+
42+
#region Helper methods
43+
/// <summary>
44+
/// Get the data to create pie chart.
45+
/// </summary>
46+
private static DataTable GetDataTable()
47+
{
48+
string path = Path.GetFullPath(@"../../Data/DataBase.mdb");
49+
//Create a new instance of OleDbConnection
50+
OleDbConnection connection = new OleDbConnection();
51+
//Set the string to open a Database
52+
connection.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Password=\"\";User ID=Admin;Data Source=" + path;
53+
//Open the Database connection
54+
connection.Open();
55+
//Get all the data from the Database
56+
OleDbCommand query = new OleDbCommand("select * from Products", connection);
57+
//Create a new instance of OleDbDataAdapter
58+
OleDbDataAdapter adapter = new OleDbDataAdapter(query);
59+
//Create a new instance of DataSet
60+
DataSet dataSet = new DataSet();
61+
//Add rows in the Dataset
62+
adapter.Fill(dataSet);
63+
//Create a DataTable from the Dataset
64+
DataTable table = dataSet.Tables[0];
65+
table.TableName = "Products";
66+
return table;
67+
}
68+
/// <summary>
69+
/// Set the value for the chart.
70+
/// </summary>
71+
private static void AddChartData(WChart chart, DataTable dataTable)
72+
{
73+
//Set the value for chart data.
74+
chart.ChartData.SetValue(1, 1, "Names");
75+
chart.ChartData.SetValue(1, 2, "Product");
76+
77+
int rowIndex = 2;
78+
int colIndex = 1;
79+
//Get the value from the DataTable and set the value for chart data
80+
foreach (DataRow row in dataTable.Rows)
81+
{
82+
foreach (object val in row.ItemArray)
83+
{
84+
string value = val.ToString();
85+
chart.ChartData.SetValue(rowIndex, colIndex, value);
86+
colIndex++;
87+
if (colIndex == 3)
88+
break;
89+
}
90+
colIndex = 1;
91+
rowIndex++;
92+
}
93+
}
94+
#endregion
95+
}
96+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Create-Pie-chart-from-database")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Create-Pie-chart-from-database")]
13+
[assembly: AssemblyCopyright("Copyright © 2024")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("c60a4689-d9d8-4b34-81ba-9f155029a99a")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Syncfusion.Compression.Base" version="24.2.7" targetFramework="net472" />
4+
<package id="Syncfusion.DocIO.WinForms" version="24.2.7" targetFramework="net472" />
5+
<package id="Syncfusion.Licensing" version="24.2.7" targetFramework="net472" />
6+
<package id="Syncfusion.OfficeChart.Base" version="24.2.7" targetFramework="net472" />
7+
<package id="System.Data.OleDb" version="8.0.0" targetFramework="net472" />
8+
</packages>

0 commit comments

Comments
 (0)