Skip to content

Commit c1864e2

Browse files
Merge pull request #50 from BigThinkcode/stackedbar
Stackedbar bar chart
2 parents 9b0dac1 + f2f6aef commit c1864e2

File tree

18 files changed

+251
-105
lines changed

18 files changed

+251
-105
lines changed

lib/matplotex.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ defmodule Matplotex do
213213
bar(pos, values, width, [])
214214
end
215215

216-
def bar(pos, values, width, opts) do
217-
BarChart.create(%Figure{axes: %BarChart{}}, {pos, values, width}, opts)
218-
end
219-
220216
@doc """
221217
Adds an additional dataset to a bar plot in the given `%Figure{}`.
222218
@@ -251,6 +247,13 @@ defmodule Matplotex do
251247
|> M.bar(0, values2, width, label: "Dataset2", color: "#D3D3D3")
252248
|> M.bar(width, values2, width, label: "Dataset3", color: "green")
253249
"""
250+
251+
def bar(%Figure{} = figure, values, width, opts), do: bar(figure, width, values, width, opts)
252+
253+
def bar(pos, values, width, opts) do
254+
BarChart.create(%Figure{axes: %BarChart{}}, {pos, values, width}, opts)
255+
end
256+
254257
def bar(%Figure{} = figure, pos, values, width, opts) do
255258
figure
256259
|> show_legend()
@@ -752,6 +755,10 @@ defmodule Matplotex do
752755
Figure.show_legend(figure)
753756
end
754757

758+
def hide_legend(figure) do
759+
Figure.hide_legend(figure)
760+
end
761+
755762
def set_options(figure, opts) do
756763
PlotOptions.set_options_in_figure(figure, opts)
757764
end

lib/matplotex/colorscheme/blender.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
defmodule Matplotex.Colorscheme.Blender do
2-
@moduledoc false
3-
alias Matplotex.Colorscheme.Rgb
2+
@moduledoc false
3+
alias Matplotex.Colorscheme.Rgb
44

55
@rgb_fields [:red, :green, :blue, :alpha]
66

77
@type color :: %Rgb{
8-
red: :float,
9-
green: :float,
10-
blue: :float,
11-
alpha: :float
12-
}
13-
8+
red: :float,
9+
green: :float,
10+
blue: :float,
11+
alpha: :float
12+
}
1413

1514
def mix(color1, color2, weight \\ 0.5) do
16-
1715
p = weight
1816
w = p * 2 - 1
1917
a = color1.alpha - color2.alpha
@@ -30,10 +28,10 @@ alias Matplotex.Colorscheme.Rgb
3028
alpha = get_alpha(color1) * p + get_alpha(color2) * (1 - p)
3129
rgb(r, g, b, alpha)
3230
end
31+
3332
defdelegate rgb(red, green, blue), to: Rgb
3433
defdelegate rgb(red, green, blue, alpha), to: Rgb
3534

36-
3735
@doc """
3836
Gets the `:red` property of the color.
3937
"""
@@ -87,6 +85,4 @@ alias Matplotex.Colorscheme.Rgb
8785
end
8886

8987
defp cast_color_by_attribute(color, attribute) when attribute in @rgb_fields, do: color
90-
91-
9288
end
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
defmodule Matplotex.Colorscheme.Colormap do
2-
@moduledoc false
2+
@moduledoc false
33
defstruct [:color, :offset, opacity: 1]
4+
45
def viridis do
5-
["#fde725","#21918c","#3b528b","#440154"]
6+
["#fde725", "#21918c", "#3b528b", "#440154"]
67
end
78

89
def plasma do
9-
["#F7E425","#ED6925", "#9C179E", "#0C0786" ]
10+
["#F7E425", "#ED6925", "#9C179E", "#0C0786"]
1011
end
1112

1213
def inferno do
13-
["#FCFFA4","#F56C3E","#B12A90","#000004"]
14+
["#FCFFA4", "#F56C3E", "#B12A90", "#000004"]
1415
end
1516

1617
def magma do
17-
["#FCFDBF","#FB8861", "#B73779", "#000004"]
18+
["#FCFDBF", "#FB8861", "#B73779", "#000004"]
1819
end
1920

2021
def fetch_cmap(cmap) when is_binary(cmap), do: cmap |> String.to_atom() |> fetch_cmap()
2122

2223
def fetch_cmap(cmap) do
2324
apply(__MODULE__, cmap, []) |> make_colormap()
2425
end
26+
2527
def make_colormap(colors) do
2628
size = length(colors)
29+
2730
colors
2831
|> Enum.with_index()
2932
|> Enum.map(&colormap(&1, size))
@@ -35,6 +38,4 @@ defmodule Matplotex.Colorscheme.Colormap do
3538
offset = idx / size * 100
3639
%__MODULE__{color: color, offset: offset}
3740
end
38-
39-
4041
end
Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
defmodule Matplotex.Colorscheme.Garner do
2-
@moduledoc false
3-
alias Matplotex.Colorscheme.Rgb
4-
alias Matplotex.Colorscheme.Blender
5-
alias Matplotex.InputError
2+
@moduledoc false
3+
alias Matplotex.Colorscheme.Rgb
4+
alias Matplotex.Colorscheme.Blender
5+
alias Matplotex.InputError
66

7-
defstruct [:range, :color_cue, :cmap, :preceeding, :minor, :major, :final]
7+
defstruct [:range, :color_cue, :cmap, :preceeding, :minor, :major, :final]
88

99
def garn_color({min, max} = range, point, cmap) when max != min do
1010
cue = (point - min) / (max - min)
11+
1112
cmap
1213
|> make_from_cmap()
1314
|> put_range(range, cue)
@@ -16,8 +17,8 @@ alias Matplotex.InputError
1617

1718
defp make_from_cmap(cmap) do
1819
cmap
19-
|>to_rgb()
20-
|>place_edges()
20+
|> to_rgb()
21+
|> place_edges()
2122
end
2223

2324
defp put_range(%__MODULE__{} = garner, range, cue) do
@@ -28,22 +29,29 @@ alias Matplotex.InputError
2829
Enum.map(color_map, &Rgb.from_cmap!(&1))
2930
end
3031

31-
defp place_edges([preceeding, minor, major,final]) do
32-
%__MODULE__{preceeding: preceeding.color, minor: minor.color, major: major.color, final: final.color}
32+
defp place_edges([preceeding, minor, major, final]) do
33+
%__MODULE__{
34+
preceeding: preceeding.color,
35+
minor: minor.color,
36+
major: major.color,
37+
final: final.color
38+
}
3339
end
40+
3441
defp place_edges(_) do
3542
raise InputError, message: "Invalid colormap"
3643
end
3744

38-
defp point_color(%__MODULE__{color_cue: cue, preceeding: preceeding, minor: minor}) when cue < minor do
39-
minor|> Blender.mix(preceeding, cue)|> Rgb.to_string()
45+
defp point_color(%__MODULE__{color_cue: cue, preceeding: preceeding, minor: minor})
46+
when cue < minor do
47+
minor |> Blender.mix(preceeding, cue) |> Rgb.to_string()
4048
end
4149

4250
defp point_color(%__MODULE__{color_cue: cue, minor: minor, major: major}) when cue < major do
43-
major|> Blender.mix(minor, cue)|> Rgb.to_string()
51+
major |> Blender.mix(minor, cue) |> Rgb.to_string()
4452
end
4553

4654
defp point_color(%__MODULE__{color_cue: cue, major: major, final: final}) when cue >= major do
47-
final|> Blender.mix(major)|> Rgb.to_string()
55+
final |> Blender.mix(major) |> Rgb.to_string()
4856
end
49-
end
57+
end

lib/matplotex/colorscheme/rgb.ex

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
defmodule Matplotex.Colorscheme.Rgb do
22
@moduledoc false
3-
alias Matplotex.Colorscheme.Colormap
3+
alias Matplotex.Colorscheme.Colormap
4+
45
defstruct [
5-
red: 0.0, # 0-255
6-
green: 0.0, # 0-255
7-
blue: 0.0, # 0-255
8-
alpha: 1.0 # 0-1
6+
# 0-255
7+
red: 0.0,
8+
# 0-255
9+
green: 0.0,
10+
# 0-255
11+
blue: 0.0,
12+
# 0-1
13+
alpha: 1.0
914
]
1015

11-
def rgb(red, green, blue, alpha\\1.0)
12-
def rgb({red, :percent}, {green, :percent}, {blue, :percent}, alpha) do
16+
def rgb(red, green, blue, alpha \\ 1.0)
17+
18+
def rgb({red, :percent}, {green, :percent}, {blue, :percent}, alpha) do
1319
rgb(red * 255, green * 255, blue * 255, alpha)
1420
end
15-
def rgb(red, green, blue, alpha) do
21+
22+
def rgb(red, green, blue, alpha) do
1623
%__MODULE__{
1724
red: cast(red, :red),
1825
green: cast(green, :green),
@@ -21,65 +28,64 @@ alias Matplotex.Colorscheme.Colormap
2128
}
2229
end
2330

24-
def to_string(struct, type\\nil)
31+
def to_string(struct, type \\ nil)
32+
33+
def to_string(struct, nil) do
34+
type =
35+
case struct.alpha do
36+
1.0 -> :hex
37+
_ -> :rgba
38+
end
2539

