combo/combo/apps/dataviz/static/js/combo.cubes-barchart.js

90 lines
3.0 KiB
JavaScript

$(function() {
var Colors = {};
Colors.spaced_hsla = function (i, n, s, l, a) {
var h = 360 * i/n;
return "hsla(" + h.toString() + ', ' + s.toString() + '%, ' + l.toString() + '%, ' + a.toString() + ')';
}
$('.combo-cube-aggregate').each(function(idx, elem) {
var cube_url = $(elem).data('cube-url');
var aggregate_url = $(elem).data('aggregate-url');
var model = null;
var ctx = $('canvas', elem)[0].getContext("2d");
var id = $(elem).data('combo-cube-aggregate-id');
var option = {
//Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
scaleBeginAtZero : true,
//Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true,
//String - Colour of the grid lines
scaleGridLineColor : "rgba(0,0,0,.05)",
//Number - Width of the grid lines
scaleGridLineWidth : 1,
//Boolean - Whether to show horizontal lines (except X axis)
scaleShowHorizontalLines: true,
//Boolean - Whether to show vertical lines (except Y axis)
scaleShowVerticalLines: true,
//Boolean - If there is a stroke on each bar
barShowStroke : true,
//Number - Pixel width of the bar stroke
barStrokeWidth : 2,
//Number - Spacing between each of the X value sets
barValueSpacing : 5,
//Number - Spacing between data sets within X values
barDatasetSpacing : 1,
//String - A legend template
legendTemplate : "ul",
multiTooltipTemplate: "<%= datasetLabel %>: <%= value %>",
responsive: true,
}
var data = window['combo_cube_aggregate_' + id];
// Set one color by dataset
var n = data.datasets.length;
for (var i = 0; i < n; i++) {
var dataset = data.datasets[i];
$.extend(dataset, {
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)
})
}
var clone = function(obj){
var objClone = {};
for (var key in obj) {
if (obj.hasOwnProperty(key)) objClone[key] = obj[key];
};
return objClone;
}
var chart = new Chart(ctx).Bar(data, option);
if (chart.datasets.length == 1) {
// Set one color by bar
var n = chart.datasets[0].bars.length;
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();
}
}
window.chart = chart;
})
})