2014-04-18 31 views
0

我有這種方法,其顯示使用GraphET的圖表:GraphET菲羅Smalltalk的標籤

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. 

該方法採用從SQL查詢aPGResult結果並顯示水平條。 這一切工作正常,但我想標籤使用每個欄左邊的OrderedCollection名稱。我嘗試使用下面的代碼在此論壇上看到:

values do: [ :value | 
| bar label | 
label := ROLabel elementOn: value asString. 
diagram rawView add: label. 
bar := diagram rawView elementFromModel: value. 
ROConstraint move: label onTheLeftOf: bar ]. 

但它給出錯誤:接收方的位置是零。這意味着elementFromModel方法找不到模型。

回答

1

使用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

+0

這看起來不錯。我怎樣才能把它變成一個魅力窗口? –

+0

您能否將此答案標記爲正確,併爲此創建另一個問題。通過這種方式,搜索引擎可以更容易地找到「將GraphET2可視化變爲魅力窗口」。 –

0

在這裏你有一個例子,我認爲,做你想做的。

|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: [email protected]) open. 

結果是這樣的: Top 5 Soccer Scorers example

這是很麻煩的GraphET添加這些標籤,在GraphET2是相當容易。我將在明天發佈一個GraphET2的例子。

它對您有幫助嗎?

+0

感謝那些在你的榜樣工作,但我需要regularAxisAsInteger標籤,當我改變了它後面的名字出現所有的地方。 –

+0

我可以用GraphET得到它,但它會很黑。我有一個更好的解決方案使用GraphET2,我會發布它作爲一個不同的答案。 –

+0

當我使用上面它顯示一個空白ROASAL頁面 –