A Powerful Combination--Charting in .Net using SVG
page 1 of 1
Published: 20 Sep 2003
Unedited - Community Contributed
Abstract
When graphic needs exceed the capabilities of third-party components, the programmer needs tools for generating customized charts. This article focuses on generating customized charts using the combination of .Net and SVG.
by Daniel Olson
Feedback
Average Rating: 
Views (Total / Last 10 Days): 26723/ 44

It is well known that a picture is worth a thousand words. Likewise, a good graph is far superior to a large list of numbers, and a long series of numbers can quickly become meaningful when represented in a well-designed chart. Often all that is needed for good numeric presentations are standard graphing capabilities, and there are several excellent third-party components for making charts in .Net (see links below). Problems arise, however, when a programmer needs charting options that the third-party tool cannot provide. Naturally, it is impossible for a component to offer every feature that users could possibly want. For example, Server Objects used to have a component called AspChart, but this was discontinued and is no longer supported because users wanted to have too many different features than was reasonable to supply.

When graphic needs exceed the capabilities of third-party components, the programmer needs tools for generating customized charts. This article focuses on the use of SVG. The advantages of SVG include the following:

  • SVG is XML based, integrates well with other W3C standards, and includes a complete DOM
  • Events and animation can be added declaratively or using ECMAScript
  • Charts print well from a web browser
  • Charts can be zoomed in on and still maintain clean, sharp lines
  • Charts can modified easily without the need to regenerate the whole chart
  • An SVG file can be included in a web page

A couple disadvantages of SVG are that a plug in is necessary and animation on big files can be sluggish.

SVG Code Example

Here is an example of an SVG file that produces three bars, ie. for the beginning of a simple bar chart.

<svg width="120" height="170" x="0" y="0">
<path style="stroke-width:2; stroke:black; fill:red" d="M 20 150 L 20 35 L 40 35 L 40 150 Z"/>
<path style="stroke-width:2; stroke:black; fill:yellow" d="M 50 150 L 50 31 L 70 31 L 70 150 Z"/>
<path style="stroke-width:2; stroke:black; fill:purple" d="M 80 150 L 80 45 L 100 45 L 100 150 Z"/>
</svg>

This would produce a graphic that looks like the one below:
bars
See in SVG format

To be included in a web page, an SVG file requires either an object tag or an embed tag.

<object data="bars.svg" width="500" height="500" type="image/svg-xml">
<embed src="bars.svg" width="500" height="500" type="image/svg-xml">

Combining SVG and .Net

The rest of the examples require a web browser with Adobe's SVG Viewer plugin installed. (If you do not have this plugin, you can get it at http://www.adobe.com/svg/viewer/install/main.html.)

By using .Net, charts can be built from dynamic data. As the data changes, so do the charts. The following are examples of charts that can be built with SVG and .Net:

Simple Bar Chart
The first SVG chart example is a bar chart that displays the names and ages of a list of people. The data for this chart comes from an xml file that has the names and birthdates of my children and my brother-in-law's children. The age is always accurate because it is calculated based on the birthday and today's date. Pointing to a different xml file produces a different bar chart. If I need to change the chart, I simply change the xml file.

Simple Line Chart
The next example is a line chart that shows the price of a stock over a period of time. It reads an xml file that has the date and closing price of the stock. The two stocks I used for this example are Microsoft (MSFT) and Sun Microsystems (SUNW). I obtained the data by downloading historical data since the beginning of the year from Yahoo. (At work, where I have access to real-time data, we have line charts that show our current profit / loss for a symbol, and the charts are updated once per minute.)

Combo Chart
The Combol Chart example combines a bar chart and a line chart. The price of a stock is represented by a line, and the volume traded is represented by bars. The bars are reduced to a fraction of the graph area and their opacity is reduced so that the view of the line is not obstructed. The combo chart reads from an xml file exactly the same way as in the previous example.

Multi Chart
The last example is a chart that actaully contains multiple charts. The user can switch from one chart to the next by clicking on the Symbol for the desired chart. This is accomplished by using SVG's scripting capability. The script is written in ECMA script and is included as part of the SVG code. The script in the Multi Chart causes the opacity of the lines to change from 0.0 (transparent) to 1.0 (completely opaque). The ASPX code loops through the symbols loading the xml file for each symbol and writes the SVG code for that symbol. All of the lines are in the SVG code for each symbol, but the lines for only one chart show at one time.

For a good example of a pie chart using .Net, go to yaflaSVG by Dennis Forbes.

SVG by itself is brilliant, but, when advanced graphing capabilities are needed, the combination of SVG and .Net is a powerhouse.

Third party Chart Components
Chart FX for .Net
Dundas Chart for .Net
Infragistics .Net Chart
ComponentOne Chart for .Net
Advanced Software Engineering Chart Director
.net Charting

SVG Links
Dynamically Generating SVG Graphs with .NET (By Dan Wahlin)
yaflaSVG: Example of generating a pie chart using .Net (By Dennis Forbes)
SVG Tutorials, Samples, Games, etc (By Kevin Lindsey)
Official W3C overview of the Scalable Vector Graphics (SVG) format



User Comments

Title: SVG and SQL SERVER 2005   
Name: WIKI
Date: 2006-12-12 4:21:32 PM
Comment:
HELLO

I would like to have my SVG images in SQL SRV 2005. Have you got any suggestions how to display it on the ASP web page?

thanks
Title: Super 2d/3d Graph Library   
Name: MCota
Date: 2006-10-27 5:51:43 AM
Comment:
With Super 2d/3d Graph Library (current version 6) you can either draw any chart on ASP.NET page or WinForm applications. Same componente works either way.

Check it out at http://www.softwaresigloxxi.com
Title: outdated MIME type used   
Name: stelt
Date: 2006-10-26 5:50:45 PM
Comment:
image/svg-xml is wrong
image/svg+xml is right

a tiny change making all the difference
Title: And I   
Name: dhill
Date: 2006-07-11 4:22:19 AM
Comment:
I too am unable to see the examples.
I have SVG viewer installed. SVG images on other sites work, just not this site. Looks very useful. Thanks for your hard work.
Title: Unable to see images   
Name: vishal
Date: 2006-01-25 3:11:51 AM
Comment:
Hello sir i m not being able to see the svg images or charts in examples at my place.

I had installed the svg viewer.

The article is really very helpful and nice

I want to learn about asp.net and svg charts more please help. my id : vishal.jsk@gmail.com

Thanx






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-09-21 4:47:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search