Search code examples
smalltalkpharo

GraphET Pharo Smalltalk Labelling


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.


Solution

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

    Result