Chart
Create line, column, bar, Sankey and other ECharts charts with ordinary F# options. The host handles lazy loading, theme, resizing and cleanup; your application owns the data and interactions.
Line
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let lineChartOptions =
{| tooltip = {| trigger = "axis" |}
grid =
{| left = 8
right = 16
top = 16
bottom = 8
containLabel = true |}
xAxis =
{| ``type`` = "category"
data = [| "Jan"; "Feb"; "Mar" |] |}
yAxis = {| ``type`` = "value" |}
series =
[| {| name = "Revenue"
``type`` = "line"
data = [| 24; 36; 42 |] |} |] |}
let defaultChart =
fveChart {
_id "line-chart"
_ariaLabel "Revenue: January 24, February 36, March 42"
_options lineChartOptions
}On this page
Installation
Copy this component and its required dependencies into the consumer-owned Components project.
dotnet fve add chart --config src/Acme.Components/fve.jsonAdd to src/Acme.Web/js/app.js once, alongside your other component imports:
import '../../Acme.Components/Components/Assets/chart.js';ECharts loads automatically when a nonempty chart connects.
The CLI resolves dependencies and preserves consumer-authored files. See Installation for project initialization, Tailwind, the shared runtime, and building/loading your app entry.
Usage
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let lineChartOptions =
{| tooltip = {| trigger = "axis" |}
grid =
{| left = 8
right = 16
top = 16
bottom = 8
containLabel = true |}
xAxis =
{| ``type`` = "category"
data = [| "Jan"; "Feb"; "Mar" |] |}
yAxis = {| ``type`` = "value" |}
series =
[| {| name = "Revenue"
``type`` = "line"
data = [| 24; 36; 42 |] |} |] |}
let defaultChart =
fveChart {
_id "line-chart"
_ariaLabel "Revenue: January 24, February 36, March 42"
_options lineChartOptions
}_options accepts JSON-compatible F# records or anonymous records using ECharts option names. Column and horizontal-bar charts both use type = "bar"; the category axis chooses their direction. Sankey uses nodes and links instead of axes. --fve-chart-height controls height. The default palette and text follow the owning theme; explicit ECharts colors remain caller-owned.
The value passed to _options follows the ECharts options API.
Use _events to opt into ECharts events: click becomes chart-click, brushend becomes chart-brushend. evt.detail contains the original payload. After each options/theme update, chart-render exposes evt.detail.chart for application-owned JavaScript formatters or actions. _empty true displays the empty state without interpreting your datasets.
See the Application template for currency formatting, date-range brushing and server-owned filtering.
Column
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let columnChartOptions =
{| tooltip = {| trigger = "axis" |}
grid =
{| left = 8
right = 16
top = 16
bottom = 8
containLabel = true |}
xAxis =
{| ``type`` = "category"
data = [| "Jan"; "Feb"; "Mar" |] |}
yAxis = {| ``type`` = "value" |}
series =
[| {| name = "Revenue"
``type`` = "bar"
data = [| 24; 36; 42 |] |} |] |}
let columnChart =
fveChart {
_id "column-chart"
_ariaLabel "Revenue by month, as columns"
_options columnChartOptions
}Horizontal bar
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let barChartOptions =
{| tooltip = {| trigger = "axis" |}
grid =
{| left = 8
right = 16
top = 16
bottom = 8
containLabel = true |}
xAxis = {| ``type`` = "value" |}
yAxis =
{| ``type`` = "category"
data = [| "Jan"; "Feb"; "Mar" |] |}
series =
[| {| name = "Revenue"
``type`` = "bar"
data = [| 24; 36; 42 |] |} |] |}
let barChart =
fveChart {
_id "bar-chart"
_ariaLabel "Revenue by month, as horizontal bars"
_options barChartOptions
}Sankey
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let sankeyChartOptions =
{| tooltip = {| trigger = "item" |}
series =
[| {| ``type`` = "sankey"
left = 8
right = 8
top = 16
bottom = 16
data =
[| {| name = "Income"
label = {| position = "right" |} |}
{| name = "Costs"
label = {| position = "left" |} |}
{| name = "Savings"
label = {| position = "left" |} |} |]
links =
[| {| source = "Income"
target = "Costs"
value = 70 |}
{| source = "Income"
target = "Savings"
value = 30 |} |]
lineStyle = {| color = "gradient"; opacity = 0.35 |} |} |] |}
let sankeyChart =
fveChart {
_id "sankey-chart"
_ariaLabel "Income 100 flows into costs 70 and savings 30"
_options sankeyChartOptions
}Updating options and handling events
Click a point or column.
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let lineChartOptions =
{| tooltip = {| trigger = "axis" |}
grid =
{| left = 8
right = 16
top = 16
bottom = 8
containLabel = true |}
xAxis =
{| ``type`` = "category"
data = [| "Jan"; "Feb"; "Mar" |] |}
yAxis = {| ``type`` = "value" |}
series =
[| {| name = "Revenue"
``type`` = "line"
data = [| 24; 36; 42 |] |} |] |}
let columnChartOptions =
{| tooltip = {| trigger = "axis" |}
grid =
{| left = 8
right = 16
top = 16
bottom = 8
containLabel = true |}
xAxis =
{| ``type`` = "category"
data = [| "Jan"; "Feb"; "Mar" |] |}
yAxis = {| ``type`` = "value" |}
series =
[| {| name = "Revenue"
``type`` = "bar"
data = [| 24; 36; 42 |] |} |] |}
let chartTypeControl (label: string) options =
fveButton {
_dataOn (
"click",
$"$_chart_element.setAttribute('options', JSON.stringify({System.Text.Json.JsonSerializer.Serialize(options)}))"
)
label
}
let interactiveChart =
div {
_class "grid min-w-0 gap-3"
_dataSignals "{ _chart_selection: 'Click a point or column.' }"
fveChart {
_id "interactive-values-chart"
_dataRef "_chart_element"
_ariaLabel "Revenue with application-owned chart type and selection"
_options lineChartOptions
_events [ "click" ]
_dataOn ("chart-click", "$_chart_selection = evt.detail.name + ': ' + evt.detail.value")
}
div {
_class "flex flex-wrap gap-2"
chartTypeControl "Show line" lineChartOptions
chartTypeControl "Show columns" columnChartOptions
}
p {
_role "status"
_dataText "$_chart_selection"
"Click a point or column."
}
}This example also uses Button. See their Installation sections for any additional setup.
Empty data
open System
open FSharp.ViewEngine
open Acme.Components
open type Html
open type Svg
open type Datastar
let emptyChart =
fveChart {
_id "empty-values-chart"
_ariaLabel "Revenue with no available data"
_options {| series = Array.empty<obj> |}
_empty true
}API reference
module ChartDsl =
val fveChart : ComponentElementBuilder
module ChartAttributes =
val Html._options.Static : value:'t -> HtmlAttribute
val Html._events.Static : value:string list -> HtmlAttribute
val Html._empty.Static : value:bool -> HtmlAttribute