Merge branch 'ent-4490-Cambio-de-dato-a-percentil-segun-el-tamano-de-custom-graph-en-consola-visual' into 'develop'

fixed error labels pie chart

See merge request artica/pandorafms!2679
This commit is contained in:
Alejandro Fraguas 2019-08-27 12:02:27 +02:00
commit fe088a59d6
2 changed files with 887 additions and 806 deletions

View File

@ -56,7 +56,6 @@ More detail and specific examples can be found in the included HTML file.
*/
(function($) {
// Maximum redraw attempts when fitting labels within the plot
var REDRAW_ATTEMPTS = 10;
@ -66,7 +65,6 @@ More detail and specific examples can be found in the included HTML file.
var REDRAW_SHRINK = 0.95;
function init(plot) {
var canvas = null,
target = null,
options = null,
@ -84,7 +82,6 @@ More detail and specific examples can be found in the included HTML file.
plot.hooks.processOptions.push(function(plot, options) {
if (options.series.pie.show) {
options.grid.show = false;
// set labels.show
@ -150,7 +147,7 @@ More detail and specific examples can be found in the included HTML file.
}
});
function processDatapoints(plot, series, datapoints) {
function processDatapoints(plot) {
if (!processed) {
processed = true;
canvas = plot.getCanvas();
@ -161,7 +158,6 @@ More detail and specific examples can be found in the included HTML file.
}
function combine(data) {
var total = 0,
combined = 0,
numCombined = 0,
@ -171,7 +167,6 @@ More detail and specific examples can be found in the included HTML file.
// Fix up the raw data from Flot, ensuring the data is numeric
for (var i = 0; i < data.length; ++i) {
var value = data[i].data;
// If the data is an array, we'll assume that it's a standard
@ -223,14 +218,18 @@ More detail and specific examples can be found in the included HTML file.
for (var i = 0; i < data.length; ++i) {
var value = data[i].data[0][1];
if (numCombined < 2 || value / total > options.series.pie.combine.threshold) {
if (
numCombined < 2 ||
value / total > options.series.pie.combine.threshold
) {
newdata.push(
$.extend(data[i], { /* extend to allow keeping all other original data values
$.extend(data[i], {
/* extend to allow keeping all other original data values
and using them e.g. in labelFormatter. */
data: [[1, value]],
color: data[i].color,
label: data[i].label,
angle: value * Math.PI * 2 / total,
angle: (value * Math.PI * 2) / total,
percent: value / (total / 100)
})
);
@ -242,7 +241,7 @@ More detail and specific examples can be found in the included HTML file.
data: [[1, combined]],
color: color,
label: options.series.pie.combine.label,
angle: combined * Math.PI * 2 / total,
angle: (combined * Math.PI * 2) / total,
percent: combined / (total / 100)
});
}
@ -251,14 +250,18 @@ More detail and specific examples can be found in the included HTML file.
}
function draw(plot, newCtx) {
if (!target) {
return; // if no series were passed
}
var canvasWidth = plot.getPlaceholder().width(),
canvasHeight = plot.getPlaceholder().height(),
legendWidth = target.children().filter(".legend").children().width() || 0;
legendWidth =
target
.children()
.filter(".legend")
.children()
.width() || 0;
ctx = newCtx;
@ -287,7 +290,8 @@ More detail and specific examples can be found in the included HTML file.
// calculate maximum radius and center point
maxRadius = Math.min(canvasWidth, canvasHeight / options.series.pie.tilt) / 2;
maxRadius =
Math.min(canvasWidth, canvasHeight / options.series.pie.tilt) / 2;
centerTop = canvasHeight / 2 + options.series.pie.offset.top;
centerLeft = canvasWidth / 2;
@ -321,11 +325,13 @@ More detail and specific examples can be found in the included HTML file.
if (options.series.pie.tilt <= 0.8) {
drawShadow();
}
} while (!drawPie() && attempts < REDRAW_ATTEMPTS)
} while (!drawPie() && attempts < REDRAW_ATTEMPTS);
if (attempts >= REDRAW_ATTEMPTS) {
clear();
target.prepend("<div class='error'>Could not draw pie with labels contained inside canvas</div>");
target.prepend(
"<div class='error'>Could not draw pie with labels contained inside canvas</div>"
);
}
if (plot.setSeries && plot.insertLegend) {
@ -337,18 +343,27 @@ More detail and specific examples can be found in the included HTML file.
function clear() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
target.children().filter(".pieLabel, .pieLabelBackground").remove();
target
.children()
.filter(".pieLabel, .pieLabelBackground")
.remove();
}
function drawShadow() {
var shadowLeft = options.series.pie.shadow.left;
var shadowTop = options.series.pie.shadow.top;
var edge = 10;
var alpha = options.series.pie.shadow.alpha;
var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
var radius =
options.series.pie.radius > 1
? options.series.pie.radius
: maxRadius * options.series.pie.radius;
if (radius >= canvasWidth / 2 - shadowLeft || radius * options.series.pie.tilt >= canvasHeight / 2 - shadowTop || radius <= edge) {
if (
radius >= canvasWidth / 2 - shadowLeft ||
radius * options.series.pie.tilt >= canvasHeight / 2 - shadowTop ||
radius <= edge
) {
return; // shadow would be outside canvas, so don't draw it
}
@ -375,9 +390,11 @@ More detail and specific examples can be found in the included HTML file.
}
function drawPie() {
var startAngle = Math.PI * options.series.pie.startAngle;
var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
var radius =
options.series.pie.radius > 1
? options.series.pie.radius
: maxRadius * options.series.pie.radius;
// center and rotate to starting position
@ -421,7 +438,6 @@ More detail and specific examples can be found in the included HTML file.
} else return true;
function drawSlice(angle, color, fill) {
if (angle <= 0 || isNaN(angle)) {
return;
}
@ -440,7 +456,14 @@ More detail and specific examples can be found in the included HTML file.
//ctx.arc(0, 0, radius, 0, angle, false); // This doesn't work properly in Opera
ctx.arc(0, 0, radius, currentAngle, currentAngle + angle / 2, false);
ctx.arc(0, 0, radius,currentAngle + angle / 2, currentAngle + angle, false);
ctx.arc(
0,
0,
radius,
currentAngle + angle / 2,
currentAngle + angle,
false
);
ctx.closePath();
//ctx.rotate(angle); // This doesn't work properly in Opera
currentAngle += angle;
@ -453,9 +476,12 @@ More detail and specific examples can be found in the included HTML file.
}
function drawLabels() {
var labels = [];
var currentAngle = startAngle;
var radius = options.series.pie.label.radius > 1 ? options.series.pie.label.radius : maxRadius * options.series.pie.label.radius;
var radius =
options.series.pie.label.radius > 1
? options.series.pie.label.radius
: maxRadius * options.series.pie.label.radius;
for (var i = 0; i < slices.length; ++i) {
if (slices[i].percent >= options.series.pie.label.threshold * 100) {
@ -469,14 +495,15 @@ More detail and specific examples can be found in the included HTML file.
return true;
function drawLabel(slice, startAngle, index) {
if (slice.data[0][1] == 0) {
return true;
}
// format label text
var lf = options.legend.labelFormatter, text, plf = options.series.pie.label.formatter;
var lf = options.legend.labelFormatter,
text,
plf = options.series.pie.label.formatter;
if (lf) {
text = lf(slice.label, slice);
@ -488,28 +515,76 @@ More detail and specific examples can be found in the included HTML file.
text = plf(text, slice);
}
var halfAngle = ((startAngle + slice.angle) + startAngle) / 2;
var halfAngle = (startAngle + slice.angle + startAngle) / 2;
var x = centerLeft + Math.round(Math.cos(halfAngle) * radius);
var y = centerTop + Math.round(Math.sin(halfAngle) * radius) * options.series.pie.tilt;
var y =
centerTop +
Math.round(Math.sin(halfAngle) * radius) *
options.series.pie.tilt;
var html = "<span class='pieLabel' id='pieLabel" + index + "' style='position:absolute;top:" + y + "px;left:" + x + "px;'>" + text + "</span>";
var html =
"<span class='pieLabel' id='pieLabel" +
index +
"' style='position:absolute;top:" +
y +
"px;left:" +
x +
"px;'>" +
text +
"</span>";
target.append(html);
var label = target.children("#pieLabel" + index);
var labelTop = (y - label.height() / 2);
var labelLeft = (x - label.width() / 2);
var labelTop = y - label.height() / 2;
var labelLeft = x - label.width() / 2;
label.css("top", labelTop);
label.css("left", labelLeft);
// check to make sure that the label doesn't overlap one of the other labels
var label_pos = getPositions(label);
for (var j = 0; j < labels.length; j++) {
var tmpPos = getPositions(labels[j]);
var horizontalMatch = comparePositions(label_pos[0], tmpPos[0]);
var verticalMatch = comparePositions(label_pos[1], tmpPos[1]);
var match = horizontalMatch && verticalMatch;
if (match) {
var newTop = tmpPos[1][0] - (label.height() + 1);
label.css("top", newTop);
labelTop = newTop;
}
}
function getPositions(box) {
var $box = $(box);
var pos = $box.position();
var width = $box.width();
var height = $box.height();
return [
[pos.left, pos.left + width],
[pos.top, pos.top + height]
];
}
function comparePositions(p1, p2) {
var x1 = p1[0] < p2[0] ? p1 : p2;
var x2 = p1[0] < p2[0] ? p2 : p1;
return x1[1] > x2[0] || x1[0] === x2[0] ? true : false;
}
labels.push(label);
// check to make sure that the label is not outside the canvas
if (0 - labelTop > 0 || 0 - labelLeft > 0 || canvasHeight - (labelTop + label.height()) < 0 || canvasWidth - (labelLeft + label.width()) < 0) {
if (
0 - labelTop > 0 ||
0 - labelLeft > 0 ||
canvasHeight - (labelTop + label.height()) < 0 ||
canvasWidth - (labelLeft + label.width()) < 0
) {
return false;
}
if (options.series.pie.label.background.opacity != 0) {
// put in the transparent background separately to avoid blended labels and label boxes
var c = options.series.pie.label.background.color;
@ -519,7 +594,17 @@ More detail and specific examples can be found in the included HTML file.
}
var pos = "top:" + labelTop + "px;left:" + labelLeft + "px;";
$("<div class='pieLabelBackground' style='position:absolute;width:" + label.width() + "px;height:" + label.height() + "px;" + pos + "background-color:" + c + ";'></div>")
$(
"<div class='pieLabelBackground' style='position:absolute;width:" +
label.width() +
"px;height:" +
label.height() +
"px;" +
pos +
"background-color:" +
c +
";'></div>"
)
.css("opacity", options.series.pie.label.background.opacity)
.insertBefore(label);
}
@ -534,11 +619,13 @@ More detail and specific examples can be found in the included HTML file.
function drawDonutHole(layer) {
if (options.series.pie.innerRadius > 0) {
// subtract the center
layer.save();
var innerRadius = options.series.pie.innerRadius > 1 ? options.series.pie.innerRadius : maxRadius * options.series.pie.innerRadius;
var innerRadius =
options.series.pie.innerRadius > 1
? options.series.pie.innerRadius
: maxRadius * options.series.pie.innerRadius;
layer.globalCompositeOperation = "destination-out"; // this does not work with excanvas, but it will fall back to using the stroke color
layer.beginPath();
layer.fillStyle = options.series.pie.stroke.color;
@ -565,31 +652,50 @@ More detail and specific examples can be found in the included HTML file.
function isPointInPoly(poly, pt) {
for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) || (poly[j][1] <= pt[1] && pt[1]< poly[i][1]))
&& (pt[0] < (poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0])
&& (c = !c);
((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) ||
(poly[j][1] <= pt[1] && pt[1] < poly[i][1])) &&
pt[0] <
((poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1])) /
(poly[j][1] - poly[i][1]) +
poly[i][0] &&
(c = !c);
return c;
}
function findNearbySlice(mouseX, mouseY) {
var slices = plot.getData(),
options = plot.getOptions(),
radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius,
x, y;
radius =
options.series.pie.radius > 1
? options.series.pie.radius
: maxRadius * options.series.pie.radius,
x,
y;
for (var i = 0; i < slices.length; ++i) {
var s = slices[i];
if (s.pie.show) {
ctx.save();
ctx.beginPath();
ctx.moveTo(0, 0); // Center of the pie
//ctx.scale(1, options.series.pie.tilt); // this actually seems to break everything when here.
ctx.arc(0, 0, radius, s.startAngle, s.startAngle + s.angle / 2, false);
ctx.arc(0, 0, radius, s.startAngle + s.angle / 2, s.startAngle + s.angle, false);
ctx.arc(
0,
0,
radius,
s.startAngle,
s.startAngle + s.angle / 2,
false
);
ctx.arc(
0,
0,
radius,
s.startAngle + s.angle / 2,
s.startAngle + s.angle,
false
);
ctx.closePath();
x = mouseX - centerLeft;
y = mouseY - centerTop;
@ -605,7 +711,6 @@ More detail and specific examples can be found in the included HTML file.
};
}
} else {
// excanvas for IE doesn;t support isPointInPath, this is a workaround.
var p1X = radius * Math.cos(s.startAngle),
@ -618,7 +723,14 @@ More detail and specific examples can be found in the included HTML file.
p4Y = radius * Math.sin(s.startAngle + s.angle / 1.5),
p5X = radius * Math.cos(s.startAngle + s.angle),
p5Y = radius * Math.sin(s.startAngle + s.angle),
arrPoly = [[0, 0], [p1X, p1Y], [p2X, p2Y], [p3X, p3Y], [p4X, p4Y], [p5X, p5Y]],
arrPoly = [
[0, 0],
[p1X, p1Y],
[p2X, p2Y],
[p3X, p3Y],
[p4X, p4Y],
[p5X, p5Y]
],
arrPoint = [x, y];
// TODO: perhaps do some mathmatical trickery here with the Y-coordinate to compensate for pie tilt?
@ -652,14 +764,12 @@ More detail and specific examples can be found in the included HTML file.
// trigger click or hover event (they send the same parameters so we share their code)
function triggerClickHoverEvent(eventname, e) {
var offset = plot.offset();
var canvasX = parseInt(e.pageX - offset.left);
var canvasY = parseInt(e.pageY - offset.top);
var item = findNearbySlice(canvasX, canvasY);
if (options.grid.autoHighlight) {
// clear auto-highlights
for (var i = 0; i < highlights.length; ++i) {
@ -718,17 +828,18 @@ More detail and specific examples can be found in the included HTML file.
function indexOfHighlight(s) {
for (var i = 0; i < highlights.length; ++i) {
var h = highlights[i];
if (h.series == s)
return i;
if (h.series == s) return i;
}
return -1;
}
function drawOverlay(plot, octx) {
var options = plot.getOptions();
var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
var radius =
options.series.pie.radius > 1
? options.series.pie.radius
: maxRadius * options.series.pie.radius;
octx.save();
octx.translate(centerLeft, centerTop);
@ -743,19 +854,33 @@ More detail and specific examples can be found in the included HTML file.
octx.restore();
function drawHighlight(series) {
if (series.angle <= 0 || isNaN(series.angle)) {
return;
}
//octx.fillStyle = parseColor(options.series.pie.highlight.color).scale(null, null, null, options.series.pie.highlight.opacity).toString();
octx.fillStyle = "rgba(255, 255, 255, " + options.series.pie.highlight.opacity + ")"; // this is temporary until we have access to parseColor
octx.fillStyle =
"rgba(255, 255, 255, " + options.series.pie.highlight.opacity + ")"; // this is temporary until we have access to parseColor
octx.beginPath();
if (Math.abs(series.angle - Math.PI * 2) > 0.000000001) {
octx.moveTo(0, 0); // Center of the pie
}
octx.arc(0, 0, radius, series.startAngle, series.startAngle + series.angle / 2, false);
octx.arc(0, 0, radius, series.startAngle + series.angle / 2, series.startAngle + series.angle, false);
octx.arc(
0,
0,
radius,
series.startAngle,
series.startAngle + series.angle / 2,
false
);
octx.arc(
0,
0,
radius,
series.startAngle + series.angle / 2,
series.startAngle + series.angle,
false
);
octx.closePath();
octx.fill();
}
@ -769,7 +894,7 @@ More detail and specific examples can be found in the included HTML file.
pie: {
show: false,
radius: "auto", // actual radius of the visible pie (based on full calculated radius if <=1, or hard pixel value)
innerRadius: 0, /* for donut */
innerRadius: 0 /* for donut */,
startAngle: 3 / 2,
tilt: 1,
shadow: {
@ -788,7 +913,15 @@ More detail and specific examples can be found in the included HTML file.
label: {
show: "auto",
formatter: function(label, slice) {
return "<div style='font-size:x-small;text-align:center;padding:2px;color:" + slice.color + ";'>" + label + "<br/>" + Math.round(slice.percent) + "%</div>";
return (
"<div style='font-size:x-small;text-align:center;padding:2px;color:" +
slice.color +
";'>" +
label +
"<br/>" +
Math.round(slice.percent) +
"%</div>"
);
}, // formatter function
radius: 1, // radius at which to place the labels (based on full calculated radius if <=1, or hard pixel value)
background: {
@ -816,5 +949,4 @@ More detail and specific examples can be found in the included HTML file.
name: "pie",
version: "1.1"
});
})(jQuery);

View File

@ -1,4 +1,5 @@
/* global $ */
/* exported pandoraFlotPie, pandoraFlotPieCustom */
function pandoraFlotPie(
graph_id,
@ -14,7 +15,7 @@ function pandoraFlotPie(
colors,
hide_labels
) {
var labels = labels.split(separator);
labels = labels.split(separator);
var data = values.split(separator);
if (colors != "") {
@ -92,7 +93,7 @@ function pandoraFlotPie(
function pieHover(event, pos, obj) {
if (!obj) return;
index = obj.seriesIndex;
var index = obj.seriesIndex;
legends.css("color", "#3F3F3D");
legends.eq(index).css("color", "");
}
@ -144,21 +145,14 @@ function pandoraFlotPieCustom(
.pop()
.split(".")
.shift();
var labels = labels.split(separator);
var legend = legend.split(separator);
labels = labels.split(separator);
legend = legend.split(separator);
var data = values.split(separator);
var no_data = 0;
if (colors != "") {
colors = colors.split(separator);
}
var colors_data = [
"#e63c52",
"#FFA631",
"#f3b200",
"#5BB6E5",
"#F2919D",
"#82b92e"
];
var color = null;
for (var i = 0; i < data.length; i++) {
if (colors != "") {
@ -174,28 +168,31 @@ function pandoraFlotPieCustom(
if (width <= 450) {
show_legend = false;
label_conf = {
show: false
};
} else {
label_conf = {
show: true,
radius: 0.75,
radius: 5 / 8,
formatter: function(label, series) {
console.log(series);
return (
'<div style="font-size:' +
font_size +
"pt;" +
'text-align:center;padding:2px;color:white;">' +
series.percent.toFixed(2) +
"%</div>"
"pt; font-weight:bolder;" +
"text-align:center;padding:2px;color:rgb(63, 63, 61)" +
'">' +
label +
":<br>" +
series.data[0][1] +
"</div>"
);
},
background: {
opacity: 0.5,
color: ""
opacity: 0.5
}
};
} else {
label_conf = {
show: false
};
}
var conf_pie = {
@ -203,8 +200,8 @@ function pandoraFlotPieCustom(
pie: {
show: true,
radius: 3 / 4,
innerRadius: 0.4
//label: label_conf
innerRadius: 0.4,
label: label_conf
}
},
legend: {
@ -234,7 +231,7 @@ function pandoraFlotPieCustom(
var legends = $("#" + graph_id + " .legendLabel");
var j = 0;
legends.each(function() {
//$(this).css('width', $(this).width());
//$(this).css("width", $(this).width());
$(this).css("font-size", font_size + "pt");
$(this).removeClass("legendLabel");
$(this).addClass(font);
@ -264,19 +261,6 @@ function pandoraFlotPieCustom(
return false;
});
var pielegends = $("#" + graph_id + " .pieLabelBackground");
pielegends.each(function() {
$(this)
.css("transform", "rotate(-35deg)")
.css("color", "black");
});
var labelpielegends = $("#" + graph_id + " .pieLabel");
labelpielegends.each(function() {
$(this)
.css("transform", "rotate(-35deg)")
.css("color", "black");
});
// Events
$("#" + graph_id).bind("plothover", pieHover);
$("#" + graph_id).bind("plotclick", Clickpie);
@ -287,16 +271,17 @@ function pandoraFlotPieCustom(
function pieHover(event, pos, obj) {
if (!obj) return;
index = obj.seriesIndex;
var index = obj.seriesIndex;
legends.css("color", "#3F3F3D");
legends.eq(index).css("color", "");
}
function Clickpie(event, pos, obj) {
if (!obj) return;
percent = parseFloat(obj.series.percent).toFixed(2);
valor = parseFloat(obj.series.data[0][1]);
var percent = parseFloat(obj.series.percent).toFixed(2);
var valor = parseFloat(obj.series.data[0][1]);
var value = "";
if (valor > 1000000) {
value = Math.round((valor / 1000000) * 100) / 100;
value = value + "M";
@ -325,42 +310,6 @@ function pandoraFlotPieCustom(
$("#watermark_image_" + graph_id).attr("src")
);
}
/*
window.onresize = function(event) {
$.plot($('#' + graph_id), data, conf_pie);
if (no_data == data.length) {
$('#'+graph_id+' .overlay').remove();
$('#'+graph_id+' .base').remove();
$('#'+graph_id).prepend("<img style='width:50%;' src='images/no_data_toshow.png' />");
}
var legends = $('#'+graph_id+' .legendLabel');
var j = 0;
legends.each(function () {
//$(this).css('width', $(this).width());
$(this).css('font-size', font_size+'pt');
$(this).removeClass("legendLabel");
$(this).addClass(font);
$(this).text(legend[j]);
j++;
});
if ($('input[name="custom_graph"]').val()) {
$('.legend>div').css('right',($('.legend>div').height()*-1));
$('.legend>table').css('right',($('.legend>div').height()*-1));
}
//$('.legend>table').css('border',"1px solid #E2E2E2");
$('.legend>table').css('background-color',"transparent");
var pielegends = $('#'+graph_id+' .pieLabelBackground');
pielegends.each(function () {
$(this).css('transform', "rotate(-35deg)").css('color', 'black');
});
var labelpielegends = $('#'+graph_id+' .pieLabel');
labelpielegends.each(function () {
$(this).css('transform', "rotate(-35deg)").css('color', 'black');
});
}
*/
}
function pandoraFlotHBars(