Skip to content

Commit a496805

Browse files
committed
4. Very first try to draw latex with MathJax (#frac in legend first)
git-svn-id: https://subversion.gsi.de/dabc/trunk/plugins/root/js@3060 bcbf6573-9a26-0410-9ebc-ce4ab7aade96
1 parent 5d13312 commit a496805

File tree

1 file changed

+85
-69
lines changed

1 file changed

+85
-69
lines changed

scripts/JSRootPainter.js

Lines changed: 85 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@
433433
if (this.style!=null)
434434
selection.attr("font-style", this.style);
435435
}
436+
437+
res.asStyle = function(sz) {
438+
// return font name, which could be applied with d3.select().style('font')
439+
return ((sz!=null) ? sz : this.size) + "px " + this.name;
440+
}
436441

437442
res.stringWidth = function(svg, line) {
438443
/* compute the bounding box of a string by using temporary svg:text */
@@ -6228,42 +6233,36 @@
62286233
.style("stroke", lcolor.color);
62296234

62306235
var tcolor = JSROOT.Painter.root_colors[pave['fTextColor']];
6231-
var tpos_x = pave['fMargin'] * w;
6236+
var tpos_x = Math.round(pave['fMargin'] * w);
6237+
var padding_x = Math.round(0.05 * w);
6238+
var padding_y = Math.round(0.05 * h);
62326239
var nlines = pave.fPrimitives.arr.length;
62336240
var font = JSROOT.Painter.getFontDetails(pave['fTextFont'], h / (nlines * 1.5));
62346241

6235-
var max_len = 0, mul = 1.4;
6242+
var min_fact = 1.;
62366243
for (var j = 0; j < nlines; ++j) {
6237-
var label = JSROOT.Painter.translateLaTeX(pave.fPrimitives.arr[j]['fLabel']);
6238-
var lw = tpos_x + font.stringWidth(svg, label);
6239-
if (lw > max_len) max_len = lw;
6240-
}
6241-
if (max_len > w) {
6242-
font.size = Math.floor(font.size * 0.95 * (w / max_len));
6243-
mul *= 0.95 * (max_len / w);
6244-
}
6245-
var x1 = pave['fX1NDC'];
6246-
var x2 = pave['fX2NDC'];
6247-
var y1 = pave['fY1NDC'];
6248-
var y2 = pave['fY2NDC'];
6249-
var margin = pave['fMargin'] * (x2 - x1) / pave['fNColumns'];
6250-
var yspace = (y2 - y1) / nlines;
6251-
var ytext = y2 + 0.5 * yspace; // y-location of 0th entry
6252-
var boxw = margin * 0.35;
6244+
var leg = pave.fPrimitives.arr[j];
6245+
var lopt = leg['fOption'].toLowerCase();
6246+
var label = JSROOT.Painter.translateLaTeX(leg['fLabel']);
6247+
var lw = font.stringWidth(svg, label);
6248+
var allowed = w - 2*padding_x;
6249+
if ((lopt.indexOf("h")<0) && (lopt.length>0)) allowed = w - tpos_x - padding_x;
6250+
var fact = lw > 5 ? allowed / lw : 1.;
6251+
if (fact < min_fact) min_fact = fact;
6252+
}
6253+
6254+
if (min_fact<1) font.size = Math.floor(font.size * min_fact);
62536255

