Skip to content

Commit e16c83d

Browse files
Merge pull request #25 from BigThinkcode/spline_curve
Spline curve
2 parents 46cf1d2 + 5736eec commit e16c83d

File tree

22 files changed

+290
-81
lines changed

22 files changed

+290
-81
lines changed

lib/matplotex.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule Matplotex do
22
@moduledoc """
33
Module to generate a graph.
44
"""
5+
alias Matplotex.Figure.Areal.Spline
56
alias Matplotex.Figure.Areal.Histogram
67
alias Matplotex.InputError
78
alias Matplotex.Figure.Radial.Pie
@@ -33,6 +34,9 @@ defmodule Matplotex do
3334
|> BarChart.create({pos, values, width}, opts)
3435
end
3536

37+
@doc """
38+
Creates a scatter plot based on the given data
39+
"""
3640
def scatter(stream, opts) when is_struct(stream, Stream) do
3741
Scatter.create(stream, opts)
3842
end
@@ -51,6 +55,9 @@ defmodule Matplotex do
5155
|> Scatter.create({x, y}, opts)
5256
end
5357

58+
@doc """
59+
Creates a piec charts based on the size and opts
60+
"""
5461
def pie(sizes, opts \\ []) do
5562
Pie.create(%Figure{axes: %Pie{}}, sizes, opts)
5663
end
@@ -84,10 +91,26 @@ defmodule Matplotex do
8491
|> LinePlot.create({x, y}, opts)
8592
end
8693

94+
@doc """
95+
Creates a histogram with given data and bins.
96+
97+
## Examples
98+
99+
iex> Matplotex.hist([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5)
100+
"""
101+
def hist(data, bins), do: hist(data, bins, [])
102+
87103
def hist(data, bins, opts) do
88104
Histogram.create(%Figure{axes: %Histogram{}}, {data, bins}, opts)
89105
end
90106

107+
def spline(x, y), do: spline(x, y, [])
108+
def spline(x, y, opts), do: spline(%Figure{axes: %Spline{}}, x, y, opts)
109+
110+
def spline(%Figure{} = figure, x, y, opts) do
111+
Spline.create(figure, {x, y}, opts)
112+
end
113+
91114
@doc """
92115
Sets X and Y labels for the graph with given font details
93116

lib/matplotex/element.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
defmodule Matplotex.Element do
22
@callback assemble(element :: struct()) :: String.t()
3-
@callback flipy(element :: struct(), height :: number()) :: struct()
43
def assemble(%module{} = element), do: module.assemble(element)
54

5+
def to_pixel({x, y}) do
6+
"#{to_pixel(x)},#{to_pixel(y)}"
7+
end
8+
69
def to_pixel(inch) when is_number(inch), do: inch * 96
710
def to_pixel(_), do: 0
811

lib/matplotex/element/circle.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,4 @@ defmodule Matplotex.Element.Circle do
3232
def get_cx(%{cx: x}), do: to_pixel(x)
3333
def get_cy(%{cy: y}), do: to_pixel(y)
3434
def get_r(%{r: r}), do: to_pixel(r)
35-
@impl Element
36-
def flipy(%__MODULE__{cy: y} = circle, height) do
37-
%__MODULE__{circle | cy: height - y}
38-
end
3935
end

lib/matplotex/element/label.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,4 @@ defmodule Matplotex.Element.Label do
7171
def get_y(%{y: y}), do: to_pixel(y)
7272
def get_width(%{width: width}), do: to_pixel(width)
7373
def get_height(%{height: height}), do: to_pixel(height)
74-
@impl Element
75-
def flipy(%__MODULE__{y: y} = label, height) do
76-
%__MODULE__{label | y: height - y}
77-
end
7874
end

lib/matplotex/element/legend.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ defmodule Matplotex.Element.Legend do
3030
"""
3131
end
3232

33-
@impl Element
34-
def flipy(%__MODULE__{y: y} = legend, height) do
35-
%__MODULE__{legend | y: height - y, label: Label.flipy(legend.label, height)}
36-
end
37-
3833
defp handle(%__MODULE__{handle: %handle_element{} = handle}) do
3934
handle_element.assemble(handle)
4035
end

lib/matplotex/element/line.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ defmodule Matplotex.Element.Line do
5252
def get_x2(%{x2: x2}), do: to_pixel(x2)
5353
def get_y1(%{y1: y1}), do: to_pixel(y1)
5454
def get_y2(%{y2: y2}), do: to_pixel(y2)
55-
@impl Element
56-
def flipy(%__MODULE__{y1: y1, y2: y2} = line, height) do
57-
%__MODULE__{line | y1: height - y1, y2: height - y2}
58-
end
5955

6056
defp stroke_dasharray(%{linestyle: "_"}), do: nil
6157

lib/matplotex/element/polygon.ex

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ defmodule Matplotex.Element.Polygon do
1515
)
1616
end
1717

18-
@impl Element
19-
def flipy(%__MODULE__{points: point} = label, height) do
20-
%__MODULE__{label | points: flip_point(point, height)}
21-
end
22-
23-
defp flip_point(point, height) do
24-
Enum.map(point, &flip_coord(&1, height))
25-
end
26-
27-
defp flip_coord({x, y}, height) do
28-
{x, height - y}
29-
end
3018

3119
defp assemble_point(%{points: point}) do
3220
for {x, y} <- point do

lib/matplotex/element/rad_legend.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ defmodule Matplotex.Element.RadLegend do
3030
"""
3131
end
3232

33-
@impl Element
34-
def flipy(%__MODULE__{y: y} = legend, height) do
35-
%__MODULE__{legend | y: height - y, label: Label.flipy(legend.label, height)}
36-
end
37-
3833
def with_label(
3934
%__MODULE__{
4035
label: text,

lib/matplotex/element/rect.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,4 @@ defmodule Matplotex.Element.Rect do
5050
def get_y(%{y: y}), do: to_pixel(y)
5151
def get_width(%{width: width}), do: to_pixel(width)
5252
def get_height(%{height: height}), do: to_pixel(height)
53-
@impl Element
54-
def flipy(%__MODULE__{y: y} = rect, height) do
55-
%__MODULE__{rect | y: height - y}
56-
end
5753
end

lib/matplotex/element/slice.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,4 @@ defmodule Matplotex.Element.Slice do
3737
def get_cx(%{cx: cx}), do: to_pixel(cx)
3838
def get_cy(%{cy: cy}), do: to_pixel(cy)
3939
def get_radius(%{radius: radius}), do: to_pixel(radius)
40-
@impl Element
41-
def flipy(slice, _height) do
42-
slice
43-
end
4440
end

0 commit comments

Comments
 (0)