Skip to content

Commit

Permalink
Merge pull request #58 from jordanmontt/feature-add-relateive-y-axis-…
Browse files Browse the repository at this point in the history
…data

Feature add relateive y axis label values
  • Loading branch information
jecisc authored Mar 5, 2024
2 parents 8698565 + 800e775 commit 5d00570
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/Roassal-Chart-Examples/RSHistogramExample.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@ RSHistogramExample >> example10Objects [
^ c canvas
]

{ #category : 'examples' }
RSHistogramExample >> example11RelativeYAxisLabels [

<script: 'self new example11RelativeYAxisLabels open'>
| data plot |
"The 0 element appears 14 times: 70%"
"The 2 element appears 2 times: 10%"
"The 10 element appears 4 times: 20%"
data := #( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 10 10 10 10 ).

plot := RSHistogramPlot new
x: data;
relativeVerticalTicks;
yourself.
"Add a label"
plot ylabel: 'Percent'.

plot yTicksRangeMax: 100 numberOfTicks: 6.

^ plot
]

{ #category : 'computing' }
RSHistogramExample >> normal: x mean: mean stdDev: std [
^ (-0.5 * Float twoPi log - std log -
Expand Down
36 changes: 34 additions & 2 deletions src/Roassal-Chart/RSHistogramPlot.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Class {
'x',
'bins',
'bars',
'binningStrategy'
'binningStrategy',
'isRelativeVerticalTicks'
],
#category : 'Roassal-Chart-Core',
#package : 'Roassal-Chart',
Expand All @@ -52,6 +53,13 @@ RSHistogramPlot class >> of: aCollection on: aBloc [
^ self new collectDataOf: aCollection on: aBloc
]

{ #category : 'accessing' }
RSHistogramPlot >> absoluteVerticalTicks [

isRelativeVerticalTicks := false.
self verticalTick labelConversion: [ :labelValue | labelValue ]
]

{ #category : 'accessing' }
RSHistogramPlot >> bars [
^ bars
Expand Down Expand Up @@ -139,8 +147,10 @@ RSHistogramPlot >> definedValuesY [

{ #category : 'initialization' }
RSHistogramPlot >> initialize [

super initialize.
self binningStrategy: RSDefaultBinning new
self binningStrategy: RSDefaultBinning new.
isRelativeVerticalTicks := false
]

{ #category : 'accessing' }
Expand Down Expand Up @@ -179,6 +189,16 @@ RSHistogramPlot >> numberOfBins: aNumber [
self computeXYValues
]

{ #category : 'accessing' }
RSHistogramPlot >> relativeVerticalTicks [

"Adjust the vertical ticks label to show the relative labels. E.g. instead of showing that a bin appeared 17 times it will show 50%"

isRelativeVerticalTicks := true.
self verticalTick
labelConversion: [ :labelValue | (labelValue / x size) * 100 asInteger ]
]

{ #category : 'rendering' }
RSHistogramPlot >> renderIn: canvas [

Expand Down Expand Up @@ -209,3 +229,15 @@ RSHistogramPlot >> x: aCollection [
x := aCollection sorted.
self computeXYValues
]

{ #category : 'accessing' }
RSHistogramPlot >> yTicksRangeMax: max numberOfTicks: numberOfTicks [

| relativeMax |
relativeMax := isRelativeVerticalTicks
ifFalse: [ max ]
ifTrue: [ x size * (max / 100) ].

self verticalTick ticksData:
((0 to: relativeMax count: numberOfTicks) collect: #asInteger)
]

0 comments on commit 5d00570

Please sign in to comment.