BarPlot #4 - kendmaclean/pharo12 GitHub Wiki

Bar Chart with RSBarPlot using Associations in Spec2; but rather than creating your own RSCanvas, access it from the RSBarPlot bars and container when you create and open it all by itself.



| data barPlot canvas |
"Create sample data - array of associations"
data := {
    'Apples' -> 120.
    'Oranges' -> 85.
    'Bananas' -> 97.
    'Grapes' -> 65.
    'Pears' -> 72.
}.

"Create the bar plot"
barPlot := RSBarPlot new
    y: (data collect: #value);
    xTickLabels: (data collect: #key); 
    yourself.

barPlot open.

allElements := barPlot bars select: [:each | 
		each model notNil and: [ 
			each class = RSBox]].
	
allElements doWithIndex: [:bar :index |
    | label value |
    value := (data at: index) value.
    label := RSLabel new
        text: value asString;
        color: Color black;
        position: bar position x @ (bar position y - bar height / 2 - 10);
        yourself.
    barPlot container add: label.
].