Android Question Creating a bar chart using two array's

Colin Evans

Active Member
Licensed User
Longtime User
Following on from my previous post, I would like to know how best to add the data contained in two array's
B4X:
For i = 0 To 99
                    Dim str As Map = results.Get(i)
                    Dim strVar As String = str.Get("interval_end")
                    Dim strArray() As String=Regex.Split("T",strVar)
                    Dim TextExtract As String=strArray(0)
                    from=TextExtract
                    cons = str.Get("consumption")
                    tim0 = str.Get("interval_start")
                    tim0 = tim0.SubString2(11,16)
                    tim1 = str.Get("interval_end")
                    tim1 = tim1.SubString2(11,16)
                    N(i) = tim0 
                    V(i) = cons
                    AddRow(Array As String(from, tim0, tim1, cons))
                Next

the data held in N and V, I basically want to show the bar chart based on three segments of the data, i.e. N(1) and V(1) to N(33) and V(33), then be able to select N(34) V(34) to N(66) V(66) and then select N(67) V(67) to N(100) V(100), hope that makes sense, I have tried using a Bar Chart example but sadly getting nowhere
 

klaus

Expert
Licensed User
Longtime User
You cannot do it like in your example.
You need to redraw the chart for each set, which means recreate it with the new data.

How do you want to switch from the first set of 33 samples to the second set and then to the third.
Be aware that the indexes of arrays begin with 0 !
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Preferably have three buttons to select each of the segments of data,

Ideally I would prefer to have the ability to select the data based on a specific date, the interval_start variable is structure like this interval_start: '2020-05-05T00:30:00+01:00' and each of the entries for the day selected is half hour segments a short example below, usually the data returned in the JSON contains a mixture of dates covering three days, there is always 0 to 99, so being able to just enter say 05-05-2020 and select the 48 pieces of data and present them in a graph would be brilliant, unfortunately I don't profess to be a developer, so everything is a learning curve,

Thanks for the reply, any advice greatly appreciated

B4X:
results
[LIST]
[*]0
[LIST]
[*]interval_end: 2020-05-05T01:00:00+01:00
[*]consumption: 0.165
[*]interval_start: 2020-05-05T00:30:00+01:00
[/LIST]
[*]1
[LIST]
[*]interval_end: 2020-05-05T00:30:00+01:00
[*]consumption: 0.564
[*]interval_start: 2020-05-05T00:00:00+01:00
[/LIST]
[*]2
[LIST]
[*]interval_end: 2020-05-05T00:00:00+01:00
[*]consumption: 1.129
[*]interval_start: 2020-05-04T23:30:00+01:00
[/LIST]
[*]3
[LIST]
[*]interval_end: 2020-05-04T23:30:00+01:00
[*]consumption: 1.122
[*]interval_start: 2020-05-04T23:00:00+01:00
[/LIST]
[*]4
[LIST]
[*]interval_end: 2020-05-04T23:00:00+01:00
[*]consumption: 1.12
[*]interval_start: 2020-05-04T22:30:00+01:00
[/LIST]
[*]5
[LIST]
[*]interval_end: 2020-05-04T22:30:00+01:00
[*]consumption: 1.145
[*]interval_start: 2020-05-04T22:00:00+01:00
[/LIST]
[/LIST]
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What does AddRow() do with the data you give it?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Sort of off topic:
B4X:
Dim strVar As String = str.Get("interval_end")
Dim strArray() As String=Regex.Split("T",strVar)
Dim TextExtract As String=strArray(0) from=TextExtract
from=TextExtract

Could just be:

B4X:
Dim strVar As String = str.Get("interval_end")
from = strVar.SubString2(0,10)

On topic (from previous thread that started this thread): You have your 0's worked out? Previously you stated that you only had 0's in your V array.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
the data held in N and V, I basically want to show the bar chart based on three segments of the data, i.e. N(1) and V(1) to N(33) and V(33), then be able to select N(34) V(34) to N(66) V(66) and then select N(67) V(67) to N(100) V(100), hope that makes sense,
thanks, any idea's on showing in a Bar Chart
Are you trying to show the totals for each day only? If so you can add up the consumption for each day an then show 3 bars (one for each day)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
No I want to show it every half hour as the data shows, I realise that would be 48 bars is that possible?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
grouped-column-chart-reference-small.png


Sorry for the big pic. First one I found. I think I know what you are trying to do. The cities would be the days. The colors would be the half-hour periods. The count would be the consumption. How to do it? No clue, but maybe someone else can tell if this is doable and how.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
thanks, any idea's on showing in a Bar Chart
You need to write a routine to show the chart, something like this.
B4X:
Private Sub FillBarChart(BeginIndex As Int, EndIndex As Int)
    BarChart1.ClearPoints
    BarChart1.AddBar("Consumption", xui.Color_Red)
    For i = BeginIndex To EndIndex
        BarChart1.AddBarPointData(N(i), V(i))
    Next
    BarChart1.DrawChart
End Sub

No I want to show it every half hour as the data shows, I realise that would be 48 bars is that possible?
It depends on the chart width, maybe yes, maybe not.
 
Last edited:
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Thanks, tried it but weird results
B4X:
    FillBarChart(0,47)
B4X:
Private Sub FillBarChart(BeginIndex As Int, EndIndex As Int)
    BarChart.ClearPoints
    BarChart.AddBar("Consumption", xui.Color_Red)
    For i = BeginIndex To EndIndex
        BarChart.AddBarPointData(N(i), V(i))
    Next
    BarChart.DrawChart
End Sub

But the graph doesn't show any bars, nor do the values appear to correct compared to the data in the table, if you scroll across the x-axis it shows consumption as = 0 , if you change the FillBarChart(48, 64) you can scroll across and maybe half way across it shows some consumption figures

Screenshot_1588783829.png
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It's difficult to debug a project without the whole code.
I suppose that you added the BarChart in the Designer.
Did you set the ChartType to BAR?
Is C() defined as Double?
Then, set the indexes from 0 to 15 to see if you get a chart.
Then set a breakpoint in the FillBarChart routine at this line BarChart.AddBarPointData(N(i), V(i)) and check if the content of N(i) and V(i) is the correct one.
Otherwise post your project as a zip file.

You will need the latest xChart b4xlib not yet officially published.
You find it HERE.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
It could look like this with 48 items:

1588788406534.png


It works only in landscape, the number of bars is to big for portrait.
Tested on my Samsung Galaxy S8
The attached test project contains the xChart.bas Class module.
If have already updated the library you can remove the xChart module.
 

Attachments

  • BarChart.zip
    33.9 KB · Views: 190
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Klaus, sorry but when I ran your BarChart code it sated that xChart was defined twice, once as a Module and another as a library, I removed the module which allowed the program to run, but unfortunately whilst the graph was there I couldn't see anything, just a white box, you could see the red line if you scrolled horizontal
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Thanks Klaus, that works okay, thanks again, I'll have a go and see how many will display in portrait mode
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Klaus, sorry to be a pain I've amend your sample program using for i = 1 to 48 and for i = 1 to 24, both of which I would have expected to be seen in portrait, and whilst they show in landscape they don't in portrait, is sthere something in the xChart library that controls the size of the barchart, if so can I amend it and how
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Have you tried 1-5, less, more, just to see where the limit is? Meaning at what point will the graph show up in portrait mode?
 
Upvote 0
Top