Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: RSAbstractBandPlot to abstract bands behavior AND Add horizontal option to RSBoxPlot #584

Merged
merged 13 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Roassal3-Chart-Tests/RSAbstractPlotTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ RSAbstractPlotTest >> testExtentIsCorrect [
self assert: plot extent equals: 250 @ 200
]

{ #category : #tests }
RSAbstractPlotTest >> testInvert [
| xScale yScale |

plot yLinear.
plot xLog.
xScale := plot xScale.
yScale := plot yScale.
plot invertCoordinates.
plot build.
self assert: plot xScale equals: yScale.
self assert: plot yScale equals: xScale.
self assert: plot x equals: y.
self assert: plot y equals: x.
]

{ #category : #tests }
RSAbstractPlotTest >> testMinMaxValue2 [

Expand Down
32 changes: 30 additions & 2 deletions src/Roassal3-Chart-Tests/RSBoxPlotShapeTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ RSBoxPlotShapeTest >> testBoxGraphHasScales [
aScale := NSLinearScale new.
boxGraph := RSBoxPlotShape data: data.
boxGraph scales: {aScale. aScale }.
self assert: boxGraph xScale class equals: NSLinearScale.
self assert: boxGraph yScale class equals: NSLinearScale.
self assert: boxGraph bandScale class equals: NSLinearScale.
self assert: boxGraph dataScale class equals: NSLinearScale.
self assert: (boxGraph scalePoint: aPoint) class equals: Point.
]

Expand Down Expand Up @@ -81,6 +81,34 @@ RSBoxPlotShapeTest >> testDefaultUpperWhisker [
self assert: boxShape upperWhisker border width equals: 1.
]

{ #category : #tests }
RSBoxPlotShapeTest >> testHorizontal [
"| data boxShape xScale yScale canvas boxBefore boxAfter |
data := { 5. 1. 2. 3. 4. }.
canvas := RSCanvas new.
canvas @ RSCanvasController.
xScale := NSScale ordinal
domain: {1};
rangeBands: {0. 360. }.
yScale := NSScale linear
domain: data;
range: {0. canvas extent x.}.
boxShape := RSBoxPlotShape data: data.
boxShape scales: {xScale. yScale}.
boxShape bandOffset: (xScale scale: 1).
boxShape bandWidth: (xScale scale: 1).
boxShape color: Color blue.
boxShape addChildrenToComposite.
boxBefore := boxShape box copy.
boxShape notch: true.
boxShape horizontal.
boxShape renderIn: canvas.
boxAfter := boxShape box copy.
canvas open.
self assert: boxAfter points equals: (boxBefore points collect: [:p| p y@p x])."
"self assert: boxAfter position equals: boxBefore position y@boxBefore position x."
]

{ #category : #tests }
RSBoxPlotShapeTest >> testLowerLimit [
| boxGraph data |
Expand Down
18 changes: 18 additions & 0 deletions src/Roassal3-Chart-Tests/RSScatterPlotTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@ Class {
RSScatterPlotTest >> classToTest [
^ RSScatterPlot
]

{ #category : #tests }
RSScatterPlotTest >> testCreateScatterPlot [
| scatterPlot xScale yScale |
scatterPlot := RSScatterPlot new.
x:= -3.14 to: 3.14 by: 0.1.
y := x sin * 0.22 + 0.5.
scatterPlot x: x y: y.
scatterPlot yLinear.
scatterPlot xLog.
xScale := scatterPlot xScale.
yScale := scatterPlot yScale.
scatterPlot invertCoordinates.
self assert: scatterPlot xScale equals: yScale.
self assert: scatterPlot yScale equals: xScale.
self assert: scatterPlot x equals: y.
self assert: scatterPlot y equals: x.
]
100 changes: 100 additions & 0 deletions src/Roassal3-Chart/RSAbstractBandPlot.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"
`RSAbstractBandPlot` is an abstract kind of plot that will show some graphics inside of bands. In this way a band plot have two main properties: the bands width and the bands offset.

**Responsibility:**
- This class abstracts the common behavior and the concept of the bands.

**Collaborators:**
- **`NSOrdinalScale`:** This class allows rendering each graphic in one band by assigning the `bandWidth` and the `bandOffset`. In this way, each graphic (`RSBoxShape`, `RSViolinPlotShape`) knows which part of the canvas it needs to fill.

**Public API and Key Messages**

**Instance Variables:**

**Example:**

"
Class {
#name : #RSAbstractBandPlot,
#superclass : #RSAbstractPlot,
#instVars : [
'bandScale',
'dataScale',
'bandWidth',
'offset',
'graphics',
'horizontal'
],
#category : #'Roassal3-Chart-Core'
}

{ #category : #'accessing - defaults' }
RSAbstractBandPlot >> bandValues [
^ self subclassResponsibility
]

{ #category : #rendering }
RSAbstractBandPlot >> bandsOffset: aNumberInRange [
offset := aNumberInRange
]

{ #category : #rendering }
RSAbstractBandPlot >> bandsWidth [
^ bandWidth ifNil: [ bandWidth := self defaultBandsWidth ].
]

{ #category : #rendering }
RSAbstractBandPlot >> bandsWidth: aNumber [
bandWidth := aNumber.
]

{ #category : #rendering }
RSAbstractBandPlot >> computeBandsOffset [
| bandValues |
bandValues := horizontal ifFalse: [ xValues ] ifTrue: [ yValues ].
graphics doWithIndex: [ :graphic :idx |
graphic bandOffset: (bandScale scale: (bandValues at: idx)) + offset
].
]

{ #category : #rendering }
RSAbstractBandPlot >> computeBandsWidth [
graphics do: [ :graphic | graphic bandWidth: self bandsWidth ].
]

{ #category : #private }
RSAbstractBandPlot >> computeXAndYValues [
| dataValues bandValues maxDataRelatedValue minDataRelatedValue |
maxDataRelatedValue := (graphics collect: [ :boxShape | boxShape maxYValue ]) max.
minDataRelatedValue := (graphics collect: [ :boxGraph | boxGraph minYValue ]) min.
dataValues := {minDataRelatedValue. maxDataRelatedValue.}.
bandValues := self bandValues.
horizontal
ifTrue: [
xValues := dataValues.
yValues := bandValues ]
ifFalse: [
xValues := bandValues.
yValues := dataValues ]
]

{ #category : #accessing }
RSAbstractBandPlot >> createdShapes [
^ self subclassResponsibility
]

{ #category : #accessing }
RSAbstractBandPlot >> defaultBandsWidth [
^ bandScale scale: 1
]

{ #category : #'accessing - defaults' }
RSAbstractBandPlot >> defaultShape [
^ self subclassResponsibility
]

{ #category : #accessing }
RSAbstractBandPlot >> horizontal [
graphics do: [ :bs | bs horizontal ].
horizontal := true.
]
6 changes: 3 additions & 3 deletions src/Roassal3-Chart/RSAbstractBarPlot.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"
Class {
#name : #RSAbstractBarPlot,
#superclass : #RSAbstractPlot,
#superclass : #RSAbstractBandPlot,
#instVars : [
'barSize',
'bars',
Expand All @@ -30,12 +30,12 @@ RSAbstractBarPlot class >> isAbstract [
^ self == RSAbstractBarPlot
]

{ #category : #accessing }
{ #category : #rendering }
RSAbstractBarPlot >> bandsOffset: aNumber [
self barOffset: aNumber.
]

{ #category : #accessing }
{ #category : #rendering }
RSAbstractBarPlot >> bandsWidth: barWidthInScaleRangeUnits [
self barSize: barWidthInScaleRangeUnits.
]
Expand Down
11 changes: 10 additions & 1 deletion src/Roassal3-Chart/RSAbstractPlot.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ RSAbstractPlot >> color [
^ self shape color
]

{ #category : #'public - shape' }
{ #category : #'accessing - computed' }
RSAbstractPlot >> color: aColor [

self shape color: aColor
Expand Down Expand Up @@ -142,6 +142,15 @@ RSAbstractPlot >> initialize [
shape := self defaultShape
]

{ #category : #rendering }
RSAbstractPlot >> invertCoordinates [
| auxScale |
auxScale := self yScale.
self yScale: self xScale.
self xScale: auxScale.
self x: self y y: self x.
]

{ #category : #testing }
RSAbstractPlot >> isBarPlot [

Expand Down
37 changes: 26 additions & 11 deletions src/Roassal3-Chart/RSBarPlot.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ Class {
#category : #'Roassal3-Chart-Core'
}

{ #category : #examples }
RSBarPlot class >> example01BarPlot [
| p1 data1 x |
data1 := { 10. 9. 7 }.
x := { 1. 2. 3. }.
p1 := self x: x y: data1.
^ p1 open.
]

{ #category : #examples }
RSBarPlot class >> exampleClusterBarPlots [
| p1 p2 chart data1 data2 x |
Expand All @@ -41,7 +50,7 @@ RSBarPlot class >> exampleClusterBarPlots [
x := { 1. 2. 3. }.
p1 := self x: x y: data1.
p2 := self x: x y: data2.
chart := p1 "+ p2".
chart := p1 + p2.
^ chart open.
]

Expand All @@ -55,10 +64,15 @@ RSBarPlot class >> exampleClusterBarPlots4Bars [
p2 := self x: x y: data2.
p3 := self x: x y: data1.
p4 := self x: x y: data2.
chart := p1 "+ p2 + p3 + p4".
chart := p1 + p2 + p3 + p4.
^ chart open.
]

{ #category : #hooks }
RSBarPlot >> bandValues [
^ (1 to: self numberOfBands)
]

{ #category : #accessing }
RSBarPlot >> barScale [
^ xScale
Expand All @@ -67,13 +81,13 @@ RSBarPlot >> barScale [
{ #category : #rendering }
RSBarPlot >> beforeRenderingIn: aChart [

| barScale |
super beforeRenderingIn: aChart.
dataScale := yScale.
xScale class = NSOrdinalScale ifTrue: [ ^ self ].
barScale := NSOrdinalScale new
bandScale := NSOrdinalScale new
domain: xValues;
rangeBands: xScale range padding: gapRatio.
aChart xScale: barScale.
aChart xScale: bandScale.
]

{ #category : #accessing }
Expand All @@ -96,18 +110,23 @@ RSBarPlot >> canHandleCluster [

{ #category : #hooks }
RSBarPlot >> computeRectagleFor: aPoint index: index [
| origin corner sizeOffset offset zero |
| origin corner sizeOffset zero |
zero := 0.
bottom ifNotNil: [ zero := bottom at: index ].
origin := self scalePoint: aPoint + (0@ zero).
corner := origin x @ (yScale scale: zero ).
corner := origin x @ (dataScale scale: zero ).
sizeOffset := (self barSize / 2.0) @ 0.
offset := self barOffset @ 0.
^ Rectangle
origin: origin + offset - sizeOffset
corner: corner + offset + sizeOffset
]

{ #category : #hooks }
RSBarPlot >> computeXAndYValues [
^ self
]

{ #category : #rendering }
RSBarPlot >> definedValuesY [
"Return the list Y values that are defined"
Expand All @@ -129,10 +148,6 @@ RSBarPlot >> modelFor: aPoint [

{ #category : #hooks }
RSBarPlot >> numberOfBands [
| bandScale |
self beforeRenderingIn: chart.
bandScale := NSOrdinalScale new
domain: xValues;
rangeBands: xScale range padding: gapRatio.
^ bandScale range size.
]
Loading
Loading