Creating linear chart from data from two Listboxes

Hello,

I have two single column list boxes. Each of them have 10 data points. The datawere created with calculations. Data in both listboxes are in string form.

ListBox1
ListBox2

I want the data in the first Listbox to be the ‘x’ value and data in the second listbox to be the ‘y’ value of a linear graph. I wanted the graph to be drawn when I press a button.

The method I coded was:

Var labels() As String
Var firstSetOfData() As Double

For i As Integer = 0 To ListBox1.LastRowIndex
Dim cellValue As String = ListBox1.Cell(i, 0)
labels.Add(cellValue)
Next

For i As Integer = 0 To ListBox2.LastRowIndex
Dim cellValue As string = ListBox2.Cell(i, 0)
If IsNumeric(cellValue) Then
Dim doubleValue As Double = Val(cellValue)
firstSetOfData.Add(doubleValue)
End If
Next

Chart1.mode = DesktopChart.Modes.Line
Chart1.Title = “Chart Example With Two Datasets”
Chart1.AddLabels labels

Var DS1 As New ChartLinearDataset(“DS1”, Color.Blue, True, firstSetOfData)
DS1.ChartType = ChartLinearDataset.ChartTypes.Line

Chart1.AddDatasets DS1

However, when I run I get the following error messages:

Window1.plotDraw line 8
“Type mismatch error. Expected String, but got Int32 Dim cellvalue As String=ListservBox1.Cells(i,0)”

Window1.plotDraw line 13
"Type mismatch error. Expected String, but got Int32 Dimcellvalue As String=ListservBox1.Cells(i,0)

Window1.plotDraw line 13
Type “DesktopListBox” has no member named “cell” Dimm cellValue As string = ListBox2.Cell(i,0)

Thank you in advance for any guidance!

Read the documentation on DesktopListbox and its properties and methods.

thank you!

I have sorted the problem out with the following code:

Var labels() As String
Var firstSetOfData() As Double

var n as integer
n=ListBox1.LastRowIndex

For i As Integer = 0 To n
labels.Add(ListBox1.CellTextAt(i, 0))
firstSetOfData.Add(ListBox2.CellTextAt(i, 0).ToDouble)
Next

'For i As Integer = 0 To ListBox1.LastRowIndex

'labels.Add( ListBox1.Cell(i, 0))
'Next

'For i As Integer = 0 To ListBox2.LastRowIndex
'firstSetOfData = Val(ListBox2.Cell(i, 0))



'Next

Chart1.mode = DesktopChart.Modes.Line
Chart1.Title = “Chart Example With Two Datasets”
Chart1.AddLabels labels

Var DS1 As New ChartLinearDataset(“DS1”, Color.Blue, True, firstSetOfData)
DS1.ChartType = ChartLinearDataset.ChartTypes.Line

Chart1.AddDatasets DS1

Using the list boxes to store data for plotting is going to least to loss of precision. The listbox allows only text to be stored in the cells. This means you are converting with some sort of format involved. You would be better storing the original numbers in arrays or some other form of storage. Loading it into the Listbox and the graph at the same time.

Thank you for the advice regarding keeping the original data in arrays!

In Xojo os there a way of having x and y axis as numerical (rather than x as string)?

Is using the chart control that come with Xojo the best method or is there any plugin that make it a better solution.

Thank you!

Do you mean in a DesktopListbox? You may use CellTagAt.

You can use DesktopChart with a Mode of DesktopChart.Modes.Scatter. Which uses ChartScatterDataset, which contains X, Y. The Radius allows you to specify the size of the symbol.

Thank you! Great to know that the scatter mode will do what I am after.

I have figured out with linear graph.

I am getting error on the following line:

Var DS1 As New ChartScatterDataset(“x changes”, Color.Blue, True, firstSetOfData)

I presume I have to code for ‘x’ and ‘y’. Could you please point towards any guide that would help me to figure how to overcome this.

Thank you!

I will provide an example when I get back to my computer later.

Thank you! Very kind!

This is a copy of my chart prototypes I’m testing. If you click on Scattergram you should find what you need. To see the code click on the Scattergram tab and double click on the DesktopChart control. The code is in the Opening event.

Chart prototypes.xojo_binary_project.zip (11.4 KB)

Thank you very much! Great resource for all chart types.
I will use the prototype to work on my file. Will post the results when successful.
Thank you!

It’s based on thee Example in Xojo, with additions.

I was successful!

Scheduled to teach medical statistics to doctors (free; no course fee). The application is to graphically demonstrate/simulate the effect of confounding variable (z) on the ‘independent’ (x) and ‘dependent’ variable (y). Anyone feel free to download the following files.

Scatter plot:

(I have acknowledged Mr Ian Kennedy)

Linear graph:

Ian, Thank you again!

You’re very welcome. You don’t need to acknowledge it, but I’ve no problem if you want to.

The only thing I would say is that the scale on the left hand graph has a very large font.

Congrats.

BTW you don’t need to use SugarSync to share your projects, you can zip and upload directly to this forum.

Thank you for the note about font size and uploading zip files into posts!
I have made a couple of minor changes to the parameters for the fonts to look approximately the same size.
confounding_2_final.xojo_binary_project.zip (12.9 KB)
confounding_scatter_graph_final.xojo_binary_project.zip (13.3 KB)