Results 1 to 10 of 10

Thread: [RESOLVED] How to insert a Pie Chart in a DataGridView cell?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Resolved [RESOLVED] How to insert a Pie Chart in a DataGridView cell?

    Hi guys,
    Is there a way I can put a Pie Chart in a DataGridView cell?

    Thanks

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: How to insert a Pie Chart in a DataGridView cell?

    Though I cant comment on the performance, the chart control has a SaveImage method that you could potentially use in an iteration. I would think it the sample was small it should process in a reasonable amount of time

  3. #3
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: How to insert a Pie Chart in a DataGridView cell?

    Is this what you want?

    It can be fancied up as desired.

    There is a form with a dgv and a chart on it. The chart is not visible.

    Name:  c2b.png
Views: 2861
Size:  18.9 KB

    Code:
    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Sample A"
            DataGridView1.Columns(1).Name = "Sample B"
            DataGridView1.Columns(2).Name = "Sample C"
            DataGridView1.RowTemplate.Height = 100
            DataGridView1.Columns(0).Width = 50
            DataGridView1.Columns(1).Width = 50
            DataGridView1.Columns(2).Width = 50
            DataGridView1.Rows.Add(New String() {"2", "1", "4"})
            DataGridView1.Rows.Add(New String() {"5", "3", "2"})
    
            Dim img As New DataGridViewImageColumn()
            img.Name = "Image"
            img.Width = 160
            DataGridView1.Columns.Add(img)
    
            Chart1.Visible = False
            Chart1.ClientSize = New Size(200, 100)
            Dim bmp As New Bitmap(Chart1.ClientSize.Width, Chart1.ClientSize.Height)
    
            For row As Integer = 0 To DataGridView1.RowCount - 2
                DrawChart(row)
                Chart1.DrawToBitmap(bmp, Chart1.ClientRectangle)
                DataGridView1.Rows.Item(row).Cells(3).Value = bmp.Clone
            Next
    
        End Sub
    
        Private Sub DrawChart(row As Integer)
    
            With Chart1.ChartAreas(0)
                .AxisX.MajorGrid.LineColor = Color.LightGray
                .AxisX.Title = "X Axis"
                .AxisY.Title = "Y Axis"
                .AxisX.Minimum = 0
                .AxisX.Maximum = 4
                .AxisX.Interval = 1
                .AxisY.Minimum = 0
                .AxisY.Maximum = 6
                .AxisY.Interval = 1
    
            End With
    
            'draw the chart
            Chart1.Series.Clear()
            Chart1.Series.Add("")
    
            With Chart1.Series(0)
                .IsVisibleInLegend = False
                .ChartType = DataVisualization.Charting.SeriesChartType.Column
    
                For col As Integer = 0 To 2
                    .Points.AddXY(DataGridView1.Columns(col).Name, DataGridView1.Rows.Item(row).Cells(col).Value)
                Next
    
            End With
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: How to insert a Pie Chart in a DataGridView cell?

    Quote Originally Posted by tommytwotrain View Post
    Is this what you want?

    It can be fancied up as desired.

    There is a form with a dgv and a chart on it. The chart is not visible.

    Name:  c2b.png
Views: 2861
Size:  18.9 KB

    Code:
    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Sample A"
            DataGridView1.Columns(1).Name = "Sample B"
            DataGridView1.Columns(2).Name = "Sample C"
            DataGridView1.RowTemplate.Height = 100
            DataGridView1.Columns(0).Width = 50
            DataGridView1.Columns(1).Width = 50
            DataGridView1.Columns(2).Width = 50
            DataGridView1.Rows.Add(New String() {"2", "1", "4"})
            DataGridView1.Rows.Add(New String() {"5", "3", "2"})
    
            Dim img As New DataGridViewImageColumn()
            img.Name = "Image"
            img.Width = 160
            DataGridView1.Columns.Add(img)
    
            Chart1.Visible = False
            Chart1.ClientSize = New Size(200, 100)
            Dim bmp As New Bitmap(Chart1.ClientSize.Width, Chart1.ClientSize.Height)
    
            For row As Integer = 0 To DataGridView1.RowCount - 2
                DrawChart(row)
                Chart1.DrawToBitmap(bmp, Chart1.ClientRectangle)
                DataGridView1.Rows.Item(row).Cells(3).Value = bmp.Clone
            Next
    
        End Sub
    
        Private Sub DrawChart(row As Integer)
    
            With Chart1.ChartAreas(0)
                .AxisX.MajorGrid.LineColor = Color.LightGray
                .AxisX.Title = "X Axis"
                .AxisY.Title = "Y Axis"
                .AxisX.Minimum = 0
                .AxisX.Maximum = 4
                .AxisX.Interval = 1
                .AxisY.Minimum = 0
                .AxisY.Maximum = 6
                .AxisY.Interval = 1
    
            End With
    
            'draw the chart
            Chart1.Series.Clear()
            Chart1.Series.Add("")
    
            With Chart1.Series(0)
                .IsVisibleInLegend = False
                .ChartType = DataVisualization.Charting.SeriesChartType.Column
    
                For col As Integer = 0 To 2
                    .Points.AddXY(DataGridView1.Columns(col).Name, DataGridView1.Rows.Item(row).Cells(col).Value)
                Next
    
            End With
        End Sub
    
    End Class
    Yes, this is what I'm looking for (just as a pie, but I think I can manage it...)
    I'll test it and if it works, I'll mark this as resolved...
    Thanks

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: How to insert a Pie Chart in a DataGridView cell?

    Quote Originally Posted by tommytwotrain View Post
    Is this what you want?

    It can be fancied up as desired.

    There is a form with a dgv and a chart on it. The chart is not visible.

    Name:  c2b.png
