1 people like it.

Simple console bar chart

Creates a simple bar chart from a list consisting of a tuple (title:string * value:'a).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
// based on: https://www.daniweb.com/programming/software-development/code/238532/very-simple-console-barchart
let inline drawBarChart (xs: (string * 'a) seq) =
    // calculate maximum of the data
    let maxTitleWidth = xs |> Seq.map (fst >> String.length) |> Seq.max
    let maxValue = xs |> Seq.map snd |> Seq.max
    // use full width of console
    let factor = float (System.Console.WindowWidth - maxTitleWidth - 2)
    // draw chart
    let emptyTitle = String.replicate maxTitleWidth " "
    printfn "%s|" emptyTitle
    for (title, value) in xs do
        let paddedTitle = title.PadLeft(maxTitleWidth)
        let width = int (float value / float maxValue * factor)
        let segments = String.replicate width "#"
        printfn "%s|%s" paddedTitle segments
    printfn "%s|" emptyTitle
val drawBarChart : xs:seq<string * 'a> -> unit (requires comparison and member op_Explicit)

Full name: Script.drawBarChart
val xs : seq<string * 'a> (requires comparison and member op_Explicit)
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val maxTitleWidth : int
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
module String

from Microsoft.FSharp.Core
val length : str:string -> int

Full name: Microsoft.FSharp.Core.String.length
val max : source:seq<'T> -> 'T (requires comparison)

Full name: Microsoft.FSharp.Collections.Seq.max
val maxValue : 'a (requires comparison and member op_Explicit)
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val factor : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

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

Full name: Microsoft.FSharp.Core.float<_>
namespace System
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
property System.Console.WindowWidth: int
val emptyTitle : string
val replicate : count:int -> str:string -> string

Full name: Microsoft.FSharp.Core.String.replicate
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val title : string
val value : 'a (requires comparison and member op_Explicit)
val paddedTitle : string
System.String.PadLeft(totalWidth: int) : string
System.String.PadLeft(totalWidth: int, paddingChar: char) : string
val width : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val segments : string
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7UB
Posted:6 years ago
Author:Tobias Burger
Tags: #console , #chart , #barchart