CSharp - 在 C# 中,创建 2D 个分组的圆环图

  显示原文与译文双语对照的内容

我正在尝试创建一个 2d 分组圆环图 C#.net 使用 vs 2012. 我能够做到这一点,但是输出稍微偏离了期望的输出。

下面是表格数据,我用来创建图表。

Table - Heap

下面是我用来创建图表的代码:


private void GenerateHeapStatsChart()

 {
 double HeapTotalStock=0,HeapProcessedStock=0;
 HeapStatsChart.ChartAreas.Clear();
 HeapStatsChart.Series.Clear();
 HeapStatsChart.Legends.Clear();

 List<Heap> heap = Heap.getAllHeap();
 for (int i = 0; i <heap.Count; i++)
 {
 HeapTotalStock += Convert.ToDouble(heap[i].Total_Stock);
 HeapProcessedStock += Convert.ToDouble(heap[i].Processed_Stock);
 }

 HeapStatsChart.Size = new Size(500, 500);

 float baseDoughnutWidth = 25;
//values 0 to 99

//Create the outer area
 float outerSize = 100;
//percent
 ChartArea outerArea = new ChartArea("OUTER_AREA");
 outerArea.Position = new ElementPosition(0, 0, 100, 100);
//Set the plot position to the middle of the area depending on the size
 outerArea.InnerPlotPosition = new ElementPosition((100 - outerSize)/2, (100 - outerSize)/2 + 10, outerSize, outerSize - 10);
 outerArea.BackColor = Color.Linen;
//Add the area to the chart
 HeapStatsChart.ChartAreas.Add(outerArea);

//Create the inner area
 float innerSize = 70;
//percent
 ChartArea innerArea = new ChartArea("INNER_AREA");
 innerArea.Position = new ElementPosition(0, 0, 100, 100);
 innerArea.InnerPlotPosition = new ElementPosition((100 - innerSize)/2, (100 - innerSize)/2 + 10, innerSize, innerSize - 10);
 innerArea.BackColor = Color.Transparent;
 HeapStatsChart.ChartAreas.Add(innerArea);

//Create the outer series
 Series outerSeries = new Series("OUTER_SERIES");
 outerSeries.ChartArea ="OUTER_AREA";
 outerSeries.ChartType = SeriesChartType.Doughnut;
//Since the areas are different size, the width of each dougnut have to be set in proportion to the 
//size of the area in order to get the doughnut widths the same.
 outerSeries["DoughnutRadius"] = Math.Min(baseDoughnutWidth * (100/outerSize), 99).ToString().Replace(",",".");
 outerSeries.Palette = ChartColorPalette.Pastel;
//Add points to the series
 DataPoint[] dp = new DataPoint[heap.Count];
 for(int i=0;i<heap.Count;i++)
 {
 dp[i] = new DataPoint(0D, (Convert.ToDouble(heap[i].Total_Stock)*100)/HeapTotalStock);
 dp[i].Label = heap[i].Total_Stock;
 dp[i].LegendText = heap[i].Heap_Name;
 dp[i].LegendToolTip = heap[i].Heap_Name;
 outerSeries.Points.Add(dp[i]);
 }

//Legend legend1 = new Legend("OuterLegend");
//HeapStatsChart.Legends.Add(legend1);
//outerSeries.Legend ="OuterLegend";

//Add the series to the chart
 HeapStatsChart.Series.Add(outerSeries);

//Create the inner series
 Series innerSeries = new Series("INNER_SERIES");
 innerSeries.ChartArea ="INNER_AREA";
 innerSeries.ChartType = SeriesChartType.Doughnut;
 innerSeries["DoughnutRadius"] = Math.Min(baseDoughnutWidth * (100/innerSize), 99).ToString().Replace(",",".");
 innerSeries.Palette = ChartColorPalette.BrightPastel;

 DataPoint[][] dp1 = new DataPoint[heap.Count][];

 for (int i = 0; i <heap.Count; i++)
 {
 dp1[i] = new DataPoint[3];
 dp1[i][0] = new DataPoint(0D, (Convert.ToDouble(heap[i].Processed_Stock) * 100)/Convert.ToDouble(heap[i].Total_Stock));
 dp1[i][0].Label = heap[i].Processed_Stock;
 innerSeries.Points.Add(dp1[i][0]);
 dp1[i][2] = new DataPoint(0D, (Convert.ToDouble(heap[i].Sold_Stock) * 100)/Convert.ToDouble(heap[i].Total_Stock));
 dp1[i][3].Label = heap[i].Sold_Stock;
 innerSeries.Points.Add(dp1[i][4]);
 dp1[i][2] = new DataPoint(0D, ((Convert.ToDouble(heap[i].Total_Stock) - Convert.ToDouble(heap[i].Processed_Stock) - Convert.ToDouble(heap[i].Sold_Stock)) * 100)/Convert.ToDouble(heap[i].Total_Stock));
 dp1[i][2].Label = (Convert.ToDouble(heap[i].Total_Stock) - Convert.ToDouble(heap[i].Processed_Stock) - Convert.ToDouble(heap[i].Sold_Stock)).ToString();
 innerSeries.Points.Add(dp1[i][2]);
 }
 HeapStatsChart.Series.Add(innerSeries);



 HeapStatsChart.Titles.Add(new Title("Heap Statisitcs",Docking.Top,new Font(new FontFamily("Times New Roman"),14f,FontStyle.Bold),Color.Blue));
 ChartTablePane.Controls.Add(HeapStatsChart, 0, 0);

 }

