implementation module TimeGraph // An example of a task that uses SVG to render a list of values and measures from iTasks.WF.Definition import class iTask import iTasks.Engine import iTasks.WF.Tasks.Interaction import iTasks.UI.Prompt import Graphics.Scalable.Image import iTasks.Extensions.SVG.SVGEditor import StdEnum, StdList from StdFunc import id, o, const // shorthand definitions for the used fonts in these examples times = normalFontDef "Times New Roman" // shorthand definitions for the used colours in these examples none = toSVGColor "none" linecolour = toSVGColor "red" backgroundcolour = toSVGColor "aliceblue" barcolour = toSVGColor "steelblue" barstrokecolour = backgroundcolour Start :: !*World -> *World Start world = doTasks (time_graph options ([1.0..5.0] ++ [4.5,3.5..0.5])) world where options = {TimeGraphOptions | valueSpan = \v = px (50.0*v) , measures = [ {TimeGraphMeasure | base = 3.14 , label = "low" , left = True , font = times 8.0 } , {TimeGraphMeasure | base = 4.88 , label = "high" , left = False , font = times 8.0 } ] , distance = px 10.0 , maxValue = 10.0 , maxNoElts = 10 } time_graph :: !(TimeGraphOptions a) ![a] -> Task [a] | JSEncode{|*|}, JSDecode{|*|}, iTask a time_graph options values = viewInformation "TimeGraph" [ViewUsing id (fromSVGEditor { initView = id , renderImage = const (graph options) , updView = \m _ = m , updModel = \_ v = v })] values graph :: !(TimeGraphOptions a) ![a] !*TagSource -> Image [a] graph options=:{TimeGraphOptions | valueSpan,distance,measures,maxValue,maxNoElts} values tags = beside (repeat AtBottom) [] Nothing [] [ left_labels , collage [(zero,height-diagram_height):[(zero,height-(valueSpan m.base)) \\ m <- measures]] [diagram : [xline diagram_width <@< {stroke = linecolour} \\ m <- measures]] (Host (rect diagram_width height <@< {fill = backgroundcolour} <@< {stroke = none})) , right_labels ] (Host (empty width height)) where left_measures = filter (\m = m.left) measures right_measures = filter (\m = not m.left) measures left_labels = collage [(zero,height-(valueSpan m.base)) \\ m <- left_measures] [text m.font m.label \\ m <- left_measures] (Host (empty left_labels_width height)) right_labels = collage [(zero,height-(valueSpan m.base)) \\ m <- right_measures] [text m.font m.label \\ m <- right_measures] (Host (empty right_labels_width height)) left_labels_width = maxSpan [textxspan m.font m.label \\ m <- left_measures] right_labels_width = maxSpan [textxspan m.font m.label \\ m <- right_measures] diagram = beside (repeat AtBottom) [] Nothing [] bars (Host (empty diagram_width height)) bars = [rect distance (valueSpan v) <@< {fill = barcolour} <@< {stroke = barstrokecolour} \\ v <- take maxNoElts values] width = left_labels_width + diagram_width + right_labels_width diagram_width = distance *. maxNoElts diagram_height = maxSpan (map valueSpan values) height = valueSpan maxValue