|
| 1 | +defmodule Matplotex.Figure.Areal.Step do |
| 2 | + |
| 3 | + alias Matplotex.Figure.Areal.LinePlot |
| 4 | + alias Matplotex.Figure |
| 5 | + alias Matplotex.InputError |
| 6 | + |
| 7 | + def create(x, y, opts) do |
| 8 | + {x, y} = step_segments(x, y) |
| 9 | + LinePlot.create(%Figure{axes: %LinePlot{}}, {x, y}, opts) |
| 10 | + end |
| 11 | + def create(%Figure{} = figure, x, y, opts) do |
| 12 | + {x, y} = step_segments(x, y) |
| 13 | + LinePlot.create(figure, {x, y}, opts) |
| 14 | + end |
| 15 | + defp step_segments(x, y) do |
| 16 | + x = horizontal_segments(x) |
| 17 | + y = vertical_segments(y) |
| 18 | + {x,y} |
| 19 | + end |
| 20 | + defp horizontal_segments(data) when is_list(data) do |
| 21 | + first_value = List.first(data) |
| 22 | + second_last_index = length(data) - 1 |
| 23 | + repeated = data|>Enum.slice(1..second_last_index)|>List.duplicate(2)|> List.flatten() |
| 24 | + [first_value | repeated]|>Enum.sort() |
| 25 | + end |
| 26 | + defp horizontal_segments(_data), do: list_error(:y) |
| 27 | + |
| 28 | + defp vertical_segments(data) when is_list(data) do |
| 29 | + data|>List.duplicate(2)|>List.flatten()|>Enum.sort() |
| 30 | + end |
| 31 | + defp vertical_segments(_data), do: list_error(:y) |
| 32 | + |
| 33 | + defp list_error(arg) do |
| 34 | + raise InputError, message: "Expected a list for arg: #{arg}" |
| 35 | + end |
| 36 | + |
| 37 | +end |
0 commit comments