none
Como proceder para limpar um Chart já existente para apor novos dados? RRS feed

  • Pergunta

  • Boa noite a todos!

    A dificuldade agora é outra, pois preciso limpar um gráfico já existente e com dados para colocar novos dados de acordo com a seleção do cliente? A quem puder ajudar-me agradeço!

    Att.


    Dilson J. Hasselmann Desenvolvedor VB.NET

    sábado, 18 de abril de 2015 22:43

Respostas

  • Dilson,

    Vc está utilizando o Microsoft Chart Controls? Em caso afirmativo, o processo é bem simples, apenas associar os novos dados ao DataSource do controle e acionar o método DataBind:

    exemploChart.DataSource = dados;
    exemploChart.DataBind();

    Caso tenha interesse em conhecer mais sobre o Microsoft Chart Controls, escrevi há algum tempo um artigo para a revista .NET Magazine em que abordo em maiores detalhes o uso deste framework:

    http://www.devmedia.com.br/microsoft-chart-controls-revista-net-magazine-94/23664

    segunda-feira, 20 de abril de 2015 16:35
  • Altere o método Monta_Grafico para que fique da seguinte maneira (as mudanças envolvem a atribuição do valor à propriedade DataSource e chamar o DataBind ao final):

    Private Sub Monta_Grafico()
    
          Dim tblFields As String = "SELECT * from tbRelGrafVenda Order By CodPizzaFormula, Dia"
     
          '~~> Connecting to Data base and storing the data in a dataset
          Dim oCmd As New OleDbCommand(tblFields, gCN)
          Dim oData As New OleDbDataAdapter(tblFields, gCN)
    
          Dim ds As New DataSet
    
          If gCN.State = ConnectionState.Open Then gCN.Close()
          gCN.Open()
    
          oData.Fill(ds, "tbRelGrafVenda")
          gCN.Close()
    
    
          ''''''''''''''''''''''''''''''''
          '~~> WORKING WITH CHARTAREA <~~'
          ''''''''''''''''''''''''''''''''
          Dim CArea As ChartArea = Chart1.ChartAreas(0)
    
          CArea.BackColor = Color.DarkOliveGreen  '~~> Changing the Back Color of the Chart Area 
          CArea.BackGradientStyle = GradientStyle.TopBottom
          CArea.ShadowColor = Color.Red           '~~> Changing the Shadow Color of the Chart Area 
          CArea.ShadowColor = Color.Red           '~~> Changing the Shadow Color of the Chart Area 
          CArea.Area3DStyle.Enable3D = False      '~~> Changing the Chart Style Not 3D 
    
          '~~> Formatting X Axis
          CArea.AxisX.MajorGrid.Enabled = False   '~~> Removed the X axis major grids
          CArea.AxisX.LabelStyle.Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Italic) '~~> Setting Font, Font Size and Italicizing
          CArea.AxisX.Interval = 1
          CArea.AxisX.Maximum = 31
          CArea.AxisX.Minimum = 1
    
          '~~> Formatting Y Axis
          CArea.AxisY.MinorGrid.Enabled = False   '~~> Removed the Y axis major grids
          CArea.AxisY.LabelStyle.Format = "000"   '~~> Formatting Y axis to display values in %age
          CArea.AxisY.Interval = 2                '~~> Formatting Y axis Interval to 20%
    
          ''''''''''''''''''''''''''''
          '~~> WORKING WITH TITLE <~~'
          ''''''''''''''''''''''''''''
          '~~> Adding a Title
          Dim T As Title = Chart1.Titles.Add("Relatório de Vendas - MES")
    
          '~~> Formatting the Title
          With T
             .ForeColor = Color.Black            '~~> Changing the Fore Color of the Title 
             .BackColor = Color.White            '~~> Changing the Back Color of the Title 
             '~~> Setting Font, Font Size and Bold/Italicizing
             .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
             .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Underline)
             .BorderColor = Color.Black                   '~~> Changing the Border Color of the Title 
             .BorderDashStyle = ChartDashStyle.NotSet  '~~> Changing the Border Dash Style of the Title 
          End With
    
          '''''''''''''''''''''''''''''
          '~~> WORKING WITH SERIES <~~'
          '''''''''''''''''''''''''''''
          '~~> Assigning values to X and Y Axis
    
          'Tamanho Grande
          Chart1.Series(0).LabelFormat = "00"
          Chart1.Series(0).XValueMember = "Dia"
          Chart1.Series(0).YValueMembers = "TotQtd"
          Chart1.Series(0).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(0).YValueType = ChartValueType.Double
          Chart1.Series(0).IsValueShownAsLabel = False
    
          'Tamanho Médio
          Chart1.Series(1).LabelFormat = "00"
          Chart1.Series(1).XValueMember = "Dia"
          Chart1.Series(1).YValueMembers = "TotQtd1"
          Chart1.Series(1).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(1).YValueType = ChartValueType.Double
          Chart1.Series(1).IsValueShownAsLabel = False
    
          'Tamanho Broto
          Chart1.Series(2).LabelFormat = "00"
          Chart1.Series(2).XValueMember = "Dia"
          Chart1.Series(2).YValueMembers = "TotQtd2"
          Chart1.Series(2).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(2).YValueType = ChartValueType.Double
          Chart1.Series(2).IsValueShownAsLabel = False
    
          '''''''''''''''''''''''''''''
          '~~> WORKING WITH LEGEND <~~'
          '''''''''''''''''''''''''''''
          Dim LG As Legend = Chart1.Legends(0)
          '~~> Changing the Back Color of the Legend 
          LG.BackColor = Color.Wheat
          '~~> Changing the Fore Color of the Legend
          LG.ForeColor = Color.DarkSlateBlue
          '~~> Setting Font, Font Size and Bold
          LG.Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          '~~> Assigning a title for the legend
          LG.Title = "Legenda"
    
          '~~> Setting the location for the chart
          Chart1.Size = New System.Drawing.Size(1232, 594)
          Chart1.Location = New Point(12, 68)
    
          '''''''''''''''''''''''''''''
          '~~> SET DATA SOURCE <~~' Movido para este ponto
          '''''''''''''''''''''''''''''
          Chart1.DataSource = ds.Tables("tbRelGrafVenda")
          Chart1.DataBind
    
       End Sub


    • Editado Renato GroffeMVP segunda-feira, 20 de abril de 2015 19:52
    • Marcado como Resposta DJHasselmann quarta-feira, 22 de abril de 2015 01:53
    segunda-feira, 20 de abril de 2015 19:51

