Load/Fill/Populate Chart with BackgroundWorker in Vb.Net
December 10, 2013 1 Comment
Please find the code below to load CHART with backgroundworker:
' Create a delegate Private Delegate Sub chartGenInvDelegate(ByVal chrt As Chart, ByVal invAmt() As Decimal) Private Sub ChartGenInv(ByVal chrt As Chart, ByVal invAmt() As Decimal) Try If chrt.InvokeRequired Then chrt.Invoke(New chartGenInvDelegate(AddressOf ChartGenInv), chrt, invAmt) Else Dim yValues As Double() = {invAmt(2), invAmt(3), invAmt(4)} Dim xValues As String() = {"Current Month", "Last Month", "2 Months Old"} Dim seriesName As String = Nothing chrt = chrtInvStats chrt.Series.Clear() chrt.Titles.Clear() seriesName = "InvoiceAmount" chrt.Series.Add(seriesName) chrt.Series(seriesName).Points.DataBindXY(xValues, yValues) ' Chart Area Modification Dim CArea As ChartArea = chrt.ChartAreas(0) CArea.BackColor = Color.Azure CArea.ShadowColor = Color.Red CArea.Area3DStyle.Enable3D = True CArea.AxisX.MajorGrid.Enabled = False CArea.AxisY.MajorGrid.Enabled = False chrt.Series(seriesName).Points(0).Color = Color.MediumSeaGreen chrt.Series(seriesName).Points(1).Color = Color.PaleGreen chrt.Series(seriesName).Points(2).Color = Color.LawnGreen chrt.Series(seriesName).ChartType = SeriesChartType.Column chrt.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True ' Formatting the Chart Title Dim T As Title = chrt.Titles.Add("Last 3 Months Invoices Total($)") With T .ForeColor = Color.Black .BackColor = Color.LightBlue .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold) .BorderColor = Color.Black End With chrt.Series(seriesName).IsValueShownAsLabel = True chrt.Legends(0).Enabled = False End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub bgwSuburbs_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwSuburbs.DoWork Try ' Get your data in Array ' Pass chart and Array as a parameter to the invoking function ChartGenInv(chrtInvStats, invValues) Catch ex As Exception MsgBox("No response from server. Please try again."& Chr(10) & ex.Message, MsgBoxStyle.Exclamation, "Data Load Failure") End Try End Sub Private Sub bgwSuburbs_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwSuburbs.RunWorkerCompleted progressBarLoad.Visible = False End Sub