|
|
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 AreaInverted()
{
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<AreaSeriesData> johnData = new List<AreaSeriesData>();
List<AreaSeriesData> janeData = new List<AreaSeriesData>();
johnValues.ForEach(p => johnData.Add(new AreaSeriesData { Y = p }));
janeValues.ForEach(p => janeData.Add(new AreaSeriesData { 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;
<script type="text/javascript">
function formatYAxis() {
return this.value;
}
</script>
@{ var chartOptions = new Highcharts
{
Chart = new Highsoft.Web.Mvc.Charts.Chart
{
Inverted = true
},
Title = new Title
{
Text = "Average fruit consumption during one week"
},
Legend = new Legend
{
Layout = LegendLayout.Vertical,
Align = LegendAlign.Right,
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" }
}
},
YAxis = new List<YAxis>
{
new YAxis
{
Title = new YAxisTitle
{
Text = "Number of Units"
},
Labels = new YAxisLabels
{
Formatter = "formatYAxis"
},
Min = 0
}
},
PlotOptions = new PlotOptions
{
Area = new PlotOptionsArea
{
FillOpacity = 0.5
}
},
Series = new List<Series>
{
new AreaSeries
{
Name = "John",
Data = @ViewData["johnData"] as List<AreaSeriesData>
},
new AreaSeries
{
Name = "Jane",
Data = @ViewData["janeData"] as List<AreaSeriesData>
}
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())