PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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