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