GraphET Pharo Smalltalk标签

GraphET Pharo Smalltalk标签,smalltalk,pharo,Smalltalk,Pharo,我有一种使用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 := Or

我有一种使用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.
该方法从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方法无法找到该模型。

这里有一个示例,我认为它可以满足您的需要

|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.
|图表视图|
“假数据”
球员:=#(#梅西#克里斯蒂亚诺罗纳尔多#路易斯萨雷斯#阿列克谢桑切斯#兹拉塔尼布拉希莫维奇)。
分数:=(玩家收集:[:p | p->(35 atRandom)])作为字典。
视图:=ROView new。
图表:=GETDiagramBuilder新建。
水平图解
模型:(分数值排序:[:a:b | a>b]);
条宽:15;
宽度:500;
颜色:蓝色;
“RegularAxisInteger;使用图表2

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

谢谢你的例子,但我需要RegularAxisaInteger标签,当我把它改回来时,名字到处都是。我可以用GraphET得到这个,但它会非常粗糙。我有一个更好的解决方案,使用GraphET2,我会将它发布为不同的答案。这看起来不错。我怎样才能把它放到魅力窗口中?请您将此答案标记为正确,并为此创建另一个问题。通过这种方式,您将更容易找到搜索引擎的“将图形2可视化到魅力窗口”。