Project

General

Profile

Download (9.54 KB) Statistics
| Branch: | Tag: | Revision:
870e7fcc Amos Benari
$.fn.flot_pie = function(){
var options = arguments[0] || {};
$(this).each(function(i,el){
var target = $(el);
a7f7ee02 Amos Benari
870e7fcc Amos Benari
$.plot(target, target.data('series'), {
colors: ['#4572A7','#AA4643','#89A54E','#80699B','#3D96AE','#DB843D','#92A8CD','#A47D7C','#B5CA92'],
series: {
pie: options.pie || {
show: true,
radius: 1,
label: {
show: true,
radius: 1/2,
formatter: function(label, series){
return '<div class="pie-labels">'+label+'<br/>'+Math.round(series.percent)+'%</div>';
},
threshold: 0.1
},
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);
});
b7cb4996 Ohad Levy
});
}
48c5cd3e Amos Benari
870e7fcc Amos Benari
function expanded_pie(target, data){
$.plot(target, data, {
colors: ['#4572A7','#AA4643','#89A54E','#80699B','#3D96AE','#DB843D','#92A8CD','#A47D7C','#B5CA92'],
series: {
pie: {
show: true,
labels: {
show:true
b7cb4996 Ohad Levy
}
}
5f0e3715 Amos Benari
},
legend: {
870e7fcc Amos Benari
show: false
5f0e3715 Amos Benari
},
870e7fcc Amos Benari
grid: {
clickable: true
}
b7cb4996 Ohad Levy
});
5f0e3715 Amos Benari
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,
fill: 0.8
},
color: "#4572A7"
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,
tickDecimals: 0
},
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
});
}
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,
tickDecimals: 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
}
$.plot($(target), data , chart_options);
bind_hover_event(target, function(item){return "<b>" + item.series.label + ":</b> " + item.series.data[item.dataIndex][1];});
target.bind("plotselected", function (event, ranges) {flot_zoom(target, chart_options, ranges)});
}

$.fn.flot_chart = function(){
$(this).each(function(i,el){
flot_time_chart($(el), $(el).data('series'), chart_legend_options($(el)) );
b7cb4996 Ohad Levy
});
}
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
}
}
870e7fcc Amos Benari
case "hide":
return {show: false};
default:
return {show: true};
}
}

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 }
}));
if(target.parents('.stats-well').find('.reset-zoom').size() == 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
}).appendTo("body").show();
}

$previousPoint=null;
function bind_hover_event(target, formater){
$(target).bind("plothover", function (event, pos, item) {
if (item) {
if ($previousPoint != item.datapoint) {
$previousPoint = item.datapoint;
$("#flot-tooltip").remove();
showTooltip(pos, item, formater);
fe4629de rbirnie
}
870e7fcc Amos Benari
} else {
$("#flot-tooltip").remove();
$previousPoint = null;
}
});
}

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 +'"]';
link = $(event.currentTarget).parents('.stats-well').find(selector).next('a').attr('href')
870e7fcc Amos Benari
} else {
2af890f2 Amos Benari
if (link.indexOf("~VAL2~") != -1) {
var strSplit = item.series.label.split(" ");
var val1 = strSplit[0];
var val2 = strSplit[1];
link = link.replace("~VAL2~", val2);
} else {
var val1 = item.series.label;
if (val1.indexOf(" ") != -1) val1 = '"' + val1 +'"';
}
link = link.replace("~VAL1~", val1);
870e7fcc Amos Benari
}
event.preventDefault();
window.location.href = link;
}

function get_pie_chart(div, url) {
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>');
ad01d4d2 Walden Raines
$("#"+div+" .modal-content").append('<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">' + __('Fact Chart') + '</h4></div>')
.append('<div id="' + div + '-body" class="fact_chart modal-body">' + __('Loading') + ' ...</div>')
bf4a13d3 Walden Raines
.append('<div class="modal-footer"></div>')

870e7fcc Amos Benari
$("#"+div).modal('show');
$.getJSON(url, function(data) {
var target = $("#"+div+"-body");
8cd09f95 Ohad Levy
target.empty();
870e7fcc Amos Benari
expanded_pie(target, data.values)
target.attr('data-url', foreman_url("/hosts?search=facts." + data.name + "~~VAL1~"));
});
} else {$("#"+div).modal('show');}
}

function expand_chart(ref){
var chart = $(ref);
if (!chart.hasClass('statistics-pie')){
chart = $(ref).parent().find('.statistics-pie');
}
var modal_id = chart.attr('id')+'_modal';
if($("#"+modal_id).length == 0)
{
var new_chart = chart.clone().empty().attr('id', modal_id + "_chart").removeClass('small');
$('body').append('<div id="' + modal_id + '" class="modal fade"></div>');
$("#"+modal_id).append('<div class="modal-header"><a href="#" class="close" data-dismiss="modal">&times;</a><h3> ' +chart.data('title')+ ' </h3></div>')
.append(new_chart);
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
$(function() {
$(".statistics-pie").flot_pie();
$(".statistics-bar").flot_bar();
$(".statistics-chart").flot_chart();
$(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)});
});

$(window).resize(function() {
$(".statistics-bar").flot_bar();
$(".statistics-chart").flot_chart();
3dfb6e4c Dominic Cleal
});