|
|
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 PieMonochrome()
{
List<PieSeriesData> pieData = new List<PieSeriesData>();
pieData.Add(new PieSeriesData { Name = "FireFox", Y = 45.0 });
pieData.Add(new PieSeriesData { Name = "IE", Y = 26.8 });
pieData.Add(new PieSeriesData { Name = "Chrome", Y = 12.8, Sliced = true, Selected = true });
pieData.Add(new PieSeriesData { Name = "Safari", Y = 8.5 });
pieData.Add(new PieSeriesData { Name = "Opera", Y = 6.2 });
pieData.Add(new PieSeriesData { Name = "Others", Y = 0.7 });
ViewData["pieData"] = pieData;
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
@using System.Collections
<script type="text/javascript">
// Make monochrome colors and set them as default for all pies
Highcharts.getOptions().plotOptions.pie.colors = (function () {
var colors = [],
base = Highcharts.getOptions().colors[0],
i;
for (i = 0; i < 10; i += 1) {
// Start out with a darkened base color (negative brighten), and end
// up with a much brighter color
colors.push(Highcharts.Color(base).brighten((i - 3) / 7).get());
}
return colors;
}());
</script>
@{ var chartOptions =
new Highcharts
{
Chart = new Highsoft.Web.Mvc.Charts.Chart
{
PlotBackgroundColor = null,
PlotBorderWidth = null,
PlotShadow = new Shadow
{
Enabled = false
}
},
Title = new Title
{
Text = "'Browser market shares at a specific website, 2014"
},
Tooltip = new Tooltip
{
PointFormat = "{series.name}: <b>{point.percentage:.1f}%</b>"
},
PlotOptions = new PlotOptions
{
Pie = new PlotOptionsPie
{
AllowPointSelect = true,
Cursor = PlotOptionsPieCursor.Pointer,
DataLabels = new Hashtable { { "enabled", true }, { "format", "<b>{point.name}</b>: {point.percentage:.1f} %" } }
}
},
Series = new List<Series>
{
new PieSeries
{
Name = "Browser Share",
Data = @ViewData["pieData"] as List<PieSeriesData>
},
}
};
chartOptions.ID = "chart";
var renderer = new HighchartsRenderer(chartOptions);
}
@Html.Raw(renderer.RenderHtml())