|
|
Controller Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Highsoft.Web.Mvc.Charts;
namespace MVC_Demo.Areas.Highcharts.Controllers.Shared
{
public partial class SharedController : Controller
{
public ActionResult Streamgraph()
{
List<string> colors = new List<string> { "#2f7ed8", "#0d233a", "#8bbc21", "#910000", "#1aadce", "#7c5ca3", "#63438a", "#492970", "#f28f43", "#77a1e5", "#c42525", "#a6c96a", "#2f7ed8", "#0d233a", "#910000", "#72a308", "#588900", "#3f7000" };
List<string> categories = new List<string>
{
"",
"1924 Chamonix",
"1928 St. Moritz",
"1932 Lake Placid",
"1936 Garmisch-Partenkirchen",
"1940 <i>Cancelled (Sapporo)</i>",
"1944 <i>Cancelled (Cortina d\"Ampezzo)</i>",
"1948 St. Moritz",
"1952 Oslo",
"1956 Cortina d\"Ampezzo",
"1960 Squaw Valley",
"1964 Innsbruck",
"1968 Grenoble",
"1972 Sapporo",
"1976 Innsbruck",
"1980 Lake Placid",
"1984 Sarajevo",
"1988 Calgary",
"1992 Albertville",
"1994 Lillehammer",
"1998 Nagano",
"2002 Salt Lake City",
"2006 Turin",
"2010 Vancouver",
"2014 Sochi"
};
Dictionary<string, List<int>> medals = new Dictionary<string, List<int>>();
medals.Add("Finland", new List<int> { 0, 11, 4, 3, 6, 0, 0, 6, 9, 7, 8, 10, 5, 5, 7, 9, 13, 7, 7, 6, 12, 7, 9, 5, 5 });
medals.Add("Austria", new List<int> { 0, 3, 4, 2, 4, 0, 0, 8, 8, 11, 6, 12, 11, 5, 6, 7, 1, 10, 21, 9, 17, 17, 23, 16, 17 });
medals.Add("Sweden", new List<int> { 0, 2, 5, 3, 7, 0, 0, 10, 4, 10, 7, 7, 8, 4, 2, 4, 8, 6, 4, 3, 3, 7, 14, 11, 15 });
medals.Add("Norway", new List<int> { 0, 17, 15, 10, 15, 0, 0, 10, 16, 4, 6, 15, 14, 12, 7, 10, 9, 5, 20, 26, 25, 25, 19, 23, 26 });
medals.Add("U.S.", new List<int> { 0, 4, 6, 12, 4, 0, 0, 9, 11, 7, 10, 7, 7, 8, 10, 12, 8, 6, 11, 13, 13, 34, 25, 37, 28 });
medals.Add("East Germany", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 14, 19, 23, 24, 25, 0, 0, 0, 0, 0, 0, 0 });
medals.Add("West Germany", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 5, 10, 5, 4, 8, 0, 0, 0, 0, 0, 0, 0 });
medals.Add("Germany", new List<int> { 0, 0, 1, 2, 6, 0, 0, 0, 7, 2, 8, 9, 0, 0, 0, 0, 0, 0, 26, 24, 29, 36, 29, 30, 19 });
medals.Add("Netherlands", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 2, 2, 9, 9, 6, 4, 0, 7, 4, 4, 11, 8, 9, 8, 24 });
medals.Add("Italy", new List<int> { 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 1, 4, 4, 5, 4, 2, 2, 5, 14, 20, 10, 13, 11, 5, 8 });
medals.Add("Canada", new List<int> { 0, 1, 1, 7, 1, 0, 0, 3, 2, 3, 4, 3, 3, 1, 3, 2, 4, 5, 7, 13, 15, 17, 24, 26, 25 });
medals.Add("Switzerland", new List<int> { 0, 3, 1, 1, 3, 0, 0, 10, 2, 6, 2, 0, 6, 10, 5, 5, 5, 15, 3, 9, 7, 11, 14, 9, 11 });
medals.Add("Great Britain", new List<int> { 0, 4, 1, 0, 3, 0, 0, 2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 2, 1, 2, 1, 1, 4 });
medals.Add("France", new List<int> { 0, 3, 1, 1, 1, 0, 0, 5, 1, 0, 3, 7, 9, 3, 1, 1, 3, 2, 9, 5, 8, 11, 9, 11, 15 });
medals.Add("Hungary", new List<int> { 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
medals.Add("Unified Team", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0 });
medals.Add("Soviet Union", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 21, 25, 13, 16, 27, 22, 25, 29, 0, 0, 0, 0, 0, 0, 0 });
medals.Add("Russia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 18, 13, 22, 15, 33 });
medals.Add("Japan", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 1, 1, 1, 7, 5, 10, 2, 1, 5, 8 });
medals.Add("Czechoslovakia", new List<int> { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 4, 3, 1, 1, 6, 3, 3, 0, 0, 0, 0, 0, 0 });
medals.Add("Poland", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 6 });
medals.Add("Spain", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 });
medals.Add("China", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 8, 8, 11, 11, 9 });
medals.Add("South Korea", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 6, 4, 11, 14, 8 });
medals.Add("Czech Republic", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 4, 6, 8 });
medals.Add("Belarus", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 1, 3, 6 });
medals.Add("Kazakhstan", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 1, 1 });
medals.Add("Bulgaria", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 3, 1, 0, 0 });
medals.Add("Denmark", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 });
medals.Add("Ukraine", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 2, 0, 2 });
medals.Add("Australia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3 });
medals.Add("Belgium", new List<int> { 0, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 });
medals.Add("Romania", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
medals.Add("Liechtenstein", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0 });
medals.Add("Yugoslavia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0 });
medals.Add("Luxembourg", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 });
medals.Add("New Zealand", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 });
medals.Add("North Korea", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 });
medals.Add("Slovakia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1 });
medals.Add("Croatia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 3, 3, 1 });
medals.Add("Slovenia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 0, 3, 8 });
medals.Add("Latvia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4 });
medals.Add("Estonia", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 1, 0 });
medals.Add("Uzbekistan", new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 });
Dictionary<string, StreamgraphSeries> medalsHistory = new Dictionary<string, StreamgraphSeries>();
foreach (var item in medals)
{
List<StreamgraphSeriesData> data = new List<StreamgraphSeriesData>();
foreach (var value in item.Value)
data.Add(new StreamgraphSeriesData { Name = item.Key, Y = value });
medalsHistory.Add(item.Key, new StreamgraphSeries { Name = item.Key, Data = data });
}
medals.Clear();
ViewData["Colors"] = colors;
ViewData["Categories"] = categories;
ViewData["Medals"] = medalsHistory;
return View();
}
}
}
Controller Code
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/streamgraph.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/annotations.js"></script>
@using Highsoft.Web.Mvc.Charts
@using Highsoft.Web.Mvc.Charts.Rendering
@{ var chartOptions = new Highcharts
{
Chart = new Highsoft.Web.Mvc.Charts.Chart
{
Type = ChartType.Streamgraph,
MarginBottom = 30,
ZoomType = ChartZoomType.X
},
Colors = ViewData["Colors"] as List<string>,
Title = new Title
{
Floating = true,
Align = TitleAlign.Left,
Text = "Winter Olympic Medal Wins"
},
Subtitle = new Subtitle
{
Floating = true,
Align = SubtitleAlign.Left,
Y = 30,
Text = "Source: <a href=\"https://www.sports-reference.com/olympics/winter/1924/\">sports-reference.com</a>"
},
XAxis = new List<XAxis>
{
new XAxis
{
MaxPadding = 0,
Type = "category",
Crosshair = new XAxisCrosshair { Width = 10 },
Categories = ViewData["Categories"] as List<string>,
Labels = new XAxisLabels
{
Align = XAxisLabelsAlign.Left,
ReserveSpace = false,
Rotation = 270
},
LineWidth = 0,
TickWidth = 0
}
},
YAxis = new List<YAxis>
{
new YAxis
{
Visible = false,
StartOnTick = false,
EndOnTick = false
}
},
Legend = new Legend { Enabled = false },
Annotations = new List<Annotations>
{
new Annotations
{
Labels = new List<AnnotationsLabels>
{
new AnnotationsLabels
{
Point = new
{
X = 5.5,
Y = 30,
XAxis = "0",
YAxis = "0"
},
Text = "Cancelled<br>during<br>World War II"
},
new AnnotationsLabels
{
Point = new
{
X = 18,
Y = 90,
XAxis = "0",
YAxis = "0"
},
Text = "Soviet Union fell,<br>Germany united"
}
},
LabelOptions = new AnnotationsLabelOptions
{
BackgroundColor = "rgba(255,255,255,0.5)",
BorderColor = "silver"
}
}
},
PlotOptions = new PlotOptions
{
Series = new PlotOptionsSeries
{
Label = new PlotOptionsSeriesLabel
{
MinFontSize = 5,
MaxFontSize = 15
}
}
},
Series = new List<Series>
{
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Finland"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Austria"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Sweden"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Norway"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["U.S."],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["East Germany"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["West Germany"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Germany"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Netherlands"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Italy"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Canada"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Switzerland"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Great Britain"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["France"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Hungary"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Unified Team"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Soviet Union"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Russia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Japan"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Czechoslovakia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Poland"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Spain"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["China"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["South Korea"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Czech Republic"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Belarus"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Kazakhstan"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Bulgaria"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Denmark"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Ukraine"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Australia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Belgium"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Romania"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Liechtenstein"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Yugoslavia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Luxembourg"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["New Zealand"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["North Korea"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Slovakia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Croatia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Slovenia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Latvia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Estonia"],
(ViewData["Medals"] as Dictionary<string,StreamgraphSeries>)["Uzbekistan"]
},
Exporting = new Exporting
{
SourceWidth = 800,
SourceHeight = 600
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())