|
|
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 ColumnPlacement()
{
List<double> employeeValues = new List<double> { 150, 73, 20 };
List<double> employeeOptimizedValues = new List<double> { 140, 90, 40 };
List<double> profitValues = new List<double> { 183.6, 178.8, 198.5 };
List<double> profitOptimizedValues = new List<double> { 203.6, 198.8, 208.5 };
List<ColumnSeriesData> employeeData = new List<ColumnSeriesData>();
List<ColumnSeriesData> employeeOptimizedData = new List<ColumnSeriesData>();
List<ColumnSeriesData> profitData = new List<ColumnSeriesData>();
List<ColumnSeriesData> profitOptimizedData = new List<ColumnSeriesData>();
employeeValues.ForEach(p => employeeData.Add(new ColumnSeriesData { Y = p }));
employeeOptimizedValues.ForEach(p => employeeOptimizedData.Add(new ColumnSeriesData { Y = p }));
profitValues.ForEach(p => profitData.Add(new ColumnSeriesData { Y = p }));
profitOptimizedValues.ForEach(p => profitOptimizedData.Add(new ColumnSeriesData { Y = p }));
ViewData["employeeData"] = employeeData;
ViewData["employeeOptimizedData"] = employeeOptimizedData;
ViewData["profitData"] = profitData;
ViewData["profitOptimizedData"] = profitOptimizedData;
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 = "Efficiency Optimization by Branch"
},
XAxis = new List<XAxis>
{
new XAxis
{
Categories = new List<string> {
"Seattle HQ",
"San Francisco",
"Tokyo"
}
}
},
YAxis = new List<YAxis>
{
new YAxis
{
Min = 0,
Title = new YAxisTitle
{
Text = "Employees"
}
},
new YAxis
{
Title = new YAxisTitle
{
Text = "Profit (millions)"
},
Opposite = true,
Id = "Profit"
}
},
Legend = new Legend
{
Shadow = new Shadow
{
Enabled = false
}
},
Tooltip = new Tooltip
{
Shared = true
},
PlotOptions = new PlotOptions
{
Column = new PlotOptionsColumn
{
Grouping = false,
//Shadow = new Shadow
//{
// Enabled = false
//},
BorderWidth = 0
}
},
Series = new List<Series>
{
new ColumnSeries
{
Name = "Employees",
Color = "rgba(165,170,217,1)",
PointPadding = 0.3,
PointPlacement = new PointPlacement
{
Value = -0.2
},
Data = @ViewData["employeeData"] as List<ColumnSeriesData>
},
new ColumnSeries
{
Name = "Employees Optimized",
Color = "rgba(126,86,134,.9)",
PointPadding = 0.4,
PointPlacement = new PointPlacement
{
Value = -0.2
},
Data = @ViewData["employeeOptimizedData"] as List<ColumnSeriesData>
},
new ColumnSeries
{
Name = "Profit",
Color = "rgba(248,161,63,1)",
PointPadding = 0.3,
YAxis = "Profit",
PointPlacement = new PointPlacement
{
Value = 0.2
},
Data = @ViewData["profitData"] as List<ColumnSeriesData>
},
new ColumnSeries
{
Name = "Profit Optimized",
Color = "rgba(186,60,61,.9)",
PointPadding = 0.4,
YAxis = "Profit",
PointPlacement = new PointPlacement
{
Value = 0.2
},
Data = @ViewData["profitOptimizedData"] as List<ColumnSeriesData>
}
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())