Views: 2861
Size:  18.9 KB

    Code:
    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Sample A"
            DataGridView1.Columns(1).Name = "Sample B"
            DataGridView1.Columns(2).Name = "Sample C"
            DataGridView1.RowTemplate.Height = 100
            DataGridView1.Columns(0).Width = 50
            DataGridView1.Columns(1).Width = 50
            DataGridView1.Columns(2).Width = 50
            DataGridView1.Rows.Add(New String() {"2", "1", "4"})
            DataGridView1.Rows.Add(New String() {"5", "3", "2"})
    
            Dim img As New DataGridViewImageColumn()
            img.Name = "Image"
            img.Width = 160
            DataGridView1.Columns.Add(img)
    
            Chart1.Visible = False
            Chart1.ClientSize = New Size(200, 100)
            Dim bmp As New Bitmap(Chart1.ClientSize.Width, Chart1.ClientSize.Height)
    
            For row As Integer = 0 To DataGridView1.RowCount - 2
                DrawChart(row)
                Chart1.DrawToBitmap(bmp, Chart1.ClientRectangle)
                DataGridView1.Rows.Item(row).Cells(3).Value = bmp.Clone
            Next
    
        End Sub
    
        Private Sub DrawChart(row As Integer)
    
            With Chart1.ChartAreas(0)
                .AxisX.MajorGrid.LineColor = Color.LightGray
                .AxisX.Title = "X Axis"
                .AxisY.Title = "Y Axis"
                .AxisX.Minimum = 0
                .AxisX.Maximum = 4
                .AxisX.Interval = 1
                .AxisY.Minimum = 0
                .AxisY.Maximum = 6
                .AxisY.Interval = 1
    
            End With
    
            'draw the chart
            Chart1.Series.Clear()
            Chart1.Series.Add("")
    
            With Chart1.Series(0)
                .IsVisibleInLegend = False
                .ChartType = DataVisualization.Charting.SeriesChartType.Column
    
                For col As Integer = 0 To 2
                    .Points.AddXY(DataGridView1.Columns(col).Name, DataGridView1.Rows.Item(row).Cells(col).Value)
                Next
    
            End With
        End Sub
    
    End Class
    This works grate!
    I just need to modify it so it will get the data from a list of strings...
    Code:
    Dim test As String() = {"05/12/19,5,8,2", "05/13/19,4,3,6", "05/14/19,4,4,2", "05/15/19,4,8,2", "05/16/19,1,8,4"}
    Each string is split by "," (so we have 4 (sub)strings in each string, date and 3 numbers)
    I need the Pie chart title to be the date, and the 3 numbers are the data for the chart (each data has a specific color)

    How do I modify your code for this?
    Thanks

  6. #6
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: How to insert a Pie Chart in a DataGridView cell?

    I did more for you but that is all I will do.



    Here are samples you can find what you need.

    The samples are a c# proj you run in Visual Studio. Very easy and fun and has the most info on charts although there are now better chart docs at msdn.

    In the future, show us what you have tried and any specific questions you have and then we will try to help.

    Name:  a1.png
