Skip to content

Commit dd2253f

Browse files
author
Mohammed Sadique
committed
some cleanups
1 parent dcb9702 commit dd2253f

11 files changed

Lines changed: 26 additions & 244 deletions

File tree

lib/matplotex/figure/areal.ex

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Matplotex.Figure.Areal do
2+
alias Matplotex.Figure.Areal.Ticker
23
alias Matplotex.Utils.Algebra
34
alias Matplotex.Figure.Dataset
45
alias Matplotex.Figure.TwoD
@@ -79,8 +80,13 @@ defmodule Matplotex.Figure.Areal do
7980
|> update_tick(tick)
8081
end
8182

82-
def add_ticks(%__MODULE__{tick: tick, size: size} = axes, {key, {_min, _max} = lim}) do
83-
{ticks, lim} = __MODULE__.generate_ticks(lim)
83+
def add_ticks(%__MODULE__{tick: tick, size: {width, height}= size} = axes, {key, {_min, _max} = lim}) do
84+
number_of_ticks = if key == :x do
85+
ceil(width)
86+
else
87+
ceil(height)
88+
end
89+
{ticks, lim} = Ticker.generate_ticks(lim, number_of_ticks )
8490

8591
tick = Map.put(tick, key, ticks)
8692

@@ -116,23 +122,6 @@ defmodule Matplotex.Figure.Areal do
116122
%{axes | legend: legend}
117123
end
118124

119-
def generate_xticks(%module{data: {x, _y}, tick: tick, limit: limit} = axes) do
120-
{xticks, xlim} =
121-
module.generate_ticks(x)
122-
123-
tick = Map.put(tick, :x, xticks)
124-
limit = update_limit(limit, :x, xlim)
125-
%__MODULE__{axes | tick: tick, limit: limit}
126-
end
127-
128-
def generate_yticks(%module{data: {_x, y}, tick: tick, limit: limit} = axes) do
129-
{yticks, ylim} =
130-
module.generate_ticks(y)
131-
132-
tick = Map.put(tick, :y, yticks)
133-
limit = update_limit(limit, :y, ylim)
134-
%__MODULE__{axes | tick: tick, limit: limit}
135-
end
136125

137126
defp update_limit(%TwoD{x: nil} = limit, :x, xlim) do
138127
%TwoD{limit | x: xlim}

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,6 @@ defmodule Matplotex.Figure.Areal.BarChart do
9191
%Figure{figure | axes: %{axes | element: elements_with_bar}}
9292
end
9393

94-
def plotify(value, {minl, maxl}, axis_size, transition, _data, :x) do
95-
s = axis_size / (maxl - minl)
96-
value * s + transition - minl * s
97-
end
98-
99-
def plotify(value, {minl, maxl}, axis_size, transition, _data, :y) do
100-
s = axis_size / (maxl - minl)
101-
value * s + transition - minl * s
102-
end
103-
10494
@impl Areal
10595
def with_legend_handle(
10696
%Legend{x: x, y: y, color: color, width: width, height: height} = legend,

lib/matplotex/figure/areal/histogram.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ defmodule Matplotex.Figure.Areal.Histogram do
88
alias Matplotex.Figure.Areal
99
use Areal
1010

11+
@make_it_zero 0
12+
1113
frame(
1214
tick: %TwoD{},
1315
limit: %TwoD{},
1416
label: %TwoD{},
17+
scale: %TwoD{},
1518
region_x: %Region{},
1619
region_y: %Region{},
1720
region_title: %Region{},
@@ -20,12 +23,11 @@ defmodule Matplotex.Figure.Areal.Histogram do
2023
)
2124

2225
@impl Areal
23-
def create(%Figure{axes: %__MODULE__{} = axes} = figure, {data, bins}, opts) do
26+
def create(%Figure{axes: %__MODULE__{} = axes, rc_params: rc_params} = figure, {data, bins}, opts) do
2427
{x, y} = bins_and_hists(data, bins)
2528

2629
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)
27-
28-
%Figure{figure | axes: %__MODULE__{axes | data: {x, y}, dataset: [dataset]}}
30+
%Figure{figure | axes: %__MODULE__{axes | data: {x, y}, dataset: [dataset]}, rc_params: %RcParams{rc_params | y_padding: @make_it_zero}}
2931
|> PlotOptions.set_options_in_figure(opts)
3032
end
3133

