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
5 changes: 4 additions & 1 deletion lib/matplotex/blueprint/chord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ defmodule Matplotex.Blueprint.Chord do
element: [],
legend_pos: nil,
border: nil,
show_legend: @false_by_default
show_legend: @false_by_default,
region_title: nil,
region_legend: nil,
region_content: nil
]
|> Keyword.merge(opts)
)
Expand Down
14 changes: 7 additions & 7 deletions lib/matplotex/element/rad_legend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ defmodule Matplotex.Element.RadLegend do
x: x,
y: y,
width: width,
height: height,
label_margin: label_margin
} = legend
height: height
} = legend, legend_font
) do

%{
legend
| label: %Label{
x: x + width + label_margin,
y: y - height / 4,
x: x + width ,
y: y + height / 2,
text: text,
type: @label_type,
text_anchor: "start"
type: @label_type
}
|> Label.cast_label(legend_font)
}
end
end
6 changes: 6 additions & 0 deletions lib/matplotex/figure.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ defmodule Matplotex.Figure do
def new(opts) do
struct(__MODULE__, opts)
end
# TODO: put error message in error
# def put_error(figure, opts) do

# end

def add_label(%__MODULE__{axes: %module{} = axes} = figure, label, opts),
do: %{figure | axes: module.add_label(axes, label, opts)}
Expand Down Expand Up @@ -110,4 +114,6 @@ defmodule Matplotex.Figure do
defp update_rc_params(_, _) do
raise Matplotex.InputError, keys: [:rc_params], message: "Invalid Input"
end


end
116 changes: 113 additions & 3 deletions lib/matplotex/figure/areal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ defmodule Matplotex.Figure.Areal do
alias Matplotex.Figure.Dataset

alias Matplotex.Figure.Text
alias Matplotex.Figure.Areal.Region
alias Matplotex.Figure.RcParams
@default_tick_minimum 0
@zero_to_move 0
def add_label(%__MODULE__{label: nil} = axes, {key, label}, opts) when is_binary(label) do
label =
Map.new()
Expand Down Expand Up @@ -143,7 +146,7 @@ defmodule Matplotex.Figure.Areal do

def materialized_by_region(figure) do
figure
|> Lead.set_regions()
|> Lead.set_regions_areal()
|> Cast.cast_xticks_by_region()
|> Cast.cast_yticks_by_region()
|> Cast.cast_hgrids_by_region()
Expand Down Expand Up @@ -211,11 +214,119 @@ defmodule Matplotex.Figure.Areal do
def set_frame_size(%__MODULE__{} = axes, frame_size) do
%__MODULE__{axes | size: frame_size}
end

def set_region_title(
%Figure{
axes:
%{
title: title,
region_x: %Region{width: region_x_width},
region_y: %Region{height: region_y_height} = region_y,
region_title: region_title,
size: {_f_width, _f_height},
border: {lx, _by, _, ty}
} = axes,
rc_params: %RcParams{title_font: title_font}
} = figure
) do
space_for_title = Lead.height_required_for_text(title_font, title)

{x_region_title, y_region_title} =
Algebra.transform_given_point(@zero_to_move, -space_for_title, lx, ty)

%Figure{
figure
| axes: %{
axes
| region_title: %Region{
region_title
| x: x_region_title,
y: y_region_title + space_for_title,
width: region_x_width,
height: space_for_title
},
region_y: %Region{
region_y
| height: region_y_height - space_for_title
}
}
}
end

def set_region_title(figure), do: figure


def set_region_legend(
%Figure{
axes:
%{
show_legend: true,
region_x: %Region{width: region_x_width} = region_x,
region_title: %Region{height: region_title_height},
region_legend: region_legend,
size: {f_width, _f_height},
border: {_lx, by, rx, ty}
} = axes,
rc_params: %RcParams{legend_width: legend_width}
} = figure
) do
region_legend_width = f_width * legend_width
region_x_width_after_legend = region_x_width - region_legend_width

{x_region_legend, y_region_legend} =
Algebra.transform_given_point(-region_legend_width, -region_title_height, rx, ty, 0)

%Figure{
figure
| axes: %{
axes
| region_x: %Region{
region_x
| width: region_x_width_after_legend
},
region_legend: %Region{
region_legend
| x: x_region_legend,
y: y_region_legend,
width: region_legend_width,
height: y_region_legend - by
}
}
}
end
def set_region_legend(figure), do: figure
def set_region_content(
%Figure{
axes:
%{
region_x: %Region{x: x_region_x, width: region_x_width},
region_y: %Region{y: y_region_y, height: region_y_height},
region_content: region_content
} = axes
} = figure
) do
%Figure{
figure
| axes: %{
axes
| region_content: %Region{
region_content
| x: x_region_x,
y: y_region_y,
width: region_x_width,
height: region_y_height
}
}
}
end
def set_region_content(figure), do: figure
end
end

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
Expand All @@ -224,7 +335,6 @@ defmodule Matplotex.Figure.Areal do
transformation(x, y, xminmax, yminmax, width, height, transition)
end


