The OnCreate handler
The OnCreate handler is sent to a widget when it is first created by LiveCode.
This handler can be used to initialise default data and, where applicable, reduce the burden for calculating constants etc in the OnPaint handler.
Add the OnCreate handler to the LCB file
- Define a variable, tSectors, to hold the number of sectors
- Call setValues to set the default values of the mValues variable.
- Call setLabels to set the default values of the mLabels variable
- Set the value of mShowLabels to true
- Update tSectors with the number of elements in the list of values
- Call colorList to intitialise the mColors variabler
Setting the property values in the OnCreate handle ensures the values of the properties are set when we test from the Extension Builder.
widget community.livecode.elanorb.piechart ...previous code public handler OnCreate()   variable tSectors   setValues("1,2,3,5,8,13")   setLabels("Jan,Feb,Mar,Apr,May,Jun")   put true into mShowLabels   put the number of elements in mValues into tSectors   put colorList(tSectors) into mColors end handler end widget
LiveCode Builder Colors
LiveCode Builder colors are expressions which evaluates to a list of 3 or 4 numbers, the red, green, blue, and (optional) alpha components of the color.
The component value denotes the intensity of that component, expressed as a real number between 0 and 1. The alpha component represents the opacity of the color. If the alpha component is not specified then it is assumed to be 1 (fully opaque).
variable tColor -- Set tColor to opaque red put color [1.0, 0.0, 0.0] into tColor -- Set tColor to partially transparent cyan put color [0.0, 1.0, 1.0, 0.75] into tColor
The colorList handler
The colorList handler returns a list of unique colors for use when coloring the sectors of the chart. The number of colors to be returned is given by the pNumber parameter.
The handler loops calculating RGB values for colors.
- Highest intensity primary colors
- Highest intensity secondary colors
- Reduce the color intensity by half
Each iteration adds 6 colors to the list.
- R,0,0,1
- 0,G,0,1
- 0,0,B,1
- R,G,0,1
- R,0,B,1
- 0,G,B,1
Add the colorList handler to the source file.
- Declare the variables that will be used.
- Assign a value to tColorLevel: the color intensity to be used.
- Calculate the number of repeats
- Divide pNumber by 6, 6 colors are add to the color list on each iteration of the loop
- Round the result
- Add 1 to the result to ensure we always calculate enough colors
public handler colorList(in pNumber) returns List   variable tColors as List     variable tColorLevel as Number   variable tRepeats as Number   put 1 into tColorLevel   put pNumber / 6 into tRepeats   round tRepeats   add 1 to tRepeats  … continues on next slide end handler
- Repeat tRepeats times
- Push 6 colors to the tColors list
- tColorLevel,0,0,1
- 0,tColorLevel,0,1
- 0,0,tColorLevel,1
- tColorLevel,tColorLevel,0,1
- tColorLevel,0,tColorLevel,1
- 0,tColorLevel,tColorLevel,1
- Divide tColorLevel by 2
- Push 6 colors to the tColors list
- Return the list of colors
public handler colorList(in pNumber) returns List … previous code   repeat tRepeats times        push color [tColorLevel,0,0] onto tColors       push color [0,tColorLevel,0] onto tColors       push color [0,0,tColorLevel] onto tColors       push color [tColorLevel,tColorLevel,0] onto tColors       push color [tColorLevel,0,tColorLevel] onto tColors       push color [0,tColorLevel,tColorLevel] onto tColors       divide tColorLevel by 2   end repeat   return tColors end handler