commit b33c3915370ab8ede27556a2b0c9993bc0dcbfb8
parent 0cd5305ce0d4503ecc69cee466295db2fbf62211
Author: Brendan Gregg <brendan.d.gregg@gmail.com>
Date: Thu, 16 Oct 2014 12:19:47 +0200
Merge pull request #35 from Saruspete/master
Added Zoom inside SVG
Diffstat:
| M | flamegraph.pl | | | 102 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
1 file changed, 97 insertions(+), 5 deletions(-)
diff --git a/flamegraph.pl b/flamegraph.pl
@@ -186,7 +186,7 @@ SVG
my @g_attr = map {
exists $attr->{$_} ? sprintf(qq/$_="%s"/, $attr->{$_}) : ()
- } qw(class style onmouseover onmouseout);
+ } qw(class style onmouseover onmouseout onclick);
push @g_attr, $attr->{g_extra} if $attr->{g_extra};
$self->{svg} .= sprintf qq/<g %s>\n/, join(' ', @g_attr);
@@ -408,10 +408,99 @@ my $inc = <<INC;
</style>
<script type="text/ecmascript">
<![CDATA[
- var details;
- function init(evt) { details = document.getElementById("details").firstChild; }
+ var details, svg;
+ function init(evt) {
+ details = document.getElementById("details").firstChild;
+ svg = document.getElementsByTagName("svg")[0];
+ }
function s(info) { details.nodeValue = "$nametype " + info; }
function c() { details.nodeValue = ' '; }
+ function orig_save(e, attr, val) {
+ if (e.attributes["_orig_"+attr] != undefined) return;
+ if (e.attributes[attr] == undefined) return;
+ if (val == undefined) val = e.attributes[attr].value;
+ e.setAttribute("_orig_"+attr, val);
+ }
+ function orig_load(e, attr) {
+ if (e.attributes["_orig_"+attr] == undefined) return;
+ e.attributes[attr].value = e.attributes["_orig_"+attr].value;
+ e.removeAttribute("_orig_"+attr);
+ }
+ function update_text(e) {
+ var w = parseFloat(e.childNodes[2].attributes["width"].value);
+ var t = e.childNodes[4];
+ var txt = e.childNodes[1].textContent.replace(/\\([^(]*\\)/,"");
+
+ t.textContent = txt;
+ if (t.getSubStringLength(0, txt.length) > w) {
+ for (var x=txt.length-2; x>0; x--) {
+ if (t.getSubStringLength(0, x+2) <= w) {
+ t.textContent = txt.substring(0,x) + "..";
+ return;
+ }
+ }
+ t.textContent = "";
+ }
+ }
+ function zoom_reset(e) {
+ if (e.attributes != undefined) {
+ orig_load(e, "x");
+ orig_load(e, "width");
+ }
+ if (e.childNodes == undefined) return;
+ for(var i=0, c=e.childNodes; i<c.length; i++) {
+ zoom_reset(c[i]);
+ }
+ }
+ function zoom_child(e, x1, ratio) {
+ if (e.attributes != undefined) {
+ if (e.attributes["x"] != undefined) {
+ orig_save(e, "x");
+ e.attributes["x"].value = (parseFloat(e.attributes["x"].value) - x1) * ratio;
+ if(e.tagName == "text") e.attributes["x"].value = e.parentNode.childNodes["2"].attributes["x"].value + 3;
+ }
+ if (e.attributes["width"] != undefined) {
+ orig_save(e, "width");
+ e.attributes["width"].value = parseFloat(e.attributes["width"].value * ratio);
+ }
+ }
+
+ if (e.childNodes == undefined) return;
+ for(var i=0, c=e.childNodes; i<c.length; i++) {
+ zoom_child(c[i], x1, ratio);
+ }
+ }
+ function zoom(node) {
+ var a = node.childNodes[2].attributes; // childNodes[2] = rect
+ var xmin = parseFloat(a["x"].value);
+ var xmax = xmin + parseFloat(a["width"].value);
+ var ymin = parseFloat(a["y"].value);
+ var width = parseInt(a["width"].value);
+ var x1 = xmin;
+ var x2 = xmin+width;
+ var ratio = svg.getBBox().width / width;
+
+ var el = document.getElementsByTagName("g");
+ for(i=0;i<el.length;i++){ // For each elements
+ var e=el[i];
+ var a = e.childNodes[2].attributes;
+ if (parseFloat(a["y"].value)>ymin || parseFloat(a["x"].value) < xmin || parseFloat(a["x"].value) > xmax) {
+ e.style["display"] = "none";
+ }
+ else {
+ zoom_child(e, x1, ratio);
+ }
+ update_text(e);
+ }
+ }
+ function unzoom() {
+ var el = document.getElementsByTagName("g");
+ for(i=0;i<el.length;i++) {
+ el[i].style["display"] = "block";
+ zoom_reset(el[i]);
+ update_text(el[i]);
+ }
+ }
]]>
</script>
INC
@@ -425,6 +514,7 @@ my ($white, $black, $vvdgrey, $vdgrey) = (
);
$im->stringTTF($black, $fonttype, $fontsize + 5, 0.0, int($imagewidth / 2), $fontsize * 2, $titletext, "middle");
$im->stringTTF($black, $fonttype, $fontsize, 0.0, $xpad, $imageheight - ($ypad2 / 2), " ", "", 'id="details"');
+$im->stringTTF($black, $fonttype, $fontsize, 0.0, $xpad, $imageheight, "Reset Zoom", "", 'id="unzoom" onclick="unzoom()"');
if ($palette) {
read_palette();
@@ -462,6 +552,7 @@ while (my ($id, $node) = each %Node) {
$nameattr->{class} ||= "func_g";
$nameattr->{onmouseover} ||= "s('".$info."')";
$nameattr->{onmouseout} ||= "c()";
+ $nameattr->{onclick} ||= "zoom(this)";
$nameattr->{title} ||= $info;
$im->group_start($nameattr);
@@ -473,14 +564,15 @@ while (my ($id, $node) = each %Node) {
}
my $chars = int( ($x2 - $x1) / ($fontsize * $fontwidth));
+ my $text = "";
if ($chars >= 3) { # room for one char plus two dots
- my $text = substr $func, 0, $chars;
+ $text = substr $func, 0, $chars;
substr($text, -2, 2) = ".." if $chars < length $func;
$text =~ s/&/&/g;
$text =~ s/</</g;
$text =~ s/>/>/g;
- $im->stringTTF($black, $fonttype, $fontsize, 0.0, $x1 + 3, 3 + ($y1 + $y2) / 2, $text, "");
}
+ $im->stringTTF($black, $fonttype, $fontsize, 0.0, $x1 + 3, 3 + ($y1 + $y2) / 2, $text, "");
$im->group_end($nameattr);
}