Anyone of us could have met the need to be
able to create a chart within a web page or a pdf.
This article will explain how to build a chart using MSChart and save the output
to a stream of bytes, this stream will be redirected to a generic handler, so
we'll not use a webform.
The same procedure can be used to display the chart in a pdf file, for example
using iTextSharp.
In this project we'll use Northwind database and Entity Data Model and to
retrieve data from table orders, we want to display the orders by shipping
country.
So we create a new ASP.NET empty project called "WebChartStream", add our
database into App_Data and an Entity Model with Orders table mapped in it.
Now is the time to write the class which will build the chart, so we add a new
class called " ChartStream".
We need some imports, the System.Web.UI.DataVisualization.Charting is the
MsChart reference while System.IO will be used to build the MemoryStream:
Imports
System.IO
Imports
System.Web
Imports
System.Web.UI.DataVisualization.Charting
The private fields of the class
Private
c3d As
ChartArea3DStyle
Private
db As
NORTHWNDEntities
Private
serie As Series
Private
mchart As Chart
Variable c3d will be used to transform our chart in 3D visualization mode.
The main function of the class is
Public Function ToBytesStream() As Byte()
the output of this function is a steam of bytes, will use this stream to print
our chart within the generic handler.
The LINQ to Entity query will select the Orders data grouped and ordered by
ShipCountry field:
db =
New NORTHWNDEntities
Dim
query = From c In
db.Orders _
Group
c By c.ShipCountry Into
g = Group _
Select New
With {.Country = ShipCountry, .Orders = g}
Now we can create an instance of chart class and set title, size and the quality
of the image, then we add a new chart area.
mchart = New
Chart
With
mchart
' Set size of chart
.Width = 600
.Height = 400
' Set image
quality
.RenderType = RenderType.ImageTag
.AntiAliasing = AntiAliasingStyles.All
.TextAntiAliasingQuality =
TextAntiAliasingQuality.High
' Add title of
the cart
.Titles.Add("Orders by Countries")
.Titles(0).Font = New System.Drawing.Font("Arial",
16.0F)
' Add area
.ChartAreas.Add("orders")
The 3D view is made creating a ChartArea3DStyle object and setting the desired
light style in it:
' Set 3D style
Dim
c3d As New
ChartArea3DStyle(.ChartAreas(0))
c3d.Enable3D = True
c3d.LightStyle = LightStyle.Realistic
Then we need some settings like Axis X and Y title, font size and type, etc., as
you can see in the attached source files so we can omit this obvious part in
this article.


Comments
Join the conversation! Your thoughts help the community grow.