Skip to content

Commit 935d311

Browse files
Added sample to set 2D array data values for a combination chart in a Word document
1 parent 9f2d2d7 commit 935d311

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-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.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Set-2D-array-data-in-combination-chart", "Set-2D-array-data-in-combination-chart\Set-2D-array-data-in-combination-chart.csproj", "{E214E65C-980A-46F5-8207-6D02212A49F5}"
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+
{E214E65C-980A-46F5-8207-6D02212A49F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E214E65C-980A-46F5-8207-6D02212A49F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E214E65C-980A-46F5-8207-6D02212A49F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E214E65C-980A-46F5-8207-6D02212A49F5}.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 = {404A7F36-0167-4219-AE4D-2B0626CEB7E3}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.IO;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using Syncfusion.OfficeChart;
5+
6+
namespace Set_2D_array_data_in_combination_chart
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Define a 2D array with dimensions [3, 13]
13+
string[,] data = new string[3, 13]
14+
{
15+
{ "Month", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" },
16+
{ "Rainy Days", "12", "11", "10", "9", "8", "6", "4", "6", "7", "8", "10", "11" },
17+
{ "Profit", "3574", "4708", "5332", "6693", "8843", "12347", "15180", "11198", "9739", "9846", "6620", "5085" }
18+
};
19+
//Create a new Word document.
20+
using (WordDocument document = new WordDocument())
21+
{
22+
//Add a section to the document.
23+
IWSection section = document.AddSection();
24+
//Add a paragraph to the section.
25+
IWParagraph paragraph = section.AddParagraph();
26+
//Create and append the chart to the paragraph.
27+
WChart chart = paragraph.AppendChart(446, 270);
28+
//Set chart data using values from the 2D array.
29+
for (int i = 0; i < data.GetLength(0); i++)
30+
{
31+
for (int j = 0; j < data.GetLength(1); j++)
32+
{
33+
string value = data[i, j];
34+
//Try to parse the value as an integer, if possible
35+
if (int.TryParse(value, out int intValue))
36+
{
37+
//Set the integer value.
38+
chart.ChartData.SetValue(j + 1, i + 1, intValue);
39+
}
40+
else
41+
{
42+
//Set the string value.
43+
chart.ChartData.SetValue(j + 1, i + 1, value);
44+
}
45+
}
46+
}
47+
//Set region of Chart data.
48+
chart.DataRange = chart.ChartData[1, 1, 13, 3];
49+
//Set chart series in the column for assigned data region.
50+
chart.IsSeriesInRows = false;
51+
//Set a Chart Title.
52+
chart.ChartTitle = "Combination Chart";
53+
//Set Datalabels.
54+
IOfficeChartSerie series1 = chart.Series[0];
55+
IOfficeChartSerie series2 = chart.Series[1];
56+
//Set Serie type.
57+
series1.SerieType = OfficeChartType.Column_Clustered;
58+
series2.SerieType = OfficeChartType.Line;
59+
series2.UsePrimaryAxis = false;
60+
//Set Datalabels.
61+
series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
62+
//Set legend.
63+
chart.HasLegend = true;
64+
chart.Legend.Position = OfficeLegendPosition.Bottom;
65+
//Set chart type.
66+
chart.ChartType = OfficeChartType.Combination_Chart;
67+
//Set secondary axis on right side.
68+
chart.SecondaryValueAxis.TickLabelPosition = OfficeTickLabelPosition.TickLabelPosition_High;
69+
//Create a file stream.
70+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite))
71+
{
72+
//Save the Word document to the file stream.
73+
document.Save(outputFileStream, FormatType.Docx);
74+
}
75+
}
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Set_2D_array_data_in_combination_chart</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Output\.gitkeep">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
19+
</Project>

0 commit comments

Comments
 (0)