Packages

Create beautiful JavaScript charts with one line of Elixir

Current section

7 Versions

Jump to

Compare versions

5 files changed
+93 additions
-76 deletions
  @@ -1,8 +1,24 @@
1 1 ### HEAD
2 2
3 + * Remove defer option as we now automatically defer if you defer loading the
4 + Chartkick javascript library.
5 +
6 + ### 1.0.0-rc.0
7 +
8 + * :warning: `json_serializer` must be specified since the dependency on `poison` has been removed
9 + ```
10 + # config.exs
11 + config :chartkick, json_serializer: Jason
12 + ```
13 + _Replace `Jason` with the encoder of your choice_. \
14 + [PR#28](https://github.com/buren/chartkick-ex/pull/28)
15 + * Chart Options: `round` and `zeros` [PR#35](https://github.com/buren/chartkick-ex/pull/35)
16 + * `thousands` wasn't correct with the code shown [PR#34](https://github.com/buren/chartkick-ex/pull/34)
17 + * Replace `uuid` with `elixir_uuid` as it was renamed [PR#24](https://github.com/buren/chartkick-ex/pull/24)
18 +
3 19 ### 0.4.0
4 20
5 - * JSON serializer can be optionally configured in Aplication env
21 + * Support `Jason` serializer configuration option
6 22
7 23 ### 0.3.0
  @@ -13,7 +13,10 @@ Any feedback, suggestions or comments please open an issue or PR.
13 13 All charts expect a JSON string.
14 14
15 15 ```elixir
16 - data = Poison.encode!([[175, 60], [190, 80], [180, 75]])
16 + raw_data = [[175, 60], [190, 80], [180, 75]]
17 + data = Poison.encode!(raw_data)
18 + # or if you are using Jason
19 + data = Jason.encode!(raw_data)
17 20 ```
18 21
19 22 Line chart
  @@ -56,6 +59,8 @@ Geo chart
56 59
57 60 ```elixir
58 61 Chartkick.geo_chart Poison.encode!("[[\"United States\",44],[\"Germany\",23]]")
62 + # or if you are using Jason
63 + Chartkick.geo_chart Jason.encode!("[[\"United States\",44],[\"Germany\",23]]")
59 64 ```
60 65
61 66 Timeline
  @@ -144,12 +149,6 @@ Specify legend position
144 149 Chartkick.line_chart data, legend: "bottom"
145 150 ```
146 151
147 - Defer chart creation until after the page loads
148 -
149 - ```elixir
150 - Chartkick.line_chart data, defer: true
151 - ```
152 -
153 152 Donut chart
154 153
155 154 ```elixir
  @@ -170,10 +169,22 @@ Chartkick.line_chart data, suffix: "%"
170 169
171 170 Set a thousands separator - _Chart.js, Highcharts_
172 171
172 + ```elixir
173 + Chartkick.line_chart data, thousands: ","
174 + ```
175 +
176 + Set a decimal separator - _Chart.js, Highcharts_
177 +
173 178 ```elixir
174 179 Chartkick.line_chart data, decimal: ","
175 180 ```
176 181
182 + Show insignificant zeros, useful for currency - _Chart.js, Highcharts_
183 +
184 + ```elixir
185 + Chartkick.line_chart data, round: 2, zeros: true
186 + ```
187 +
177 188 Show a message when data is empty
178 189
179 190 ```elixir
  @@ -229,14 +240,8 @@ Chartkick.line_chart "{
229 240
230 241 ## Installation
231 242
232 - Add the following to your project :deps list:
233 -
234 - ```elixir
235 - {:chartkick, "~>0.4.0"}
236 - ```
237 -
238 - Optionally, you can set different JSON encoder, by default it's Poison.
239 - It's used to encode options passed to Chartkick.
243 + You need to set JSON encoder in your config file. This encoder
244 + is used to encode options passed to Chartkick.
240 245 ```
241 246 # config.exs
242 247 config :chartkick, json_serializer: Jason
  @@ -8,17 +8,11 @@
8 8 <<"LICENSE">>,<<"CHANGELOG.md">>]}.
9 9 {<<"licenses">>,[<<"MIT">>]}.
10 10 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/buren/chartkick-ex">>}]}.
11 - {<<"maintainers">>,[<<"Jacob Burenstam">>]}.
12 11 {<<"name">>,<<"chartkick">>}.
13 12 {<<"requirements">>,
14 - [[{<<"app">>,<<"poison">>},
15 - {<<"name">>,<<"poison">>},
13 + [[{<<"app">>,<<"elixir_uuid">>},
14 + {<<"name">>,<<"elixir_uuid">>},
16 15 {<<"optional">>,false},
17 16 {<<"repository">>,<<"hexpm">>},
18 - {<<"requirement">>,<<"~> 3.0">>}],
19 - [{<<"app">>,<<"uuid">>},
20 - {<<"name">>,<<"uuid">>},
21 - {<<"optional">>,false},
22 - {<<"repository">>,<<"hexpm">>},
23 - {<<"requirement">>,<<"~> 1.1">>}]]}.
24 - {<<"version">>,<<"0.4.0">>}.
17 + {<<"requirement">>,<<"~> 1.2">>}]]}.
18 + {<<"version">>,<<"1.0.0">>}.
  @@ -1,18 +1,15 @@
