Skip to content

Commit 0850fbb

Browse files
author
Mohammed Sadique
committed
function based documentation
1 parent fc55876 commit 0850fbb

1 file changed

Lines changed: 176 additions & 11 deletions

File tree

lib/matplotex.ex

Lines changed: 176 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ defmodule Matplotex do
110110
it is generating those elements through elixir data structure, all element data structure contains some svg equivalent data that converts the elements to
111111
SVG string, the output SVG string can be used directly in the web application.
112112
113+
## Figure
114+
The execution a plot carriec out by a data structure named Matplotex.Figure it holds all the adequate information to generate a figure it containst the keys
115+
`:figsize` - is a tuple carries width and height of the figure eg: {10,6}
116+
`:axes` - is another object that will varie according to the plot
117+
`:rc_params` - the runtime configurations
118+
`:margin` - the margin of the figure
119+
120+
## M.show/1
121+
All examples above using this function `M.show/1` after a plot generation API call
122+
The all APIs from this module is ment to return an svg equivalent Data Matplotex.Figure with distinct object associated with the `axes` key, so to convert that data to
123+
an SVG chart use `M.show/1`
113124
114125
"""
115126
alias Matplotex.Figure.Areal.Spline
@@ -121,7 +132,36 @@ defmodule Matplotex do
121132
alias Matplotex.Figure.Sketch
122133
alias Matplotex.Figure
123134
alias Matplotex.Figure.Areal.BarChart
135+
@doc """
136+
Generates a bar chart using the provided values and bar widths.
137+
138+
## Parameters
139+
140+
- `values` (list of numbers): A list of numerical values representing the heights of the bars in the chart.
141+
- `width` (floatiung point number): The width of each bar in inches.
142+
- `opts` (keyword list): It will support all opts mentioned above, some bar specific options are there those are
143+
- `:label` (string): Label for specific dataset passed on first argument.
144+
- `:color` (string): Color of the bar.
145+
- `:edge_color` (string): Color of the edge of the bar.
146+
147+
148+
## Returns
149+
150+
- A figure with the axes of a bar chart
151+
```elixir
124152
153+
alias Matplotex as: M
154+
categories = ["apple", "banana", "fig", "avocado"]
155+
values1 = [22, 33, 28, 34]
156+
iex> Matplotex.bar(width, values1, width, label: "Dataset1", color: "#255199")
157+
%M.Figure{axes: %M.Figure.Areal.BarChart{...}, ...}
158+
```
159+
160+
This function takes a list of numerical `values` and a single `width` value to create a bar chart where:
161+
- The height of each bar corresponds to its respective value from the list.
162+
- Each bar has the specified constant width.
163+
"""
164+
@spec bar(list(), float()) :: Matplotex.Figure.t()
125165
def bar(values, width) do
126166
bar(width, values, width, [])
127167
end
@@ -137,7 +177,33 @@ defmodule Matplotex do
137177
def bar(pos, values, width, opts) do
138178
BarChart.create(%Figure{axes: %BarChart{}}, {pos, values, width}, opts)
139179
end
180+
@doc """
181+
Adds an additional dataset to a bar plot in the given `%Figure{}`.
182+
183+
This function allows you to append multiple datasets to a bar plot by providing new values and corresponding options. Each dataset can be customized with options such as color, label, and bar width.
184+
185+
## Parameters
140186
187+
- `figure` (%Figure{}): The figure to which the new dataset will be added.
188+
- `values` (list): A list of numerical values representing the heights of the bars in the new dataset.
189+
- `width` (float): The width of the bars in the dataset.
190+
- `opts` (keyword list, optional): A set of options for customizing the appearance of the new dataset, such as color and label.
191+
192+
## Usage
193+
194+
This function is used when generating multi-bar plots to represent data from multiple datasets. Here's an example demonstrating its usage:
195+
196+
```elixir
197+
alias Matplotex, as: M
198+
199+
categories = ["apple", "banana", "fig", "avocado"]
200+
values1 = [22, 33, 28, 34]
201+
values2 = [53, 63, 59, 60]
202+
width = 0.22
203+
204+
Matplotex.bar(width, values1, width, label: "Dataset1", color: "#255199")
205+
|> M.bar(width, values2, width, label: "Dataset2", color: "#D3D3D3")
206+
"""
141207
def bar(%Figure{} = figure, pos, values, width, opts) do
142208
figure
143209
|> show_legend()
@@ -150,38 +216,137 @@ defmodule Matplotex do
150216
def scatter(stream, opts) when is_struct(stream, Stream) do
151217
Scatter.create(stream, opts)
152218
end
219+
@doc """
220+
Creates a scatter plot based on the given `x` and `y` values, with optional customization provided via `opts`.
153221
222+
## Parameters
223+
224+
- `x` (list): A list of numerical values representing the x-coordinates.
225+
- `y` (list): A list of numerical values representing the y-coordinates.
226+
- `opts` (keyword list, optional): A set of options for customizing the scatter plot, such as color, marker size, and labels.
227+
228+
## Examples
229+
230+
```elixir
231+
# Basic usage:
232+
x = [1, 2, 3, 4]
233+
y = [10, 20, 30, 40]
234+
opts = [color: "blue", marker_size: 5]
235+
236+
iex> M.scatter(x, y, opts)
237+
%M.Figure{axes: %Matplotex.Figure.Areal.Scatter{...}, ...}
238+
"""
154239
def scatter(x, y) do
155240
scatter(x, y, [])
156241
end
157242

158243
def scatter(x, y, opts) do
159244
Scatter.create(%Figure{axes: %Scatter{}}, {x, y}, opts)
160245
end
246+
@doc """
247+
Adds an additional dataset to a scatter plot in the given `%Figure{}`.
248+
249+
This function allows you to overlay multiple scatter plots on the same figure by providing new `x` and `y` values, along with customization options via `opts`.
161250
251+
## Parameters
252+
253+
- `figure` (%Figure{}): The figure to which the new dataset will be added.
254+
- `x` (list): A list of numerical values representing the x-coordinates of the new dataset.
255+
- `y` (list): A list of numerical values representing the y-coordinates of the new dataset.
256+
- `opts` (keyword list, optional): A set of options for customizing the appearance of the new dataset, such as color, marker style, line style, and labels.
257+
258+
## Usage
259+
260+
This function is typically used when you want to generate multi-pattern scatter plots with multiple datasets. The following example demonstrates its usage:
261+
262+
```elixir
263+
x = [1, 2, 3, 4, 5]
264+
265+
# Dataset 1
266+
y1 = [20, 5, 12, 16, 25]
267+
268+
# Dataset 2
269+
y2 = [10, 1, 6, 10, 15]
270+
271+
# Dataset 3
272+
y3 = [17, 5, 8, 12, 17]
273+
274+
x
275+
|> Matplotex.scatter(y1, color: "blue", linestyle: "_", marker: "o", label: "Dataset 1")
276+
|> Matplotex.scatter(x, y2, color: "red", linestyle: "--", marker: "^", label: "Dataset 2")
277+
|> Matplotex.scatter(x, y3, color: "green", linestyle: "-.", marker: "s", label: "Dataset 3")
278+
"""
162279
def scatter(%Figure{} = figure, x, y, opts) do
163280
figure
164281
|> show_legend()
165282
|> Scatter.create({x, y}, opts)
166283
end
167284

168285
@doc """
169-
Creates a piec charts based on the size and opts
170-
"""
171-
def pie(sizes, opts \\ []) do
172-
Pie.create(%Figure{axes: %Pie{}}, sizes, opts)
173-
end
286+
Generates a pie chart based on the provided data, labels, and options.
174287
175-
@doc """
176-
Creates a line plot with given x and y data.
288+
### Parameters:
289+
- `sizes` (list of integers/floats): Percentages or proportions for each slice of the pie chart.
290+
- `opts` (keyword list): Options to customize the chart, such as:
291+
- `:labels` (list of strings): Labels for each slice.
292+
- `:colors` (list of strings): Colors for the slices.
177293
178-
## Examples
294+
### Example:
179295
180-
iex> Matplotex.plot([1, 2, 3], [4, 5, 6])
181-
%Matplotex.Figure{}
296+
```elixir
297+
# Percentages for each slice
298+
sizes = [25, 35, 20, 20]
182299
183-
"""
300+
# Labels for each slice
301+
labels = ["A", "B", "C", "D"]
302+
303+
# Colors for the slices
304+
colors = ["lightblue", "lightgreen", "orange", "pink"]
305+
306+
# Generate the pie chart
307+
sizes
308+
|> Matplotex.pie(colors: colors, labels: labels)
309+
310+
%M.Figure{axes: %Matplotex.Figure.Radial.Pie{...}, ...}
311+
"""
312+
def pie(sizes, opts \\ []) do
313+
Pie.create(%Figure{axes: %Pie{}}, sizes, opts)
314+
end
184315

