Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotex/blueprint/frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ defmodule Matplotex.Blueprint.Frame do
show_h_grid: @show_by_default,
show_x_ticks: @show_by_default,
show_y_ticks: @show_by_default,
show_ticks: @show_by_default
show_ticks: @show_by_default,
border: nil
]
|> Keyword.merge(opts)
)
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotex/element/label.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ defmodule Matplotex.Element.Label do
"""
end

def cast_label(label, font) do
font = Map.from_struct(font)
struct(label, font)
end

defp rotate(%{rotate: nil}), do: nil

defp rotate(%{rotate: rotate, x: x, y: y}),
Expand Down
82 changes: 36 additions & 46 deletions lib/matplotex/figure/areal.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Matplotex.Figure.Areal do
alias Matplotex.Utils.Algebra
alias Matplotex.Figure.Dataset
alias Matplotex.Figure.TwoD
@callback create(struct(), any(), keyword()) :: struct()
Expand Down Expand Up @@ -29,7 +30,7 @@ defmodule Matplotex.Figure.Areal do
alias Matplotex.Figure.Dataset

alias Matplotex.Figure.Text

@default_tick_minimum 0
def add_label(%__MODULE__{label: nil} = axes, {key, label}, opts) when is_binary(label) do
label =
Map.new()
Expand Down Expand Up @@ -61,15 +62,18 @@ defmodule Matplotex.Figure.Areal do
end

def add_ticks(%__MODULE__{tick: tick} = axes, {key, ticks}) when is_list(ticks) do
ticks =
{ticks, lim} =
if number_based?(ticks) do
ticks
{ticks, Enum.min_max(ticks)}
else
Enum.with_index(ticks, 1)
{Enum.with_index(ticks), {@default_tick_minimum, length(ticks)}}
end

tick = Map.put(tick, key, ticks)
update_tick(axes, tick)

axes
|> set_limit({key, lim})
|> update_tick(tick)
end

def add_ticks(%__MODULE__{tick: tick, size: size} = axes, {key, {_min, _max} = lim}) do
Expand All @@ -78,18 +82,18 @@ defmodule Matplotex.Figure.Areal do
tick = Map.put(tick, key, ticks)

axes
|> set_limit(lim)
|> set_limit({key, lim})
|> update_tick(tick)
end

def hide_v_grid(axes) do
%{axes | show_v_grid: false}
end

def add_ticks(_, _) do
raise Matplotex.InputError, keys: [:tick], message: "Invalid Input"
end

def hide_v_grid(axes) do
%{axes | show_v_grid: false}
end

def set_limit(%__MODULE__{limit: limit} = axes, {key, {_, _} = lim}) do
limit = Map.put(limit, key, lim)
update_limit(axes, limit)
Expand Down Expand Up @@ -137,16 +141,16 @@ defmodule Matplotex.Figure.Areal do

defp update_limit(limit, _, _), do: limit

def materialized(figure) do
def materialized_by_region(figure) do
figure
|> Lead.set_spines()
|> Cast.cast_xticks()
|> Cast.cast_yticks()
|> Cast.cast_hgrids()
|> Cast.cast_vgrids()
|> Cast.cast_spines()
|> Cast.cast_label()
|> Cast.cast_title()
|> Lead.set_regions()
|> Cast.cast_xticks_by_region()
|> Cast.cast_yticks_by_region()
|> Cast.cast_hgrids_by_region()
|> Cast.cast_vgrids_by_region()
|> Cast.cast_spines_by_region()
|> Cast.cast_label_by_region()
|> Cast.cast_title_by_region()
end

defp update_tick(axes, tick) do
Expand Down Expand Up @@ -209,19 +213,18 @@ defmodule Matplotex.Figure.Areal do
end
end
end

@tensor_data_type_bits 64

alias Nx

def transformation({_label, value}, y, xminmax, yminmax, width, height, transition) do
transformation(value, y, xminmax, yminmax, width, height, transition)
def transformation({_labelx, x}, {_labely, y}, xminmax, yminmax, width, height, transition) do
transformation(x, y, xminmax, yminmax, width, height, transition)
end
def transformation({_label, x}, y, xminmax, yminmax, width, height, transition) do
transformation(x, y, xminmax, yminmax, width, height, transition)
end

def transformation(x, {_label, value}, y, xminmax, yminmax, width, transition) do
transformation(x, value, y, xminmax, yminmax, width, transition)
def transformation(x, {_label, y}, xminmax, yminmax, width, height, transition) do
transformation(x, y, xminmax, yminmax, width, height, transition)
end


def transformation(
x,
y,
Expand All @@ -233,34 +236,21 @@ defmodule Matplotex.Figure.Areal do
) do
sx = svg_width / (xmax - xmin)
sy = svg_height / (ymax - ymin)

tx = transition_x - xmin * sx
ty = transition_y - ymin * sy

# TODO: work for the datasets which has values in a range way far from zero in both directi
point_matrix = Nx.tensor([x, y, 1], type: {:f, @tensor_data_type_bits})

Nx.tensor(
[
[sx, 0, tx],
[0, sy, ty],
[0, 0, 1]
],
type: {:f, @tensor_data_type_bits}
)
|> Nx.dot(point_matrix)
|> Nx.to_flat_list()
|> then(fn [x_trans, y_trans, _] -> {x_trans, y_trans} end)
Algebra.transform_given_point(x, y, sx, sy, tx, ty)
end

def do_transform(%Dataset{x: x, y: y} = dataset, xlim, ylim, width, height, transition) do
transformed =
x
|> Enum.zip(y)
|> Enum.map(fn {x, y} ->
transformation(x, y, xlim, ylim, width, height, transition)
end)

x
|> transformation(y, xlim, ylim, width, height, transition)
|> Algebra.flip_y_coordinate()
end)
%Dataset{dataset | transformed: transformed}
end
end
39 changes: 25 additions & 14 deletions lib/matplotex/figure/areal/bar_chart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule Matplotex.Figure.Areal.BarChart do
alias Matplotex.Figure.Areal
use Areal

@xmin_value 0

frame(
legend: %Legend{},
coords: %Coords{},
Expand All @@ -33,12 +35,12 @@ defmodule Matplotex.Figure.Areal.BarChart do
dataset = Dataset.cast(%Dataset{x: x, y: values, pos: pos, width: width}, opts)
datasets = data ++ [dataset]
xydata = flatten_for_data(datasets)
%Figure{figure | axes: %{axes | data: xydata, dataset: datasets}}
%Figure{figure | rc_params: %RcParams{white_space: width, y_padding: 0}, axes: %{axes | data: xydata, dataset: datasets}}
end

@impl Areal
def materialize(figure) do
__MODULE__.materialized(figure)
__MODULE__.materialized_by_region(figure)
|> materialize_bars()
end

Expand All @@ -48,24 +50,32 @@ defmodule Matplotex.Figure.Areal.BarChart do
%{
dataset: data,
limit: %{x: xlim, y: ylim},
size: {width, height},
coords: %Coords{bottom_left: {blx, bly}},
region_content: %Region{
x: x_region_content,
y: y_region_content,
width: width_region_content,
height: height_region_content
},
element: elements
} = axes,
rc_params: %RcParams{x_padding: padding}
rc_params: %RcParams{x_padding: x_padding, white_space: white_space}
} = figure
) do
px = width * padding
width = width - px * 2
py = height * padding
height = height - py * 2
x_padding_value = width_region_content * x_padding + white_space
shrinked_width_region_content = width_region_content - x_padding_value * 2

bar_elements =
data
|> Enum.map(fn dataset ->
dataset
|> do_transform(xlim, ylim, width, height, {blx + px, bly + py})
|> capture(bly)
|> do_transform(
xlim,
ylim,
shrinked_width_region_content,
height_region_content,
{x_region_content + x_padding_value, y_region_content}
)
|> capture(-y_region_content)
end)
|> List.flatten()

Expand All @@ -92,7 +102,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
def generate_ticks(data) do
max = Enum.max(data)
step = max |> round_to_best() |> div(5) |> round_to_best()
{list_of_ticks(data, step), {0, max}}
{list_of_ticks(data, step), {@xmin_value, max}}
end

def generate_ticks(side, {min, max} = lim) do
Expand Down Expand Up @@ -123,7 +133,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
x: bar_position(x, pos_factor),
y: y,
width: width,
height: y - bly,
height: bly - y,
color: color
}
],
Expand All @@ -135,7 +145,8 @@ defmodule Matplotex.Figure.Areal.BarChart do
defp capture([], captured, _dataset, _bly), do: captured

defp hypox(y) do
1..length(y) |> Enum.into([])
nof_x = length(y)
@xmin_value|>Nx.linspace(nof_x, n: nof_x)|>Nx.to_list()
end

defp bar_position(x, pos_factor) when pos_factor < 0 do
Expand Down
27 changes: 19 additions & 8 deletions lib/matplotex/figure/areal/line_plot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Matplotex.Figure.Areal.LinePlot do
@impl Areal
def materialize(figure) do
figure
|> __MODULE__.materialized()
|> __MODULE__.materialized_by_region()
|> materialize_lines()
end

Expand All @@ -53,23 +53,34 @@ defmodule Matplotex.Figure.Areal.LinePlot do
%{
dataset: data,
limit: %{x: xlim, y: ylim},
size: {width, height},
coords: %Coords{bottom_left: {blx, bly}},
region_content: %Region{
x: x_region_content,
y: y_region_content,
width: width_region_content,
height: height_region_content
},
element: elements
} = axes,
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}
} = figure
) do
px = width * x_padding
py = height * y_padding
width = width - px * 2
height = height - py * 2
# {x_region_content, y_region_content} = Algebra.flip_y_coordinate({x_region_content, y_region_content})
x_padding_value = width_region_content * x_padding
y_padding_value = height_region_content * y_padding
shrinked_width_region_content = width_region_content - x_padding_value * 2
shrinked_height_region_content = height_region_content - y_padding_value * 2

line_elements =
data
|> Enum.map(fn dataset ->
dataset
|> do_transform(xlim, ylim, width, height, {blx + px, bly + py})
|> do_transform(
xlim,
ylim,
shrinked_width_region_content,
shrinked_height_region_content,
{x_region_content + x_padding_value, y_region_content + y_padding_value}
)
|> capture()
end)
|> List.flatten()
Expand Down
12 changes: 11 additions & 1 deletion lib/matplotex/figure/areal/region.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
defmodule Matplotex.Figure.Areal.Region do
defstruct [:x, :y, :width, :height, :name, :theta, :coords]
alias Matplotex.Figure.Areal.XyRegion.Coords
@zero_by_default 0
defstruct x: @zero_by_default, y: @zero_by_default, width: @zero_by_default, height: @zero_by_default, name: nil, theta: @zero_by_default, coords: nil

def get_label_coords(%__MODULE__{x: x, y: y, coords: %Coords{label: {label_x, label_y}}}) do
{x + label_x, y + label_y}
end

def get_tick_coords(%__MODULE__{x: x, y: y, coords: %Coords{ticks: {tick_x, tick_y}}}) do
{x + tick_x, y + tick_y}
end
end
28 changes: 19 additions & 9 deletions lib/matplotex/figure/areal/scatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Matplotex.Figure.Areal.Scatter do

@impl Areal
def materialize(figure) do
__MODULE__.materialized(figure)
__MODULE__.materialized_by_region(figure)
|> materialize_elements()
end

Expand All @@ -48,23 +48,33 @@ defmodule Matplotex.Figure.Areal.Scatter do
%{
dataset: data,
limit: %{x: xlim, y: ylim},
size: {width, height},
coords: %Coords{bottom_left: {blx, bly}},
region_content: %Region{
x: x_region_content,
y: y_region_content,
width: width_region_content,
height: height_region_content
},
element: elements
} = axes,
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}
} = figure
) do
px = width * x_padding
py = height * y_padding
width = width - px * 2
height = height - py * 2
x_padding_value = width_region_content * x_padding
y_padding_value = height_region_content * y_padding
shrinked_width_region_content = width_region_content - x_padding_value * 2
shrinked_height_region_content = height_region_content - y_padding_value * 2

line_elements =
data
|> Enum.map(fn dataset ->
dataset
|> do_transform(xlim, ylim, width, height, {blx + px, bly + py})
|> do_transform(
xlim,
ylim,
shrinked_width_region_content,
shrinked_height_region_content,
{x_region_content + x_padding_value, y_region_content + y_padding_value}
)
|> capture()
end)
|> List.flatten()
Expand All @@ -74,7 +84,7 @@ defmodule Matplotex.Figure.Areal.Scatter do
end

def materialize(xystream, figure) do
__MODULE__.materialized(figure)
__MODULE__.materialized_by_region(figure)
|> material_stream(xystream)
end

Expand Down
Loading
Loading