Views: 2085
Size:  42.4 KB

    Code:
    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 4
            DataGridView1.Columns(0).Name = "Date"
            DataGridView1.Columns(1).Name = "A"
            DataGridView1.Columns(2).Name = "B"
            DataGridView1.Columns(3).Name = "C"
            DataGridView1.RowTemplate.Height = 100
            DataGridView1.Columns(0).Width = 80
            DataGridView1.Columns(1).Width = 30
            DataGridView1.Columns(2).Width = 30
            DataGridView1.Columns(3).Width = 30
    
            Dim test As String() = {"05/12/19,5,8,2", "05/13/19,4,3,6", "05/14/19,4,4,2", "05/15/19,4,8,2", "05/16/19,1,8,4"}
            For Each tst In test
                DataGridView1.Rows.Add(tst.Split(","))
            Next
    
            Dim img As New DataGridViewImageColumn()
            img.Name = "Chart"
            img.Width = 100
            DataGridView1.Columns.Add(img)
    
            Chart1.Visible = False
            Chart1.ClientSize = New Size(img.Width, DataGridView1.RowTemplate.Height)
    
            For row As Integer = 0 To DataGridView1.RowCount - 2
                DataGridView1.Rows.Item(row).Cells(4).Value = DrawChart(row)
            Next
    
        End Sub
    
        Private Function DrawChart(row As Integer) As Bitmap
            ' Set chart title
            Chart1.Titles.Clear()
            Chart1.Titles.Add(DataGridView1.Rows.Item(row).Cells(0).Value)
    
            Chart1.Series.Clear()
            Chart1.Series.Add("")
    
            With Chart1.Series(0)
                .IsVisibleInLegend = False
                .ChartType = DataVisualization.Charting.SeriesChartType.Pie
    
                For col As Integer = 1 To 3
                    .Points.AddXY(DataGridView1.Columns(col).Name, DataGridView1.Rows.Item(row).Cells(col).Value)
                Next
    
                .Points(0).Color = Color.FromArgb(100, Color.Red)
                .Points(1).Color = Color.FromArgb(100, Color.Green)
                .Points(2).Color = Color.FromArgb(100, Color.SkyBlue)
            End With
    
            Dim bmp As New Bitmap(Chart1.ClientSize.Width, Chart1.ClientSize.Height)
            Chart1.DrawToBitmap(bmp, Chart1.ClientRectangle)
    
            Return bmp
        End Function
    End Class

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: How to insert a Pie Chart in a DataGridView cell?

    Quote Originally Posted by tommytwotrain View Post
    I did more for you but that is all I will do.



    Here are samples you can find what you need.

    The samples are a c# proj you run in Visual Studio. Very easy and fun and has the most info on charts although there are now better chart docs at msdn.

    In the future, show us what you have tried and any specific questions you have and then we will try to help.

    Name:  a1.png