def transformation(
x,
y,
Expand All @@ -246,11 +356,11 @@ defmodule Matplotex.Figure.Areal do
x
|> Enum.zip(y)
|> Enum.map(fn {x, y} ->

x
|> transformation(y, xlim, ylim, width, height, transition)
|> Algebra.flip_y_coordinate()
end)

%Dataset{dataset | transformed: transformed}
end
end
9 changes: 7 additions & 2 deletions lib/matplotex/figure/areal/bar_chart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +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 | rc_params: %RcParams{white_space: width, y_padding: 0}, 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
Expand Down Expand Up @@ -146,7 +151,7 @@ defmodule Matplotex.Figure.Areal.BarChart do

defp hypox(y) do
nof_x = length(y)
@xmin_value|>Nx.linspace(nof_x, n: nof_x)|>Nx.to_list()
@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
8 changes: 7 additions & 1 deletion lib/matplotex/figure/areal/region.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
defmodule Matplotex.Figure.Areal.Region do
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
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}
Expand Down
20 changes: 12 additions & 8 deletions lib/matplotex/figure/cast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ defmodule Matplotex.Figure.Cast do
rc_params: %RcParams{line_width: line_width}
} = figure
) do
{lx, by} = Algebra.flip_y_coordinate({lx, by})
{rx, ty} = Algebra.flip_y_coordinate({rx, ty})
left = %Line{x1: lx, y1: by, x2: lx, y2: ty, type: "border.left", stroke_width: line_width}
right = %Line{x1: rx, y1: by, x2: rx, y2: ty, type: "border.right", stroke_width: line_width}
top = %Line{x1: lx, x2: rx, y1: ty, y2: ty, type: "border.top", stroke_width: line_width}
Expand All @@ -158,14 +160,14 @@ defmodule Matplotex.Figure.Cast do
%Figure{
axes:
%{
coords: %Coords{title: title_coord} = coords,
region_title: region_title,
title: title,
element: elements
} = axes,
rc_params: %RcParams{title_font: title_font}
} = figure
) do
{ttx, tty} = calculate_center(coords, title_coord, :x)
{ttx, tty} = calculate_center(region_title, :x)

title =
%Label{
Expand All @@ -189,14 +191,15 @@ defmodule Matplotex.Figure.Cast do
%Figure{
axes:
%{
region_title: region,
region_title: region_title,
title: title,
element: elements
} = axes,
rc_params: %RcParams{title_font: title_font, label_padding: title_padding}
} = figure
) do
{title_x, title_y} = region |> calculate_center(:x) |> Algebra.flip_y_coordinate()

{title_x, title_y} = region_title |> calculate_center(:x) |> Algebra.flip_y_coordinate()

title =
%Label{
Expand Down Expand Up @@ -430,7 +433,6 @@ defmodule Matplotex.Figure.Cast do
} = figure
)
when is_list(x_ticks) do

x_ticks = confine_ticks(x_ticks, xlim)
x_data = confine_data(x_data, xlim)
dataset = confine_data(dataset, xlim, :x)
Expand All @@ -448,12 +450,12 @@ defmodule Matplotex.Figure.Cast do
Enum.map(ticks_width_position, fn {tick_position, label} ->
{_, y_x_tick_line} =
@zero_to_move
|>Algebra.transform_given_point(height_region_x, x_region_x, y_region_x)
|> Algebra.transform_given_point(height_region_x, x_region_x, y_region_x)
|> Algebra.flip_y_coordinate()

{x_x_tick, y_x_tick} =
tick_position
|>Algebra.transform_given_point(
|> Algebra.transform_given_point(
@zero_to_move,
x_region_x_with_padding,
y_x_tick
Expand All @@ -480,6 +482,7 @@ defmodule Matplotex.Figure.Cast do
{%Tick{type: @xtick_type, tick_line: line, label: label}, {x_x_tick, y_x_tick_line}}
end)
|> Enum.unzip()

elements = elements ++ x_tick_elements

%Figure{
Expand All @@ -498,6 +501,7 @@ defmodule Matplotex.Figure.Cast do
defp format_tick_label({label, _index}), do: label
defp format_tick_label(label) when is_float(label), do: Float.round(label, 2)
defp format_tick_label(label), do: label

defp content_linespace(number_of_ticks_required, axis_size) do
@lowest_tick |> Nx.linspace(axis_size, n: number_of_ticks_required) |> Nx.to_list()
end
Expand Down Expand Up @@ -630,7 +634,7 @@ defmodule Matplotex.Figure.Cast do

{x_y_tick, y_y_tick} =
@zero_to_move
|>Algebra.transform_given_point(
|> Algebra.transform_given_point(
tick_position,
x_y_tick,
y_region_y_with_padding
Expand Down
Loading
Loading