DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • All Things ASP.NET Core and MVC: Tutorials and Articles
  • Add Watermark Text to Images in ASP.NET MVC
  • OAuth Implicit flow Using Angular 6 and ASP.NET MVC Core
  • Identify ASP.NET MVC Assembly Version

Trending

  • Breaking Free From the Cloud With Kamal: Just Enough Orchestration for Your Apps
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • Selecting the Right Automated Tests
  • DevOps Uses a Capability Model, Not a Maturity Model
  1. DZone
  2. Coding
  3. Frameworks
  4. Chart Helper in ASP.NET MVC 3 with Transparent Background

Chart Helper in ASP.NET MVC 3 with Transparent Background

Imran Baloch user avatar by
Imran Baloch
·
Mar. 22, 11 · News
Like (0)
Save
Tweet
Share
13.43K Views

Join the DZone community and get the full member experience.

Join For Free
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. 

 

ASP.NET MVC ASP.NET Chart

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

Opinions expressed by DZone contributors are their own.

Related

  • All Things ASP.NET Core and MVC: Tutorials and Articles
  • Add Watermark Text to Images in ASP.NET MVC
  • OAuth Implicit flow Using Angular 6 and ASP.NET MVC Core
  • Identify ASP.NET MVC Assembly Version

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: