1
1
"""Tools to build Columns HighCharts parameters."""
2
2
from .base import JSONView
3
-
3
+ from .. colors import next_color
4
4
5
5
class BaseColumnsHighChartsView (JSONView ):
6
6
"""Base Class to generate Column HighCharts configuration.
@@ -12,19 +12,23 @@ class BaseColumnsHighChartsView(JSONView):
12
12
providers = {}
13
13
credits = {"enabled" : True }
14
14
15
+ def get_colors (self ):
16
+ return next_color ()
17
+
15
18
def get_context_data (self , ** kwargs ):
16
19
"""Return graph configuration."""
17
20
context = super (BaseColumnsHighChartsView , self ).get_context_data (** kwargs )
18
21
context .update (
19
22
{
23
+ 'labels' : self .get_labels (),
20
24
"chart" : self .get_type (),
21
25
"title" : self .get_title (),
22
26
"subtitle" : self .get_subtitle (),
23
27
"xAxis" : self .get_xAxis (),
24
28
"yAxis" : self .get_yAxis (),
25
29
"tooltip" : self .get_tooltip (),
26
30
"plotOptions" : self .get_plotOptions (),
27
- "series " : self .get_series (),
31
+ "datasets " : self .get_series (),
28
32
"credits" : self .credits ,
29
33
}
30
34
)
@@ -100,13 +104,26 @@ def get_plotOptions(self):
100
104
return {"column" : {"pointPadding" : 0.2 , "borderWidth" : 0 }}
101
105
102
106
def get_series (self ):
103
- """Generate HighCharts series from providers and data."""
104
- series = []
107
+ datasets = []
108
+ color_generator = self . get_colors ()
105
109
data = self .get_data ()
106
110
providers = self .get_providers ()
107
- for i , d in enumerate (data ):
108
- series .append ({"name" : providers [i ], "data" : d })
109
- return series
111
+ for i , entry in enumerate (data ):
112
+ color = tuple (next (color_generator ))
113
+ dataset = {"data" : entry ,
114
+ "label" : providers [i ],}
115
+ dataset .update (self .get_dataset_options (i , color ))
116
+ datasets .append (dataset )
117
+ return datasets
118
+
119
+ def get_dataset_options (self , index , color ):
120
+ default_opt = {
121
+ "backgroundColor" : "rgba(%d, %d, %d, 0.5)" % color ,
122
+ "borderColor" : "rgba(%d, %d, %d, 1)" % color ,
123
+ "pointBackgroundColor" : "rgba(%d, %d, %d, 1)" % color ,
124
+ "pointBorderColor" : "#fff" ,
125
+ }
126
+ return default_opt
110
127
111
128
def get_data (self ):
112
129
"""Return a list of series [[25, 34, 0, 1, 50], ...]).
0 commit comments