Skip to content

Commit

Permalink
[feenkcom/gtoolkit#3775] added examples for vertical and horizontal b…
Browse files Browse the repository at this point in the history
…ar lines
  • Loading branch information
akevalion committed May 15, 2024
1 parent 92a6d24 commit 9cebe71
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 69 deletions.
6 changes: 6 additions & 0 deletions src/GToolkit-Plotter/GtPlotterBarChart.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ GtPlotterBarChart >> initialize [

]

{ #category : #accessing }
GtPlotterBarChart >> lines [
<return: #OrderedCollection>
^ lines
]

{ #category : #accessing }
GtPlotterBarChart >> scalableContents [
hasScalableContents := true
Expand Down
57 changes: 57 additions & 0 deletions src/GToolkit-Plotter/GtPlotterBarChartElement.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Class {
#name : #GtPlotterBarChartElement,
#superclass : #BlElement,
#instVars : [
'tickLabels',
'bars',
'lines'
],
#category : #'GToolkit-Plotter-Builder - Bar Chart'
}

{ #category : #accessing }
GtPlotterBarChartElement >> barsElement [
<return: #BlElement>
^ bars
]

{ #category : #accessing }
GtPlotterBarChartElement >> barsElement: anBlElement [
bars := anBlElement.
self addChild: bars
]

{ #category : #initialization }
GtPlotterBarChartElement >> initialize [
super initialize.
self
clipChildren: false;
padding: (BlInsets all: 10);
constraintsDo: [ :c |
c horizontal matchParent.
c vertical matchParent ].
]

{ #category : #accessing }
GtPlotterBarChartElement >> lines [
<return: #OrderedCollection>
^ lines
]

{ #category : #accessing }
GtPlotterBarChartElement >> lines: aCollectionOfLineElements [
lines := aCollectionOfLineElements.
self addChildren: lines.
]

{ #category : #accessing }
GtPlotterBarChartElement >> tickLabelsElement [
<return: #GtPlotterTickLabelsElement>
^ tickLabels
]

{ #category : #accessing }
GtPlotterBarChartElement >> tickLabelsElement: aGtPlotterTickLabelsElement [
tickLabels := aGtPlotterTickLabelsElement.
self addChild: tickLabels.
]
44 changes: 44 additions & 0 deletions src/GToolkit-Plotter/GtPlotterBarLineExamples.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Class {
#name : #GtPlotterBarLineExamples,
#superclass : #Object,
#category : #'GToolkit-Plotter-Example - Bar Lines'
}

{ #category : #accessing }
GtPlotterBarLineExamples >> barChartLineStyle [
| barChart border chartElement |
barChart := self thresholdChartScrollableContentsAddsOneLine.
border := BlBorderBuilder new
paint: Color blue;
dashed;
capRound;
width: 2;
build.
barChart lines first
lineStencil: [ BlLineElement new border: border ].
chartElement := self createChartElement: barChart.
self assert: chartElement lines first border equals: border.
^ chartElement
]

{ #category : #accessing }
GtPlotterBarLineExamples >> barChartScalableContents [
| barChart |
barChart := self thresholdChartScrollableContentsAddsOneLine.
barChart scalableContents.
^ barChart
]

{ #category : #accessing }
GtPlotterBarLineExamples >> createChartElement: barChart [
| chartElement |
chartElement := barChart create.
chartElement size: 300 asPoint.
chartElement forceLayout.
^ chartElement.
]

{ #category : #accessing }
GtPlotterBarLineExamples >> thresholdChartScrollableContentsAddsOneLine [
^ self subclassResponsibility
]
62 changes: 49 additions & 13 deletions src/GToolkit-Plotter/GtPlotterHorizontalBarChart.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ Class {
#category : #'GToolkit-Plotter-Builder - Bar Chart'
}

{ #category : #'instance - creation' }
GtPlotterHorizontalBarChart class >> newFromValues: aCollectionOfObjects [
^ self new
with: (GtPlotterDataGroup new values: aCollectionOfObjects);
barWidthScale: (GtPlotterLinearScale new
domainFrom: 0 to: aCollectionOfObjects max;
yourself);
yourself
]

{ #category : #'api - bars' }
GtPlotterHorizontalBarChart >> barGroupStencil: aGtPlotterHorizontalBarGroupStencilBuilder [
"[ :aGtPlotterBar :aGtPlotterDataEntity | ]"
Expand Down Expand Up @@ -86,15 +96,11 @@ GtPlotterHorizontalBarChart >> create [
<return: #Object>
| aContainer theBars |

aContainer := BlElement new
aContainer := GtPlotterBarChartElement new
layout: BlLinearLayout vertical;
clipChildren: false;
padding: (BlInsets right: 20);
constraintsDo: [ :c |
c horizontal matchParent.
c vertical matchParent ].
yourself.

aContainer addChild: (GtPlotterHorizontalTickLabelsElement new
aContainer tickLabelsElement: (GtPlotterHorizontalTickLabelsElement new
clipChildren: false;
scale: barWidthScale;
padding: self chartPadding;
Expand All @@ -106,13 +112,10 @@ GtPlotterHorizontalBarChart >> create [
text: (aGtPlotterScaleContext scale formattedTick: aGtPlotterScaleContext originalValue) ];
labels: self amountOfTicks).

theBars := GtPlotterSingleScaleScrollElement new.
theBars stencil: [ :eachGroup |
self createBarRowFor: eachGroup grouping: data isLogicalGroup ].
theBars values: data.
theBars := self createBars.

aContainer addChild: theBars.
aContainer addChildren: (lines collect: [ :line |
aContainer barsElement: theBars.
aContainer lines: (lines collect: [ :line |
line
barChart: self;
parent: theBars.
Expand Down Expand Up @@ -150,6 +153,39 @@ GtPlotterHorizontalBarChart >> createBarRowFor: aGtPlotterDataEntity grouping: s
^ aContainer
]

{ #category : #'api - instantiation' }
GtPlotterHorizontalBarChart >> createBars [
| theBars |
self hasScalableContents ifTrue: [
| totalSize |
theBars := BrVerticalPane new.
theBars matchParent.
totalSize := 0.
data do: [:eachGroup | totalSize := eachGroup size ].
theBars cellSpacing: (totalSize = data size
ifTrue: [ self barGap]
ifFalse: [ 0 ]).

data do: [:eachGroup |
| barElement |
barElement := self
createBarRowFor: eachGroup
grouping: data isLogicalGroup.
barElement constraintsDo: [:c |
c horizontal matchParent.
c linear weight: eachGroup size / totalSize.
].
theBars addChild: barElement
].
^ theBars.
].
theBars := GtPlotterSingleScaleScrollElement new.
theBars stencil: [ :eachGroup |
self createBarRowFor: eachGroup grouping: data isLogicalGroup ].
theBars values: data.
^ theBars
]

{ #category : #'private - instance creation' }
GtPlotterHorizontalBarChart >> createGroupBarsFor: aGtPlotterDataEntity [
| isStacking theBars aContainer |
Expand Down
5 changes: 2 additions & 3 deletions src/GToolkit-Plotter/GtPlotterHorizontalLine.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ GtPlotterHorizontalLine >> setPercent: aNumber in: aBlPercentSideAnchor [

{ #category : #accessing }
GtPlotterHorizontalLine >> transformRectangle: aRectangle [
| e corner |
e := parent list children first computeHorizontalScrollExtent.
corner := aRectangle corner.
| e |
e := parent list children first computeHorizontalScrollRange.
^ Rectangle
origin: aRectangle origin
extent: (aRectangle width min: e) @ aRectangle height
Expand Down
93 changes: 93 additions & 0 deletions src/GToolkit-Plotter/GtPlotterHorizontalLineExamples.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"
GtPlotterVerticalBarChart utilizes rectangles to represent bars. However, the chart also has the capability to generate horizontal lines for displaying minimum, maximum, average, and threshold values. To create a horizontal line instance for such purposes, utilize the method `chart newHorizontalLine`, which generates and returns a new instance of GtPlotterHorizontalLine.
"
Class {
#name : #GtPlotterHorizontalLineExamples,
#superclass : #GtPlotterBarLineExamples,
#category : #'GToolkit-Plotter-Example - Bar Lines'
}

{ #category : #examples }
GtPlotterHorizontalLineExamples >> barChartForNumbers [
<gtExample>
| barChart |
barChart := GtPlotterVerticalBarChart newFromValues: #(8 10 15 9 12).
self assert: barChart lines size = 0.
^ barChart
]

{ #category : #examples }
GtPlotterHorizontalLineExamples >> barChartLineStyle [
<gtExample>
^ super barChartLineStyle
]

{ #category : #examples }
GtPlotterHorizontalLineExamples >> barChartScalableContentsPercentMax [
<gtExample>
| barChart chartElement max |
barChart := self barChartScalableContents.
max := 0.5.
barChart lines first
min: 0;
max: max.
chartElement := self createChartElement: barChart.
self assert: chartElement lines isNotEmpty.
self
assert: chartElement lines first width
equals: chartElement barsElement width * max.
^ chartElement
]

{ #category : #examples }
GtPlotterHorizontalLineExamples >> barChartScalableContentsPercentMin [
<gtExample>
| barChart chartElement min |
barChart := self barChartScalableContents.
min := 0.5.
barChart lines first
min: min;
max: 1.
chartElement := self createChartElement: barChart.
self assert: chartElement lines isNotEmpty.
self
assert: chartElement lines first width
equals: chartElement barsElement width * min.
^ chartElement
]

{ #category : #examples }
GtPlotterHorizontalLineExamples >> barChartScalableContentsShouldExpandLine [
<gtExample>
| barChart chartElement |
barChart := self barChartScalableContents.
chartElement := self createChartElement: barChart.
self assert: chartElement lines isNotEmpty.
self
assert: chartElement lines first width
equals: chartElement barsElement width.
^ chartElement
]

{ #category : #accessing }
GtPlotterHorizontalLineExamples >> barChartScrollabelContentsShouldNotPassBarsExtent [
<gtExample>
| barChart barElement width |
barChart := self thresholdChartScrollableContentsAddsOneLine.
barElement := self createChartElement: barChart.
width := barElement lines first width.
self assert: width < barElement width.
^ barElement
]

{ #category : #examples }
GtPlotterHorizontalLineExamples >> thresholdChartScrollableContentsAddsOneLine [
<gtExample>
| barChart threshold |
barChart := self barChartForNumbers.
threshold := 10.
barChart newHorizontalLine dataValue: threshold.
self assert: barChart lines size equals: 1.
^ barChart
]
44 changes: 19 additions & 25 deletions src/GToolkit-Plotter/GtPlotterVerticalBarChart.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,27 @@ GtPlotterVerticalBarChart >> chartPadding [
GtPlotterVerticalBarChart >> create [
<return: #Object>
| aContainer theBars |
aContainer := BlElement new
layout: BlLinearLayout horizontal;
clipChildren: false;
padding: (BlInsets right: 20);
constraintsDo: [ :c |
c horizontal matchParent.
c vertical matchParent ].

aContainer
addChild: (GtPlotterVerticalTickLabelsElement new
clipChildren: false;
inverse;
scale: barHeightScale;
padding: self chartPadding;
constraintsDo: [ :c | c horizontal fitContent ];
labelFormat: self tickLabelFormat;
labelElement: [ :aGtPlotterScaleContext |
BrLabel new
aptitude: (self tickLabelLook create foreground: self tickLabelColor);
text: (aGtPlotterScaleContext scale
formattedTick: aGtPlotterScaleContext originalValue) ];
labels: self amountOfTicks).
aContainer := GtPlotterBarChartElement new
layout: BlLinearLayout horizontal;
yourself.
aContainer tickLabelsElement: (GtPlotterVerticalTickLabelsElement new
clipChildren: false;
inverse;
scale: barHeightScale;
padding: self chartPadding;
constraintsDo: [ :c | c horizontal fitContent ];
labelFormat: self tickLabelFormat;
labelElement: [ :aGtPlotterScaleContext |
BrLabel new
aptitude: (self tickLabelLook create
foreground: self tickLabelColor);
text: (aGtPlotterScaleContext scale formattedTick: aGtPlotterScaleContext originalValue) ];
labels: self amountOfTicks).

theBars := self createBars.
aContainer addChild: theBars.

aContainer addChildren: (lines collect: [ :line |
aContainer barsElement: theBars.
aContainer lines: (lines collect: [ :line |
line
barChart: self;
parent: theBars.
Expand Down
Loading

0 comments on commit 9cebe71

Please sign in to comment.