1

我正在尝试使用 VS 2012 创建一个 2d 分组圆环图 C#.net。我能够做到,但输出与所需的输出略有偏差。

这是表格数据,我用于创建图表。

表 - 堆

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

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

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

堆 - 图表

我想要的输出是获得 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.

我该如何做到这一点?我需要用数学方法计算任何东西来实现这一点吗?

任何帮助表示赞赏

4

1 回答 1

1

自己找到了答案。

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

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

希望这篇文章对其他人有所帮助。

于 2013-08-12T06:09:08.487 回答