Last active
June 26, 2021 21:41
-
-
Save bhavika/fa653c388004c6a73eb5 to your computer and use it in GitHub Desktop.
Creating a stacked bar graph in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Web.Helpers; | |
using System.Web.Mvc; | |
using System.Web.UI; | |
public class StackedBarGraph | |
{ | |
public ActionResult CreateStackedBarGraph() | |
{ | |
//Using Web Helpers Chart methods | |
// chartType: StackedBar, StackedColumn, etc | |
//Writing this because no documentation or code samples on how to pass multiple values for x and y exist | |
var key = new Chart(width: 300, height: 200, theme: ChartTheme.Green).AddTitle("Stacked bar demo") | |
.AddSeries | |
( | |
chartType: "StackedBar", | |
legend: "Item1", | |
xValue: new[] { "2012", "2013", "2014", "2015"}, | |
yValues: new [] {34, 50, 10, 25} | |
) | |
.AddSeries( | |
chartType:"StackedBar", | |
xValue: new[] { "2012", "2013", "2014", "2015" }, | |
yValues: new [] {12,54,85,3} | |
) | |
.AddSeries(chartType: "StackedBar", | |
xValue: new[] {"2012", "2013", "2014", "2015"}, | |
yValues: new [] {15, 5, 74, 32} | |
); | |
return File(key.ToWebImage().GetBytes(), "image/jpeg"); | |
} | |
} | |
} |
Thank you! There just aren't FULL examples like this of StackedBar anywhere! So Thanks!
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you look up how to create charts in MVC 4 using Razor, this is all you'll get. Unfortunately, the example is only enough for a simple bar graph or pie chart. I couldn't understand how to create a stacked bar or stacked column graph from any documentation.
My example works now. I picked numbers myself but you can get them from a list or database too. You can use this code to get the image in any image tag on your .cshtml page.