Todas as Respostas

  • Dilson,

    Vc está utilizando o Microsoft Chart Controls? Em caso afirmativo, o processo é bem simples, apenas associar os novos dados ao DataSource do controle e acionar o método DataBind:

    exemploChart.DataSource = dados;
    exemploChart.DataBind();

    Caso tenha interesse em conhecer mais sobre o Microsoft Chart Controls, escrevi há algum tempo um artigo para a revista .NET Magazine em que abordo em maiores detalhes o uso deste framework:

    http://www.devmedia.com.br/microsoft-chart-controls-revista-net-magazine-94/23664

    segunda-feira, 20 de abril de 2015 16:35
  • Boa tarde!

    Sim estou utilizando o Chart Controls no VB.NET 2012 o fragmento do código segue abaixo:

    Private Sub Monta_Grafico()
    
          Dim tblFields As String = "SELECT * from tbRelGrafVenda Order By CodPizzaFormula, Dia"
     
          '~~> Connecting to Data base and storing the data in a dataset
          Dim oCmd As New OleDbCommand(tblFields, gCN)
          Dim oData As New OleDbDataAdapter(tblFields, gCN)
    
          Dim ds As New DataSet
    
          If gCN.State = ConnectionState.Open Then gCN.Close()
          gCN.Open()
    
          oData.Fill(ds, "tbRelGrafVenda")
          gCN.Close()
    
          '''''''''''''''''''''''''''''
          '~~> SET DATA SOURCE <~~'
          '''''''''''''''''''''''''''''
          Chart1.DataSource = ds.Tables("tbRelGrafVenda")
    
          ''''''''''''''''''''''''''''''''
          '~~> WORKING WITH CHARTAREA <~~'
          ''''''''''''''''''''''''''''''''
          Dim CArea As ChartArea = Chart1.ChartAreas(0)
    
          CArea.BackColor = Color.DarkOliveGreen  '~~> Changing the Back Color of the Chart Area 
          CArea.BackGradientStyle = GradientStyle.TopBottom
          CArea.ShadowColor = Color.Red           '~~> Changing the Shadow Color of the Chart Area 
          CArea.ShadowColor = Color.Red           '~~> Changing the Shadow Color of the Chart Area 
          CArea.Area3DStyle.Enable3D = False      '~~> Changing the Chart Style Not 3D 
    
          '~~> Formatting X Axis
          CArea.AxisX.MajorGrid.Enabled = False   '~~> Removed the X axis major grids
          CArea.AxisX.LabelStyle.Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Italic) '~~> Setting Font, Font Size and Italicizing
          CArea.AxisX.Interval = 1
          CArea.AxisX.Maximum = 31
          CArea.AxisX.Minimum = 1
    
          '~~> Formatting Y Axis
          CArea.AxisY.MinorGrid.Enabled = False   '~~> Removed the Y axis major grids
          CArea.AxisY.LabelStyle.Format = "000"   '~~> Formatting Y axis to display values in %age
          CArea.AxisY.Interval = 2                '~~> Formatting Y axis Interval to 20%
    
          ''''''''''''''''''''''''''''
          '~~> WORKING WITH TITLE <~~'
          ''''''''''''''''''''''''''''
          '~~> Adding a Title
          Dim T As Title = Chart1.Titles.Add("Relatório de Vendas - MES")
    
          '~~> Formatting the Title
          With T
             .ForeColor = Color.Black            '~~> Changing the Fore Color of the Title 
             .BackColor = Color.White            '~~> Changing the Back Color of the Title 
             '~~> Setting Font, Font Size and Bold/Italicizing
             .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
             .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Underline)
             .BorderColor = Color.Black                   '~~> Changing the Border Color of the Title 
             .BorderDashStyle = ChartDashStyle.NotSet  '~~> Changing the Border Dash Style of the Title 
          End With
    
          '''''''''''''''''''''''''''''
          '~~> WORKING WITH SERIES <~~'
          '''''''''''''''''''''''''''''
          '~~> Assigning values to X and Y Axis
    
          'Tamanho Grande
          Chart1.Series(0).LabelFormat = "00"
          Chart1.Series(0).XValueMember = "Dia"
          Chart1.Series(0).YValueMembers = "TotQtd"
          Chart1.Series(0).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(0).YValueType = ChartValueType.Double
          Chart1.Series(0).IsValueShownAsLabel = False
    
          'Tamanho Médio
          Chart1.Series(1).LabelFormat = "00"
          Chart1.Series(1).XValueMember = "Dia"
          Chart1.Series(1).YValueMembers = "TotQtd1"
          Chart1.Series(1).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(1).YValueType = ChartValueType.Double
          Chart1.Series(1).IsValueShownAsLabel = False
    
          'Tamanho Broto
          Chart1.Series(2).LabelFormat = "00"
          Chart1.Series(2).XValueMember = "Dia"
          Chart1.Series(2).YValueMembers = "TotQtd2"
          Chart1.Series(2).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(2).YValueType = ChartValueType.Double
          Chart1.Series(2).IsValueShownAsLabel = False
    
          '''''''''''''''''''''''''''''
          '~~> WORKING WITH LEGEND <~~'
          '''''''''''''''''''''''''''''
          Dim LG As Legend = Chart1.Legends(0)
          '~~> Changing the Back Color of the Legend 
          LG.BackColor = Color.Wheat
          '~~> Changing the Fore Color of the Legend
          LG.ForeColor = Color.DarkSlateBlue
          '~~> Setting Font, Font Size and Bold
          LG.Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          '~~> Assigning a title for the legend
          LG.Title = "Legenda"
    
          '~~> Setting the location for the chart
          Chart1.Size = New System.Drawing.Size(1232, 594)
          Chart1.Location = New Point(12, 68)
    
       End Sub

    Aqui chamo a rotina para montar a Tabela classificada!

    Private Sub dtpMesAno_ValueChanged(sender As Object, e As EventArgs) Handles dtpMesAno.ValueChanged
    
          Call Classificar_Dados()
    
       End Sub

    E aqui chamo o gráfico proprieamente dito!

             gSQL = Nothing
             gCMD.Dispose()
             gDtR.Close()
    
             Call Monta_Grafico()
    
          Catch ex As Exception

    Se puder ajudar-me de como usar a sua dica em seu post agradeço!A

    Att.


    Dilson J. Hasselmann Desenvolvedor VB.NET

    segunda-feira, 20 de abril de 2015 18:31
  • Altere o método Monta_Grafico para que fique da seguinte maneira (as mudanças envolvem a atribuição do valor à propriedade DataSource e chamar o DataBind ao final):

    Private Sub Monta_Grafico()
    
          Dim tblFields As String = "SELECT * from tbRelGrafVenda Order By CodPizzaFormula, Dia"
     
          '~~> Connecting to Data base and storing the data in a dataset
          Dim oCmd As New OleDbCommand(tblFields, gCN)
          Dim oData As New OleDbDataAdapter(tblFields, gCN)
    
          Dim ds As New DataSet
    
          If gCN.State = ConnectionState.Open Then gCN.Close()
          gCN.Open()
    
          oData.Fill(ds, "tbRelGrafVenda")
          gCN.Close()
    
    
          ''''''''''''''''''''''''''''''''
          '~~> WORKING WITH CHARTAREA <~~'
          ''''''''''''''''''''''''''''''''
          Dim CArea As ChartArea = Chart1.ChartAreas(0)
    
          CArea.BackColor = Color.DarkOliveGreen  '~~> Changing the Back Color of the Chart Area 
          CArea.BackGradientStyle = GradientStyle.TopBottom
          CArea.ShadowColor = Color.Red           '~~> Changing the Shadow Color of the Chart Area 
          CArea.ShadowColor = Color.Red           '~~> Changing the Shadow Color of the Chart Area 
          CArea.Area3DStyle.Enable3D = False      '~~> Changing the Chart Style Not 3D 
    
          '~~> Formatting X Axis
          CArea.AxisX.MajorGrid.Enabled = False   '~~> Removed the X axis major grids
          CArea.AxisX.LabelStyle.Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Italic) '~~> Setting Font, Font Size and Italicizing
          CArea.AxisX.Interval = 1
          CArea.AxisX.Maximum = 31
          CArea.AxisX.Minimum = 1
    
          '~~> Formatting Y Axis
          CArea.AxisY.MinorGrid.Enabled = False   '~~> Removed the Y axis major grids
          CArea.AxisY.LabelStyle.Format = "000"   '~~> Formatting Y axis to display values in %age
          CArea.AxisY.Interval = 2                '~~> Formatting Y axis Interval to 20%
    
          ''''''''''''''''''''''''''''
          '~~> WORKING WITH TITLE <~~'
          ''''''''''''''''''''''''''''
          '~~> Adding a Title
          Dim T As Title = Chart1.Titles.Add("Relatório de Vendas - MES")
    
          '~~> Formatting the Title
          With T
             .ForeColor = Color.Black            '~~> Changing the Fore Color of the Title 
             .BackColor = Color.White            '~~> Changing the Back Color of the Title 
             '~~> Setting Font, Font Size and Bold/Italicizing
             .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
             .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Underline)
             .BorderColor = Color.Black                   '~~> Changing the Border Color of the Title 
             .BorderDashStyle = ChartDashStyle.NotSet  '~~> Changing the Border Dash Style of the Title 
          End With
    
          '''''''''''''''''''''''''''''
          '~~> WORKING WITH SERIES <~~'
          '''''''''''''''''''''''''''''
          '~~> Assigning values to X and Y Axis
    
          'Tamanho Grande
          Chart1.Series(0).LabelFormat = "00"
          Chart1.Series(0).XValueMember = "Dia"
          Chart1.Series(0).YValueMembers = "TotQtd"
          Chart1.Series(0).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(0).YValueType = ChartValueType.Double
          Chart1.Series(0).IsValueShownAsLabel = False
    
          'Tamanho Médio
          Chart1.Series(1).LabelFormat = "00"
          Chart1.Series(1).XValueMember = "Dia"
          Chart1.Series(1).YValueMembers = "TotQtd1"
          Chart1.Series(1).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(1).YValueType = ChartValueType.Double
          Chart1.Series(1).IsValueShownAsLabel = False
    
          'Tamanho Broto
          Chart1.Series(2).LabelFormat = "00"
          Chart1.Series(2).XValueMember = "Dia"
          Chart1.Series(2).YValueMembers = "TotQtd2"
          Chart1.Series(2).Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          Chart1.Series(2).YValueType = ChartValueType.Double
          Chart1.Series(2).IsValueShownAsLabel = False
    
          '''''''''''''''''''''''''''''
          '~~> WORKING WITH LEGEND <~~'
          '''''''''''''''''''''''''''''
          Dim LG As Legend = Chart1.Legends(0)
          '~~> Changing the Back Color of the Legend 
          LG.BackColor = Color.Wheat
          '~~> Changing the Fore Color of the Legend
          LG.ForeColor = Color.DarkSlateBlue
          '~~> Setting Font, Font Size and Bold
          LG.Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
          '~~> Assigning a title for the legend
          LG.Title = "Legenda"
    
          '~~> Setting the location for the chart
          Chart1.Size = New System.Drawing.Size(1232, 594)
          Chart1.Location = New Point(12, 68)
    
          '''''''''''''''''''''''''''''
          '~~> SET DATA SOURCE <~~' Movido para este ponto
          '''''''''''''''''''''''''''''
          Chart1.DataSource = ds.Tables("tbRelGrafVenda")
          Chart1.DataBind
    
       End Sub


    • Editado Renato GroffeMVP segunda-feira, 20 de abril de 2015 19:52
    • Marcado como Resposta DJHasselmann quarta-feira, 22 de abril de 2015 01:53
    segunda-feira, 20 de abril de 2015 19:51