{{fscript
"It would be nice if we could click on a slice and it takes us to the list page. Unfortunately there is a bug in g.pie.js that causes the hrefs to line up. The array based approach that it uses for arguments is fragile to say the least. It should be reworked to use arrays of dictionaries for arguments."
cats := objectContext objectsForEntityNamed:'Category' qualifierLocum:'active == 1 and forContact == 1' bindings:nil.
all := {}.
others := {}.
cats do:[:cat |
v := objectContext resultCountForEntityNamed:'Contact' qualifierLocum:'categoryID == $catID' bindings:#{'catID' -> (cat categoryID)}.
clr := ((cat color) mcrHTMLRepresentation).
(clr == nil) ifTrue: [
clr := '#000'.
].
dict := #{'name' -> (cat name), 'cID' -> (cat categoryID), 'value' -> v, 'label' -> ('%% - '++ (cat name)), 'color' -> clr}.
all add:dict.
].
sorted := all sortedArrayUsingDescriptors:{(NSSortDescriptor sortDescriptorWithKey:'value' ascending:false)}.
(sorted count > 10) ifTrue: [
top := sorted at:{0,1,2,3,4,5,6,7,8,9}.
others := sorted difference:top.
"we have to resort others..."
others := (others sortedArrayUsingDescriptors:{(NSSortDescriptor sortDescriptorWithKey:'value' ascending:false)}).
]
ifFalse: [
top := sorted.
].
globals setTop:top.
globals setOthers:others.
"now we iterate the top and morph it to work with the chart"
labels := {}.
colors := {}.
values := {}.
all do:[:dict |
values add:(dict valueForKey:'value').
labels add:(dict valueForKey:'label').
colors add:(dict valueForKey:'color').
].
globals setLabels:labels JSONRepresentation.
globals setValues:values JSONRepresentation.
globals setColors:colors JSONRepresentation.
}}