[英]ASP.NET MVC Google pie chart controller
I am looking a friends code to implement google charts into my mvc project. 我正在寻找一个朋友代码来将谷歌图表实施到我的mvc项目中。
I have a model that I am pulling data from: 我有一个要从中提取数据的模型:
public partial class HoursPerSite
{
public string SiteName { get; set; }
public Nullable<decimal> SiteHours { get; set; }
}
The chart has rendered and it looks good but the legend is showing SiteHours and not SiteName as I'd like it to. 该图表已呈现,看起来不错,但图例显示的是SiteHours,而不是我想要的SiteName。
Here is the controller part: 这是控制器部分:
// HOLIDAY PIE START
ViewBag.msg = db.HoursPerSites.Count().ToString();
var query = from r in db.HoursPerSites
select new { Count = r.SiteHours, Value = r.SiteHours };
var result2 = query.ToList();
var datachart2 = new object[result2.Count];
int l = 0;
foreach (var i in result2)
{
datachart2[l] = new object[] { i.Value.ToString(), i.Count };
l++;
}
string datastr2 = JsonConvert.SerializeObject(datachart2, Formatting.None);
ViewBag.dataj2 = new HtmlString(datastr2);
// HOLIDAY PIE END
I'd like it so the pie chart legend/key shows the site not the hours. 我想要它,所以饼图图例/键显示的是网站而不是小时。
Not sure but is this: 不确定,但这是:
select new { Count = r.SiteHours, Value = r.SiteHours };
Not supposed to be this: ( SiteName ): 不应该是这样的:( SiteName ):
select new { Count = r.SiteHours, Value = r.SiteName };
Also, your naming is very bad if I may say. 另外,我可能会说你的名字很不好。 That will not make your future work easier.
那不会使您的未来工作变得更轻松。 Try naming your variables and obejects more specifically.
尝试命名您的变量,并更具体地针对对象。
EDIT: 编辑:
I would change your current code to this: 我将您当前的代码更改为此:
Please note that I don't have any editor and that there could be syntax errors 请注意,我没有任何编辑器,可能存在语法错误
#region Holiday Pie Chart
ViewBag.msg = db.HoursPerSites.Count().ToString();
var queryHoursPerSites = from r in db.HoursPerSites
select new { Count = r.SiteHours, Value = r.SiteHours };
var resultsQueryHoursPerSites = queryHoursPerSites.ToList(); // or HoursPerSitesCollection
var holidayPieChart = new object[resultsQueryHoursPerSites.Count];
int counter = 0; //
foreach (var record in resultsQueryHoursPerSites)
{
holidayPieChart[counter] = new object[] { record.Value.ToString(), record.Count };
counter++;
}
string deserialisedResults = JsonConvert.SerializeObject(holidayPieChart, Formatting.None);
// no idea what dataj2 is here ...
ViewBag.dataj2 = new HtmlString(deserialisedResults);
#endregion
I am sure there is even more to "improve" or "change" but that would in my opinion already be an improvement. 我相信还有更多的“改进”或“改变”,但我认为这已经是一种改进。
I am sure others can elaborate even better on what I am suggesting here :) 我相信其他人可以对我在这里提出的建议做得更好:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.