Current section
Files
Jump to
Current section
Files
lib/charts_ex/candlestick_chart.ex
defmodule ChartsEx.CandlestickChart do
@moduledoc """
Candlestick (OHLC) chart for financial data.
Each data point is `[open, close, low, high]`.
## Example
CandlestickChart.new()
|> CandlestickChart.title("AAPL")
|> CandlestickChart.x_axis(["Jan", "Feb", "Mar"])
|> CandlestickChart.add_series("Price", [
[20.0, 34.0, 10.0, 38.0],
[40.0, 35.0, 30.0, 50.0],
[31.0, 38.0, 33.0, 44.0]
])
|> ChartsEx.render()
"""
@behaviour ChartsEx.Chart
defstruct [
:title_text,
:title_font_size,
:title_font_color,
:title_font_weight,
:title_margin,
:title_align,
:title_height,
:sub_title_text,
:sub_title_font_size,
:sub_title_font_color,
:sub_title_font_weight,
:sub_title_margin,
:sub_title_align,
:sub_title_height,
:width,
:height,
:margin,
:font_family,
:background_color,
:theme,
:legend_font_size,
:legend_font_color,
:legend_font_weight,
:legend_align,
:legend_margin,
:legend_category,
:legend_show,
:x_axis_data,
:x_axis_height,
:x_axis_stroke_color,
:x_axis_font_size,
:x_axis_font_color,
:x_axis_font_weight,
:x_axis_name_gap,
:x_axis_name_rotate,
:x_axis_margin,
:x_axis_hidden,
:x_boundary_gap,
:y_axis_configs,
:y_axis_hidden,
:grid_stroke_color,
:grid_stroke_width,
:series_list,
:series_stroke_width,
:series_label_font_color,
:series_label_font_size,
:series_label_font_weight,
:series_label_formatter,
:series_colors,
:series_symbol,
:series_smooth,
:series_fill,
# Candlestick-specific
:candlestick_up_color,
:candlestick_up_border_color,
:candlestick_down_color,
:candlestick_down_border_color
]
@doc "Creates a new empty candlestick chart."
def new, do: %__MODULE__{}
@doc "Sets the chart title."
def title(chart, text), do: %{chart | title_text: text}
@doc "Sets the chart subtitle."
def sub_title(chart, text), do: %{chart | sub_title_text: text}
@doc "Sets the theme. See `ChartsEx.Theme.list/0` for options."
def theme(chart, name), do: %{chart | theme: ChartsEx.Theme.validate!(name)}
@doc "Sets the chart width in pixels."
def width(chart, w), do: %{chart | width: w}
@doc "Sets the chart height in pixels."
def height(chart, h), do: %{chart | height: h}
@doc "Sets the x-axis category labels."
def x_axis(chart, labels), do: %{chart | x_axis_data: labels}
@doc """
Adds a data series to the chart.
Each data point is `[open, close, low, high]`.
## Options
* `:label_show` - whether to show value labels (boolean)
"""
def add_series(chart, name, data, opts \\ []) do
series = %{name: name, data: data} |> Map.merge(Map.new(opts))
%{chart | series_list: (chart.series_list || []) ++ [series]}
end
@doc "Sets the chart margin as `%{left: _, top: _, right: _, bottom: _}`."
def margin(chart, m) when is_map(m), do: %{chart | margin: m}
@doc "Sets series colors as a list of hex strings."
def series_colors(chart, colors), do: %{chart | series_colors: colors}
@doc "Sets the background color as a hex string."
def background_color(chart, color), do: %{chart | background_color: color}
@doc "Sets the color for up (bullish) candles."
def up_color(chart, color), do: %{chart | candlestick_up_color: color}
@doc "Sets the border color for up candles."
def up_border_color(chart, color), do: %{chart | candlestick_up_border_color: color}
@doc "Sets the color for down (bearish) candles."
def down_color(chart, color), do: %{chart | candlestick_down_color: color}
@doc "Sets the border color for down candles."
def down_border_color(chart, color), do: %{chart | candlestick_down_border_color: color}
@impl ChartsEx.Chart
def to_json(chart), do: ChartsEx.Serializer.to_json(chart, "candlestick")
end