Powered by AppSignal & Oban Pro

Custom Gauge Chart

31_custom_gauge_chart.livemd

Custom Gauge Chart

Mix.install([
  {:plotly_ex, "~> 0.3"},
  {:kino, "~> 0.18"}
])

Section

Full gauge styling: gauge.bar: %{color:} sets the value bar color. gauge.bgcolor: sets the arc background. gauge.borderwidth: and gauge.bordercolor: style the border. Custom steps and threshold can use any CSS color. number.prefix: and delta.relative: true format the display.

alias Plotly.{Figure, Indicator}

Figure.new()
|> Figure.add_trace(
  Indicator.new(
    mode: "gauge+number+delta",
    value: 420,
    title: %{text: "Speed", font: %{size: 24}},
    delta: %{reference: 400, increasing: %{color: "RebeccaPurple"}},
    number: %{suffix: " km/h", font: %{size: 20}},
    gauge: %{
      axis: %{range: [nil, 500], tickwidth: 1, tickcolor: "darkblue"},
      bar: %{color: "darkblue"},
      bgcolor: "white",
      borderwidth: 2,
      bordercolor: "gray",
      steps: [
        %{range: [0, 250], color: "cyan"},
        %{range: [250, 400], color: "royalblue"}
      ],
      threshold: %{
        line: %{color: "red", width: 4},
        thickness: 0.75,
        value: 490
      }
    },
    domain: %{x: [0, 1], y: [0, 1]}
  )
)
|> Figure.update_layout(
  width: 600,
  height: 400,
  paper_bgcolor: "lavender",
  font: %{color: "darkblue", family: "Arial"}
)
|> Plotly.show()