26-
def to_string(struct, :nil) do
27-
type = case struct.alpha do
28-
1.0 -> :hex
29-
_ -> :rgba
30-
end
3140
to_string(struct, type)
3241
end
3342

3443
def to_string(%__MODULE__{red: r, green: g, blue: b, alpha: alpha}, :rgba) do
3544
"rgba(#{round(r)}, #{round(g)}, #{round(b)}, #{alpha})"
3645
end
46+
3747
def to_string(%__MODULE__{red: r, green: g, blue: b, alpha: 1.0}, :hex) do
3848
"#" <> to_hex(r) <> to_hex(g) <> to_hex(b)
3949
end
4050

4151
def cast(value, field) when field in [:red, :green, :blue] do
42-
value/1
52+
(value / 1)
4353
|> min(255.0)
4454
|> max(0.0)
4555
end
56+
4657
def cast(value, :alpha) do
47-
value/1
58+
(value / 1)
4859
|> min(1.0)
4960
|> max(0.0)
5061
end
5162

52-
defp to_hex(value) when is_float(value), do:
53-
to_hex(round(value))
54-
defp to_hex(value) when value < 16, do:
55-
"0" <> Integer.to_string(value, 16)
56-
defp to_hex(value) when is_integer(value), do:
57-
Integer.to_string(value, 16)
58-
59-
def from_hex!(input) do
60-
{:ok, color} = from_hex(input)
61-
color
62-
end
63+
defp to_hex(value) when is_float(value), do: to_hex(round(value))
64+
defp to_hex(value) when value < 16, do: "0" <> Integer.to_string(value, 16)
65+
defp to_hex(value) when is_integer(value), do: Integer.to_string(value, 16)
6366

64-
def from_cmap!(%Colormap{color: color} = cmap) do
65-
%Colormap{cmap | color: from_hex!(color) }
66-
end
67+
def from_hex!(input) do
68+
{:ok, color} = from_hex(input)
69+
color
70+
end
6771

68-
def from_hex("#" <> <<r :: binary-size(2), g :: binary-size(2), b :: binary-size(2)>>) do
69-
{:ok, rgb(parse_hex(r), parse_hex(g), parse_hex(b))}
70-
end
71-
def from_hex("#" <> <<r :: binary-size(1), g :: binary-size(1), b :: binary-size(1)>>) do
72-
{:ok, rgb(parse_hex(r <> r), parse_hex(g <> g), parse_hex(b <> b))}
73-
end
72+
def from_cmap!(%Colormap{color: color} = cmap) do
73+
%Colormap{cmap | color: from_hex!(color)}
74+
end
7475

75-
defp parse_hex(s), do: String.to_integer(s, 16)
76+
def from_hex("#" <> <<r::binary-size(2), g::binary-size(2), b::binary-size(2)>>) do
77+
{:ok, rgb(parse_hex(r), parse_hex(g), parse_hex(b))}
78+
end
7679

80+
def from_hex("#" <> <<r::binary-size(1), g::binary-size(1), b::binary-size(1)>>) do
81+
{:ok, rgb(parse_hex(r <> r), parse_hex(g <> g), parse_hex(b <> b))}
82+
end
7783

84+
defp parse_hex(s), do: String.to_integer(s, 16)
7885
end
7986

80-
8187
defimpl String.Chars, for: CssColors.RGB do
82-
def to_string(struct) do
83-
Matplotex.Colorscheme.Rgb.to_string(struct)
84-
end
88+
def to_string(struct) do
89+
Matplotex.Colorscheme.Rgb.to_string(struct)
90+
end
8591
end

lib/matplotex/element.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Matplotex.Element do
1717

1818
defimpl String.Chars, for: __MODULE__ do
1919
def to_string(%module{} = element) do
20-
module.assemble(element)
20+
module.assemble(element)
2121
end
2222
end
2323
end

lib/matplotex/element/cmap.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defmodule Matplotex.Element.Cmap do
2-
@moduledoc false
2+
@moduledoc false
33
alias Matplotex.Element.Rect
44
alias Matplotex.Element
55
use Element

lib/matplotex/figure.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ defmodule Matplotex.Figure do
6363
def show_legend(%__MODULE__{axes: %module{} = axes} = figure),
6464
do: %{figure | axes: module.show_legend(axes)}
6565

66+
def hide_legend(%__MODULE__{axes: %module{} = axes} = figure),
67+
do: %{figure | axes: module.hide_legend(axes)}
68+
6669
def set_figure_size(%__MODULE__{margin: margin, axes: axes} = figure, {fwidth, fheight} = fsize) do
6770
frame_size = {fwidth - fwidth * 2 * margin, fheight - fheight * 2 * margin}
6871

0 commit comments

Comments
 (0)