下面是我写的堆类。


public class Heap
 {
 public string Row_Id, Heap_No, Heap_Name, Total_Stock, Processed_Stock, Sold_Lint, Sold_Seed, Processed_Lint, Processed_Seed, Waste, Sold_Stock;

 public Heap() { }

 public Heap(string Row_Id="", string Heap_No="", string Heap_Name="", string Total_Stock="", string Processed_Stock="", string Sold_Lint ="", string Sold_Seed ="",
 string Processed_Lint ="", string Processed_Seed ="", string Waste ="", string Sold_Stock ="")
 {
 this.Row_Id = Row_Id;
 this.Heap_No = Heap_No;
 this.Heap_Name = Heap_Name;
 this.Total_Stock = Total_Stock;
 this.Processed_Stock = Processed_Stock;
 this.Sold_Lint = Sold_Lint;
 this.Sold_Seed = Sold_Seed;
 this.Processed_Lint = Processed_Lint;
 this.Processed_Seed = Processed_Seed;
 this.Waste = Waste;
 this.Sold_Stock = Sold_Stock;
 }

public static List<Heap> getAllHeap()
 {
 List<Heap> heap = new List<Heap>();

 MySqlDataReader reader;
 string connString = ConfigurationManager.AppSettings["GMS.Properties.Settings.GMSConnectionString"];
 MySqlConnection conn = new MySqlConnection(connString);
 string query ="SELECT * from `gms`.`heap`;";
 MySqlCommand cmd = new MySqlCommand(query, conn);
 conn.Open();
 reader = cmd.ExecuteReader();
 while (reader.Read())
 {
 heap.Add(new Heap(reader[0].ToString(),//Row_Id
 reader[1].ToString(),//Heap No
 reader[2].ToString(),//Heap Name
 reader[3].ToString(),//Total Stock
 reader[4].ToString(),//Processed Stock
 reader[5].ToString(),//Sold Lint
 reader[6].ToString(),//Sold Seed
 reader[7].ToString(),//Processed Lint
 reader[8].ToString(),//Processed Seed
 reader[9].ToString(),//Waste
 reader[10].ToString()));//Sold Stock
 }
 reader.Close();
 conn.Close();
 return heap;
 }

下面是执行代码后收到的输出:

Heap - Chart

我想要的输出是为了获得相应的边缘,1个外部数据点和 3个内部系列数据。


For Example:

50 = 36.08+4+9.92
Edge of 50 should start with edge of 36.08 and should end with the edge of 9.92.

我该如何实现? 我需要计算任何数学来达到这个目的?

感谢你的帮助

时间: 原作者:

我自己找到答案了。

只是计算公式中的一个小误差。


for (int i = 0; i <heap.Count; i++)
{
 dp1[i] = new DataPoint[3];
 dp1[i][0] = new DataPoint(0D, (Convert.ToDouble(heap[i].Processed_Stock) * 360)/HeapTotalStock);//Convert.ToDouble(heap[i].Total_Stock));
 dp1[i][0].Label = heap[i].Processed_Stock;
 innerSeries.Points.Add(dp1[i][0]);
 dp1[i][1] = new DataPoint(0D, (Convert.ToDouble(heap[i].Sold_Stock) * 360)/HeapTotalStock);//Convert.ToDouble(heap[i].Total_Stock));
 dp1[i][1].Label = heap[i].Sold_Stock;
 innerSeries.Points.Add(dp1[i][1]);
 dp1[i][2] = new DataPoint(0D, ((Convert.ToDouble(heap[i].Total_Stock) - Convert.ToDouble(heap[i].Processed_Stock) - Convert.ToDouble(heap[i].Sold_Stock)) * 360)/HeapTotalStock);//Convert.ToDouble(heap[i].Total_Stock));
 dp1[i][2].Label = (Convert.ToDouble(heap[i].Total_Stock) - Convert.ToDouble(heap[i].Processed_Stock) - Convert.ToDouble(heap[i].Sold_Stock)).ToString();
 innerSeries.Points.Add(dp1[i][2]);
}

希望这篇文章对别人有帮助。

原作者:

我正在尝试实现一个像你的圆环图。 我试过你的密码。 但在heapstackchart术语上给出了错误。 我试图解决它,但找不到或者知道你使用哪个库。 我正在 Visual Studio 2013试用。 它可以在wp8和桌面应用上运行。 谢谢

原作者:
...