Skip to content

Commit 54d35cf

Browse files
Merge pull request #37 from BigThinkcode/convention
Convention
2 parents 02013bc + 706570c commit 54d35cf

13 files changed

Lines changed: 55 additions & 15 deletions

File tree

docs/documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[for detailed documentation refer](https://hexdocs.pm/matplotex/Matplotex.html)

lib/matplotex.ex

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,51 @@ defmodule Matplotex do
114114
## `M.show/1`
115115
After creating a figure using the functions provided, call M.show/1 to generate and display the final SVG representation of the plot. The show/1 function will convert the Matplotex.Figure data into a valid SVG string.
116116
117+
## Matplotex.Figure.Areal behaviour
118+
Matplotex goes beyond basic chart generation, offering a user-friendly interface for creating custom plots. It leverages two key behaviors: Matplotex.Figure.Areal and Matplotex.Element. The Areal module handles the underlying linear transformations, abstracting away unnecessary complexity. Users can seamlessly integrate SVG strings by defining structs that implement the Element behavior. Mapping data series to these elements is then achieved through the Matplotex.Figure.Areal behavior, making it a compelling tool for custom visualization.
119+
Example usage:
120+
```elixir
121+
defmodule MyCustomAreaChart do
122+
use Matplotex.Figure.Areal
123+
124+
frame(
125+
tick: %TwoD{},
126+
limit: %TwoD{},
127+
label: %TwoD{},
128+
region_x: %Region{},
129+
region_y: %Region{},
130+
region_title: %Region{},
131+
region_legend: %Region{},
132+
region_content: %Region{}
133+
)
134+
135+
136+
def create(figure, data, _opts) do
137+
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)
138+
139+
%Figure{figure | axes: %{axes | data: xydata, dataset: datasets}}
140+
|> PlotOptions.set_options_in_figure(opts)
141+
end
142+
143+
def materialize(figure) do
144+
materialize_chart_elements(figure)
145+
end
146+
defp materialize_chart_elements(%Figure{axes: %__MODULE__{elements: elements}}) do
147+
elements ++ [%CustomElement{}]
148+
end
149+
```
150+
* Figure: A common struct used by all chart APIs in the library, serving as both input and output for consistent handling.
151+
152+
* Axes: A struct containing all chart-specific information.
153+
154+
* Dataset: A modular data input structure. The axes struct expects a list of %Dataset{} structs for converting data points into their SVG element equivalents.
155+
156+
* Custom Elements: The materializer function's primary role is to generate a list of structs that implement the Matplotex.Element behavior. This enables the SVG generator to produce the appropriate tags for custom visualizations
157+
158+
### Matplotex.Element behaviour
159+
160+
The Matplotex.Element behavior defines a callback function, assemble/1, which takes a struct as input and returns its corresponding SVG tag as a string. The assemble/1 function is responsible for interpolating the struct's values into the SVG element.
161+
117162
118163
"""
119164
alias Matplotex.Figure.Areal.Spline

lib/matplotex/figure.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ defmodule Matplotex.Figure do
7474
%__MODULE__{figure | margin: margin, axes: %{axes | size: frame_size}}
7575
end
7676

77-
def materialize(%__MODULE__{axes: %module{}} = figure), do: module.materialize(figure)
77+
def materialize(%__MODULE__{axes: %module{}} = figure), do: figure|> module.materialized_by_region()|>module.materialize()
7878

7979
def update_figure(figure, params) do
8080
if valid_params?(params) do

lib/matplotex/figure/areal.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,5 @@ defmodule Matplotex.Figure.Areal do
360360

361361
%Dataset{dataset | transformed: transformed}
362362
end
363+
363364
end

lib/matplotex/figure/areal/bar_chart.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ defmodule Matplotex.Figure.Areal.BarChart do
4848

4949
@impl Areal
5050
def materialize(figure) do
51-
__MODULE__.materialized_by_region(figure)
52-
|> materialize_bars()
51+
materialize_bars(figure)
5352
end
5453

5554
defp materialize_bars(

lib/matplotex/figure/areal/histogram.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ defmodule Matplotex.Figure.Areal.Histogram do
4848
def materialize(figure) do
4949
figure
5050
|> sanitize()
51-
|> __MODULE__.materialized_by_region()
5251
|> materialize_hist()
5352
end
5453

lib/matplotex/figure/areal/line_plot.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ defmodule Matplotex.Figure.Areal.LinePlot do
4848
@impl Areal
4949
def materialize(figure) do
5050
figure
51-
|> __MODULE__.materialized_by_region()
5251
|> materialize_lines()
5352
end
5453

lib/matplotex/figure/areal/scatter.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ defmodule Matplotex.Figure.Areal.Scatter do
4141

4242
@impl Areal
4343
def materialize(figure) do
44-
__MODULE__.materialized_by_region(figure)
45-
|> materialize_elements()
44+
materialize_elements(figure)
4645
end
4746

4847
defp materialize_elements(

lib/matplotex/figure/areal/spline.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ defmodule Matplotex.Figure.Areal.Spline do
3030
) do
3131
x = determine_numeric_value(x)
3232
y = determine_numeric_value(y)
33-
opts = Keyword.put_new(opts, :color, "none")
3433
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)
3534
datasets = data ++ [dataset]
3635
xydata = flatten_for_data(datasets)
@@ -42,7 +41,6 @@ defmodule Matplotex.Figure.Areal.Spline do
4241
@impl Areal
4342
def materialize(figure) do
4443
figure
45-
|> __MODULE__.materialized_by_region()
4644
|> materialize_spline()
4745
end
4846

@@ -100,7 +98,6 @@ defmodule Matplotex.Figure.Areal.Spline do
10098
{moveto, transformed} = List.pop_at(transformed, 0, move_to_def)
10199
cubic = Enum.slice(transformed, 0..2)
102100
smooths = blend(transformed, 3)
103-
104101
%Spline{
105102
type: "figure.spline",
106103
moveto: moveto,

test/matplotex/figure/areal/bar_chart_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule Matplotex.Figure.Areal.BarChartTest do
1212
test "adds figure with rectangles for bars", %{
1313
figure: %Figure{axes: %{data: {_x, y}}} = figure
1414
} do
15-
assert %Figure{axes: %{element: elements}} = BarChart.materialize(figure)
15+
assert %Figure{axes: %{element: elements}} = BarChart.materialized_by_region(figure)|>BarChart.materialize()
1616

1717
assert assert Enum.filter(elements, fn x -> x.type == "figure.bar" end) |> length() ==
1818
length(y)

0 commit comments

Comments
 (0)