Skip to content

Commit

Permalink
Merge pull request #40 from snehasf3509/master
Browse files Browse the repository at this point in the history
Sample to create a chart and save as image
  • Loading branch information
MohanaselvamJothi authored Jan 19, 2024
2 parents 5a07e3c + 5f4da8f commit 08798e0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34322.80
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-chart-and-save-as-image", "Create-chart-and-save-as-image\Create-chart-and-save-as-image.csproj", "{8182257B-E0CB-404F-A8ED-1DB1CB0CBBDF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8182257B-E0CB-404F-A8ED-1DB1CB0CBBDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8182257B-E0CB-404F-A8ED-1DB1CB0CBBDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8182257B-E0CB-404F-A8ED-1DB1CB0CBBDF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8182257B-E0CB-404F-A8ED-1DB1CB0CBBDF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6E30F48D-7BB4-4ED0-807A-70B73F82FBD4}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Create_chart_and_save_as_image</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.PresentationRenderer.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Syncfusion.OfficeChart;
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;


//Creates a Presentation instance
IPresentation pptxDoc = Presentation.Create();
//Adds a blank slide to the Presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a Pie chart
IPresentationChart pieChart = CreatePieChart(slide);
//Initialize the PresentationRenderer
pptxDoc.PresentationRenderer = new PresentationRenderer();
// Converts the chart to image.
Stream image = new FileStream("../../../ChartToImage.jpg", FileMode.Create, FileAccess.ReadWrite);
pptxDoc.PresentationRenderer.ConvertToImage(pieChart, image);
//Closes the presentation
pptxDoc.Close();
image.Close();

IPresentationChart CreatePieChart(ISlide slide)
{
//Adds chart to the slide with position and size
IPresentationChart chart = slide.Charts.AddChart(100, 10, 500, 300);
chart.ChartType = OfficeChartType.Pie;

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

//Set data to the chart- RowIndex, columnIndex, and data
chart.ChartData.SetValue(1, 1, "Food");
chart.ChartData.SetValue(2, 1, "Fruits");
chart.ChartData.SetValue(3, 1, "Vegetables");
chart.ChartData.SetValue(4, 1, "Dairy");
chart.ChartData.SetValue(5, 1, "Protein");
chart.ChartData.SetValue(6, 1, "Grains");
chart.ChartData.SetValue(1, 2, "Percentage");
chart.ChartData.SetValue(2, 2, 36);
chart.ChartData.SetValue(3, 2, 14);
chart.ChartData.SetValue(4, 2, 13);
chart.ChartData.SetValue(5, 2, 28);
chart.ChartData.SetValue(6, 2, 9);

//Set Chart Title
chart.ChartTitle = "Pie Chart";

return chart;
}

0 comments on commit 08798e0

Please sign in to comment.