forked from peopledoc/django-chartjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
columns.py
148 lines (127 loc) · 4.76 KB
/
columns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Tools to build Columns HighCharts parameters."""
from .base import JSONView
from ..colors import next_color
class BaseColumnsHighChartsView(JSONView):
"""Base Class to generate Column HighCharts configuration.
Define at least title, yUnit, providers, get_labels() and
get_data() to get started.
"""
providers = {}
credits = {"enabled": True}
def get_colors(self):
return next_color()
def get_context_data(self, **kwargs):
"""Return graph configuration."""
context = super(BaseColumnsHighChartsView, self).get_context_data(**kwargs)
context.update(
{
'labels': self.get_labels(),
"chart": self.get_type(),
"title": self.get_title(),
"subtitle": self.get_subtitle(),
"xAxis": self.get_xAxis(),
"yAxis": self.get_yAxis(),
"tooltip": self.get_tooltip(),
"plotOptions": self.get_plotOptions(),
"datasets": self.get_series(),
"credits": self.credits,
}
)
return context
def get_type(self):
"""Return graph type."""
return {"type": "column"}
def get_title(self):
"""Return graph title."""
try:
return {"text": u"%s" % self.title}
except AttributeError: # pragma: no cover
raise NotImplementedError( # pragma: no cover
"You should define self.title or self.get_title"
)
def get_subtitle(self):
"""Return graph subtitle."""
subtitle = u"%s" % getattr(self, "subtitle", "")
return subtitle
def get_xAxis(self):
return {"categories": self.get_labels()}
def get_labels(self):
raise NotImplementedError( # pragma: no cover
"You should return a labels list. " '(i.e: ["January", ...])'
)
def get_yAxis(self):
return {"min": getattr(self, "yMin", 0), "title": self.get_yTitle()}
def get_yTitle(self):
"""Return yAxis title."""
subtitle = u"%s" % getattr(self, "subtitle", "")
return subtitle
def get_yUnit(self):
try:
return self.yUnit
except AttributeError: # pragma: no cover
raise NotImplementedError( # pragma: no cover
"Please define the yAxis unit (self.yUnit)."
)
def get_tooltip(self):
"""Return tooltip configuration."""
return {
"headerFormat": """
<span style="font-size:10px">
{point.key}
</span>
<table>""",
"pointFormat": """
<tr>
<td style="color:{series.color};padding:0">
{series.name}:
</td>
<td style="padding:0">
<b>{point.y:.0f} %s</b>
</td>
</tr>"""
% self.get_yUnit(),
"footerFormat": "</table>",
"shared": True,
"useHTML": True,
}
def get_plotOptions(self):
"""Return plotOptions configuration."""
return {"column": {"pointPadding": 0.2, "borderWidth": 0}}
def get_series(self):
datasets = []
color_generator = self.get_colors()
data = self.get_data()
providers = self.get_providers()
for i, entry in enumerate(data):
color = tuple(next(color_generator))
dataset = {"data": entry,
"label": providers[i],}
dataset.update(self.get_dataset_options(i, color))
datasets.append(dataset)
return datasets
def get_dataset_options(self, index, color):
default_opt = {
"backgroundColor": "rgba(%d, %d, %d, 0.5)" % color,
"borderColor": "rgba(%d, %d, %d, 1)" % color,
"pointBackgroundColor": "rgba(%d, %d, %d, 1)" % color,
"pointBorderColor": "#fff",
}
return default_opt
def get_data(self):
"""Return a list of series [[25, 34, 0, 1, 50], ...]).
In the same order as providers and with the same serie length
of xAxis.
"""
raise NotImplementedError( # pragma: no cover
"You should return a data list list. " "(i.e: [[25, 34, 0, 1, 50], ...])."
)
def get_providers(self):
"""Return the list of data series names.
Providers numbers should be equal to series numbers.
"""
try:
return self.providers
except AttributeError: # pragma: no cover
raise NotImplementedError( # pragma: no cover
"You should define self.providers of self.get_providers."
)