Skip to content

Commit

Permalink
Move smoothing option from line to general
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelaz3r committed Feb 6, 2024
1 parent d8b5e45 commit efdc6e5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/sparkline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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())
Expand All @@ -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(),
Expand All @@ -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})
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 1 addition & 2 deletions lib/sparkline/draw.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions test/sparkline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
"""
<svg width="100%" height="100%"\n viewBox="0 0 200 100"\n xmlns="http://www.w3.org/2000/svg">\n \n <path\n d="M6.0,94.0C6.0,94.0 194.0,6.0 194.0,6.0"\n fill="none"\n stroke="red"\n stroke-width="1" />\n\n \n</svg>
<svg width="100%" height="100%"\n viewBox="0 0 200 100"\n xmlns="http://www.w3.org/2000/svg">\n \n <path\n d="M6.0,94.0C43.6,76.4 156.4,23.6 194.0,6.0"\n fill="none"\n stroke="red"\n stroke-width="1" />\n\n \n</svg>
"""}
end

Expand All @@ -164,14 +164,14 @@ defmodule SparklineTest do
|> Sparkline.to_svg() ==
{:ok,
"""
<svg width="100%" height="100%"\n viewBox="0 0 200 100"\n xmlns="http://www.w3.org/2000/svg">\n <path\n d="M6.0,94.0C6.0,94.0 194.0,6.0 194.0,6.0V100H6.0Z"\n fill="red"\n stroke="none" />\n\n \n \n</svg>
<svg width="100%" height="100%"\n viewBox="0 0 200 100"\n xmlns="http://www.w3.org/2000/svg">\n <path\n d="M6.0,94.0C43.6,76.4 156.4,23.6 194.0,6.0V100H6.0Z"\n fill="red"\n stroke="none" />\n\n \n \n</svg>
"""}
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,
Expand Down

0 comments on commit efdc6e5

Please sign in to comment.