Jump to content

[Resolvido] Erro ao retornar a Form com Gráfico


Chamuanza

Recommended Posts

Mais uma vez um Bem Aja a todos

Há já algum tempo que ando com esta pedra no sapato,por isso aqui retorno para ajuda.

Tenho um Form que apresenta um grafico a funcionar bem, mas quando saio do form do grafico com "close" e retorno ao mesmo para rever o grafico da-me o erro:

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

na linha assinalada a vermelho

Após fazer stop Debugging e retornar de novo já funciona

Alguem que me dê umas dicas, por favor

Obrigado

Private Sub MostrarGraficoEuromilhoes()

	Dim xNumerosEuro(50) As Byte
	For T = 1 To 50
		xNumerosEuro(T) = T
	Next

	'Conta os numeros do Euromilhoes
	Dim yContagem(50) As Integer
	For i As Integer = 1 To 50
		For Linhas = 0 To DataGridView1.RowCount - 1
			For Colunas = 0 To 4
				If DataGridView1.Item(Colunas, Linhas).Value = i Then
					yContagem(i) += 1
				End If
			Next Colunas
		Next Linhas
	Next i
	'**************************************************************************************************
	'Conta as estrelas do euromilhoes
	Dim yContaEstrelas(11) As Byte
	Dim xEstrelasEuro(11) As Byte

	For T = 1 To 11
		xEstrelasEuro(T) = T
	Next

	For V As Integer = 1 To 11
		For Linhas = 0 To DataGridView1.RowCount - 1
			For Colunas = 5 To 6
				If DataGridView1.Item(Colunas, Linhas).Value = V Then
					yContaEstrelas(V) += 1
				End If
			Next Colunas
		Next Linhas
	Next V


	Dim series As Series = Chart1.Series.Add("Series")

	With Chart1
		'define o tipo de gráfico

		Chart1.Series(0)("DrawingStyle") = "Cylinder"
	    Chart1.Series(1)("DrawingStyle") = "Cylinder"

		'define o texto da legenda
		.Series(0).LegendText = "Nºs  Euromilhões"
		.Series(1).LegendText = "Nºs Estrelas"
		'define o titulo do eixo y , sua fonte e a cor
		.ChartAreas(0).AxisY.Title = "Nº Vezes Saidos"
		.ChartAreas(0).AxisY.TitleFont = New Font("Times New Roman", 12, FontStyle.Bold)
		.ChartAreas(0).AxisY.TitleForeColor = Color.Blue
		'define o titulo do eixo x , sua fonte e a cor
		.ChartAreas(0).AxisX.Title = "Nºs Euromilhões (1 a 50)"
		.ChartAreas(0).AxisX.TitleFont = New Font("Times New Roman", 10, FontStyle.Bold)
		.ChartAreas(0).AxisX.TitleForeColor = Color.Blue
		'define a paleta de cores usada
		.Palette = ChartColorPalette.Fire
		'vincula os dados ao gráfico
		.Series(0).Points.DataBindXY(xNumerosEuro, yContagem)
		.Series(1).Points.DataBindXY(xEstrelasEuro, yContaEstrelas)
		'exibe os valores nos eixos
		'.Series(0).IsValueShownAsLabel = True
		'.Series(1).IsValueShownAsLabel = True
		'habilita a exibição 3D
		.ChartAreas(0).Area3DStyle.Enable3D = True
		.ChartAreas(0).Area3DStyle.Rotation = 30

		' Set interval of X axis to 1 week, with an offset of 1 day
		Chart1.ChartAreas(0).AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount
		'Chart1.ChartAreas(0).AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount
		'Chart1.ChartAreas(0).AxisX.Interval = 25
		Chart1.ChartAreas(0).AxisX.IntervalType = DateTimeIntervalType.Number
		'Chart1.ChartAreas(0).AxisX.IntervalType = DateTimeIntervalType.NotSet
		Chart1.ChartAreas(0).AxisX.IntervalOffset = 3
		'Chart1.ChartAreas(0).AxisX.Interval = 50
		' Equally sized auto-fit font for all axes
		Chart1.ChartAreas(0).IsSameFontSizeForAllAxes = True
		' Set series points width to 20 pixels
		Chart1.Series(0)("PixelPointWidth") = "5"
		Chart1.Series(1)("PixelPointWidth") = "5"

		' Enable X axis labels automatic fitting
		Chart1.ChartAreas(0).AxisX.IsLabelAutoFit = True
		' Set docking of the legend title
		Chart1.Legends(0).Docking = Docking.Bottom
		' Set chart title
		Chart1.Titles(0).Text = "EUROMILHÕES" & ControlChars.Lf & "Nºs Sorteados"
		' Set chart title font
		Chart1.Titles(0).Font = New Font("Times New Roman", 14, FontStyle.Bold)

	End With
	Chart1.Visible = True
End Sub
Edited by Chamuanza
Link to comment
Share on other sites

Porque o gráfico já vem com uma serie adicionada por defeito e como estás a adicionar outra por código ficas com duas, ao limpares o gráfico todas são apagadas. Na proxima vez que correres o código só vais ter a que estás a adicionar por código, logo vai-te faltar uma serie.

Como estás a fazer por código deves apagar a serie por defeito e em vez de adicionares uma no código, adiciona duas.

Pedro Martins

Não respondo a duvidas por PM

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.