@@ -119,8 +121,7 @@ defmodule Matplotex.Figure.Areal.Histogram do
119121
defp sanitize(%Figure{axes: %__MODULE__{data: {x, y}}= axes} = figure) do
120122
{ymin, ymax} = Enum.min_max(y)
121123
{xmin, xmax} = Enum.min_max(x)
122-
{xmin, xmax} = {floor(xmin), ceil(xmax)}
123124

124-
%Figure{figure | axes: %__MODULE__{axes | limit: %TwoD{y: {floor(ymin), ceil(ymax)}, x: xlim }}}
125+
%Figure{figure | axes: %__MODULE__{axes | limit: %TwoD{x: {floor(xmin), ceil(xmax)},y: {floor(ymin), ceil(ymax)}}}}
125126
end
126127
end

lib/matplotex/figure/areal/line_plot.ex

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ defmodule Matplotex.Figure.Areal.LinePlot do
9393
%Figure{figure | axes: %{axes | element: elements}}
9494
end
9595

96-
@impl Areal
97-
def plotify(value, {minl, maxl}, axis_size, transition, _, _) do
98-
s = axis_size / (maxl - minl)
99-
value * s + transition - minl * s
100-
end
101-
10296
@impl Areal
10397
def with_legend_handle(
10498
%Legend{x: x, y: y, color: color, width: marker_size} = legend,

lib/matplotex/figure/areal/scatter.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ defmodule Matplotex.Figure.Areal.Scatter do
132132
end
133133

134134
defp capture(_, captured, _), do: captured
135-
@impl Areal
136-
def plotify(value, {minl, maxl}, axis_size, transition, _, _) do
137-
s = axis_size / (maxl - minl)
138-
value * s + transition - minl * s
139-
end
140135

141136
@impl Areal
142137
def with_legend_handle(

lib/matplotex/figure/areal/ticker.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ defmodule Matplotex.Figure.Areal.Ticker do
44
step = (max - min) / @tick_in_plot
55
produce_ticks(min, max, step, [format_number(min)])
66
end
7+
def generate_ticks({lower_limit, upper_limit} = lim, number_of_ticks) do
8+
{lower_limit |> Nx.linspace(upper_limit, n: number_of_ticks) |> Nx.to_list(), lim}
9+
end
710

811
defp produce_ticks(value, max, _step, ticks) when value >= max do
912
Enum.reverse(ticks)

lib/matplotex/figure/cast.ex

Lines changed: 0 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule Matplotex.Figure.Cast do
1212
alias Matplotex.Figure.Coords
1313
alias Matplotex.Figure
1414
alias Matplotex.Figure.Dataset
15-
@tickline_offset 5 / 96
1615
@xtick_type "figure.x_tick"
1716
@ytick_type "figure.y_tick"
1817
@stroke_grid "#ddd"
@@ -326,85 +325,6 @@ defmodule Matplotex.Figure.Cast do
326325
%Figure{figure | axes: %{axes | element: element}}
327326
end
328327

329-
def cast_xticks(
330-
%Figure{
331-
axes:
332-
%module{
333-
tick: %{x: x_ticks},
334-
limit: %{x: {_min, _max} = xlim},
335-
size: {width, _height},
336-
data: {x_data, y_data},
337-
dataset: dataset,
338-
element: elements,
339-
show_x_ticks: true,
340-
coords: %Coords{bottom_left: {blx, bly}, x_ticks: {_xtx, xty}} = coords
341-
} = axes,
342-
rc_params: %RcParams{x_tick_font: tick_font, x_padding: x_padding}
343-
} = figure
344-
)
345-
when is_list(x_ticks) do
346-
# TODO: Only if it has to confine
347-
x_ticks = confine_ticks(x_ticks, xlim)
348-
x_data = confine_data(x_data, xlim)
349-
dataset = confine_data(dataset, xlim, :x)
350-
351-
{xtick_elements, vgridxs} =
352-
Enum.map(x_ticks, fn tick ->
353-
{tick_position, label} =
354-
plotify_tick(
355-
module,
356-
tick,
357-
xlim,
358-
width - width * x_padding * 2,
359-
blx + width * x_padding,
360-
x_data,
361-
:x
362-
)
363-
364-
label =
365-
%Label{
366-
type: @xtick_type,
367-
x: tick_position,
368-
y: xty,
369-
text: label
370-
}
371-
|> merge_structs(tick_font)
372-
373-
# TODO: find a mechanism to pass custom font for ticks
374-
line = %Line{
375-
type: @xtick_type,
376-
x1: tick_position,
377-
y1: bly,
378-
x2: tick_position,
379-
y2: bly - @tickline_offset
380-
}
381-
382-
{%Tick{type: @xtick_type, tick_line: line, label: label}, tick_position}
383-
end)
384-
|> Enum.unzip()
385-
386-
elements = elements ++ xtick_elements
387-
vgrids = Enum.map(vgridxs, fn g -> {g, bly} end)
388-
389-
%Figure{
390-
figure
391-
| axes: %{
392-
axes
393-
| data: {x_data, y_data},
394-
dataset: dataset,
395-
element: elements,
396-
coords: %{coords | vgrids: vgrids}
397-
}
398-
}
399-
end
400-
401-
def cast_xticks(%Figure{axes: %{tick: %{x: _}, limit: %{x: nil}, show_x_ticks: true}} = figure) do
402-
figure
403-
|> set_xlim_from_ticks()
404-
|> cast_xticks()
405-
end
406-
407-
def cast_xticks(%Figure{} = figure), do: figure
408328

409329
def cast_xticks_by_region(
410330
%Figure{
@@ -507,86 +427,6 @@ defmodule Matplotex.Figure.Cast do
507427
@lowest_tick |> Nx.linspace(axis_size, n: number_of_ticks_required) |> Nx.to_list()
508428
end
509429

510-
@spec cast_yticks(Matplotex.Figure.t()) :: Matplotex.Figure.t()
511-
def cast_yticks(
512-
%Figure{
513-
axes:
514-
%module{
515-
tick: %{y: y_ticks},
516-
size: {_width, height},
517-
element: elements,
518-
coords: %Coords{bottom_left: {blx, bly}, y_ticks: {ytx, _yty}} = coords,
519-
limit: %{y: {_min, _max} = ylim},
520-
data: {x_data, y_data},
521-
dataset: dataset,
522-
show_y_ticks: true
523-
} = axes,
524-
rc_params: %RcParams{y_tick_font: tick_font, y_padding: padding}
525-
} = figure
526-
) do
527-
y_ticks = confine_ticks(y_ticks, ylim)
528-
y_data = confine_data(y_data, ylim)
529-
dataset = confine_data(dataset, ylim, :y)
530-
531-
{ytick_elements, hgridys} =
532-
Enum.map(y_ticks, fn tick ->
533-
{tick_position, label} =
534-
plotify_tick(
535-
module,
536-
tick,
537-
ylim,
538-
height - height * padding * 2,
539-
bly + height * padding,
540-
y_data,
541-
:y
542-
)
543-
544-
label =
545-
%Label{
546-
type: @ytick_type,
547-
y: tick_position,
548-
x: ytx,
549-
text: label,
550-
text_anchor: "end",
551-
dominant_baseline: "middle"
552-
}
553-
|> merge_structs(tick_font)
554-
555-
# TODO: find a mechanism to pass custom font for ticks
556-
line = %Line{
557-
type: @ytick_type,
558-
y1: tick_position,
559-
x1: blx,
560-
x2: blx - @tickline_offset,
561-
y2: tick_position
562-
}
563-
564-
{%Tick{type: @ytick_type, tick_line: line, label: label}, tick_position}
565-
end)
566-
|> Enum.unzip()
567-
568-
elements = elements ++ ytick_elements
569-
hgrids = Enum.map(hgridys, fn g -> {blx, g} end)
570-
571-
%Figure{
572-
figure
573-
| axes: %{
574-
axes
575-
| data: {x_data, y_data},
576-
dataset: dataset,
577-
element: elements,
578-
coords: %{coords | hgrids: hgrids}
579-
}
580-
}
581-
end
582-
583-
def cast_yticks(%Figure{axes: %{tick: %{y: _}, limit: %{y: nil}, show_y_ticks: true}} = figure) do
584-
figure
585-
|> set_ylim_from_ticks()
586-
|> cast_yticks()
587-
end
588-
589-
def cast_ytick(%Figure{} = figure), do: figure
590430

591431
def cast_yticks_by_region(
592432
%Figure{
@@ -853,23 +693,6 @@ defmodule Matplotex.Figure.Cast do
853693

854694
def cast_legends(figure), do: figure
855695

856-
defp plotify_tick(module, {label, value}, lim, axis_size, transition, data, axis) do
857-
{module.plotify(value, lim, axis_size, transition, data, axis), label}
858-
end
859-
860-
defp plotify_tick(module, value, lim, axis_size, transition, data, axis) do
861-
{module.plotify(value, lim, axis_size, transition, data, axis), value}
862-
end
863-
864-
defp min_max([{_pos, _label} | _] = ticks) do
865-
ticks
866-
|> Enum.min_max_by(fn {_labe, pos} -> pos end)
867-
|> then(fn {{_label_min, pos_min}, {_label_max, pos_max}} -> {pos_min, pos_max} end)
868-
end
869-
870-
defp min_max(ticks) do
871-
Enum.min_max(ticks)
872-
end
873696

874697
defp calculate_center(%Coords{bottom_left: bottom_left, bottom_right: bottom_right}, {x, y}, :x) do
875698
{calculate_distance(bottom_left, bottom_right) / 2 + x, y}
@@ -899,24 +722,6 @@ defmodule Matplotex.Figure.Cast do
899722

900723
defp rotate_label(:y), do: -90
901724

902-
defp set_xlim_from_ticks(%Figure{axes: %module{tick: %{x: xtick}} = axes} = figure) do
903-
{xmin, xmax} = min_max(xtick)
904-
905-
xscale = xmax |> round() |> div(length(xtick) - 1)
906-
907-
xlim = {round(xmin - xscale), round(xmax + xscale)}
908-
axes = module.set_limit(axes, {:x, xlim})
909-
910-
%Figure{figure | axes: axes}
911-
end
912-
913-
defp set_ylim_from_ticks(%Figure{axes: %module{tick: %{y: ytick}} = axes} = figure) do
914-
{ymin, ymax} = min_max(ytick)
915-
yscale = ymax |> round() |> div(length(ytick) - 1)
916-
ylim = {round(ymin - yscale), round(ymax + yscale)}
917-
axes = module.set_limit(axes, {:y, ylim})
918-
%Figure{figure | axes: axes}
919-
end
920725

921726
defp confine_ticks([{_l, _v} | _] = ticks, {min, max}) do
922727
ticks

lib/matplotex/figure/lead.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ defmodule Matplotex.Figure.Lead do
5656
defp ensure_ticks_are_valid(
5757
%Figure{
5858
figsize: {width, height},
59+
rc_params: %RcParams{x_ticks_count: x_ticks_count, y_ticks_count: y_ticks_count},
5960
axes:
6061
%{
6162
data: {x_data, y_data},
@@ -64,8 +65,8 @@ defmodule Matplotex.Figure.Lead do
6465
} = axes
6566
} = figure
6667
) do
67-
{x_ticks, x_lim} = maybe_generate_ticks(x_ticks, x_lim, x_data, width)
68-
{y_ticks, y_lim} = maybe_generate_ticks(y_ticks, y_lim, y_data, height)
68+
{x_ticks, x_lim} = maybe_generate_ticks(x_ticks, x_lim, x_data, x_ticks_count || width)
69+
{y_ticks, y_lim} = maybe_generate_ticks(y_ticks, y_lim, y_data, y_ticks_count || height)
6970

7071
%Figure{
7172
figure

lib/matplotex/figure/rc_params.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ defmodule Matplotex.Figure.RcParams do
4848
white_space: 0,
4949
label_padding: @label_padding,
5050
legend_width: @default_legend_width_percentage,
51-
legend_items_orientation: @default_legend_items_orientation
51+
legend_items_orientation: @default_legend_items_orientation,
52+
x_ticks_count: nil,
53+
y_ticks_count: nil
5254

5355
def get_rc(%__MODULE__{} = rc_param, get_func) do
5456
apply(__MODULE__, get_func, [rc_param])

0 commit comments

Comments
 (0)