From ebb4b1c2e0ffd879a49f86e9deac6470d914473c Mon Sep 17 00:00:00 2001 From: Joana Bergsiek Date: Wed, 18 Oct 2023 18:32:41 +0200 Subject: [PATCH] Adds bar chart --- .../Sandblocks-Core/Collection.extension.st | 9 +++ .../Sandblocks-Watch/SBAxisNotation.class.st | 4 +- packages/Sandblocks-Watch/SBBarChart.class.st | 67 +++++++++++++++++++ .../Sandblocks-Watch/SBWatchView.class.st | 3 +- 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 packages/Sandblocks-Watch/SBBarChart.class.st diff --git a/packages/Sandblocks-Core/Collection.extension.st b/packages/Sandblocks-Core/Collection.extension.st index e77b3553..17df9063 100644 --- a/packages/Sandblocks-Core/Collection.extension.st +++ b/packages/Sandblocks-Core/Collection.extension.st @@ -1,5 +1,14 @@ Extension { #name : #Collection } +{ #category : #'*Sandblocks-Core-converting' } +Collection >> asBarChart: converter [ + + + 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 [ diff --git a/packages/Sandblocks-Watch/SBAxisNotation.class.st b/packages/Sandblocks-Watch/SBAxisNotation.class.st index a8e6a4fc..4782f31d 100644 --- a/packages/Sandblocks-Watch/SBAxisNotation.class.st +++ b/packages/Sandblocks-Watch/SBAxisNotation.class.st @@ -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; @@ -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]) ] diff --git a/packages/Sandblocks-Watch/SBBarChart.class.st b/packages/Sandblocks-Watch/SBBarChart.class.st new file mode 100644 index 00000000..15a3be92 --- /dev/null +++ b/packages/Sandblocks-Watch/SBBarChart.class.st @@ -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 +] diff --git a/packages/Sandblocks-Watch/SBWatchView.class.st b/packages/Sandblocks-Watch/SBWatchView.class.st index f9300476..5797444c 100644 --- a/packages/Sandblocks-Watch/SBWatchView.class.st +++ b/packages/Sandblocks-Watch/SBWatchView.class.st @@ -47,7 +47,8 @@ SBWatchView >> changeDisplay [ | 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].