bar plot #2 - kendmaclean/pharo12 GitHub Wiki

"Bar Chart with RSBarPlot using Associations in Spec2"

| window allElements |

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

canvas := RSCanvas new.

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

"Configure the chart and add to canvas"
chart := RSCompositeChart new
    container: canvas;
    add: barPlot;
    yourself.

"Setup chart and build it"
chart
    extent: 500@400;
    build.

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.
].

"Setup interaction"
canvas @ RSCanvasController.

"10. Open in a window" canvas open setLabel: 'Clickable Bar Chart'.