Redrawing Pie chart

CharlesR

Member
Licensed User
Longtime User
Hi

I have used the charts library to add a pie chart to my app, but I am unable to force it to redraw itself when 1 or more of the inputs is changed. I know that my understanding of the modern views etc is weak and I would be really grateful if someone could give me the code snippets to make the pie chart redraw.

Many thanks

Charles
 

CharlesR

Member
Licensed User
Longtime User
Thank you Erel.

I now have the pie redrawing but on another tab. How do I check that the tab already exists and if so redraws the current pie rather than adding another tab.

Many thanks

Charles
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Thank you very much Erel.

I am sorry to be a such a newbie on this but I have 1 more question.

How do I refresh the legend before writing the second time, at the moment it is over written without being clear.

Many thanks

Charles
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
I would be grateful for some code to show the "correct" way of implementing the procedure please Erel.

Also, I now have the ability to show more than 1 piechart/tab which is useful if the operator wants to compare the effect of changing a parameter in the program, but nowhere in the documentation can I find the code for removing a tab from TabHost meaning that I end up with several tabs. Please could you give me the command to remove a tab from TabHost.

Thanks

Charles
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Thank you Klaus. The code below is the relevant sub which will slot straight into Erel's Charts package. I have set some typical values for the pie and used the "If initialised" to change a value to show the problem on the legend.

Many thanks for your help.

Charles


Sub btnCreatePie_Click
Dim p As Panel
Dim chartnum As Int
Dim Chartname As String
If pnlPie.IsInitialized = False Then
p.Initialize("")
pnlPie.Initialize("pnlPie")
p.AddView(pnlPie, 0, 0, 100%x, 100%y - 100dip)
chartnum = TabHost1.TabCount - 6
chartname = "Chart " & chartnum
TabHost1.AddTab2(chartname, p)
piepool = 200000 'poolwaste
Else
piepool = 250000
End If
Dim PD As PieData
'piepool = poolwaste
'If chkcover.Checked Then
'piecover = 0
'Else
'piecover = coversaving
'End If
piepump = 50000 '(pumpsaving + smallpumpsaving)
piepipe = 20000 'annualpipeloss
pieboiler = 0
pieahu = 30000 '(supplysaving + extractsaving)
pietotal = (piepool + piecover + piepipe + pieboiler + pieahu + piepump)
PD.Initialize
PD.Target = pnlPie 'Set the target view
'Add the items. The last parameter is the color. Passing 0 will make it a random color
Charts.AddPieItem(pd, "Pool hall ventilation " & Round2((100*(piepool/pietotal)),1) & " %", piepool, Colors.Blue)
Charts.AddPieItem(pd, "Pool cover " & Round2((100*(piecover/pietotal)),1) & " %",piecover, Colors.DarkGray)
Charts.AddPieItem(pd, "Pumps " & Round2((100*(piepump/pietotal)),1) & " %", piepump, Colors.Green)
Charts.AddPieItem(pd, "Pipes " & Round2((100*(piepipe/pietotal)),1) & " %", piepipe, Colors.Red)
Charts.AddPieItem(pd, "AHU's " & Round2((100*(pieahu/pietotal)),1) & " %", pieahu, Colors.Magenta)
PD.GapDegrees = 20 'Total size of gaps between slices. Set to 0 for no gaps.
PD.LegendBackColor = Colors.ARGB(50, 100, 100, 100) 'The background color of the legend bitmap.
Dim legend As Bitmap
'This call draws the pie.
'PD - The pie data
'Colors.Gray - The view's background color
'True - Create a legend bitmap.
legend = Charts.DrawPie(PD, Colors.White, True)
Dim ImageView1 As ImageView
ImageView1.Initialize("")
ImageView1.SetBackgroundImage(legend)
pnlPie.AddView(ImageView1, 10dip, 10dip, legend.Width, legend.Height)
TabHost1.CurrentTab = (TabHost1.CurrentTab + Chartnum + 1)
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Don't you have at least a small project as a zip file that shows the problem so we could directly test it and give you a concrete advice.
Otherwise we need to write a new project incorporating your partial code redefine variables, trying to find out what you want to do and how. Often the problem not in the small code snippet but somewhere else.

Best regards.
 
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Hi Klaus

I am sorry, I underestimated the work involved in dropping the sub routine into a project! The attached zip shows the pie being called twice with 2 values for pool hall ventilation which show the overwriting in the legend.

Many thanks for your help.

Bets Regards

Charles
 

Attachments

  • PieChartdemo.zip
    9.4 KB · Views: 292
Upvote 0

klaus

Expert
Licensed User
Longtime User
Hi Charles,
The problem is the following:
- every time you run CreatePieTab you add a new ImageView
- and as the PD.LegendBackground is partially transparent it shows also the text of the previous Imageview.

The solution is to remove the previous ImageView.
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff]
[SIZE=2][FONT=Courier New][COLOR=#0000ff][SIZE=2][FONT=Courier New][COLOR=#0000ff]Sub [/COLOR][/FONT][/SIZE][/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]CreatePieTab[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] p [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b]Panel[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] chartnum [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b]Int[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Chartname [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b][FONT=Courier New][SIZE=2][COLOR=#008b8b]String[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] pnlPie.IsInitialized = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]False [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]    p.Initialize([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]    pnlPie.Initialize([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"pnlPie"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]    p.AddView(pnlPie, [/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]100%x[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]100%y[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] - [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]100dip[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]    TabHost1.AddTab2([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Pie Chart"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], p)[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]    piepool = [/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]200000[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]'poolwaste[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  Else[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]    [COLOR=red]pnlPie.RemoveViewAt([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=red]0[/COLOR][/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=red])[/COLOR][/SIZE][/FONT]
[SIZE=2][FONT=Courier New]    piepool = piepool + [/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]50000[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
Attached your modified test program.

Best regards.
 

Attachments

  • PieChartdemo1.zip
    8.9 KB · Views: 292
Upvote 0

CharlesR

Member
Licensed User
Longtime User
Once again I am very grateful for your help Klaus! I have now got the hang of most of B4A, but I am afraid views may take a little longer!

Many thanks

Charles
 
Upvote 0
Top