我需要用查询结果 (LINQ To SQL) 的数据填充饼图
问题是我无法在这段代码中添加一个 foreach 来插入我的数据,而不是静态的 Firefox、Chrome、IE 等...
protected void Page_Load(object sender, EventArgs e)
{
//RepeaterVersionsForPie.DataSource = DAaccess.LibDataVersion.LibDataVersion.GetNumberOfCompaniesUsingEachVersions();
//RepeaterVersionsForPie.DataBind();
var test = DAaccess.LibDataVersion.LibDataVersion.GetNumberOfCompaniesUsingEachVersions();
Highcharts chart = new Highcharts("chart")
.InitChart(new Chart { PlotShadow = false })
.SetTitle(new Title { Text = "Browser market shares at a specific website, 2010" })
.SetTooltip(new Tooltip { Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; }" })
.SetPlotOptions(new PlotOptions
{
Pie = new PlotOptionsPie
{
AllowPointSelect = true,
Cursor = Cursors.Pointer,
DataLabels = new PlotOptionsPieDataLabels
{
Color = ColorTranslator.FromHtml("#000000"),
ConnectorColor = ColorTranslator.FromHtml("#000000"),
Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; }"
},
Point = new PlotOptionsPiePoint
{
Events = new PlotOptionsPiePointEvents
{
Click = "function() { alert (this.category +': '+ this.y); }"
}
}
}
})
.SetSeries(new Series
{
Type = ChartTypes.Pie,
Name = "Browser share",
Data = new Data(new object[]
{
new object[] { "Firefox", 45.0 },
new object[] { "IE", 26.8 },
new DotNet.Highcharts.Options.Point
{
Name = "Chrome",
Y = 12.8,
Sliced = true,
Selected = true
},
new object[] { "Safari", 8.5 },
new object[] { "Opera", 6.2 },
new object[] { "Others", 0.7 }
})
});
ltrChart.Text = chart.ToHtmlString();
}
实际上,我需要能够插入这样的内容:
foreach ( var item in test )
{
new object[] { item.name, item.count}
}
但是 VS 不允许我这样做 预先感谢您的帮助...
最佳答案
您可以为 DAaccess.LibDataVersion.LibDataVersion.GetNumberOfCompaniesUsingEachVersions()
返回的任何类型创建扩展方法,并让它以饼形系列格式返回结果。
public static class DotNetHighChartsExtensions
{
public static object[] ToPieChartSeries(this WhateverThatTypeIs data)
{
var returnObject = new List<object>();
foreach ( var item in data )
{
returnObject.Add(new object[] { item.name, item.count});
}
return returnObject.ToArray();
}
}
然后在您拥有静态代码的代码中,您只需将其替换为:
Data = new Data(test.ToPieChartSeries())
或者,您可以让 ToPieChartSeries
方法返回 Series
对象正在寻找的 Data
对象。
虽然我没有使用过这个 DotNet.HighCharts 项目,但我已经使用并为几个 MVC 项目构建了我自己的 HighCharts 对象。从表面上看,这似乎最终在做我所做的同样的事情:创建一个可以序列化为 JSON 并被 HighCharts javascript 库识别的 .NET 对象。
关于c# - DotNet HighCharts ,用查询结果填充饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9226560/