Creating 2D Grouped Donut chart in C#
I am trying to create a 2d Grouped Doughnut chart C#.net using VS 2012. I
was able to do it but the output is slightly deviated from the desired
output.
Here is the table data, I am using for creating chart.
Following is the code I have used to create chart:
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);
    }
Following is the Heap Class I have written.
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;
        }
Following is the output which I have received after executing the code:
The output which I desire is to get the coinciding edges for, 1 outer
Series data point and 3 Inner Series datapoints.
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.
How do I achieve that? Do I need to calculate anything mathematically to
achieve this?
Any help is appreciated
 
No comments:
Post a Comment