|
|
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 PieLegend()
{
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;
@{ 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", false } },
ShowInLegend = true
}
},
XAxis = new List<XAxis>
{
new XAxis
{
Title = new XAxisTitle
{
Text = null
},
Categories = new List<string> { "Africa", "America", "Asia", "Europe", "Oceania" }
}
},
YAxis = new List<YAxis>
{
new YAxis
{
Min = 0,
Title = new YAxisTitle
{
Text = "Population (millions)",
Align = YAxisTitleAlign.Center
}
}
},
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())