【问题标题】:GraphET Pharo Smalltalk LabellingGraphET Pharo Smalltalk 标签
【发布时间】:2014-04-18 09:33:29
【问题描述】:

我有这种使用 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 查询中获取一个 PGResult 结果并显示水平条。 一切正常,但我希望在每个栏的左侧使用 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 方法找不到模型。

【问题讨论】:

    标签: smalltalk pharo


    【解决方案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.
    

    【讨论】:

    • 看起来不错。我怎样才能把它放到 Glamour 窗口中?
    • 您能否将此答案标记为正确并为此创建另一个问题。这样就更容易为搜索引擎找到有关“将 GraphET2 可视化放入 Glamour 窗口”的信息。
    【解决方案2】:

    这里有一个例子,我认为它可以满足你的需求。

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

    结果是这样的:

    在 GraphET 中添加这些标签很麻烦,在 GraphET2 中要容易得多。明天我将在 GraphET2 中发布一个示例。

    对你有帮助吗?

    【讨论】:

    • 谢谢你的例子,但我需要 regularAxisAsInteger 标签,当我把它改回来时,名字到处都是。
    • 我可以用 GraphET 做到这一点,但它会非常 hacky。我有一个使用 GraphET2 的更好的解决方案,我会将它作为不同的答案发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多