Can chartvalues being stored in an array

I have an array with contents and values of which I do not know how many records it will contain on forehand. One day 10 and other day 6 or even 20 or more.

I want to create a bar chart of one bar per product and how many there were sold. The example for desktopchart defines per bar a data set. I want to have this in an array, because otherwise I am repeating each line:

  Var DS1 As New ChartLinearDataset(cell(2)+":"+cell(3), color.AccentThemColor, True, firstSetOfData)
  DS1.ChartType = ChartLinearDataset.ChartTypes.bar
  periodChart.AddLabel cell(2)

and I want just as much bars as I need. I tried several things, but all result in errors. I tried things like:

dim ds() as new ChartLinearDataset(content as String, color.AccentThemeColor, True, setofdata() as double)

but that result in a syntax error.

I do not want to write out every content of the chart in separate lines with a select…end select or if…end if statement. The code will be huge and I think there must be a way to solve this.

I tried:

if daySelectBx.SelectedRowText = cell(1) then

select case i //(where i is the record number in the array containing 1 product)
  
case 0
  FirstSetOfData.Add <amount like 3>
  
  Var DS1 As New ChartLinearDataset("", color.Blue, True, firstSetOfData)
  DS1.ChartType = ChartLinearDataset.ChartTypes.bar
  periodChart.AddLabel <variable content like "Fish & chips")
   
case 1
  SecondSetOfData.Add <amount like 1>
  
  Var DS2 As New ChartLinearDataset("", color.AccentThemeColor, True, SecondSetOfData)
  DS2.ChartType = ChartLinearDataset.ChartTypes.bar
  periodChart.AddLabel "Steak Hollandia"
 
.
.
.
end select

but after case 2, (DS2) it did forget the existence of DS1

Any idea how to solve this, I am out of ideas

You’re so close…

dim ds() as ChartLinearDataset
dim d as new ChartLinearDataset(content as String, color.AccentThemeColor, True, setofdata() as double)
ds.Add d

Thank you very much Greg, I changed it and it is working like a champ. However, why do I see 8 times the same value per entry. I could not figure that out, but I am afraid I am overlooking it:

var cell(),labels(), Content as string
var i,j as integer
var ds() as ChartLinearDataset
var setofdata() as double
var dc as new ChartLinearDataset(content, color.Blue, True, setofdata)

dc.IsLegendVisible = false
dc.ChartType = ChartLinearDataset.ChartTypes.bar

for i=mealStats.FirstIndex to mealStats.LastIndex
cell()=split(mealStats(i),“,”) //mealstats(i)=week,date,name,amount (“48”,“20251128”,“burger”,“3”)

if daySelectBx.SelectedRowText = cell(1) then //for today 8 records

content = cell(2)+":"+cell(3)
setofdata.Add val(cell(3))

labels.Add cell(2)
ds.Add dc               //ds contains 8 dc entries
end if
next

for i=ds.FirstIndex to ds.LastIndex
periodChart.AddDataset ds(i)
periodChart.AddLabels labels(i)
next

Did you clear the data in the period chart before adding all of the values? Try putting a breakpoint at the top of the method and see if it’s being called 8 times

var cell(),labels(), Content as string
var intTeller as integer
var ds() as ChartLinearDataset
var setofdata() as double
var dc as new ChartLinearDataset(content, color.Red, True, setofdata)

periodChart.RemoveAllDatasets
periodChart.RemoveAllLabels

dc.IsLegendVisible = false
dc.ChartType = ChartLinearDataset.ChartTypes.bar

for intTeller=mealStats.FirstIndex to mealStats.LastIndex
cell()=split(mealStats(intTeller),“,”)

if daySelectBx.SelectedRowText = cell(1) then

content = cell(2)+":"+cell(3)
setofdata.Add val(cell(3))

labels.Add cell(2)
ds.Add dc

end if
next

for intTeller=ds.FirstIndex to ds.LastIndex
periodChart.AddDatasets ds(intTeller)
periodChart.AddLabels labels(intTeller)
next

As you can see I am clearing the chartvalues and labels at the start of the method, when I check the variable values all are on 7 (8 records)

So, I have no clue where the 8 bars per record is coming from. When I select another dat with 4 entries there are 4 bars per record.

I found it! The method was called twice, one time during opening of the container and also when the combobox with dates was filled and SelectedRowText changed after the SelectedRowIndex was initial set to ‘0’.

Question stays why the second time the desktopchart.RemoveAll did not kick in, while it was at the beginning of the method.

Now at least I have a decent chart. Thank you!

1 Like