1 1 defmodule Chartkick do
2 2 require EEx
3 - Module.put_attribute(
4 - __MODULE__,
5 - :poison,
6 - if(Code.ensure_loaded?(Poison), do: Poison, else: nil)
7 - )
8 - @json_serializer Application.get_env(:chartkick, :json_serializer) || @poison
9 3
10 - gen_chart_fn = fn (chart_type) ->
4 + gen_chart_fn = fn chart_type ->
11 5 def unquote(
12 6 chart_type
13 - |> Macro.underscore
14 - |> String.to_atom
15 - )(data_source, options \\ []) do
7 + |> Macro.underscore()
8 + |> String.to_atom()
9 + )(
10 + data_source,
11 + options \\ []
12 + ) do
16 13 chartkick_chart(unquote(chart_type), data_source, options)
17 14 end
18 15 end
  @@ -27,13 +24,18 @@ defmodule Chartkick do
27 24 height = Keyword.get(options, :height, "300px")
28 25 width = Keyword.get(options, :width, "100%")
29 26 only = Keyword.get(options, :only)
30 - defer = Keyword.get(options, :defer, false)
27 +
31 28 case only do
32 - :html -> chartkick_tag(id, height, width)
33 - :script -> chartkick_script(klass, id, data_source, options, defer)
34 - _ -> """
29 + :html ->
30 + chartkick_tag(id, height, width)
31 +
32 + :script ->
33 + chartkick_script(klass, id, data_source, options)
34 +
35 + _ ->
36 + """
35 37 #{chartkick_tag(id, height, width)}
36 - #{ chartkick_script(klass, id, data_source, options, defer) }
38 + #{chartkick_script(klass, id, data_source, options)}
37 39 """
38 40 end
39 41 end
  @@ -42,14 +44,19 @@ defmodule Chartkick do
42 44 :def,
43 45 :chartkick_script,
44 46 ~s[<script type="text/javascript">
45 - <%= if defer do
46 - chartkick_defer_create_js(klass, id, data_source, options)
47 - else
48 - chartkick_create_js(klass, id, data_source, options)
49 - end
50 - %>
47 + (function() {
48 + function createChart() {
49 + <%= chartkick_create_js(klass, id, data_source, options) %>
50 + }
51 +
52 + if ("Chartkick" in window) {
53 + createChart();
54 + } else {
55 + window.addEventListener("chartkick:load", createChart, true);
56 + }
57 + })();
51 58 </script>],
52 - ~w(klass id data_source options defer)a
59 + ~w(klass id data_source options)a
53 60 )
54 61
55 62 EEx.function_from_string(
  @@ -66,33 +73,27 @@ defmodule Chartkick do
66 73 ~w(klass id data_source options)a
67 74 )
68 75
69 - EEx.function_from_string(
70 - :def,
71 - :chartkick_defer_create_js,
72 - ~s[
73 - (function() {
74 - var createChart = function() { <%= chartkick_create_js(klass, id, data_source, options) %> };
75 - if (window.addEventListener) {
76 - window.addEventListener("load", createChart, true);
77 - } else if (window.attachEvent) {
78 - window.attachEvent("onload", createChart);
79 - } else {
80 - createChart();
81 - }
82 - })();
83 - ],
84 - ~w(klass id data_source options)a
85 - )
86 -
87 - @options ~w(colors curve dataset decimal discrete donut download label legend library max messages min points prefix refresh stacked suffix thousands title xtitle xtype ytitle)a
76 + @options ~w(colors curve dataset decimal discrete donut download label legend library max messages min points prefix refresh stacked suffix thousands title xtitle xtype ytitle zeros round)a
88 77 defp options_json(opts) when is_list(opts) do
89 78 opts
90 79 |> Keyword.take(@options)
91 80 |> Enum.into(%{})
92 - |> @json_serializer.encode!()
81 + |> json_serializer().encode!()
93 82 end
94 83
95 84 defp options_json(opts) when is_bitstring(opts) do
96 85 opts
97 86 end
87 +
88 + defp json_serializer do
89 + Application.get_env(:chartkick, :json_serializer) ||
90 + raise """
91 + We could not find any JSON serializer configured. You can add this
92 + configuration on your config file
93 +
94 + config :chartkick, json_serializer: <JsonSerializer>
95 +
96 + Choose your prefered json serializer and add it
97 + """
98 + end
98 99 end
  @@ -2,22 +2,24 @@ defmodule Chartkick.Mixfile do
2 2 use Mix.Project
3 3
4 4 def project do
5 - [app: :chartkick,
6 - version: "0.4.0",
5 + [
6 + app: :chartkick,
7 + version: "1.0.0",
7 8 elixir: "~> 1.0",
8 - build_embedded: Mix.env == :prod,
9 - start_permanent: Mix.env == :prod,
9 + build_embedded: Mix.env() == :prod,
10 + start_permanent: Mix.env() == :prod,
10 11 package: package(),
11 12 description: description(),
12 13 licenses: "MIT",
13 - deps: deps()]
14 + deps: deps()
15 + ]
14 16 end
15 17
16 18 # Configuration for the OTP application
17 19 #
18 20 # Type `mix help compile.app` for more information
19 21 def application do
20 - [applications: [:logger]]
22 + [extra_applications: [:logger, :eex]]
21 23 end
22 24
23 25 defp description do
  @@ -46,7 +48,6 @@ defmodule Chartkick.Mixfile do
46 48 #
47 49 # Type `mix help deps` for more examples and options
48 50 defp deps do
49 - [{ :uuid, "~> 1.1" },
50 - {:poison, "~> 3.0"}]
51 + [{:elixir_uuid, "~> 1.2"}, {:poison, "~> 5.0", only: :test}]
51 52 end
52 53 end