使用DotNet.Highcharts可以根據需要在服務器端創建圖表,而無需使用JavaScript或JSON。這裏是你想要做的圖書館的例子:
Highcharts chart = new Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetTitle(new Title { Text = "Total fruit consumtion, grouped by gender" })
.SetXAxis(new XAxis { Categories = new[] { "Apples", "Oranges", "Pears", "Grapes", "Bananas" } })
.SetYAxis(new YAxis
{
AllowDecimals = false,
Min = 0,
Title = new YAxisTitle { Text = "Number of fruits" }
})
.SetTooltip(new Tooltip { Formatter = "TooltipFormatter" })
.SetPlotOptions(new PlotOptions { Column = new PlotOptionsColumn { Stacking = Stackings.Normal } })
.SetSeries(new[]
{
new Series
{
Name = "John",
Data = new Data(new object[] { 5, 3, 4, 7, 2 }),
Stack = "male"
},
new Series
{
Name = "Joe",
Data = new Data(new object[] { 3, 4, 4, 2, 5 }),
Stack = "male"
},
new Series
{
Name = "Jane",
Data = new Data(new object[] { 2, 5, 6, 2, 1 }),
Stack = "female"
},
new Series
{
Name = "Janet",
Data = new Data(new object[] { 3, 0, 4, 4, 3 }),
Stack = "female"
}
});
你可以找到很多的ASP.NET MVC的例子在這裏:http://dotnethighcharts.codeplex.com/releases/view/80650
我在想......是有可能使用ViewBag來建立JavaScripts的部分?這是一個非常糟糕的主意嗎? THX – Larry 2012-01-16 21:09:44
我想你可以簡單地創建一個路由/頁面來創建一個對象(包含與圖表,標題,subtile相同的屬性),並使用System.Web.Script.Serialization.JavaScriptSerializer來序列化和輸出圖表。在消費者頁面上,您可以使用jquery對REST服務進行ajax調用,然後使用返回的數據初始化圖表。我現在正在做同樣的事情(不過在PHP中)。 – gremo 2012-01-18 23:29:53
謝謝@格雷莫!如果可能的話,可否給我一些這個解決方案的例子?再次感謝! – Larry 2012-01-23 12:42:48