Skip to content

Commit

Permalink
Adds bar chart
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeAtHPI committed Oct 18, 2023
1 parent 21dd5b0 commit ebb4b1c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/Sandblocks-Core/Collection.extension.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
Extension { #name : #Collection }

{ #category : #'*Sandblocks-Core-converting' }
Collection >> asBarChart: converter [
<convert>

converter
if: [self isString not and: [self isDictionary not and: [self allSatisfy: SBBarChart supportedInterface]]]
do: [SBBarChart newWithValues: self]
]

{ #category : #'*Sandblocks-Core-converting' }
Collection >> asCollectionView: converter [
<convert>
Expand Down
4 changes: 2 additions & 2 deletions packages/Sandblocks-Watch/SBAxisNotation.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ SBAxisNotation >> visualize [
self submorphs copy do: #abandon.

self addAllMorphsBack: (
self relativeTickHeights do: [:aFraction | | annotation |
self relativeTickHeights collect: [:aFraction | | annotation |
annotation := SBOwnTextMorph new
contents: ((self scale domainValueOfRelative: aFraction) asFloat asString);
verySmall;
Expand All @@ -99,7 +99,7 @@ SBAxisNotation >> visualize [
(aFraction = 1) ifTrue: [annotation layoutFrame topOffset: 0].
(aFraction = 0) ifTrue: [annotation layoutFrame topOffset: (-1*(annotation minExtent y))].

^ annotation])
annotation])


]
67 changes: 67 additions & 0 deletions packages/Sandblocks-Watch/SBBarChart.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Class {
#name : #SBBarChart,
#superclass : #SBLineChart,
#category : #'Sandblocks-Watch'
}

{ #category : #'initialize-release' }
SBBarChart class >> newWithValues: traceValues [

| valuesToVisualize |
valuesToVisualize := traceValues
ifEmpty: [#(0)]
ifNotEmpty: [traceValues].
^ self new
traceValues: valuesToVisualize;
scaleY: (SBScale
newLinearScaleWithDomain: (({valuesToVisualize min. 0} min) to: valuesToVisualize max)
forRange: (0 to: self canvasHeight));
yourself
]

{ #category : #'visualization - constants' }
SBBarChart >> barWidth [

^ self spaceBetweenPoints / 3
]

{ #category : #visualization }
SBBarChart >> newBarFor: aValue at: positionIndex [

"There is an extra Morph containing the datapoint itself so the tooltip is far easier to activate through more area"
^ Morph new
height: self class preferredHeight;
left: ((positionIndex - 0.5) * self spaceBetweenPoints) rounded;
width: self spaceBetweenPoints;
color: Color transparent;
balloonText: aValue printString;
addMorph: (Morph new
color: (self datapointColorForValue: aValue);
width: self barWidth;
height: (self scaleY scaledValueOf: aValue);
bottom: self class canvasHeight + self class heightMargin ;
left: positionIndex * self spaceBetweenPoints;
yourself);
yourself



]

{ #category : #visualization }
SBBarChart >> newDataPoints [

^ self traceValues collectWithIndex: [:aTraceValue :index | self newBarFor: aTraceValue at: index]
]

{ #category : #visualization }
SBBarChart >> visualizationMorph [

| visualizationMorph |
visualizationMorph := self newBackground.

visualizationMorph addAllMorphs: self newDataPoints.
visualizationMorph addAllMorphsBack: (self newScaleLinesOn: visualizationMorph).

^ visualizationMorph
]
3 changes: 2 additions & 1 deletion packages/Sandblocks-Watch/SBWatchView.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ SBWatchView >> changeDisplay [
<action>

| index options |
options := Array streamContents: [:stream | self values allConversionsFor: SBInterfaces topLevel do: [:pair | stream nextPut: pair]].
options := Array streamContents: [:stream |
self values allConversionsFor: SBInterfaces topLevel do: [:pair | stream nextPut: pair]].
options := options, {{'default'. self watchValuesContainer addAllMorphsBack: (watchValues collect: #asValueMorph)}}.
index := UIManager default chooseFrom: (options collect: #first).
index = 0 ifTrue: [^ self].
Expand Down

0 comments on commit ebb4b1c

Please sign in to comment.