6256+
var step_y = (h - 2*padding_y)/nlines;
6257+
62546258
for (var i = 0; i < nlines; ++i) {
62556259
var leg = pave.fPrimitives.arr[i];
62566260
var lopt = leg['fOption'].toLowerCase();
62576261

62586262
var label = JSROOT.Painter.translateLaTeX(leg['fLabel']);
62596263

6260-
var pos_y = ((i + 1) * (font.size * mul)) - (font.size / 3);
6261-
var tpos_y = (i + 1) * (font.size * mul);
6262-
if (nlines == 1) {
6263-
var pos_y = (h * 0.75) - (font.size / 3);
6264-
var tpos_y = h * 0.75;
6265-
}
6266-
6264+
var pos_y = padding_y + (i+0.5)*step_y; // middle of each line
6265+
62676266
var attfill = leg;
62686267
var attmarker = leg;
62696268
var attline = leg;
@@ -6279,57 +6278,24 @@
62796278
var fill = this.createAttFill(attfill);
62806279
var llll = JSROOT.Painter.createAttLine(attline);
62816280

6282-
p.append("text")
6283-
.attr("class", "text")
6284-
.attr("text-anchor", "start")
6285-
.attr("x", tpos_x)
6286-
.attr("y", tpos_y)
6287-
.call(font.func)
6288-
.attr("fill", tcolor)
6289-
.text(label);
6290-
62916281
// Draw fill pattern (in a box)
62926282
if (lopt.indexOf('f') != -1) {
62936283
// box total height is yspace*0.7
62946284
// define x,y as the center of the symbol for this entry
6295-
var xsym = margin / 2;
6296-
var ysym = ytext;
6297-
var xf = new Array(4), yf = new Array(4);
6298-
xf[0] = xsym - boxw;
6299-
yf[0] = ysym - yspace * 0.35;
6300-
xf[1] = xsym + boxw;
6301-
yf[1] = yf[0];
6302-
xf[2] = xf[1];
6303-
yf[2] = ysym + yspace * 0.35;
6304-
xf[3] = xf[0];
6305-
yf[3] = yf[2];
6306-
for (var j = 0; j < 4; j++) {
6307-
xf[j] = xf[j] * Number(svg.attr("width"));
6308-
yf[j] = yf[j] * Number(svg.attr("height"));
6309-
}
6310-
var ww = xf[1] - xf[0];
6311-
var hh = yf[2] - yf[0];
6312-
pos_y = pos_y - (hh / 2);
6313-
var pos_x = (tpos_x / 2) - (ww / 2);
6314-
63156285
p.append("svg:rect")
6316-
.attr("x", pos_x)
6317-
.attr("y", pos_y)
6318-
.attr("width", ww)
6319-
.attr("height", hh)
6286+
.attr("x", padding_x)
6287+
.attr("y", pos_y-step_y/3)
6288+
.attr("width", tpos_x - 2*padding_x)
6289+
.attr("height", 2*step_y/3)
63206290
.call(llll.func)
63216291
.call(fill.func);
63226292
}
63236293
// Draw line
63246294
if (lopt.indexOf('l') != -1) {
6325-
6326-
// line total length (in x) is margin*0.8
6327-
var line_length = (0.7 * pave['fMargin']) * w;
6328-
var pos_x = (tpos_x - line_length) / 2;
63296295
p.append("svg:line")
6330-
.attr("x1", pos_x)
6296+
.attr("x1", padding_x)
63316297
.attr("y1", pos_y)
6332-
.attr("x2", pos_x + line_length)
6298+
.attr("x2", tpos_x - padding_x)
63336299
.attr("y2", pos_y)
63346300
.call(llll.func);
63356301
}
@@ -6338,15 +6304,65 @@
63386304
}
63396305
// Draw Polymarker
63406306
if (lopt.indexOf('p') != -1) {
6341-
6342-
var line_length = (0.7 * pave['fMargin']) * w;
6343-
var pos_x = tpos_x / 2;
6344-
63456307
var marker = JSROOT.Painter.createAttMarker(attmarker);
63466308
p.append("svg:path")
6347-
.attr("transform", function(d) { return "translate(" + pos_x + "," + pos_y + ")"; })
6309+
.attr("transform", function(d) { return "translate(" + tpos_x/2 + "," + pos_y + ")"; })
63486310
.call(marker.func);
63496311
}
6312+
6313+
var pos_x = tpos_x;
6314+
if ((lopt.indexOf('h')>=0) || (lopt.length==0)) pos_x = padding_x;
6315+
6316+
if ((label.indexOf("#frac")<0) || JSROOT.browser.isIE) {
6317+
p.append("text")
6318+
.attr("class", "text")
6319+
.attr("text-anchor", "start")
6320+
.attr("dominant-baseline", "central")
6321+
.attr("x", pos_x)
6322+
.attr("y", pos_y /*+ font.size*0.3*/)
6323+
.call(font.func)
6324+
.attr("fill", tcolor)
6325+
.text(label);
6326+
} else {
6327+
6328+
var fo_x = Math.round(pos_x);
6329+
var fo_y = Math.round(pos_y - 0.5*step_y);
6330+
var fo_w = Math.round(w - padding_x - pos_x);
6331+
var fo_h = Math.round(step_y);
6332+
6333+
var fo = this.draw_g.append("foreignObject").attr("width", fo_w).attr("height", fo_h);
6334+
this.SetForeignObjectPosition(fo, fo_x, fo_y);
6335+
6336+
// this is just workaround, one need real parser to find all #frac and so on
6337+
label = label.replace("#frac","\\(\\frac") + "\\)";
6338+
6339+
var body = fo.append("xhtml:body")
6340+
.style("display", "table")
6341+
.append("xhtml:div")
6342+
.style("display", "table-cell")
6343+
.style('vertical-align', 'middle') // force to align in the center
6344+
.style("font", font.asStyle())
6345+
.html(label);
6346+
6347+
JSROOT.AssertPrerequisites('mathjax', function() {
6348+
if (typeof MathJax != 'object') return;
6349+
6350+
// MathJax.Hub.Queue(["Typeset", MathJax.Hub, body.node()]);
6351+
6352+
MathJax.Hub.Queue(function() {
6353+
MathJax.Hub.Typeset(body.node());
6354+
// rescale one again
6355+
var rect = body.node().getBoundingClientRect();
6356+
var fact_x = parseInt(rect.right - rect.left) / fo_w;
6357+
var fact_y = parseInt(rect.bottom - rect.top) / fo_h;
6358+
if (Math.max(fact_x, fact_y) > 1)
6359+
body.style("font", font.asStyle(Math.round(font.size/Math.max(fact_x, fact_y))));
6360+
});
6361+
});
6362+
6363+
6364+
}
6365+
63506366
}
63516367
if (lwidth && lwidth > 1) {
63526368
p.append("svg:line")

0 commit comments

Comments
 (0)