foreman/app/assets/javascripts/charts.js @ a9ea3490
870e7fcc | Amos Benari | $.fn.flot_pie = function(){
|
|
var options = arguments[0] || {};
|
|||
$(this).each(function(i,el){
|
|||
070b83fe | Amos Benari | var label_exists = false;
|
|
870e7fcc | Amos Benari | var target = $(el);
|
|
070b83fe | Amos Benari | var max={data:0}, sum = 0;
|
|
357a9111 | ripcurld00d | $(target.data('series')).each(function(i, el){sum = sum + el.data; if (max.data< el.data) max = el;});
|
|
870e7fcc | Amos Benari | $.plot(target, target.data('series'), {
|
|
070b83fe | Amos Benari | colors: ['#0099d3', '#393f44','#00618a','#505459','#057d9f','#025167'],
|
|
870e7fcc | Amos Benari | series: {
|
|
pie: options.pie || {
|
|||
show: true,
|
|||
070b83fe | Amos Benari | innerRadius: 0.75,
|
|
870e7fcc | Amos Benari | radius: 1,
|
|
label: {
|
|||
show: true,
|
|||
070b83fe | Amos Benari | radius: 0.001,
|
|
formatter: function(label, series) {
|
|||
if (label_exists) {
|
|||
357a9111 | ripcurld00d | return '';
|
|
070b83fe | Amos Benari | }else{
|
|
label_exists = true;
|
|||
return '<div class="percent">' + Math.round(100 * max.data / sum) + '%</div>' + max.label;
|
|||
}
|
|||
}
|
|||
870e7fcc | Amos Benari | },
|
|
highlight: {
|
|||
opacity: 0.1
|
|||
b7cb4996 | Ohad Levy | }
|
|
}
|
|||
870e7fcc | Amos Benari | },
|
|
legend: {
|
|||
show: false
|
|||
},
|
|||
grid: {
|
|||
hoverable: true,
|
|||
clickable: true
|
|||
b7cb4996 | Ohad Levy | }
|
|
870e7fcc | Amos Benari | });
|
|
bind_hover_event(target, function(item){
|
|||
var percent = Math.round(item.series.percent);
|
|||
return item.series.label + ' ('+percent+'%)';
|
|||
});
|
|||
$(target).bind("plotclick", function (event, pos, item) {
|
|||
search_on_click(event, item);
|
|||
});
|
|||
5d0a54a5 | Tomer Brisker | resize_label(target.children('.pieLabel').first());
|
|
b7cb4996 | Ohad Levy | });
|
|
357a9111 | ripcurld00d | };
|
|
48c5cd3e | Amos Benari | ||
5d0a54a5 | Tomer Brisker | function resize_label(label){
|
|
var labelOffset = parseInt(label.css('left'));
|
|||
var ratio = (label.parent().width()-2*labelOffset)/label.width();
|
|||
if (ratio < 1){
|
|||
label.css('font-size', parseInt(label.css('font-size'))*ratio);
|
|||
}
|
|||
label.css('right', labelOffset); //make sure it is centered
|
|||
}
|
|||
a9ea3490 | Amir Feferkuchen | function trunc_with_tooltip(val){
|
|
if (val.length > 10) {
|
|||
var trunced_val = val.substring(0, 10) + "...";
|
|||
val = '<small data-toggle="tooltip" title=' + val + '>' + trunced_val + '</small>';
|
|||
}
|
|||
return val;
|
|||
}
|
|||
870e7fcc | Amos Benari | function expanded_pie(target, data){
|
|
a9ea3490 | Amir Feferkuchen | var max = {data:0}, sum = 0;
|
|
var label_exist = false;
|
|||
$(data).each(function(i, el){sum = sum + el.data; if (max.data< el.data) max = el;});
|
|||
870e7fcc | Amos Benari | $.plot(target, data, {
|
|
070b83fe | Amos Benari | colors: ['#0099d3', '#393f44','#00618a','#505459','#057d9f','#025167'],
|
|
870e7fcc | Amos Benari | series: {
|
|
pie: {
|
|||
show: true,
|
|||
070b83fe | Amos Benari | innerRadius: 0.8*3/4,
|
|
radius: 0.8,
|
|||
a9ea3490 | Amir Feferkuchen | label: {
|
|
show: true,
|
|||
radius: 0.001,
|
|||
formatter: function(label, series) {
|
|||
if (label_exist)
|
|||
return '';
|
|||
else{
|
|||
label_exist = true;
|
|||
return '<div id="test"> <div class="percent">' + Math.round(100 * max.data / sum) + '%</div>' + trunc_with_tooltip(max.label);
|
|||
}
|
|||
}
|
|||
},
|
|||
highlight: {
|
|||
opacity: 0.1
|
|||
b7cb4996 | Ohad Levy | }
|
|
}
|
|||
5f0e3715 | Amos Benari | },
|
|
legend: {
|
|||
a9ea3490 | Amir Feferkuchen | show: true,
|
|
container: $(".legend"),
|
|||
labelFormatter: function(label, series) {
|
|||
return trunc_with_tooltip(label)
|
|||
}
|
|||
5f0e3715 | Amos Benari | },
|
|
870e7fcc | Amos Benari | grid: {
|
|
070b83fe | Amos Benari | hoverable: true,
|
|
870e7fcc | Amos Benari | clickable: true
|
|
}
|
|||
b7cb4996 | Ohad Levy | });
|
|
a9ea3490 | Amir Feferkuchen | ||
bind_hover_event(target, function(item){
|
|||
var percent = Math.round(item.series.percent);
|
|||
return item.series.label + ' ('+percent+'%)';
|
|||
});
|
|||
870e7fcc | Amos Benari | target.bind("plotclick", function (event, pos, item) {
|
|
search_on_click(event, item);
|
|||
b7cb4996 | Ohad Levy | });
|
|
870e7fcc | Amos Benari | }
|
|
$.fn.flot_bar = function(){
|
|||
var options = arguments[0] || {};
|
|||
$(this).each(function(i,el){
|
|||
var target = $(el);
|
|||
$.plot($(target), [{ data: target.data('chart') }], {
|
|||
series: {
|
|||
bars: {
|
|||
show: true,
|
|||
barWidth: 0.6,
|
|||
070b83fe | Amos Benari | fill: 1
|
|
870e7fcc | Amos Benari | },
|
|
070b83fe | Amos Benari | color: "#00618a"
|
|
48c5cd3e | Amos Benari | },
|
|
870e7fcc | Amos Benari | xaxis: {
|
|
axisLabel: target.data('xaxis-label'),
|
|||
tickLength: 0, // hide gridlines
|
|||
ticks: target.data('ticks')
|
|||
},
|
|||
yaxis: {
|
|||
axisLabel: target.data('yaxis-label'),
|
|||
axisLabelPadding: 15,
|
|||
minTickSize: 1,
|
|||
fa517d69 | Tomer Brisker | tickDecimals: 0,
|
|
min: 0
|
|||
870e7fcc | Amos Benari | },
|
|
grid: {
|
|||
hoverable: true,
|
|||
borderWidth: 0
|
|||
},
|
|||
legend: {
|
|||
show: false
|
|||
b7cb4996 | Ohad Levy | }
|
|
870e7fcc | Amos Benari | });
|
|
bind_hover_event(target, function(item){return "<b>" + target.data('yaxis-label') + ":</b> " + item.datapoint[1];});
|
|||
b7cb4996 | Ohad Levy | });
|
|
357a9111 | ripcurld00d | };
|
|
fe4629de | rbirnie | ||
870e7fcc | Amos Benari | function flot_time_chart(target, data, legendOptions){
|
|
var chart_options = {
|
|||
series: {
|
|||
stack: target.hasClass('stack') ? true : null,
|
|||
lines: {
|
|||
show: true,
|
|||
fill: target.hasClass('stack') ? 0.8 : false
|
|||
b7cb4996 | Ohad Levy | }
|
|
},
|
|||
870e7fcc | Amos Benari | xaxis: {
|
|
mode: "time",
|
|||
axisLabel: target.data('xaxis-label'),
|
|||
tickLength: 0 // hide gridlines
|
|||
b7cb4996 | Ohad Levy | },
|
|
870e7fcc | Amos Benari | yaxis: {
|
|
axisLabel: target.data('yaxis-label'),
|
|||
axisLabelPadding: 12,
|
|||
minTickSize: 1,
|
|||
fa517d69 | Tomer Brisker | tickDecimals: 0,
|
|
min: 0
|
|||
b7cb4996 | Ohad Levy | },
|
|
870e7fcc | Amos Benari | selection: {
|
|
mode: "x"
|
|||
b7cb4996 | Ohad Levy | },
|
|
870e7fcc | Amos Benari | grid: {
|
|
hoverable: true,
|
|||
borderWidth: 0
|
|||
b7cb4996 | Ohad Levy | },
|
|
870e7fcc | Amos Benari | legend: legendOptions
|
|
357a9111 | ripcurld00d | };
|
|
870e7fcc | Amos Benari | $.plot($(target), data , chart_options);
|
|
bind_hover_event(target, function(item){return "<b>" + item.series.label + ":</b> " + item.series.data[item.dataIndex][1];});
|
|||
357a9111 | ripcurld00d | target.bind("plotselected", function (event, ranges) {flot_zoom(target, chart_options, ranges);});
|
|
870e7fcc | Amos Benari | }
|
|
$.fn.flot_chart = function(){
|
|||
$(this).each(function(i,el){
|
|||
flot_time_chart($(el), $(el).data('series'), chart_legend_options($(el)) );
|
|||
b7cb4996 | Ohad Levy | });
|
|
357a9111 | ripcurld00d | };
|
|
fe4629de | rbirnie | ||
870e7fcc | Amos Benari | function chart_legend_options(item){
|
|
if(item.data('series').length == 1) return {show: false};
|
|||
var options = item.data('legend-options');
|
|||
switch (options)
|
|||
{
|
|||
case "external":
|
|||
return {show: true,
|
|||
noColumns:4,
|
|||
container:"#legendContainer",
|
|||
labelFormatter: function(label, series) {
|
|||
ad01d4d2 | Walden Raines | return '<a rel="twipsy" data-original-title="' + __('Details') + '" href="' + series.href + '">' + label + '</a>';
|
|
fe4629de | rbirnie | }
|
|
357a9111 | ripcurld00d | };
|
|
870e7fcc | Amos Benari | case "hide":
|
|
return {show: false};
|
|||
default:
|
|||
fa517d69 | Tomer Brisker | return {show: true, margin: [0, -60]};
|
|
870e7fcc | Amos Benari | }
|
|
}
|
|||
function flot_zoom(target, options, ranges) {
|
|||
// clamp the zooming to prevent eternal zoom
|
|||
if (ranges.xaxis.to - ranges.xaxis.from < 0.00001)
|
|||
ranges.xaxis.to = ranges.xaxis.from + 0.00001;
|
|||
if (ranges.yaxis.to - ranges.yaxis.from < 0.00001)
|
|||
ranges.yaxis.to = ranges.yaxis.from + 0.00001;
|
|||
// do the zooming
|
|||
plot = $.plot(target, target.data('series'),
|
|||
$.extend(true, {}, options, {
|
|||
xaxis: { min: ranges.xaxis.from, max: ranges.xaxis.to },
|
|||
yaxis: { min: ranges.yaxis.from, max: ranges.yaxis.to }
|
|||
}));
|
|||
bb7fd69c | Tomer Brisker | if(target.parents('.stats-well').find('.reset-zoom').length == 0){
|
|
ad01d4d2 | Walden Raines | target.parents('.stats-well').prepend("<a class='reset-zoom btn btn-sm'>" + __('Reset zoom') + "</a>");
|
|
870e7fcc | Amos Benari | }
|
|
}
|
|||
function reset_zoom(item){
|
|||
target = $(item).parents('.stats-well').find('.statistics-chart');
|
|||
target.flot_chart();
|
|||
$(item).remove();
|
|||
}
|
|||
function showTooltip(pos, item, formater) {
|
|||
var content = formater(item);
|
|||
$('<div id="flot-tooltip">' + content + '</div>').css({
|
|||
top: pos.pageY - 40,
|
|||
left: pos.pageX -10,
|
|||
'border-color': item.series.color
|
|||
a9ea3490 | Amir Feferkuchen | }).appendTo("body").zIndex(5000).show();
|
|
870e7fcc | Amos Benari | }
|
|
$previousPoint=null;
|
|||
function bind_hover_event(target, formater){
|
|||
$(target).bind("plothover", function (event, pos, item) {
|
|||
if (item) {
|
|||
a9ea3490 | Amir Feferkuchen | $(target).css('cursor','pointer');
|
|
870e7fcc | Amos Benari | if ($previousPoint != item.datapoint) {
|
|
$previousPoint = item.datapoint;
|
|||
$("#flot-tooltip").remove();
|
|||
showTooltip(pos, item, formater);
|
|||
fe4629de | rbirnie | }
|
|
870e7fcc | Amos Benari | } else {
|
|
a9ea3490 | Amir Feferkuchen | $(target).css('cursor','default');
|
|
$("#flot-tooltip").remove();
|
|||
$previousPoint = null;
|
|||
870e7fcc | Amos Benari | }
|
|
});
|
|||
}
|
|||
function search_on_click(event, item) {
|
|||
var link = $(event.currentTarget).data('url');
|
|||
if (link == undefined) return;
|
|||
2af890f2 | Amos Benari | if (link.indexOf("search_by_legend") != -1){
|
|
var selector = '.label[style*="background-color:' + item.series.color +'"]';
|
|||
357a9111 | ripcurld00d | link = $(event.currentTarget).parents('.stats-well').find(selector).next('a').attr('href');
|
|
5c5dabb2 | Tomer Brisker | if (link == undefined) // we are on the overview page - no stats-well parent
|
|
link = $(event.currentTarget).parents('#dashboard').find(selector).next('a').attr('href');
|
|||
870e7fcc | Amos Benari | } else {
|
|
357a9111 | ripcurld00d | if (link.indexOf("~VAL1~") != -1 || link.indexOf("~VAL2~") != -1) {
|
|
2af890f2 | Amos Benari | var strSplit = item.series.label.split(" ");
|
|
var val1 = strSplit[0];
|
|||
357a9111 | ripcurld00d | var val2 = (strSplit.length > 1) ? strSplit[1] : "";
|
|
link = link.replace("~VAL1~", val1);
|
|||
2af890f2 | Amos Benari | link = link.replace("~VAL2~", val2);
|
|
} else {
|
|||
357a9111 | ripcurld00d | var val = item.series.label;
|
|
if (val.indexOf(" ") != -1) val = '"' + val + '"';
|
|||
link = link.replace("~VAL~", val);
|
|||
2af890f2 | Amos Benari | }
|
|
870e7fcc | Amos Benari | }
|
|
event.preventDefault();
|
|||
window.location.href = link;
|
|||
}
|
|||
function get_pie_chart(div, url) {
|
|||
f5975f94 | Shlomi Zadok | ||
870e7fcc | Amos Benari | if($("#"+div).length == 0)
|
|
{
|
|||
bf4a13d3 | Walden Raines | $('body').append('<div id="' + div + '" class="modal fade"><div class="modal-dialog"><div class="modal-content"></div></div></div>');
|
|
f5975f94 | Shlomi Zadok | $("#"+div+" .modal-content").append('<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h4 class="modal-title"></h4></div>')
|
|
a9ea3490 | Amir Feferkuchen | .append('<div class="container">')
|
|
.append('<div id="' + div + '-body" class="fact_chart modal-body">' + __('Loading') + ' ... </div>')
|
|||
.append('<div class="legend" ></div>')
|
|||
.append('</div>')
|
|||
357a9111 | ripcurld00d | .append('<div class="modal-footer"></div>');
|
|
bf4a13d3 | Walden Raines | ||
870e7fcc | Amos Benari | $("#"+div).modal('show');
|
|
ccb763a6 | Amos Benari | $("#"+div).on('shown.bs.modal', function() {
|
|
$.getJSON(url, function(data) {
|
|||
var target = $("#"+div+"-body");
|
|||
target.empty();
|
|||
f5975f94 | Shlomi Zadok | var hostsCount = 0;
|
|
$.each(data.values,function() {
|
|||
hostsCount += this.data;
|
|||
});
|
|||
357a9111 | ripcurld00d | expanded_pie(target, data.values);
|
|
f5975f94 | Shlomi Zadok | $('.modal-title').empty().append( __('Fact distribution chart') + ' - <b>' + data.name + ' </b><small> ('+ Jed.sprintf(n__("%s host", "%s hosts", hostsCount), hostsCount) +')</small>');
|
|
ccb763a6 | Amos Benari | target.attr('data-url', foreman_url("/hosts?search=facts." + data.name + "~~VAL1~"));
|
|
357a9111 | ripcurld00d | });
|
|
});
|
|||
870e7fcc | Amos Benari | } else {$("#"+div).modal('show');}
|
|
}
|
|||
function expand_chart(ref){
|
|||
var chart = $(ref);
|
|||
if (!chart.hasClass('statistics-pie')){
|
|||
8cb120ea | Tomer Brisker | chart = chart.parent().find('.statistics-pie');
|
|
870e7fcc | Amos Benari | }
|
|
8cb120ea | Tomer Brisker | var modal_id = chart[0].id+'_modal';
|
|
870e7fcc | Amos Benari | if($("#"+modal_id).length == 0)
|
|
{
|
|||
var new_chart = chart.clone().empty().attr('id', modal_id + "_chart").removeClass('small');
|
|||
070b83fe | Amos Benari | $('body').append('<div id="' + modal_id + '" class="modal fade"><div class="modal-dialog"><div class="modal-content"></div></div></div>');
|
|
$("#"+modal_id+" .modal-content").append('<div class="modal-header"><a href="#" class="close" data-dismiss="modal">×</a><h3> ' +chart.data('title')+ ' </h3></div>')
|
|||
.append('<div class="modal-body"></div>');
|
|||
$("#"+modal_id+" .modal-body").append(new_chart);
|
|||
870e7fcc | Amos Benari | expanded_pie(new_chart, new_chart.data('series'));
|
|
}
|
|||
$("#"+modal_id).modal('show');
|
|||
}
|
|||
function legend_selected(item){
|
|||
$(item).closest('td').toggleClass('disabled');
|
|||
$(item).closest('td').next().toggleClass('disabled');
|
|||
var target = $(item).parents('.statistics-chart');
|
|||
var series = target.clone().data('series');
|
|||
var legend = target.find('.legend').clone();
|
|||
var data= [];
|
|||
// Remove the data series.
|
|||
target.find('.legend td.legendLabel:not(.disabled)').each(function() {
|
|||
var key = $(this).text();
|
|||
for (var i = 0; i < series.length; i++) {
|
|||
if (series[i].label === key) {
|
|||
data.push(series[i]);
|
|||
return true;
|
|||
dcc1c508 | rbirnie | }
|
|
870e7fcc | Amos Benari | }
|
|
});
|
|||
flot_time_chart(target, data);
|
|||
target.find('.legend').remove();
|
|||
target.append(legend);
|
|||
}
|
|||
function ext_legend_selected(item){
|
|||
$(item).closest('td').toggleClass('disabled');
|
|||
$(item).closest('td').next().toggleClass('disabled');
|
|||
var target = $('.statistics-chart');
|
|||
var series = target.clone().data('series');
|
|||
var data= [];
|
|||
// Remove the data series.
|
|||
$('#legendContainer table td.legendLabel:not(.disabled)').each(function() {
|
|||
var key = $(this).text();
|
|||
for (var i = 0; i < series.length; i++) {
|
|||
if (series[i].label === key) {
|
|||
data.push(series[i]);
|
|||
return true;
|
|||
fe4629de | rbirnie | }
|
|
870e7fcc | Amos Benari | }
|
|
fe4629de | rbirnie | });
|
|
870e7fcc | Amos Benari | ||
flot_time_chart(target, data, {show: false});
|
|||
fe4629de | rbirnie | }
|
|
870e7fcc | Amos Benari | ||
bf59cec1 | Tomer Brisker | function updateChart(item, status) {
|
|
if (status == 'success')
|
|||
$(item).find(".statistics-chart").flot_chart();
|
|||
else
|
|||
$(item).text(__("Failed to load chart"));
|
|||
}
|
|||
870e7fcc | Amos Benari | $(function() {
|
|
a9ea3490 | Amir Feferkuchen | $('[data-toggle="tooltip"]').tooltip();
|
|
870e7fcc | Amos Benari | $(".statistics-pie").flot_pie();
|
|
$(".statistics-bar").flot_bar();
|
|||
$(".statistics-chart").flot_chart();
|
|||
357a9111 | ripcurld00d | $(document).on('click', '.reset-zoom', function () {reset_zoom(this);});
|
|
$(document).on('click', '.legend .legendColorBox, .legend .legendLabel', function() { legend_selected(this);});
|
|||
$(document).on('click', '#legendContainer .legendColorBox, .legendContainer .legendLabel', function() { ext_legend_selected(this);});
|
|||
870e7fcc | Amos Benari | });
|
|
$(window).resize(function() {
|
|||
$(".statistics-bar").flot_bar();
|
|||
$(".statistics-chart").flot_chart();
|
|||
3dfb6e4c | Dominic Cleal | });
|