Skip to content

Commit

Permalink
feat: add backwards compatibility with 'smoothed' option
Browse files Browse the repository at this point in the history
  • Loading branch information
larshei committed Aug 12, 2024
1 parent f5a781d commit 9a75931
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/chart/lineplot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ defmodule Contex.LinePlot do
custom_y_formatter: nil,
width: 100,
height: 100,
plot_style: :smooth,
stroke_width: "2",
colour_palette: :default
]
Expand Down Expand Up @@ -108,6 +107,8 @@ defmodule Contex.LinePlot do
- `:plot_style` : :direct (default), :smooth or :step
For backwards compatibility, the option :smoothed was kept and is translated: true -> :smooth and false -> :direct.
Note that the smoothing algorithm is a cardinal spline with tension = 0.3.
You may get strange effects (e.g. loops / backtracks) in certain circumstances, e.g.
if the x-value spacing is very uneven. This alogorithm forces the smoothed line
Expand Down Expand Up @@ -254,7 +255,10 @@ defmodule Contex.LinePlot do
y_accessor,
colour
) do
style = get_option(plot, :plot_style)
# keep backwards compatibility with old `:smoothed` option
style = get_option(plot, :plot_style) |> IO.inspect(label: "style")
smoothed = get_option(plot, :smoothed) |> IO.inspect(label: "smoothed")
plot_style = if smoothed != nil, do: smoothed, else: style
stroke_width = get_option(plot, :stroke_width)

options = [
Expand Down Expand Up @@ -282,7 +286,7 @@ defmodule Contex.LinePlot do
|> Enum.chunk_by(fn {_x, y} -> is_nil(y) end)
|> Enum.filter(fn [{_x, y} | _] -> not is_nil(y) end)

Enum.map(points_list, fn points -> line(points, style, options) end)
Enum.map(points_list, fn points -> line(points, plot_style, options) end)
end

@doc false
Expand Down
4 changes: 4 additions & 0 deletions lib/chart/svg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ defmodule Contex.SVG do

defp path([], _), do: ""

defp path(points, nil), do: path(points, :smooth)
defp path(points, true), do: path(points, IO.inspect(:smooth))
defp path(points, false), do: path(points, IO.inspect(:direct))

defp path(points, :direct) do
Enum.reduce(points, :first, fn {x, y}, acc ->
coord = ~s|#{x} #{y}|
Expand Down
8 changes: 6 additions & 2 deletions test/contex_line_chart_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ defmodule ContexLineChartTest do
C330.6666666666667,237.5 388.3333333333333,60 404,20 "
"""

output = Contex.SVG.line(points, :smooth) |> IO.iodata_to_binary()
IO.inspect(output)
output_new = Contex.SVG.line(points, :smooth) |> IO.iodata_to_binary()
output_old = Contex.SVG.line(points, true) |> IO.iodata_to_binary()

assert output_new == output_old

IO.inspect(output_new)
end
end
end

0 comments on commit 9a75931

Please sign in to comment.