Views: 2085
Size:  42.4 KB

    Code:
    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 4
            DataGridView1.Columns(0).Name = "Date"
            DataGridView1.Columns(1).Name = "A"
            DataGridView1.Columns(2).Name = "B"
            DataGridView1.Columns(3).Name = "C"
            DataGridView1.RowTemplate.Height = 100
            DataGridView1.Columns(0).Width = 80
            DataGridView1.Columns(1).Width = 30
            DataGridView1.Columns(2).Width = 30
            DataGridView1.Columns(3).Width = 30
    
            Dim test As String() = {"05/12/19,5,8,2", "05/13/19,4,3,6", "05/14/19,4,4,2", "05/15/19,4,8,2", "05/16/19,1,8,4"}
            For Each tst In test
                DataGridView1.Rows.Add(tst.Split(","))
            Next
    
            Dim img As New DataGridViewImageColumn()
            img.Name = "Chart"
            img.Width = 100
            DataGridView1.Columns.Add(img)
    
            Chart1.Visible = False
            Chart1.ClientSize = New Size(img.Width, DataGridView1.RowTemplate.Height)
    
            For row As Integer = 0 To DataGridView1.RowCount - 2
                DataGridView1.Rows.Item(row).Cells(4).Value = DrawChart(row)
            Next
    
        End Sub
    
        Private Function DrawChart(row As Integer) As Bitmap
            ' Set chart title
            Chart1.Titles.Clear()
            Chart1.Titles.Add(DataGridView1.Rows.Item(row).Cells(0).Value)
    
            Chart1.Series.Clear()
            Chart1.Series.Add("")
    
            With Chart1.Series(0)
                .IsVisibleInLegend = False
                .ChartType = DataVisualization.Charting.SeriesChartType.Pie
    
                For col As Integer = 1 To 3
                    .Points.AddXY(DataGridView1.Columns(col).Name, DataGridView1.Rows.Item(row).Cells(col).Value)
                Next
    
                .Points(0).Color = Color.FromArgb(100, Color.Red)
                .Points(1).Color = Color.FromArgb(100, Color.Green)
                .Points(2).Color = Color.FromArgb(100, Color.SkyBlue)
            End With
    
            Dim bmp As New Bitmap(Chart1.ClientSize.Width, Chart1.ClientSize.Height)
            Chart1.DrawToBitmap(bmp, Chart1.ClientRectangle)
    
            Return bmp
        End Function
    End Class
    Thank you for your help!
    It helped a lot!

    here is the code I ended up with - it is not pretty, but it works... (there is a few unnecessary lines for testing...):

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim D As DateTime = DateTime.Today.ToString("MM/dd/yy")
            Dim A As Double = 4
            Dim P As Double = 9
            Dim L As Double = 1
    
            Dim GetPie As Bitmap = MakePieChart(D, A, P, L)
    
            DataGridView1.Rows(0).Cells(0).Value = GetPie
    
            PictureBox1.Image = GetPie
            PictureBox1.Size = New Size(GetPie.Width, GetPie.Height)
    
            DataGridView1.Rows(0).Height = GetPie.Height
            DataGridView1.Columns(0).Width = GetPie.Width
        End Sub
    
        Private Function MakePieChart(D As DateTime, A As Double, P As Double, L As Double)
            Dim values() As Double = {A, P, L}
            Dim colors() As Color = {Color.Red, Color.Green, Color.Orange}
    
            ' Convert the values into angles in radians.
            Dim angles(3) As Single
            Dim total As Double = A + P + L
    
            For i As Integer = 0 To values.Length - 1
                angles(i + 1) = CSng(360 * values(i) / total)
            Next i
    
            ' Add a first value that is a tiny positive value.
            angles(0) = 0.0000001
    
            ' Make each angle be the sum of those before it.
            For i As Integer = 1 To values.Length
                angles(i) = angles(i) + angles(i - 1)
                If angles(i) > 360 Then angles(i) = 360
            Next i
    
    #Region "Draw Pie"
            ' Prepare the Bitmap.
            Dim Margin As Integer = 1 'PictureBox1.ClientSize.Width * 0.01
            Dim radius As Integer = 46 'PictureBox1.ClientSize.Width * 0.484
    
            Dim bm As New Bitmap(2 * (Margin + radius), 2 * (Margin + radius))
            Dim gr As Graphics = Graphics.FromImage(bm)
    
            ' Draw the pie chart.
            Dim clr As Integer = 0
            For i As Integer = 0 To angles.Length - 2
                gr.FillPie(New SolidBrush(colors(clr)), Margin, Margin, 2 * radius, 2 * radius, angles(i), angles(i + 1) - angles(i))
                gr.DrawPie(Pens.Black, Margin, Margin, 2 * radius, 2 * radius, angles(i), angles(i + 1) - angles(i))
                clr = (clr + 1) Mod colors.Length
            Next i
    #End Region
    
    #Region "Draw Date"
            Dim Dbmp As New Bitmap(bm.Width, 19)
            Dim Dgr As Graphics = Graphics.FromImage(Dbmp)
    
            Dgr.DrawString(D, New Font("MS SANS SERIF", 15, FontStyle.Bold, GraphicsUnit.World), Brushes.Black,
                          (53) - (Dgr.MeasureString(D, 'PictureBox1.Width / 1.8, 54.4444444444444, 47
                          New Font("MS SANS SERIF", 15, FontStyle.Regular, GraphicsUnit.World, 1, True)).Width * 0.6), 0)
    #End Region
    
    #Region "Combine"
            Dim w As Integer = bm.Width * 1.03
            Dim h As Integer = (Dbmp.Height + bm.Height) * 1.03
    
            Dim TBMP As New Bitmap(w, h)
            Dim g As Graphics = Graphics.FromImage(TBMP)
    
            g.DrawImage(Dbmp, 0, 0)
            g.DrawImage(bm, 0, Dbmp.Height)
    #End Region
    
            Dgr.Dispose()
            gr.Dispose()
            g.Dispose()
    
            Return TBMP
        End Function
    Name:  piedate.jpg
Views: 1769
Size:  19.7 KB

  8. #8
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: How to insert a Pie Chart in a DataGridView cell?

    Very good!

    But can it do this?

    Name:  a3.gif
Views: 1649
Size:  63.8 KB

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: How to insert a Pie Chart in a DataGridView cell?

    Quote Originally Posted by tommytwotrain View Post
    Very good!

    But can it do this?

    Name:  a3.gif
Views: 1649
Size:  63.8 KB
    HAHAHA
    no, but it gives me a headache just looking at it...

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2011
    Posts
    256

    Re: How to insert a Pie Chart in a DataGridView cell?

    Quote Originally Posted by tommytwotrain View Post
    Very good!

    But can it do this?

    Name:  a3.gif
Views: 1649
Size:  63.8 KB
    HAHAHA
    no, but it gives me a headache just looking at it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width