1

My Target is to implement Pi Graph in DataGridView Cell, which will show renaming time of pending Orders Point of Sale System.

When we place an Order on POS System it can be Delivery or Collection Order. Delivery and Collection Orders contain time to finish. develop this scenario in which a renaming time showed in Pi Graph an its decreases when Time closer to its completion time.

When current time reach to renaming time graph must show 0 on the graph

enter image description here

  • Please edit your question and show us what you have tried. and describe any issues you are having with your current solution. You might also want to read https://stackoverflow.com/help/how-to-ask – Linda Lawton - DaImTo Jul 04 '20 at 15:10

1 Answers1

2

Step 1: How to Create Bar Graph in Datagridview Cell

First of all we have there Parameter or Values

  1. Order Placing time
  2. Order Completion time(Time required to complete the order e.g 120 min).
  3. Current System Time.

Select a DatagridView form toolbox and also a Chart.

This Datagridview have column:

enter image description here then create a function

 private void GenerateGraphOfRemaingTiming()
{

    try
    {

        DateTime moment = DateTime.Now;
        
        chart1.Visible = false;

        chart1.ClientSize = new Size(37, 37);
        Bitmap bmp = new Bitmap(chart1.ClientSize.Width, chart1.ClientSize.Height);
        for (int row = 0; row < dataGridView_RecentOrder.RowCount; row++)
        {
            chart1.Series.Clear();
            chart1.Legends.Clear();
            int Hour = int.Parse(moment.Hour.ToString());
            int Min = int.Parse(moment.Minute.ToString());
            int Sec = int.Parse(moment.Second.ToString());
         
            //Add a new chart-series
            string seriesname = "MySeriesName";
            chart1.Series.Add(seriesname);
            //set the chart-type to "Pie"
            chart1.Series[seriesname].ChartType = SeriesChartType.Pie;

            //Add some datapoints so the series. in this case you can pass the values to this method

            chart1.Series[seriesname].LabelForeColor = Color.White;
            chart1.Series[seriesname].BackSecondaryColor = Color.FromArgb(192, 192, 255);
            
            string OrderDateTime = dataGridView_RecentOrder.Rows[row].Cells["Order_PlacingTime"].Value.ToString();
           
            var result = Convert.ToDateTime(OrderDateTime);

            int OHour = int.Parse(result.ToString("HH", System.Globalization.CultureInfo.CurrentCulture));
            int OMin = int.Parse(result.ToString("mm", System.Globalization.CultureInfo.CurrentCulture));
            int OnSec = int.Parse(result.ToString("ss", System.Globalization.CultureInfo.CurrentCulture));

            int OrderMinuts =Convert.ToInt32( (OHour * 60) + OMin + OnSec * 0.0166667);
            int NowTimeInMinuts = Convert.ToInt32( (Hour * 60) + Min + (Sec * 0.0166667));
            int FinalOrderMinutes = int.Parse(dataGridView_RecentOrder.Rows[row].Cells["Order_CompletionTime"].Value.ToString()) - (NowTimeInMinuts - OrderMinuts);
            if (FinalOrderMinutes <= 0)
            {
                FinalOrderMinutes = 0;
            }
            int OrderCompletionTime = int.Parse(dataGridView_RecentOrder.Rows[row].Cells["Order_CompletionTime"].Value.ToString());
            if (OrderCompletionTime == 0)
            {

                OrderCompletionTime = 1;
            }

            int OrderTimingDifference = OrderCompletionTime - FinalOrderMinutes;
            //  MessageBox.Show("Order Min: "+ OrderMinuts.ToString() +"\n Now Time in Min: "+NowTimeInMinuts.ToString());
            chart1.Series[seriesname].Points.AddXY("", OrderTimingDifference);
            chart1.Series[seriesname].Points.AddXY(FinalOrderMinutes.ToString(), FinalOrderMinutes);
         
            chart1.DrawToBitmap(bmp, chart1.ClientRectangle);

           // bmp = OvalImage(bmp);

            dataGridView_RecentOrder.Rows[row].Cells["Order_RemaningTime"].Value = bmp.Clone();
          
        }
    }
    catch (Exception ex)
    {

        MessageBox.Show(ex.ToString());
    }

}

Out put of this will be like.

enter image description here

enter image description here

Step 2: How to automatically graph value decrease when system time increase.

    public void StartOrderTimingCounter()
    {

        System.Windows.Forms.Timer OrderRemaningTimer = new System.Windows.Forms.Timer();
        OrderRemaningTimer.Interval = 60000; // specify interval time as you want
        
        OrderRemaningTimer.Tick += new EventHandler(timer_Tick);
        OrderRemaningTimer.Start();
        


    }
  
    void timer_Tick(object sender, EventArgs e)
    {
// call above implemented function
        GenerateGraphOfRemaingTiming();
      
    }
  • 1
    Or you could simply use a FillEllipe, a FillPie and a DrawString call in the CellPainting event. 3 lines of code, plus maybe just one for calculating the sweeping angles.. – TaW Jul 04 '20 at 08:31
  • 1
    @TaW i did not use CellPainting event, can you provide relevent content, Thanks – Engr. Khuram Shahzad Jul 04 '20 at 08:49
  • 1
    `private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == 0) { int val = (int)dataGridView1[0, 1].Value; e.PaintBackground(e.CellBounds, true); paintPie(e.Graphics, e.RowIndex, e.CellBounds, val); e.Handled = true; } }` – TaW Jul 04 '20 at 08:58
  • 1
    `void paintPie(Graphics g, int row, Rectangle rBounds, int val) { int rad = Math.Min(rBounds.Width, rBounds.Height) - 2; Rectangle r = new Rectangle(rBounds.X, rBounds.Y, rad, rad); g.FillEllipse(Brushes.LightBlue, r); g.FillPie(Brushes.Orange, r, 0, val); g.DrawString(val + "", Font, Brushes.Black, r); }` – TaW Jul 04 '20 at 08:59
  • 1
    This is only a very simple example of how to draw a pie chart. It assumes the sweepping angle is in a adjacent cell and uses the default Font. You will want to calculate the angle, maybe reversing the direction etc and display a suitable string.. – TaW Jul 04 '20 at 09:00