| |
 |
Convert Chart to Image File - Aspose.Cells
|
 |
This online demo
demonstrates how to convert a Pie
chart to image file
using
Aspose.Cells for .NET API.
The demo creates a workbook
first and inputs the chart source data into the first two columns (A
and B) of the first worksheet.
The first column represents the category data (Region)
where as the second column represents the sales data representing values.
The demo creates a pie chart dynamically representing
Sales By Region into
the worksheet named ChartSheet
based on the different sale values related to different regions. Then, it converts this chart in the first worksheet to image file.
You can either open the resultant
image file into your picture viewer
or save directly to your disk.
| ASP.NET |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<%@ Page Language="VB" MasterPageFile="~/tpl/Demo.Master" AutoEventWireup="true" Inherits="Aspose.Cells.Demos.VisualBasic.Chart2Image"
Title="Convert Chart to Image File - Aspose.Cells Demos" Codebehind="convert-chart-to-image-file.aspx.vb" %>
<asp:Content ID="Content" ContentPlaceHolderID="MainContent" runat="Server">
<p class="componentDescriptionTxt">
<span style="font-size: 10pt; font-family: Arial"></span> </p>
<p class="componentDescriptionTxt">
<span style="font-size: 10pt; font-family: Arial"></span>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="90%">
<tr>
<td valign="top" width="19">
<img alt="" height="41" src="/Common/images/heading_lft.jpg" width="19" /></td>
<td class="demos-heading-bg" width="100%">
<h2 class="demos-heading-bg">
<font face="Arial" size="4">Convert Chart to Image File - Aspose.Cells</font></h2>
</td>
<td valign="top" width="19">
<img alt="" height="41" src="/Common/images/heading_rt.jpg" width="19" /></td>
</tr>
</table>
</p>
<p align="left">
<font color="black" face="Arial" style="background-color: whitesmoke"><span style="color: black;
background-color: white; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US;
mso-fareast-language: EN-US; mso-bidi-language: AR-SA"><font size="2">This online demo
demonstrates how to convert a <span style="mso-bidi-font-weight: normal">Pie</span>
chart to image file </font></span></font><font size="2"><span style="font-family: Arial">
using </span><font face="Arial"><a href="http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx">
Aspose.Cells</a> for .NET API</font></font><span style="font-family: Arial"><font
size="2">.</font></span></p>
<p align="left">
<font color="black" face="Arial" size="2" style="background-color: whitesmoke"><span
style="color: black; background-color: white; mso-fareast-font-family: Times New Roman;
mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">
<span style="color: black; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US;
mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-bidi-font-size: 11.0pt">
</span><span style="color: black; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US;
mso-fareast-language: EN-US; mso-bidi-language: AR-SA">The demo creates a workbook
first and inputs the chart source data into the first two columns (<span style="mso-bidi-font-weight: normal">A
</span>and<span style="mso-bidi-font-weight: normal"> B</span>) of the first worksheet.
The first column represents the category data (<span style="mso-bidi-font-weight: normal">Region</span>)
where as the second column represents the sales data representing values. </span>
</span></font>
</p>
<p align="left">
<font color="black" face="Arial" size="2" style="background-color: whitesmoke"><span
style="color: black; background-color: white; mso-fareast-font-family: 'Times New Roman';
mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">
The demo creates a pie chart dynamically representing <span style="mso-bidi-font-weight: normal">
Sales By</span> <span style="mso-bidi-font-weight: normal">Region</span> into
the worksheet named <span style="mso-bidi-font-weight: normal">ChartSheet </span>
based on the different sale values related to different regions. Then, it converts this chart in the first worksheet to image file.
You can either open the resultant
image file into your picture viewer
or save directly to your disk.</span></font></p>
<asp:Button ID="btnExecute" runat="server" Text="Process" OnClick="btnExecute_Click" />
</asp:Content>
|
| Visual Basic |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
Imports System
Imports System.Data
Imports System.IO
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Aspose.Cells
Imports Aspose.Cells.Charts
Partial Public Class Chart2Image
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnExecute_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExecute.Click
CreateStaticReport()
End Sub
Protected Sub CreateStaticReport()
'Create a new Workbook.
Dim workbook As New Workbook()
'Get the first worksheet.
Dim sheet As Worksheet = workbook.Worksheets(0)
'Set the name of worksheet
sheet.Name = "ChartSheet"
'Get the cells collection in the sheet.
Dim cells As Cells = workbook.Worksheets(0).Cells
'Put some values into different cells of the sheet.
cells("A1").PutValue("Region")
cells("A2").PutValue("France")
cells("A3").PutValue("Germany")
cells("A4").PutValue("England")
cells("A5").PutValue("Sweden")
cells("A6").PutValue("Italy")
cells("A7").PutValue("Spain")
cells("A8").PutValue("Portugal")
cells("B1").PutValue("Sale")
cells("B2").PutValue(70000)
cells("B3").PutValue(55000)
cells("B4").PutValue(30000)
cells("B5").PutValue(40000)
cells("B6").PutValue(35000)
cells("B7").PutValue(32000)
cells("B8").PutValue(10000)
'Create chart
Dim chartIndex As Integer = 0
chartIndex = sheet.Charts.Add(ChartType.Pie, 2, 4, 31, 15)
Dim chart As Chart = sheet.Charts(chartIndex)
'Set properties of chart title
chart.Title.Text = "Sales By Region"
chart.Title.TextFont.Color = System.Drawing.Color.Blue
chart.Title.TextFont.IsBold = True
chart.Title.TextFont.Size = 12
'Set properties of nseries
chart.NSeries.Add("B2:B8", True)
chart.NSeries.CategoryData = "A2:A8"
chart.NSeries.IsColorVaried = True
'Set the DataLabels in the chart
Dim datalabels As DataLabels
For i As Integer = 0 To chart.NSeries.Count - 1
datalabels = chart.NSeries(i).DataLabels
datalabels.Postion = LabelPositionType.Center
datalabels.IsCategoryNameShown = False
datalabels.IsValueShown = False
datalabels.IsPercentageShown = True
datalabels.IsLegendKeyShown = False
Next i
'Set the Legend.
Dim legend As Legend = chart.Legend
legend.Position = LegendPositionType.Left
'Create a memory stream object.
Dim ms As New MemoryStream()
'Conver the chart to image file.
chart.ToImage(ms, System.Drawing.Imaging.ImageFormat.Emf)
'Set Response object to stream the image file.
Dim data() As Byte = ms.ToArray()
Me.Context.Response.Clear()
Me.Context.Response.ContentType = "image/emf"
Me.Context.Response.AddHeader("content-disposition", "attachment; filename=ChartPic.emf")
Me.Context.Response.OutputStream.Write(data, 0, data.Length)
'End response to avoid unneeded html after xls
Me.Response.End()
End Sub
End Class
|
|
|
|