שתף באמצעות


Visual Basic 2010: Plot Graph (easy?!)

Question

Monday, July 9, 2012 12:46 PM

Hi,

I am a new user of Visual Basic, and picking some things up quickly, but others are stumping me a little.

I have created a form, which has several user choices including option buttons.  These option buttons will be linked to a choice of graph plotting.  For example, option 1 = x,y 'scatter plot', option 2 = a different x1,y1 'scatter plot', and option 3 = x1,x2 data table.

Question 1.  Which is best for the above; Chart, or PictureBox?

carrying on, based on a very simple maths example:

I currently calculate the x and y values in the option 1 button click function

x=1 to 100

y=sin(x)

(note, x1 and y1 will be calculated in option 2 button click function).

Question 2.  how do I then send this data to the chart display?  Ignoring additional information like background colour and things (maybe not a good idea?). 

**Question 3. ** At which point in my code do I put this data passing code in? the function where I have calculated x and y? 

Note I already have 'Imports System.Math' in my code.

Many thanks,

Philip

All replies (10)

Tuesday, July 10, 2012 12:52 PM ✅Answered

If you can view the chart in the designer as the type you need and if the empty chart appears when you run the application then you only need to add the data: 

        For i = 0 To 100            x(i) = i / 10            y(i) = Math.Sin(x(i))            Chart1.Series(0).Points.AddXY(x(i), y(i))        Next

But I would recommend that you don't do it in the form load event.


Monday, July 9, 2012 1:46 PM

Have you downloaded and installed the chart controls?

http://msdn.microsoft.com/en-us/library/dd456632.aspx

The samples library is the best source for examples of how to configure your chart and pass the data to it.


Monday, July 9, 2012 4:44 PM

Hi,

Thanks for your reply.

I'm not sure if I've got chart controls installed, how can I tell? As far as I know everything is installed (it's a work machine).

The samples and help files seem to be pretty vague and a little unhelpful on explaining things and how best to get started, even for a simple example such as this, which I why I was hoping to get some advice / sample code / explanation here.  I understand that I need to databind the data, but at the moment I have seperate arrays for x and y.

Phil


Monday, July 9, 2012 9:45 PM

Did you work through the Getting Started page at the site I referenced, especially the Basic Chart tutorial? 

If the Chart Controls are properly installed you should be able to complete that tutorial.  If you can load and run the examples then the controls must be installed.

You do not have to bind your data to the the chart and i would recommend that you don't.   The basic chart you created in the tutorial can be modfied to use your existing x and y arrays.


Tuesday, July 10, 2012 9:02 AM

Hi,

thanks again for your response.

I have worked through the example, so I do have chart controls installed.  I'm not sure how I can implement the example done in the form properties environment in the same way in a code environment though, as I am working predominantly with code (this will be a dynamic graph).

Phil


Tuesday, July 10, 2012 9:15 AM

Which example are you going to use for your chart?  Have you inserted test data using the desinger to confirm that the chart looks correct for your requirements? 

When the chart is configured correctly you can attempt the code required to insert your data into the x and y series values for the chart.   If you have any queries about that process, post the code that you are using.


Tuesday, July 10, 2012 11:34 AM

Hello,

I'm not sure what your question is regarding "which example are you going to use for your chart?"

I have inserted test data using the designer to confirm the chart looks correct.  I have chosen a 'spline' chart, as I wish to replicate a 'scatter' graph, e.g. x vs y.

So far, the only code I have is to generate the y = sin(x).  This is placed in the form load function.  however, this is not where it will remain, as I will likely move it to another function (e.g. when a button is pushed).  however, I am not concerned with that at the moment, just plotting a basic graph.  I have no code for this, as I don't know where to start.

Thanks,

Imports System.Math

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim x(100) As Double
        Dim y(100) As Double

        For i = 0 To 100
            x(i) = i / 10
            y(i) = Sin(x(i))
        Next

    End Sub
End Class

Tuesday, July 10, 2012 1:11 PM

Brilliant, thank you.

How come the code you've added doesn't work outside the for loop, is that something inherent with the graphing / plotting?

I will need to set up some kind of initialise function, that runs as soon as the form is loaded, with default values and things, and the graph displayed using these default values.  I assume I can create this event, and then get it to run once the form is loaded? How is it best to do this?

best regards,


Tuesday, July 10, 2012 9:46 PM

The code to set the graph point values can be included anywhere, but it must process for each point in the graph.  So wherever you put it, it will be part of a loop that works through all values in the array.  If you want to assign the values in a single statement use Add or AddY instead.

The default initialisation of the point values is done in the designer, so that the graph displays with default values to indicate the format at startup.

The real data is then displayed when it is available.   This may be following user action of some sort - for instance to seelct which function is to be graphed - or it might be in an event that occurs just after startup.  It depends on where the data is coming from or how it is being generated.   You can start it from the form load event as long as you call a separate function to do the actual updating.


Wednesday, July 11, 2012 5:54 PM

Great.

many thanks.