Skip to content

Commit 5192bef

Browse files
committed
Make seriesAlong plot helper more modular
In effort to plot acceleration including gravity more succinctly.
1 parent b720024 commit 5192bef

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

examples/PlotAccelerationExample.elm

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Html.Attributes exposing (style)
55
import Svg.Attributes exposing (stroke)
66
import Task
77
import Window exposing (Size)
8-
import Device.Motion exposing (Motion)
8+
import Device.Motion exposing (Motion, Acceleration)
99
import Plot exposing (defaultSeriesPlotCustomizations)
1010
import SlidingBuffer exposing (SlidingBuffer)
1111

@@ -93,27 +93,26 @@ plotMotion model =
9393
, toDomainHighest = \y -> y + 0.1
9494
}
9595

96-
convertIndexAndMotionToCoordinate index motion =
97-
{ x = toFloat index, acceleration = motion.acceleration }
98-
99-
coordinates =
100-
List.indexedMap
101-
convertIndexAndMotionToCoordinate
102-
(SlidingBuffer.toList model.history)
103-
10496
seriesAlong getter color =
105-
{ axis = Plot.normalAxis
106-
, interpolation = Plot.Linear (Just color) [ stroke "" ]
107-
, toDataPoints = List.map (\{ x, acceleration } -> Plot.clear x (getter acceleration))
108-
}
97+
let
98+
toCoordinates =
99+
SlidingBuffer.toList >> (List.indexedMap convertIndexAndMotionToCoordinate)
100+
101+
convertIndexAndMotionToCoordinate index motion =
102+
Plot.clear (toFloat index) (getter motion)
103+
in
104+
{ axis = Plot.normalAxis
105+
, interpolation = Plot.Linear (Just color) [ stroke "" ]
106+
, toDataPoints = toCoordinates
107+
}
109108

110109
series =
111-
[ seriesAlong .x "cyan"
112-
, seriesAlong .y "magenta"
113-
, seriesAlong .z "yellow"
110+
[ seriesAlong (.acceleration >> .x) "cyan"
111+
, seriesAlong (.acceleration >> .y) "magenta"
112+
, seriesAlong (.acceleration >> .z) "yellow"
114113
]
115114
in
116-
Plot.viewSeriesCustom configuration series coordinates
115+
Plot.viewSeriesCustom configuration series model.history
117116

118117

119118
center : Html Msg -> Html Msg

src/Device/Motion.elm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ effect module Device.Motion
22
where { subscription = MySub }
33
exposing
44
( Motion
5+
, Acceleration
56
, initial
67
, changes
78
)
@@ -29,7 +30,7 @@ type alias MetersPerSecondSquared =
2930
Float
3031

3132

32-
{-| This is a type representing the acceleration of a device.
33+
{-| This is a type representing the acceleration of a device in m/s^2.
3334
-}
3435
type alias Acceleration =
3536
{ x : MetersPerSecondSquared

0 commit comments

Comments
 (0)