| 1 |
from django.utils.html import format_html, json_script, mark_safe |
| 2 |
import re |
| 3 |
import secrets |
| 4 |
|
| 5 |
__all__ = [ |
| 6 |
'LineChart', |
| 7 |
'PieChart', |
| 8 |
'ColumnChart', |
| 9 |
'BarChart', |
| 10 |
'AreaChart', |
| 11 |
'ScatterChart', |
| 12 |
'GeoChart', |
| 13 |
'Timeline' |
| 14 |
] |
| 15 |
|
| 16 |
|
| 17 |
class Chart: |
| 18 |
def __init__(self, type, data, **options): |
| 19 |
|
| 20 |
element_id = 'chart-' + secrets.token_hex(16) |
| 21 |
json_element_id = element_id + '-json' |
| 22 |
|
| 23 |
height = str(options.pop('height', '300px')) |
| 24 |
width = str(options.pop('width', '100%')) |
| 25 |
|
| 26 |
for (k, v) in [('height', height), ('width', width)]: |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
if not re.match('^[a-zA-Z0-9%.]*$', v): |
| 31 |
raise ValueError('Invalid ' + k) |
| 32 |
|
| 33 |
html_vars = [ |
| 34 |
element_id, |
| 35 |
height, |
| 36 |
width, |
| 37 |
height, |
| 38 |
options.get('loading', 'Loading...') |
| 39 |
] |
| 40 |
html = format_html("""<div id="{}" style="height: {}; width: {}; text-align: center; color: #999; line-height: {}; font-size: 14px; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helvetica, sans-serif;">{}</div>""", *html_vars) |
| 41 |
|
| 42 |
|
| 43 |
if isinstance(data, dict): |
| 44 |
data = list(data.items()) |
| 45 |
|
| 46 |
js_vars = { |
| 47 |
'type': type, |
| 48 |
'id': element_id, |
| 49 |
'data': data, |
| 50 |
'options': options |
| 51 |
} |
| 52 |
json = json_script(js_vars, element_id=json_element_id) |
| 53 |
|
| 54 |
js = """<script> |
| 55 |
(function() { |
| 56 |
var createChart = function() { |
| 57 |
var o = JSON.parse(document.currentScript.previousElementSibling.textContent); |
| 58 |
new Chartkick[o.type](o.id, o.data, o.options); |
| 59 |
}; |
| 60 |
if ("Chartkick" in window) { |
| 61 |
createChart(); |
| 62 |
} else { |
| 63 |
window.addEventListener("chartkick:load", createChart, true); |
| 64 |
} |
| 65 |
})(); |
| 66 |
</script>""" |
| 67 |
|
| 68 |
self.__str = format_html('{}\n{}\n{}', mark_safe(html), json, mark_safe(js)) |
| 69 |
|
| 70 |
def __str__(self): |
| 71 |
return self.__str |
| 72 |
|
| 73 |
|
| 74 |
class LineChart(Chart): |
| 75 |
def __init__(self, data, **options): |
| 76 |
super().__init__('LineChart', data, **options) |
| 77 |
|
| 78 |
|
| 79 |
class PieChart(Chart): |
| 80 |
def __init__(self, data, **options): |
| 81 |
super().__init__('PieChart', data, **options) |
| 82 |
|
| 83 |
|
| 84 |
class ColumnChart(Chart): |
| 85 |
def __init__(self, data, **options): |
| 86 |
super().__init__('ColumnChart', data, **options) |
| 87 |
|
| 88 |
|
| 89 |
class BarChart(Chart): |
| 90 |
def __init__(self, data, **options): |
| 91 |
super().__init__('BarChart', data, **options) |
| 92 |
|
| 93 |
|
| 94 |
class AreaChart(Chart): |
| 95 |
def __init__(self, data, **options): |
| 96 |
super().__init__('AreaChart', data, **options) |
| 97 |
|
| 98 |
|
| 99 |
class ScatterChart(Chart): |
| 100 |
def __init__(self, data, **options): |
| 101 |
super().__init__('ScatterChart', data, **options) |
| 102 |
|
| 103 |
|
| 104 |
class GeoChart(Chart): |
| 105 |
def __init__(self, data, **options): |
| 106 |
super().__init__('GeoChart', data, **options) |
| 107 |
|
| 108 |
|
| 109 |
class Timeline(Chart): |
| 110 |
def __init__(self, data, **options): |
| 111 |
super().__init__('Timeline', data, **options) |