Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Chart Helper in ASP.NET MVC 3 with Transparent Background

DZone's Guide to

Chart Helper in ASP.NET MVC 3 with Transparent Background

·
Free Resource
Introduction:
ASP.NET MVC 3 includes some new helper methods that are used for different purposes. Chart helper method is one them, which makes it very easy to create charts in ASP.NET MVC. In this article, I will show you how you can use Chart helper in ASP.NET MVC. I will also show you how to use the themes provided by ASP.NET MVC to style your charts.  Then I will show how to create a custom theme for making your chart background as transparent.

Description:

First of all, create a new ASP.NET MVC 3 application. Then add an action method named MyChart to your controller,
public ActionResult MyChart()
02 {
03 new Chart(width: 400, height: 200)
04 .AddSeries(
05 chartType: "bar",
06 xValue: new[] { "Math", "English", "Computer", "Urdu" },
07 yValues: new[] { "60", "70", "68", "88" })
08 .Write("png");
09 return null;
10 }

Then add an img tag inside your view and set the src attribute to point this action method,

1	<img src="@Url.Action("MyChart")" alt="SimpleChart" />

Now run this application, you will see the following screen,

          

The above screenshot shows you a chart with default theme. Now, add a Yellow theme to your chart,

public ActionResult MyChart()
02 {
03 new Chart(width: 400, height: 200, theme:ChartTheme.Yellow)
04 .AddSeries(
05 chartType: "bar",
06 xValue: new[] { "Math", "English", "Computer", "Urdu" },
07 yValues: new[] { "60", "70", "68", "88" })
08 .Write("png");
09 return null;
10 }


Run this application again, you will see the following screen,

         


The above screenshot shows you a chart with Yellow theme. This also shows that how easily you can change the theme of your chart. Now, let's add a custom theme which set the background color of your chart to transparent,

01	public ActionResult MyChart()
02 {
03 string myTheme =
04 @"<Chart BackColor=""Transparent"" >
05 <ChartAreas>
06 <ChartArea Name=""Default"" BackColor=""Transparent""></ChartArea>
07 </ChartAreas>
08 </Chart>";
09 new Chart(width: 400, height: 200, theme: myTheme)
10 .AddSeries(
11 chartType: "bar",
12 xValue: new[] { "Math", "English", "Computer", "Urdu" },
13 yValues: new[] { "60", "70", "68", "88" })
14 .Write("png");
15 return null;
16 }


Run this application again, you will see the following screen,

         


