Bar Plot #3 - kendmaclean/pharo12 GitHub Wiki

Bar Chart with RSBarPlot using Associations in Spec2; but rather than creating your own RSCanvas, access it from the RSBarPlot container attribute 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.

canvas := barPlot container.

allElements := canvas shapes 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.
    canvas add: label.
].