diff --git a/lib/sparkline.ex b/lib/sparkline.ex index f45a6a3..9c8d817 100644 --- a/lib/sparkline.ex +++ b/lib/sparkline.ex @@ -100,6 +100,7 @@ defmodule Sparkline do {:width, number()} | {:height, number()} | {:padding, number()} + | {:smoothing, float()} | {:placeholder, nil | String.t()} @typedoc "A general sparkline options list." @@ -112,7 +113,7 @@ defmodule Sparkline do @type dots_options :: list(option()) @typedoc "A line-related sparkline option." - @type line_option :: {:width, number()} | {:color, String.t()} | {:smoothing, float()} + @type line_option :: {:width, number()} | {:color, String.t()} @typedoc "A line-related sparkline options list." @type line_options :: list(option()) @@ -128,6 +129,7 @@ defmodule Sparkline do width: number(), height: number(), padding: number(), + smoothing: float(), placeholder: nil | String.t(), dots: nil | map(), line: nil | map(), @@ -152,7 +154,7 @@ defmodule Sparkline do @spec new(datapoints(), options()) :: Sparkline.t() def new(datapoints, options \\ []) do options = - [width: 200, height: 100, padding: 6, placeholder: nil] + [width: 200, height: 100, padding: 6, smoothing: 0.2, placeholder: nil] |> Keyword.merge(options) |> Map.new() |> Map.merge(%{dots: nil, line: nil, area: nil}) @@ -181,7 +183,7 @@ defmodule Sparkline do @spec show_line(Sparkline.t(), line_options()) :: Sparkline.t() def show_line(sparkline, options \\ []) do line_options = - [width: 0.25, color: "black", smoothing: 0.2] + [width: 0.25, color: "black"] |> Keyword.merge(options) |> Map.new() diff --git a/lib/sparkline/draw.ex b/lib/sparkline/draw.ex index 6879550..20d2bc8 100644 --- a/lib/sparkline/draw.ex +++ b/lib/sparkline/draw.ex @@ -142,10 +142,9 @@ defmodule Sparkline.Draw do ) :: {number(), number()} defp calculate_control_point(curr, prev, next, direction, options) do {length, angle} = calculate_line(prev, next) - smoothing = if options.line, do: options.line.smoothing, else: 0 angle = if direction == :right, do: angle + :math.pi(), else: angle - length = length * smoothing + length = length * options.smoothing { curr.x + :math.cos(angle) * length, diff --git a/test/sparkline_test.exs b/test/sparkline_test.exs index 07e01ab..18b3f07 100644 --- a/test/sparkline_test.exs +++ b/test/sparkline_test.exs @@ -150,11 +150,11 @@ defmodule SparklineTest do test "to_svg/2 with non-default options (for line)" do assert Sparkline.new([{1, 1}, {2, 2}]) - |> Sparkline.show_line(width: 1, color: "red", smoothing: 0) + |> Sparkline.show_line(width: 1, color: "red") |> Sparkline.to_svg() == {:ok, """ - \n \n \n\n \n + \n \n \n\n \n """} end @@ -164,14 +164,14 @@ defmodule SparklineTest do |> Sparkline.to_svg() == {:ok, """ - \n \n\n \n \n + \n \n\n \n \n """} end test "to_svg/2 with non-default options (all)" do - assert Sparkline.new([{1, 1}, {2, 2}], width: 10, height: 10, padding: 1) + assert Sparkline.new([{1, 1}, {2, 2}], width: 10, height: 10, padding: 1, smoothing: 0) |> Sparkline.show_dots(radius: 2, color: "red") - |> Sparkline.show_line(width: 1, color: "red", smoothing: 0) + |> Sparkline.show_line(width: 1, color: "red") |> Sparkline.show_area(color: "red") |> Sparkline.to_svg() == {:ok,