How to get chart scale to resize when data updated.

solorize

Member
Joined
Feb 22, 2012
Messages
7
Programming Experience
Beginner
Hi,

I am currently learning VB.Net and am now trying to workout how to use a chart.

What I have managed to do is currently display a chart with 3 bars on it linked to textboxes
what have numerical values in them when the form is loaded. i.e TextBox1 has an initial value set in it's properties of 10,
TextBox2 has a value of 20, TextBox3 has a value of 30.

When I run the program the graph displays these values correctly.

I have also got a button, Button1 which when pressed will plot the chart again, (so if I manually change the values in the
TextBoxes after the form has loaded), it will display the new values via the bars.

The problem I have is that if if once the initial values have been plotted when the form loads, i.e. 10, 20 & 30,
the Y axis of the chart has a maximum value of 35, so that all the initial values for the bars are shown.
But if I change the values to say 15, 30, 80. and then press Button1 to update that bars to thier new values, the bars
change but the one that show's 80 can not been seen fully as it goes off the chart, as the Y axis still only shows a maximum
value of 35!

Does anyone know how to get the chart to resize to be able to display all the bar values within the chart each time the values
are changed and the Button1 is pressed, whether it be reducing the graph for smaller values or enlarging the chart for higher values?

I have tried looking around the net and spent about 2hrs trying to find a solution, but I could not see anything that would help
me.

I am not 100% sure I am going about it in the correct way as I have not got any experience in using
charts and am having to stuble about the net trying to find basic example code of how to setup and use a chart,
which I am struggling to find. Therefore if anyone can also point me in the direction of a good resource of how to
get a basic understanding of charts I would very be greatful.


My code so far is:

VB.NET:
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Chart1.Titles.Add("Current Temperatures")
        Call PlotChart()
 
    End Sub

    Private Sub PlotChart()
        Dim s As New Series
        Dim OutsideTemp As Integer
        Dim DewPoint As Integer
        Dim WindChill As Integer


        'Create a new series and add data points to it.
        'Dim s As New Series
        s.Name = "Temperature"

        Chart1.Series.Clear()


        OutsideTemp = TextBox1.Text
        DewPoint = TextBox2.Text
        WindChill = TextBox3.Text

        s.ChartType = SeriesChartType.Column
        s.Points.AddXY("Current Temp", OutsideTemp)
        s.Points.AddXY("Dew Point", DewPoint)
        s.Points.AddXY("Wind Chill", WindChill)

        'Add the series to the Chart1 control.
        Chart1.Series.Add(s)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Chart1.Series.Dispose()
        Chart1.Series.Clear()
        Call PlotChart()
    End Sub

End Class

Thanks in advance

Mark
 
Last edited:

Latest posts

Back
Top