我通过从SQL表中提取数据来生成饼图。数据是不同项目的小时累计。
图表构建正常,但我希望饼图显示生成图表时正在使用的饼图的每个切片的相应百分比。
我正在使用下面显示的方法创建图表。
// Fill Chart with usable data
for (int i = 0; i <= index - 1; i++)
{
Chart6.Series["Series1"].Points.AddXY(project[i], projTime[i]);
}
Chart6.Series[0]["PieLabelStyle"] = "Disabled";
我希望有一个简单的命令,我可以在后面的代码中传递来创建它,但是在网上搜索没有提供可用的结果。我发现的最好的是this method,但这些不是Visual Studio Express 2013中的选项。
答案 0 :(得分:1)
准备这样:
Series S = Chart6.Series["Series1"];
S.ChartType = SeriesChartType.Pie;
S.IsValueShownAsLabel = true;
S["PieLabelStyle"] = "Outside";
如果您知道总数:
,请创建这样的DataPoints DataPoint p = new DataPoint(project[i], projTime[i]);
// check your data type for the calculation!
p.Label = p.YValues[0] + "h =\n" +
(100d * p.YValues[0] / total).ToString("00.00") + "%\n"; // my format
如果您不知道总数,首先设置点数,然后计算总数并最终设置标签:
for (int i = 0; i <= index - 1; i++)
{
S.Points.AddXY(project[i], projTime[i]);
}
// calculate the total:
double total = S.Points.Sum(dp => dp.YValues[0]);
// now we can set the percentages
foreach (DataPoint p in S.Points)
{
p.Label = p.YValues[0] + "h =\n" +
(100d * p.YValues[0] / total).ToString("00.00") + "%\n"; // my format
}
chart1.Titles.Add(S.Points.Count + " projects, total " +
total.ToString("###,##0") + " hours");
chart1.Titles[0].Font = new System.Drawing.Font("Arial", 14f);