The above screenshot shows you a chart with transparent background color. In addition to changing the background color of your chart, you can also change the gradient, border, rotation, line color, etc. Here are some themes provided by ASP.NET MVC by default, which gives you the idea how to style your charts.

 public static class ChartTheme {
02 // Review: Need better names.
03
04 public const string Blue =
05 @"<Chart BackColor=""#D3DFF0"" BackGradientStyle=""TopBottom"" BackSecondaryColor=""White"" BorderColor=""26, 59, 105"" BorderlineDashStyle=""Solid"" BorderWidth=""2"" Palette=""BrightPastel"">
06 <ChartAreas>
07 <ChartArea Name=""Default"" _Template_=""All"" BackColor=""64, 165, 191, 228"" BackGradientStyle=""TopBottom"" BackSecondaryColor=""White"" BorderColor=""64, 64, 64, 64"" BorderDashStyle=""Solid"" ShadowColor=""Transparent"" />
08 </ChartAreas>
09 <Legends>
10 <Legend _Template_=""All"" BackColor=""Transparent"" Font=""Trebuchet MS, 8.25pt, style=Bold"" IsTextAutoFit=""False"" />
11 </Legends>
12 <BorderSkin SkinStyle=""Emboss"" />
13 </Chart>";
14
15 public const string Green =
16 @"<Chart BackColor=""#C9DC87"" BackGradientStyle=""TopBottom"" BorderColor=""181, 64, 1"" BorderWidth=""2"" BorderlineDashStyle=""Solid"" Palette=""BrightPastel"">
17 <ChartAreas>
18 <ChartArea Name=""Default"" _Template_=""All"" BackColor=""Transparent"" BackSecondaryColor=""White"" BorderColor=""64, 64, 64, 64"" BorderDashStyle=""Solid"" ShadowColor=""Transparent"">
19 <AxisY LineColor=""64, 64, 64, 64"">
20 <MajorGrid Interval=""Auto"" LineColor=""64, 64, 64, 64"" />
21 <LabelStyle Font=""Trebuchet MS, 8.25pt, style=Bold"" />
22 </AxisY>
23 <AxisX LineColor=""64, 64, 64, 64"">
24 <MajorGrid LineColor=""64, 64, 64, 64"" />
25 <LabelStyle Font=""Trebuchet MS, 8.25pt, style=Bold"" />
26 </AxisX>
27 <Area3DStyle Inclination=""15"" IsClustered=""False"" IsRightAngleAxes=""False"" Perspective=""10"" Rotation=""10"" WallWidth=""0"" />
28 </ChartArea>
29 </ChartAreas>
30 <Legends>
31 <Legend _Template_=""All"" Alignment=""Center"" BackColor=""Transparent"" Docking=""Bottom"" Font=""Trebuchet MS, 8.25pt, style=Bold"" IsTextAutoFit =""False"" LegendStyle=""Row"">
32 </Legend>
33 </Legends>
34 <BorderSkin SkinStyle=""Emboss"" />
35 </Chart>";
36
37 public const string Vanilla =
38 @"<Chart Palette=""SemiTransparent"" BorderColor=""#000"" BorderWidth=""2"" BorderlineDashStyle=""Solid"">
39 <ChartAreas>
40 <ChartArea _Template_=""All"" Name=""Default"">
41 <AxisX>
42 <MinorGrid Enabled=""False"" />
43 <MajorGrid Enabled=""False"" />
44 </AxisX>
45 <AxisY>
46 <MajorGrid Enabled=""False"" />
47 <MinorGrid Enabled=""False"" />
48 </AxisY>
49 </ChartArea>
50 </ChartAreas>
51 </Chart>";
52
53 public const string Vanilla3D =
54 @"<Chart BackColor=""#555"" BackGradientStyle=""TopBottom"" BorderColor=""181, 64, 1"" BorderWidth=""2"" BorderlineDashStyle=""Solid"" Palette=""SemiTransparent"" AntiAliasing=""All"">
55 <ChartAreas>
56 <ChartArea Name=""Default"" _Template_=""All"" BackColor=""Transparent"" BackSecondaryColor=""White"" BorderColor=""64, 64, 64, 64"" BorderDashStyle=""Solid"" ShadowColor=""Transparent"">
57 <Area3DStyle LightStyle=""Simplistic"" Enable3D=""True"" Inclination=""30"" IsClustered=""False"" IsRightAngleAxes=""False"" Perspective=""10"" Rotation=""-30"" WallWidth=""0"" />
58 </ChartArea>
59 </ChartAreas>
60 </Chart>";
61
62 public const string Yellow =
63 @"<Chart BackColor=""#FADA5E"" BackGradientStyle=""TopBottom"" BorderColor=""#B8860B"" BorderWidth=""2"" BorderlineDashStyle=""Solid"" Palette=""EarthTones"">
64 <ChartAreas>
65 <ChartArea Name=""Default"" _Template_=""All"" BackColor=""Transparent"" BackSecondaryColor=""White"" BorderColor=""64, 64, 64, 64"" BorderDashStyle=""Solid"" ShadowColor=""Transparent"">
66 <AxisY>
67 <LabelStyle Font=""Trebuchet MS, 8.25pt, style=Bold"" />
68 </AxisY>
69 <AxisX LineColor=""64, 64, 64, 64"">
70 <LabelStyle Font=""Trebuchet MS, 8.25pt, style=Bold"" />
71 </AxisX>
72 </ChartArea>
73 </ChartAreas>
74 <Legends>
75 <Legend _Template_=""All"" BackColor=""Transparent"" Docking=""Bottom"" Font=""Trebuchet MS, 8.25pt, style=Bold"" LegendStyle=""Row"">
76 </Legend>
77 </Legends>
78 <BorderSkin SkinStyle=""Emboss"" />
79 </Chart>";
80 }


Note that from these chart themes, Yellow theme used in above example.  


 Summary:
Chart helper makes it very easy to show chart in your ASP.NET MVC application. In this article, I showed you how to create a chart in ASP.NET MVC. I also showed you how to style a chart and how to change the background color of a chart. Hopefully you will enjoy this article too. 

 

Topics:

Published at DZone with permission of Imran Baloch, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}