Want to view the source code for this page? Go ahead! Click the buttons below to see the available code.
<%@ Page Title="" Language="C#" MasterPageFile="~/Themes/Default/MasterPages/Samples.Master" AutoEventWireup="true" CodeBehind="ASPNETChartsSample.aspx.cs" Inherits="Pylus.Web.Content.ASPNETChartsSample" %>
<asp:Content ID="Content2" ContentPlaceHolderID="_contentPlaceHolderSamplesContent" runat="server">
<div class="contentPage">
<div class="contentHeader">
<h2>A Quick Look at the New Built-In ASP.NET Charts</h2>
<p class="datePosted">Dec 31, 2008</p>
<p class="technologies">Technologies</p>
<ul>
<li>ASP.NET 3.5 SP1</li>
<li>System.Web.UI.DataVisualization Library</li>
</ul>
</div>
<div class="primaryContent">
<p>One common feature in applications is charting. For a long time third-party libraries were necessary unless you wanted
to roll your own, but not any more. Late this year Microsoft released a charting library for use in ASP.NET and Windows Forms
to be included as a part of the standard ASP.NET libraries.</p>
<p>Of course, since this charting library came out after .NET 3.5 SP1, it had to be released outside of the normal framework
cycle. Here are a couple examples, both of which show the number of the downloads of sample code from this site over the last year.
As you can see, the crawler is obviously the most popular choice.</p>
<br />
<asp:chart id="_chart1" runat="server" Height="300px" Width="650px"
ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" Palette="SemiTransparent"
imagetype="Png" BorderDashStyle="Solid" BackSecondaryColor="White"
BackGradientStyle="TopBottom" BorderWidth="2" backcolor="#D3DFF0"
BorderColor="26, 59, 105">
<legends>
<asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold"></asp:Legend>
</legends>
<borderskin skinstyle="Emboss"></borderskin>
<chartareas>
<asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom">
<area3dstyle Rotation="10" perspective="10" Inclination="15" IsRightAngleAxes="False" wallwidth="0" IsClustered="False"></area3dstyle>
<axisy linecolor="64, 64, 64, 64" Title="Total Downloads">
<labelstyle font="Trebuchet MS, 8.25pt, style=Bold" />
<majorgrid linecolor="64, 64, 64, 64" />
</axisy>
<axisx linecolor="64, 64, 64, 64" Title="Month">
<labelstyle font="Trebuchet MS, 8.25pt, style=Bold" />
<majorgrid linecolor="64, 64, 64, 64" />
</axisx>
</asp:ChartArea>
</chartareas>
</asp:chart>
<br />
<asp:chart id="_chart2" runat="server" Height="300px" Width="650px"
ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" Palette="SemiTransparent"
imagetype="Png" BorderDashStyle="Solid" BackSecondaryColor="White"
BackGradientStyle="TopBottom" BorderWidth="2" backcolor="#D3DFF0"
BorderColor="26, 59, 105">
<legends>
<asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold"></asp:Legend>
</legends>
<borderskin skinstyle="Emboss"></borderskin>
<chartareas>
<asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom">
<area3dstyle Rotation="10" perspective="10" Inclination="15" IsRightAngleAxes="False" wallwidth="0" IsClustered="False"></area3dstyle>
<axisy linecolor="64, 64, 64, 64" Title="Total Downloads">
<labelstyle font="Trebuchet MS, 8.25pt, style=Bold" />
<majorgrid linecolor="64, 64, 64, 64" />
</axisy>
<axisx linecolor="64, 64, 64, 64" Title="Month">
<labelstyle font="Trebuchet MS, 8.25pt, style=Bold" />
<majorgrid linecolor="64, 64, 64, 64" />
</axisx>
</asp:ChartArea>
</chartareas>
</asp:chart>
<h3>Installation</h3>
<p>As far as getting this integrated into an existing .NET 3.5 site, that is pretty easy. You have to at least do the following
things:</p>
<ol>
<li><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=130f7986-bf49-4fe5-9ca8-910ae6ea442c&DisplayLang=en">Obtain
the dll's</a> and <a href="http://www.microsoft.com/downloads/details.aspx?familyid=1D69CE13-E1E5-4315-825C-F14D33A303E9&displaylang=en">
install the VS 2008 Add-in</a> (the latter is not actually necessary but is a good idea).</li>
<li>The dll's don't actually need to be installed on the target machine in the gac. Just make sure they are in the bin
directory. And just in case you're wondering, the dll's name is System.Web.DataVisualization.dll.</li>
<li>You will have to modify your web.config. First, you should add the node in system.web > pages > controls to point to the
new dll. And for all of these configuration file bits, I suggest taking a look at the web.config in
<a href="http://code.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=1591">the charting samples
that Microsoft provides</a>.</li>
<li>You will need to add the ChartImg.axd HttpHandler.</li>
<li>You will need to add a node in the appSettings to specify a few options, like if you want images to be saved off to
disk or are only to be stored in memory.</li>
</ol>
<p>Also, SP1 does not need to be installed on the target machine. So if your servers have 3.5 on them but not SP1, you should be
fine. I have not tried it on a server that only had the 2.0 framework installed.</p>
<h3>A Bit On How They Work</h3>
<p>If you are curious on how it all works, scroll down a bit to the "View aspx" or "View CS" buttons below and click on them.
That will show you the source behind this page so you can see how they work.</p>
<p>The charts above are pulling data from the database about 2008's downloads. If you want you can hard-code this values for
the charts, though I would imagine doing that would be the exception and not the norm. But, just in case you want to see how
that works, here you go:</p>
<asp:chart id="_chart3" runat="server" Palette="BrightPastel" BackColor="#D3DFF0" Height="296px" Width="412px" BorderDashStyle="Solid" BackGradientStyle="TopBottom" BorderWidth="2" BorderColor="26, 59, 105" IsSoftShadows="False" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)">
<legends>
<asp:Legend Title="How I Spend My Time" TitleFont="Microsoft Sans Serif, 8pt, style=Bold" BackColor="Transparent" IsEquallySpacedItems="True" Font="Trebuchet MS, 8pt, style=Bold" IsTextAutoFit="False" Name="Default"></asp:Legend>
</legends>
<borderskin SkinStyle="Emboss"></borderskin>
<series>
<asp:Series ChartArea="Area1" XValueType="Double" Name="Series1" ChartType="Pie" Font="Trebuchet MS, 8.25pt, style=Bold" CustomProperties="DoughnutRadius=25, PieDrawingStyle=Concave, CollectedLabel=Other, MinimumRelativePieSize=20" MarkerStyle="Circle" BorderColor="64, 64, 64, 64" Color="180, 65, 140, 240" YValueType="Double" Label="#PERCENT{P1}">
<points>
<asp:DataPoint LegendText="Sleep" CustomProperties="OriginalPointIndex=0" YValues="6" />
<asp:DataPoint LegendText="Coding" CustomProperties="OriginalPointIndex=1" YValues="12" />
<asp:DataPoint LegendText="Eating" CustomProperties="OriginalPointIndex=2" YValues="2" />
<asp:DataPoint LegendText="Greek" CustomProperties="OriginalPointIndex=3" YValues="1" />
<asp:DataPoint LegendText="Fam" CustomProperties="OriginalPointIndex=4" YValues="2.5" />
<asp:DataPoint LegendText="Reading" CustomProperties="OriginalPointIndex=5" YValues=".5" />
</points>
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="Area1" BorderColor="64, 64, 64, 64" BackSecondaryColor="Transparent" BackColor="Transparent" ShadowColor="Transparent" BackGradientStyle="TopBottom">
<axisy2>
<MajorGrid Enabled="False" />
<MajorTickMark Enabled="False" />
</axisy2>
<axisx2>
<MajorGrid Enabled="False" />
<MajorTickMark Enabled="False" />
</axisx2>
<area3dstyle PointGapDepth="900" Rotation="162" IsRightAngleAxes="False" WallWidth="25" IsClustered="False" />
<axisy LineColor="64, 64, 64, 64">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="64, 64, 64, 64" Enabled="False" />
<MajorTickMark Enabled="False" />
</axisy>
<axisx LineColor="64, 64, 64, 64">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="64, 64, 64, 64" Enabled="False" />
<MajorTickMark Enabled="False" />
</axisx>
</asp:ChartArea>
</chartareas>
</asp:chart>
<p>As a general rule, the data appears to be laid out in this basic way: each graph will have one or more series, and each
series will have one or more data points. For this pie chart there is but one series of data points, which I would imagine
would be the norm for pie charts. But for line and bar charts this is not the case. Though you will often see just one
series of data points, they are often used to contrast, compare or correlate multiple sets of data. Take the download data
above. Three downloads are shown there, each with the total of downloads for each month. All of the data for the crawler is
one series, with each month's bit of data as a singular data point, and so on with the other two downloads. </p>
<h3>Conclusion</h3>
<p>I am still messing around with the new library so there are a number of things still to learn. So far, though, my experience
has been positive. It is easy to setup and use. As I find interesting things
I am sure you will see many of them up here on the blog. If you have any questions, let me know.</p>
<pylus:ViewSourceControl runat="server" ID="_viewSourceControl" />
</div>
<div class="otherResources">
<h3>Other Resources</h3>
<ul>
<li>The Announcement on Scott Guthrie's Blog: <a href="http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx">http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx</a></li>
</ul>
</div>
</asp:Content>