|
1 | 1 | defmodule Matplotex do |
2 | 2 | @moduledoc """ |
3 | | - Module to generate a graph. |
| 3 | + # Matplotex |
| 4 | +
|
| 5 | + a lightweight and efficient library for Elixir projects that facilitates server-side |
| 6 | + SVG generation for data visualization. Designed to integrate seamlessly with Phoenix LiveView, |
| 7 | + it serves as a powerful tool for creating dynamic visualizations in web applications. |
| 8 | +
|
| 9 | + it supports the following graphs |
| 10 | + - Line plots |
| 11 | + - Bar charts |
| 12 | + - Pie charts |
| 13 | + - Spline graphs |
| 14 | + - Histograms |
| 15 | + - Scatter plots |
| 16 | +
|
| 17 | + The plotting of a graph comes with set of common parameters and set of plot specific parameters |
| 18 | + all of them will share with the corresponding function documentation, this section covers one examaple |
| 19 | + as a line plot. |
| 20 | + There are two approach to generate plots |
| 21 | + - by using specific function to set parameters |
| 22 | + - by using parameters along with options |
| 23 | +
|
| 24 | + ```elixir |
| 25 | + alias Matplotex as: M |
| 26 | +
|
| 27 | + x = [1, 2, 3, 4, 6, 6, 7] |
| 28 | + y = [1, 3, 4, 4, 5, 6, 7] |
| 29 | +
|
| 30 | + frame_width = 6 |
| 31 | + frame_height = 6 |
| 32 | + size = {frame_width, frame_height} |
| 33 | + margin = 0.05 |
| 34 | + font_size = "16pt" |
| 35 | + title_font_size = "18pt" |
| 36 | + ticks = [1, 2, 3, 4, 5, 6, 7] |
| 37 | +
|
| 38 | + x |
| 39 | + |> M.plot(y) |
| 40 | + |> M.figure(%{figsize: size, margin: margin}) |
| 41 | + |> M.set_title("The Plot Title") |
| 42 | + |> M.set_xlabel("X Axis") |
| 43 | + |> M.set_ylabel("Y Axis") |
| 44 | + |> M.set_xticks(ticks) |
| 45 | + |> M.set_yticks(ticks) |
| 46 | + |> M.set_xlim({4, 7}) |
| 47 | + |> M.set_ylim({4, 7}) |
| 48 | + |> M.set_rc_params( |
| 49 | + x_tick_font_size: font_size, |
| 50 | + y_tick_font_size: font_size, |
| 51 | + title_font_size: title_font_size, |
| 52 | + x_label_font_size: font_size, |
| 53 | + y_label_font_size: font_size, |
| 54 | + title_font_size: title_font_size |
| 55 | + ) |
| 56 | + |> M.show() |
| 57 | + ``` |
| 58 | + This module exposes all of the functions for setters |
| 59 | + and another approach is creating plots by using plot options the code is as follows |
| 60 | + ```elixir |
| 61 | + alis Matplotex as: M |
| 62 | + x = [1, 2, 3, 4, 6, 6, 7] |
| 63 | + y = [1, 3, 4, 4, 5, 6, 7] |
| 64 | +
|
| 65 | + frame_width = 6 |
| 66 | + frame_height = 6 |
| 67 | + size = {frame_width, frame_height} |
| 68 | + margin = 0.05 |
| 69 | + font_size = "16pt" |
| 70 | + title_font_size = "18pt" |
| 71 | + ticks = [0, 1, 2, 3, 4, 5, 6, 7] |
| 72 | +
|
| 73 | + x |
| 74 | + |> M.plot(y, |
| 75 | + figsize: size, |
| 76 | + margin: margin, |
| 77 | + title: "The plot title", |
| 78 | + x_label: "X Axis", |
| 79 | + y_label: "Y Axis", |
| 80 | + x_tick: ticks, |
| 81 | + y_tick: ticks, |
| 82 | + x_limit: {0, 7}, |
| 83 | + y_limit: {0, 7}, |
| 84 | + x_tick_font_size: font_size, |
| 85 | + y_tick_font_size: font_size, |
| 86 | + title_font_size: title_font_size, |
| 87 | + x_label_font_size: font_size, |
| 88 | + y_label_font_size: font_size, |
| 89 | + y_tick_font_text_anchor: "start" |
| 90 | + ) |
| 91 | + |> M.show() |
| 92 | + ``` |
| 93 | + just for simplicity and convenience of the user it is keeping both patterns, no difference on using one on another |
| 94 | +
|
| 95 | + So the user has the control on the all parameters on the inner elements of the chart |
| 96 | +
|
| 97 | + ## Rc Params |
| 98 | + In the first example along with the setter functions you might noticed M.set_rc_params/2 |
| 99 | + The role of this function is similar to other functions we are keeping some values with the plot data |
| 100 | + and the acronym RC stands for Runtime configuration, the plot data holds the labels limits ticks, etc |
| 101 | + The RC params are holding the font size, color, style etc, by defaul one chart object kickstart with some default values |
| 102 | + just for the sake of it needed some values, by default all the fonts are Areal, Veradana, sans-serif and using standard font size 12 |
| 103 | + if a user creates a plots with no inputs for any of these the plot will be choosing the default values |
| 104 | + besides font configuration Rc params covers |
| 105 | + `line_width, line_style, grid_color, grid_linestyle, grid_alpha, tick_line_length, x_padding, y_padding, legend_width` |
| 106 | + There is two types of padding `x_padding, y_padding` and `padding` the perfect use of those can be found on upcoming plot specific documentations |
| 107 | +
|
| 108 | + ## Elements |
| 109 | + The output format of the plot is SVG will support more formats in future, anyway the svg is a group of some elements put together, throught the execution |
| 110 | + it is generating those elements through elixir data structure, all element data structure contains some svg equivalent data that converts the elements to |
| 111 | + SVG string, the output SVG string can be used directly in the web application. |
| 112 | +
|
| 113 | +
|
4 | 114 | """ |
5 | 115 | alias Matplotex.Figure.Areal.Spline |
6 | 116 | alias Matplotex.Figure.Areal.Histogram |
|
0 commit comments