/modules/mod_base/scomps/scomp_base_chart_pie.erl

https://code.google.com/p/zotonic/ · Erlang · 59 lines · 30 code · 10 blank · 19 comment · 0 complexity · 4708796cc617661dd44c4fc096204234 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %% Date: 2009-04-16
  4. %% @doc Simple interface to the pie type of Google chart. Allows for simpler data entry.
  5. %% Parameters: data=[{label,value}, ...] colors=some_color
  6. %% and then all parameters
  7. %% Copyright 2009 Marc Worrell
  8. %%
  9. %% Licensed under the Apache License, Version 2.0 (the "License");
  10. %% you may not use this file except in compliance with the License.
  11. %% You may obtain a copy of the License at
  12. %%
  13. %% http://www.apache.org/licenses/LICENSE-2.0
  14. %%
  15. %% Unless required by applicable law or agreed to in writing, software
  16. %% distributed under the License is distributed on an "AS IS" BASIS,
  17. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. %% See the License for the specific language governing permissions and
  19. %% limitations under the License.
  20. -module(scomp_base_chart_pie).
  21. -author("Marc Worrell <marc@worrell.nl").
  22. -behaviour(gen_scomp).
  23. -export([vary/2, render/3]).
  24. -include("zotonic.hrl").
  25. vary(_Params, _Context) -> default.
  26. render(Params, Vars, Context) ->
  27. Data = proplists:get_value(data, Params, []),
  28. Colors = proplists:get_value(colors, Params),
  29. ThreeD = proplists:get_value(threed, Params, false),
  30. Params1 = proplists:delete(labels,
  31. proplists:delete(data,
  32. proplists:delete(colors,
  33. proplists:delete(threed, Params)))),
  34. {Labels,Values} = case Data of
  35. [] -> {[],[]};
  36. [{_,_}|_] -> lists:unzip(Data);
  37. [[_,_]|_] -> lists:foldr(fun([A,B],{Acc,Bcc}) -> {[A|Acc],[B|Bcc]} end, {[],[]}, Data)
  38. end,
  39. Axes = [ {axis, [{position,"bottom"},{labels,Labels}]} ],
  40. Type = case erlydtl_runtime:is_false(ThreeD) of
  41. true -> "pie";
  42. false -> "pie3d"
  43. end,
  44. Data2 = case is_list(Colors) of
  45. true -> [{data, [{values, Values}, {color, Colors}]}];
  46. false -> [{data, [{values, Values}]}]
  47. end,
  48. Params2 = [{axis,Axes}, {type,Type}, {data,Data2} | Params1],
  49. scomp_base_google_chart:render(Params2, Vars, Context).