Skip to content

Commit

Permalink
Merge pull request #125 from hpi-swa/feature/display-graph
Browse files Browse the repository at this point in the history
Graphs for Example Watches
  • Loading branch information
tom95 authored Oct 24, 2023
2 parents 5bfc96a + 6caab16 commit 51d364d
Show file tree
Hide file tree
Showing 9 changed files with 890 additions and 1 deletion.
27 changes: 27 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 All @@ -9,6 +18,24 @@ Collection >> asCollectionView: converter [
do: [SBCollection new interface: converter objectInterface object: self]
]

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

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

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

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

{ #category : #'*Sandblocks-Core' }
Collection >> asSandblockShortcut [

Expand Down
6 changes: 6 additions & 0 deletions packages/Sandblocks-Morphs/SBOwnTextMorph.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,12 @@ SBOwnTextMorph >> userString [
^ self contentsForEdit
]

{ #category : #'as yet unclassified' }
SBOwnTextMorph >> verySmall [

self font: (TextStyle default fontOfSize: 6 sbScaled)
]

{ #category : #'as yet unclassified' }
SBOwnTextMorph >> wantsKeyboardFocus [

Expand Down
105 changes: 105 additions & 0 deletions packages/Sandblocks-Watch/SBAxisNotation.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"
A numerical legend for a scale
"
Class {
#name : #SBAxisNotation,
#superclass : #Morph,
#instVars : [
'scale',
'numberTicks'
],
#category : #'Sandblocks-Watch'
}

{ #category : #'initialize-release' }
SBAxisNotation class >> newFromScale: aSBScale ticking: aNumber [

^ self new
scale: aSBScale
numberTicks: aNumber
]

{ #category : #initialization }
SBAxisNotation >> initialize [

super initialize.

numberTicks := 3.
scale := SBScale newLinearScaleWithDomain: (0 to: 100) forRange: (0 to: 100).

self color: Color transparent;
layoutPolicy: ProportionalLayout new;
hResizing: #shrinkWrap;
vResizing: #spaceFill
]

{ #category : #accessing }
SBAxisNotation >> numberTicks [

^ numberTicks
]

{ #category : #accessing }
SBAxisNotation >> numberTicks: aNumber [

numberTicks := aNumber.

self visualize
]

{ #category : #visualization }
SBAxisNotation >> relativeTickHeights [

| section adjustedTicks |
(self numberTicks < 2) ifTrue: [^#()].

"Starting count from 0 here instead of 1"
adjustedTicks := self numberTicks - 1.
section := 1 / adjustedTicks.
^ (0 to: adjustedTicks) collect: [:i | (section * i)]
]

{ #category : #accessing }
SBAxisNotation >> scale [

^ scale
]

{ #category : #accessing }
SBAxisNotation >> scale: aSBScale [

scale := aSBScale.

self visualize
]

{ #category : #accessing }
SBAxisNotation >> scale: aSBScale numberTicks: aNumber [

scale := aSBScale.
numberTicks := aNumber.

self visualize
]

{ #category : #visualization }
SBAxisNotation >> visualize [

self submorphs copy do: #abandon.

self addAllMorphsBack: (
self relativeTickHeights collect: [:aFraction | | annotation |
annotation := SBOwnTextMorph new
contents: ((self scale domainValueOfRelative: aFraction) asFloat asString);
verySmall;
layoutFrame: (LayoutFrame new topFraction: (1 - aFraction)).

"Move text center to be fraction. 0 and 1 will be adjusted to align at the borders."
annotation layoutFrame topOffset: (-0.5*(annotation minExtent y)).
(aFraction = 1) ifTrue: [annotation layoutFrame topOffset: 0].
(aFraction = 0) ifTrue: [annotation layoutFrame topOffset: (-1*(annotation minExtent y))].

annotation])


]
68 changes: 68 additions & 0 deletions packages/Sandblocks-Watch/SBBarChart.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 / 2
]

{ #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 datapointDefaultColor;
width: self barWidth;
height: {(self scaleY scaledValueOf: aValue). 1} max;
bottom: self class canvasHeight + self class heightMargin;
left: positionIndex * self spaceBetweenPoints;
setProperty: #chartValue toValue: (self scaleY scaledValueOf: aValue);
yourself);
yourself



]

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

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

{ #category : #visualization }
SBBarChart >> newLineFrom: aDataPointMorph1 to: aDataPointMorph2 [

^ LineMorph
from: aDataPointMorph1 topCenter
to: aDataPointMorph2 topCenter
color: ((self lineColorFrom: aDataPointMorph1 to: aDataPointMorph2) alpha: 0.2)
width: self lineWidth


]
Loading

0 comments on commit 51d364d

Please sign in to comment.