Search code examples
f#websharper

Number formatting in WebSharper Google Visualization


I have a question concerning WebSharper's Google Visualization library. I was trying to format the data when the mouse hovers over countries in a Geo Chart.

However, there is the following definition for Legend on https://github.com/intellifactory/websharper.google.visualization/blob/master/IntelliFactory.WebSharper.Google.Visualization/Base.fs

type Legend [<Inline "{}">] () =
  [<DefaultValue>]
  val mutable position : LegendPosition
  [<DefaultValue>]
  val mutable alignment : LegendAlignment
  [<DefaultValue>]
  val mutable textStyle : TextStyle

This does not take into account the numberFormat which is used in such charts as GeoChart https://developers.google.com/chart/interactive/docs/gallery/geochart

Is there a way to circumvent this (to format tooltips/legends) ?

Many thanks


Solution

  • A general workaround: the x?y <- z dynamic assignment can be used in WebSharper code to get x.y = z in the JavaScript translation. So in your case, for example legend?numberFormat <- ".##".

    You can also expand the legend type with a helper method for this:

    type Legend with
        [<JavaScript; Inline>]
        member this.WithNumberFormat(format: string) =
            this?numberFormat <- format
            this
    

    Or you can create a JavaScript object expression by New [ "numberformat" => ".##" ] to use as the Legend object.

    WebSharper's Google.Visualization typed bindings are a bit outdated. We will get around to review it completely someday, but feel free to create a pull request if you encounter any missing API functionality.