8
8
:license: New BSD License, see LICENSE for further details
9
9
"""
10
10
import sys
11
- import pygal
12
11
from functools import partial
13
12
14
- from lml .plugin import PluginInfo , PluginManager
15
-
16
13
from pyexcel .renderer import Renderer
14
+ from lml .plugin import PluginManager
17
15
18
16
19
17
PY2 = sys .version_info [0 ] == 2
20
- DEFAULT_TITLE = 'pyexcel chart rendered by pygal'
21
- KEYWORD_CHART_TYPE = 'chart_type'
18
+ DEFAULT_TITLE = 'pyexce-chart renders'
22
19
DEFAULT_CHART_TYPE = 'bar'
23
- CHART_TYPES = dict (
24
- pie = 'Pie' ,
25
- box = 'Box' ,
26
- line = 'Line' ,
27
- bar = 'Bar' ,
28
- stacked_bar = 'StackedBar' ,
29
- radar = 'Radar' ,
30
- dot = 'Dot' ,
31
- funnel = 'Funnel' ,
32
- xy = 'XY' ,
33
- histogram = 'Histogram' )
34
20
35
21
36
22
if PY2 :
39
25
from io import BytesIO
40
26
41
27
42
- class Chart (object ):
43
-
44
- def __init__ (self , cls_name ):
45
- self ._chart_class = CHART_TYPES .get (cls_name , 'line' )
46
-
47
-
48
- @PluginInfo ('chart' , tags = ['pie' , 'box' ])
49
- class SimpleLayout (Chart ):
50
-
51
- def render_sheet (self , sheet , title = DEFAULT_TITLE ,
52
- label_y_in_row = 0 ,
53
- ** keywords ):
54
- params = {}
55
- self .params = {}
56
- if len (sheet .colnames ) == 0 :
57
- sheet .name_columns_by_row (label_y_in_row )
58
- params .update (keywords )
59
- the_dict = sheet .to_dict ()
60
- cls = getattr (pygal , self ._chart_class )
61
- instance = cls (title = title , ** params )
62
- for key in the_dict :
63
- data_array = [value for value in the_dict [key ] if value != '' ]
64
- instance .add (key , data_array )
65
- chart_content = instance .render ()
66
- return chart_content
67
-
68
-
69
- @PluginInfo ('chart' ,
70
- tags = ['line' , 'bar' , 'stacked_bar' , 'radar' , 'dot' , 'funnel' ])
71
- class ComplexLayout (Chart ):
72
-
73
- def render_sheet (self , sheet , title = DEFAULT_TITLE ,
74
- label_x_in_column = 0 , label_y_in_row = 0 ,
75
- ** keywords ):
76
- params = {}
77
- self .params = {}
78
- if len (sheet .colnames ) == 0 :
79
- sheet .name_columns_by_row (label_y_in_row )
80
- if len (sheet .rownames ) == 0 :
81
- sheet .name_rows_by_column (label_x_in_column )
82
- params ['x_labels' ] = sheet .rownames
83
- params .update (keywords )
84
- the_dict = sheet .to_dict ()
85
- cls = getattr (pygal , self ._chart_class )
86
- instance = cls (title = title , ** params )
87
- for key in the_dict :
88
- data_array = [value for value in the_dict [key ] if value != '' ]
89
- instance .add (key , data_array )
90
- chart_content = instance .render ()
91
- return chart_content
92
-
93
-
94
- @PluginInfo ('chart' , tags = ['histogram' ])
95
- class Histogram (Chart ):
96
- def render_sheet (self , sheet , title = DEFAULT_TITLE ,
97
- height_in_column = 0 , start_in_column = 1 ,
98
- stop_in_column = 2 ,
99
- ** keywords ):
100
- histograms = zip (sheet .column [height_in_column ],
101
- sheet .column [start_in_column ],
102
- sheet .column [stop_in_column ])
103
- cls = getattr (pygal , self ._chart_class )
104
- instance = cls (title = title , ** keywords )
105
- instance .add (sheet .name , histograms )
106
- chart_content = instance .render ()
107
- return chart_content
108
-
109
- def render_book (self , book , title = DEFAULT_TITLE ,
110
- height_in_column = 0 , start_in_column = 1 ,
111
- stop_in_column = 2 ,
112
- ** keywords ):
113
- from pyexcel .book import to_book
114
- cls = getattr (pygal , self ._chart_class )
115
- instance = cls (title = title , ** keywords )
116
- for sheet in to_book (book ):
117
- histograms = zip (sheet .column [height_in_column ],
118
- sheet .column [start_in_column ],
119
- sheet .column [stop_in_column ])
120
- instance .add (sheet .name , histograms )
121
- chart_content = instance .render ()
122
- return chart_content
123
-
124
-
125
- @PluginInfo ('chart' , tags = ['xy' ])
126
- class XY (Chart ):
127
-
128
- def render_sheet (self , sheet , title = DEFAULT_TITLE ,
129
- x_in_column = 0 ,
130
- y_in_column = 1 ,
131
- ** keywords ):
132
- cls = getattr (pygal , self ._chart_class )
133
- instance = cls (title = title , ** keywords )
134
- points = zip (sheet .column [x_in_column ],
135
- sheet .column [y_in_column ])
136
- instance .add (sheet .name , points )
137
- chart_content = instance .render ()
138
- return chart_content
139
-
140
- def render_book (self , book , title = DEFAULT_TITLE ,
141
- x_in_column = 0 ,
142
- y_in_column = 1 ,
143
- ** keywords ):
144
- from pyexcel .book import to_book
145
- cls = getattr (pygal , self ._chart_class )
146
- instance = cls (title = title , ** keywords )
147
- for sheet in to_book (book ):
148
- points = zip (sheet .column [x_in_column ],
149
- sheet .column [y_in_column ])
150
- instance .add (sheet .name , points )
151
- chart_content = instance .render ()
152
- return chart_content
153
-
154
-
155
28
class ChartManager (PluginManager ):
156
29
def __init__ (self ):
157
30
PluginManager .__init__ (self , 'chart' )
@@ -164,8 +37,10 @@ def get_a_plugin(self, key, **keywords):
164
37
def raise_exception (self , key ):
165
38
raise Exception ("No support for " + key )
166
39
40
+
167
41
MANAGER = ChartManager ()
168
42
43
+
169
44
class ChartRenderer (Renderer ):
170
45
171
46
def __init__ (self , file_type ):
0 commit comments