0 people like it.

Charting

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
open System
open System.Drawing
open System.IO
open System.Windows.Forms.DataVisualization.Charting
open MSDN.FSharp.Charting

/// parse date and distance info from file
let getRunData path =
    File.ReadAllLines(path)
    |> Array.map (fun line ->
        match line.Split([|' '|], StringSplitOptions.RemoveEmptyEntries) with
        | [| date; dist |] ->
            (DateTime.Parse(date) , float dist)
        | _ -> failwith "Unable to parse line")

/// plot total progress, daily distance, and goal progress
let makeRunChart (runData : (DateTime * float) array) goal =
    let lastDate = fst (runData.[runData.Length - 1])
    let endDate =   // only chart up to the end of the current month
        DateTime.MinValue.AddYears(lastDate.Year - 1).AddMonths(lastDate.Month).AddDays(-1.)
    let startDate = DateTime.MinValue.AddYears(lastDate.Year - 1)
    let dailyGoal = goal / 365.  // not worrying about leap years, I know...

    // small series representing first and last day at goal pace
    let goalPts =
        let totalDays = (endDate - startDate).TotalDays + 1.
        [(startDate, dailyGoal); (endDate, dailyGoal * totalDays)]

    // convert input list of daily distances into a list of cumulative distance
    let sumPts = 
        let sum = ref 0.
        runData
        |> Array.map (fun (date, dist) ->
            sum := !sum + dist
            (date, !sum))

    // column chart of daily runs
    let runChrt = 
        FSharpChart.Column (runData, Name = "Daily miles run")
        |> FSharpChart.WithSeries.Style (Color = System.Drawing.Color.Gray)
        |> FSharpChart.WithSeries.AxisType (YAxisType = AxisType.Secondary)

    // line chart of total progress
    let progressChrt =    
        FSharpChart.Line (sumPts, Name = "Total miles run")
        |> FSharpChart.WithSeries.Style (Color = System.Drawing.Color.Red, BorderWidth = 4)
        |> FSharpChart.WithSeries.Marker (Style = MarkerStyle.Circle, Size = 7)

    // line chart of goal progress
    let goalChrt = 
        FSharpChart.Line (goalPts, Name = "Target miles")
        |> FSharpChart.WithSeries.Style (Color = System.Drawing.Color.LightSteelBlue, BorderWidth = 4)

    // complete chart
    FSharpChart.Combine [runChrt; goalChrt; progressChrt]
    |> FSharpChart.WithArea.AxisX (Minimum = startDate.ToOADate(), Maximum = endDate.ToOADate(), MajorGrid = Grid(Enabled = false))
    |> FSharpChart.WithArea.AxisY (Title = "Total miles", TitleFont = new Font("Calibri", 11.0f), MajorGrid = Grid(LineColor = System.Drawing.Color.LightGray))
    |> FSharpChart.WithArea.AxisY2 (Title = "Daily miles", TitleFont = new Font("Calibri", 11.0f), MajorGrid = Grid(Enabled = false), Maximum = 10.)
    |> FSharpChart.WithTitle (Text = (sprintf "%.0f Miles in %d - Progress" goal lastDate.Year), Font = new Font("Impact", 14.0f))
    |> FSharpChart.WithLegend (Font = new Font("Calibri", 10.0f))   
    |> FSharpChart.WithCreate
namespace System
namespace System.Drawing
namespace System.IO
namespace System.Windows
namespace System.Windows.Forms
namespace Microsoft.FSharp
val getRunData : path:string -> (DateTime * float) []

Full name: Script.getRunData


 parse date and distance info from file
val path : string
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.ReadAllLines(path: string) : string []
File.ReadAllLines(path: string, encoding: Text.Encoding) : string []
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val line : string
String.Split([<ParamArray>] separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field StringSplitOptions.RemoveEmptyEntries = 1
val date : string
val dist : string
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime.Parse(s: string) : DateTime
DateTime.Parse(s: string, provider: IFormatProvider) : DateTime
DateTime.Parse(s: string, provider: IFormatProvider, styles: Globalization.DateTimeStyles) : DateTime
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val makeRunChart : runData:(DateTime * float) array -> goal:float -> 'a

Full name: Script.makeRunChart


 plot total progress, daily distance, and goal progress
val runData : (DateTime * float) array
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
val goal : float
val lastDate : DateTime
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
property Array.Length: int
val endDate : DateTime
field DateTime.MinValue
DateTime.AddYears(value: int) : DateTime
property DateTime.Year: int
property DateTime.Month: int
val startDate : DateTime
val dailyGoal : float
val goalPts : (DateTime * float) list
val totalDays : float
val sumPts : (DateTime * float) []
val sum : float ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val date : DateTime
val dist : float
val runChrt : obj
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool
    member G : byte
    member GetBrightness : unit -> float32
    member GetHashCode : unit -> int
    member GetHue : unit -> float32
    member GetSaturation : unit -> float32
    member IsEmpty : bool
    member IsKnownColor : bool
    ...
  end

Full name: System.Drawing.Color
property Color.Gray: Color
val progressChrt : obj
property Color.Red: Color
Multiple items
type Size =
  struct
    new : pt:Point -> Size + 1 overload
    member Equals : obj:obj -> bool
    member GetHashCode : unit -> int
    member Height : int with get, set
    member IsEmpty : bool
    member ToString : unit -> string
    member Width : int with get, set
    static val Empty : Size
    static member Add : sz1:Size * sz2:Size -> Size
    static member Ceiling : value:SizeF -> Size
    ...
  end

Full name: System.Drawing.Size

--------------------
Size()
Size(pt: Point) : unit
Size(width: int, height: int) : unit
val goalChrt : obj
property Color.LightSteelBlue: Color
DateTime.ToOADate() : float
Multiple items
type Font =
  inherit MarshalByRefObject
  new : prototype:Font * newStyle:FontStyle -> Font + 12 overloads
  member Bold : bool
  member Clone : unit -> obj
  member Dispose : unit -> unit
  member Equals : obj:obj -> bool
  member FontFamily : FontFamily
  member GdiCharSet : byte
  member GdiVerticalFont : bool
  member GetHashCode : unit -> int
  member GetHeight : unit -> float32 + 2 overloads
  ...

Full name: System.Drawing.Font

--------------------
Font(prototype: Font, newStyle: FontStyle) : unit
   (+0 other overloads)
Font(family: FontFamily, emSize: float32) : unit
   (+0 other overloads)
Font(familyName: string, emSize: float32) : unit
   (+0 other overloads)
Font(family: FontFamily, emSize: float32, style: FontStyle) : unit
   (+0 other overloads)
Font(family: FontFamily, emSize: float32, unit: GraphicsUnit) : unit
   (+0 other overloads)
Font(familyName: string, emSize: float32, style: FontStyle) : unit
   (+0 other overloads)
Font(familyName: string, emSize: float32, unit: GraphicsUnit) : unit
   (+0 other overloads)
Font(family: FontFamily, emSize: float32, style: FontStyle, unit: GraphicsUnit) : unit
   (+0 other overloads)
Font(familyName: string, emSize: float32, style: FontStyle, unit: GraphicsUnit) : unit
   (+0 other overloads)
Font(family: FontFamily, emSize: float32, style: FontStyle, unit: GraphicsUnit, gdiCharSet: byte) : unit
   (+0 other overloads)
property Color.LightGray: Color
Multiple items
namespace System.Drawing.Text

--------------------
namespace System.Text
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Raw view Test code New version

More information

Link:http://fssnip.net/gv
Posted:11 years ago
Author:
Tags: