|
|
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 AreaSpline()
{
List<double?> johnValues = new List<double?> { 3, 4, 3, 5, 4, 10, 12 };
List<double?> janeValues = new List<double?> { 1, 3, 4, 3, 3, 5, 4 };
List<AreasplineSeriesData> johnData = new List<AreasplineSeriesData>();
List<AreasplineSeriesData> janeData = new List<AreasplineSeriesData>();
johnValues.ForEach(p => johnData.Add(new AreasplineSeriesData { Y = p }));
janeValues.ForEach(p => janeData.Add(new AreasplineSeriesData { Y = p }));
ViewData["johnData"] = johnData;
ViewData["janeData"] = janeData;
return View();
}
}
}
Controller Code
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
@using Highsoft.Web.Mvc.Charts
@using Highsoft.Web.Mvc.Charts.Rendering;
@{var chartOptions = new Highcharts
{
Title = new Title
{
Text = "Average fruit consumption during one week"
},
Legend = new Legend
{
Layout = LegendLayout.Vertical,
Align = LegendAlign.Left,
VerticalAlign = LegendVerticalAlign.Top,
X = 150,
Y = 100,
Floating = true,
BorderWidth = 1,
},
XAxis = new List<XAxis>
{
new XAxis
{
Categories = new List<string> {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
},
PlotBands = new List<XAxisPlotBands>
{
new XAxisPlotBands
{
From = 4.5,
To = 6.5,
Color = "rgba(68, 170, 213, .2)"
}
}
}
},
YAxis = new List<YAxis>
{
new YAxis
{
Title = new YAxisTitle
{
Text = "Fruit units"
}
}
},
Tooltip = new Tooltip
{
Shared = true,
ValueSuffix = " units"
},
PlotOptions = new PlotOptions
{
Areaspline = new PlotOptionsAreaspline
{
FillOpacity = 0.5
}
},
Credits = new Credits
{
Enabled = false
},
Series = new List<Series>
{
new AreasplineSeries
{
Name = "John",
Data = @ViewData["johnData"] as List<AreasplineSeriesData>
},
new AreasplineSeries
{
Name = "Jane",
Data = @ViewData["janeData"] as List<AreasplineSeriesData>
}
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())