316+
@doc """
317+
Generates a line plot using the provided `x` and `y` data points.
318+
##Parameters
319+
`x`: A list of x-coordinates for the data points (e.g., [1, 2, 3]).
320+
`y`: A list of y-coordinates corresponding to x (e.g., [2, 4, 6]).
321+
`opts`: it also expect some plot specific options such as
322+
`color`: line color
323+
`linestyle`: line style
324+
`marker`: marker style
325+
326+
### Example
327+
328+
```elixir
329+
# Define the data points
330+
x = [1, 2, 3, 4, 6, 6, 7]
331+
y = [1, 3, 4, 4, 5, 6, 7]
332+
333+
# Specify plot configurations
334+
frame_width = 6
335+
frame_height = 6
336+
size = {frame_width, frame_height}
337+
margin = 0.05
338+
font_size = "16pt"
339+
title_font_size = "18pt"
340+
ticks = [1, 2, 3, 4, 5, 6, 7]
341+
342+
# Create and configure the plot
343+
x
344+
|> Matplotex.plot(y) # Create a line plot
345+
|> Matplotex.figure(%{ # Configure the figure
346+
figsize: size,
347+
margin: margin
348+
})
349+
"""
185350
@spec plot(list(), list()) :: Figure.t()
186351
def plot(x, y) when is_list(x) and is_list(y) do
187352
plot(x, y, [])

0 commit comments

Comments
 (0)