|
|
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 ColumnNegative()
{
List<double?> johnValues = new List<double?> { 5, 3, 4, 7, 2 };
List<double?> janeValues = new List<double?> { 2, -2, -3, 2, 1 };
List<double?> joeValues = new List<double?> { 3, 4, 4, -2, 5 };
List<ColumnSeriesData> johnData = new List<ColumnSeriesData>();
List<ColumnSeriesData> janeData = new List<ColumnSeriesData>();
List<ColumnSeriesData> joeData = new List<ColumnSeriesData>();
johnValues.ForEach(p => johnData.Add(new ColumnSeriesData { Y = p }));
janeValues.ForEach(p => janeData.Add(new ColumnSeriesData { Y = p }));
joeValues.ForEach(p => joeData.Add(new ColumnSeriesData { Y = p }));
ViewData["johnData"] = johnData;
ViewData["janeData"] = janeData;
ViewData["joeData"] = joeData;
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 = "Column chart with negative values"
},
XAxis = new List<XAxis>
{
new XAxis
{
Categories = new List<string> { "Apples", "Oranges", "Pears", "Grapes", "Bananas" }
}
},
Credits = new Credits
{
Enabled = false
},
Series = new List<Series>
{
new ColumnSeries
{
Name = "John",
Data = @ViewData["johnData"] as List<ColumnSeriesData>
},
new ColumnSeries
{
Name = "Jane",
Data = @ViewData["janeData"] as List<ColumnSeriesData>
},
new ColumnSeries
{
Name = "Joe",
Data = @ViewData["joeData"] as List<ColumnSeriesData>
}
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())