Reputation: 54
I have this method which displays a chart using Graph-ET:
displayChart: aPGResult
"Takes result of SQL query and calculates duration from activityend MINUS activitystart and draws bars of duration length (in days)"
| diagram values names |
values := OrderedCollection new.
names := OrderedCollection new.
aPGResult rows do: [:row | | data duration actStart actEnd a b monthA monthB job |
data := row rawData.
a := data at: 2.
b := data at: 3.
job := data at: 1.
monthA := (a copyFrom: 6 to: 7) asInteger.
monthB := (b copyFrom: 6 to: 7) asInteger.
actStart := Date newDay: ((a copyFrom: 9 to: 10) asInteger) month: (months at: monthA) year: ((a copyFrom: 1 to: 4) asInteger).
actEnd := Date newDay: ((b copyFrom: 9 to: 10) asInteger) month: (months at: monthB) year: ((b copyFrom: 1 to: 4) asInteger).
duration:= actEnd subtractDate: actStart.
duration = 0 ifTrue: [ duration := 1 ].
values add: duration.
names add: job ].
diagram := GETDiagramBuilder new.
diagram horizontalBarDiagram
models: values;
barWidth: 15;
width: 500;
color: Color blue;
regularAxisAsInteger;
xAxisLabel: 'Days';
yAxisLabel: 'Activity';
spacing: 2;
titleLabel: 'My Chart'.
diagram interaction popUpText.
^diagram open.
The method takes aPGResult result from a SQL query and displays horizontal bars. That all works fine but I want labels using OrderedCollection names on the left of each bar. I tried using the code below as seen on this forum:
values do: [ :value |
| bar label |
label := ROLabel elementOn: value asString.
diagram rawView add: label.
bar := diagram rawView elementFromModel: value.
ROConstraint move: label onTheLeftOf: bar ].
But it gives error: Receiver of position is nil. This means that elementFromModel method cannot find the model.
Upvotes: 0
Views: 358
Reputation: 81
Using GraphET2
|builder players scores |
" fake data "
players := #( #Messi #CristianoRonaldo #LuisSuarez #AlexisSanchez #ZlatanIbrahimovic).
scores := players collect: [ :p | {p . (35 atRandom)} ].
scores sort: [ :a :b| a second > b second ].
builder := GET2HorizontalBar data: scores.
builder x: #second;
color: Color blue;
barWidth: 15;
title: 'Top 5 - Soccer Scorers';
width: 500.
builder xAxis formatInteger; title: 'Scores'.
builder yAxis addModelLabels:[:p| p first ]; title: 'Player'.
builder open.
Upvotes: 1
Reputation: 81
Here you have an example that, i think, does what you want.
|diagram players scores view|
" fake data "
players := #( #Messi #CristianoRonaldo #LuisSuarez #AlexisSanchez #ZlatanIbrahimovic).
scores := (players collect: [ :p | p -> (35 atRandom) ]) asDictionary.
view := ROView new.
diagram := GETDiagramBuilder new.
diagram horizontalBarDiagram
models: (scores values sort: [:a :b | a > b]);
barWidth: 15;
width: 500;
color: Color blue;
" regularAxisAsInteger; <-- replaced by the two following lines"
baseAxisLine;
valueAxisLine;
xAxisLabel: 'Scores';
yAxisLabel: 'Players';
spacing: 2;
titleLabel: 'Top 5 - Soccer Scorers'.
diagram interaction popUpText.
" We need to generate the graphic elements to customize it "
diagram openIn: view.
" Now graphic elements do exist, lets add the labels"
scores keysAndValuesDo: [ :player :goals |
| bar label |
label := ROLabel elementOn: player asString.
diagram rawView add: label.
bar := diagram rawView elementFromModel: goals.
ROConstraint move: label onTheLeftOf: bar ].
" Small display tweak due to Ibrahimociv is a long lastname, and open it"
(view translateBy: 120@0) open.
The result is this:
It is cumbersome to add those labels in GraphET, in GraphET2 is quite easier. I will publish an example in GraphET2 tomorrow.
Does it help you?
Upvotes: 0