【问题标题】:Visual Studio Pie Chart - Generate PercentagesVisual Studio 饼图 - 生成百分比
【发布时间】:2015-03-18 04:03:32
【问题描述】:

我通过从 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 中的选项。

【问题讨论】:

  • 您的问题解决了吗?如果其中一个答案对您有所帮助,请考虑将其标记为已接受的答案!

标签: c# charts percentage visual-studio-express


【解决方案1】:

这样准备:

   Series S = Chart6.Series["Series1"];
   S.ChartType = SeriesChartType.Pie;
   S.IsValueShownAsLabel = true;
   S["PieLabelStyle"] = "Outside";

如果您知道总数,请像这样创建数据点:

   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);      

【讨论】:

  • 非常感谢!什么是查找此类信息的好资源? MSDN 一点帮助都没有。
  • 好问题!此外,当我钻入 MSDN 时,我经常会成功,并且越来越努力。我希望我知道这一切都聚集在一起的一个地方..! (查看我对总百分比的正确处理的更新!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多