dataviz: replace Chart.js by ChartNew.js for rendering charts (fixes #9217)

This commit is contained in:
Benjamin Dauvergne 2015-12-06 11:26:07 +01:00
parent c206fe11f7
commit 3e3d892f3e
7 changed files with 62 additions and 28 deletions

View File

@ -25,6 +25,7 @@ from django.conf import settings
from combo.data.models import CellBase
from combo.data.library import register_cell_class
from . import utils
@register_cell_class
class Gauge(CellBase):
@ -85,17 +86,34 @@ class BaseCubesChart(CellBase):
return CubesBarChartForm
def get_cell_extra_context(self):
return {
ctx = {
'cell': self,
'title': self.title,
'url': self.url,
'aggregate': self.get_aggregate(),
}
cube = utils.get_cube(self.cube)
aggregates = dict((ag['name'], ag['label']) for ag in cube.get('aggregates', []))
drilldowns = utils.get_drilldown(self.cube)
if self.aggregate1 and aggregates:
ctx['aggregate1_label'] = aggregates.get(self.aggregate1)
if self.drilldown1 and drilldowns:
ctx['drilldown1_label'] = dict(drilldowns).get(self.drilldown1)
if self.drilldown2 and drilldowns:
ctx['drilldown2_label'] = dict(drilldowns).get(self.drilldown2)
return ctx
def get_aggregate(self):
'''Get aggregate defined by chosen cube and the two drildown paths, request ordering of the
data by natural order of each axis.'''
from .utils import get_aggregate, get_cube, compute_levels
def simplify_integers(l):
for x in l:
if isinstance(x, float):
if x - round(x) < 0.001:
x = int(x)
yield x
aggregate = get_aggregate(name=self.cube,
aggregate1=self.aggregate1,
drilldown1=self.drilldown1,
@ -127,12 +145,12 @@ class BaseCubesChart(CellBase):
datasets = OrderedDict()
for cell in aggregate['cells']:
label1 = u' / '.join(map(unicode, cell_ref(cell, label_refs1)))
label1 = u' / '.join(map(unicode, simplify_integers(cell_ref(cell, label_refs1))))
key1 = cell_ref(cell, key_refs1)
labels[key1] = label1
keys1[key1] = 1
if key_refs2:
label2 = u' / '.join(map(unicode, cell_ref(cell, label_refs2)))
label2 = u' / '.join(map(unicode, simplify_integers(cell_ref(cell, label_refs2))))
key2 = cell_ref(cell, key_refs2)
else:
label2 = ''
@ -158,7 +176,7 @@ class CubesBarChart(BaseCubesChart):
template_name = 'combo/cubes-barchart.html'
class Media:
js = ('xstatic/Chart.min.js', 'js/combo.cubes-barchart.js')
js = ('xstatic/ChartNew.js', 'js/combo.cubes-barchart.js')
class Meta:
verbose_name = _('Cubes Barchart')

View File

@ -45,9 +45,14 @@ $(function() {
barDatasetSpacing : 1,
//String - A legend template
legendTemplate : "ul",
multiTooltipTemplate: "<%= datasetLabel %>: <%= value %>",
responsive: true,
legend: true,
}
if ($(this).data('x-label')) {
option['xAxisLabel'] = $(this).data('x-label');
}
if ($(this).data('y-label')) {
option['yAxisLabel'] = $(this).data('y-label');
}
var data = window['combo_cube_aggregate_' + id];
// Set one color by dataset
@ -68,22 +73,23 @@ $(function() {
};
return objClone;
}
var chart = new Chart(ctx).Bar(data, option);
if (chart.datasets.length == 1) {
if (data.datasets.length == 1) {
// Set one color by bar
var n = chart.datasets[0].bars.length;
var dataset = data.datasets[0];
var n = dataset.data.length;
$.extend(dataset, {
fillColor: [],
strokeColor: [],
highlightFill: [],
highlightStroke: [],
})
for (var i = 0; i < n; i++) {
var bar = chart.datasets[0].bars[i];
$.extend(bar, {
fillColor: Colors.spaced_hsla(i, n, 100, 30, 0.5),
strokeColor: Colors.spaced_hsla(i, n, 100, 30, 0.75),
highlightFill: Colors.spaced_hsla(i, n, 100, 30, 0.75),
highlightStroke: Colors.spaced_hsla(i, n, 100, 30, 1)
})
bar['_saved'] = clone(bar);
bar.update();
dataset["fillColor"].push(Colors.spaced_hsla(i, n, 100, 30, 0.5));
dataset["strokeColor"].push(Colors.spaced_hsla(i, n, 100, 30, 0.75));
dataset["highlightFill"].push(Colors.spaced_hsla(i, n, 100, 30, 0.75));
dataset["highlightStroke"].push(Colors.spaced_hsla(i, n, 100, 30, 1));
}
}
window.chart = chart;
new Chart(ctx).Bar(data, option);
})
})

View File

@ -3,7 +3,10 @@
</script>
<div
class="combo-cube-aggregate"
data-combo-cube-aggregate-id="{{ cell.id }}">
data-combo-cube-aggregate-id="{{ cell.id }}"
data-y-label="{{ aggregate1_label }}"
data-x-label="{{ drilldown1_label }}"
>
{% if title %}
{% if url %}<a href="{{url}}">{% endif %}{{title}}{% if url %}</a>{% endif %}
{% endif %}

View File

@ -18,6 +18,7 @@ import requests
from requests.exceptions import RequestException
import urlparse
from django.utils.translation import ugettext as _
from django.conf import settings
@ -53,6 +54,7 @@ def get_drilldown(name):
if not cube:
return []
l = []
seen = set()
for dimension in cube.get('dimensions', []):
dim_name = dimension['name']
dim_label = dimension.get('label') or dim_name
@ -66,15 +68,19 @@ def get_drilldown(name):
h_label = hierarchy.get('label') or h_name
if h_name == 'default':
h_label = ''
for level in hierarchy['levels']:
labels = filter(None, [dim_label, h_label, levels[level]])
label = ' - '.join(labels)
for i in range(1, len(hierarchy['levels'])+1):
level = hierarchy['levels'][i-1]
label = _(u'by ') + _(u' and ').join(
levels[level] for level in hierarchy['levels'][:i]
)
name = '%s@%s:%s' % (dim_name, h_name, level)
l.append((name, label))
if label not in seen:
l.append((name, label))
seen.add(label)
else:
raise NotImplementedError
else:
l.append((dim_name, dim_label))
l.append((dim_name, _(u'by %s') % dim_label))
return l

View File

@ -64,7 +64,7 @@ INSTALLED_APPS = (
'combo.apps.publik',
'combo.apps.family',
'combo.apps.dataviz',
'xstatic.pkg.chart_js',
'xstatic.pkg.chartnew_js',
)
INSTALLED_APPS = plugins.register_plugins_apps(INSTALLED_APPS)

3
debian/control vendored
View File

@ -13,7 +13,8 @@ Depends: ${misc:Depends}, ${python:Depends},
python-gadjo,
python-requests,
python-feedparser,
python-django-cmsplugin-blurp
python-django-cmsplugin-blurp,
python-xstatic-chartnew-js
Recommends: python-django-mellon
Description: Portal Management System (Python module)

View File

@ -110,7 +110,7 @@ setup(
'feedparser',
'django-jsonfield',
'requests',
'XStatic-Chart.js',
'XStatic-ChartNew.js',
],
zip_safe=False,
cmdclass={