|
433 | 433 | if (this.style!=null)
|
434 | 434 | selection.attr("font-style", this.style);
|
435 | 435 | }
|
| 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 | + } |
436 | 441 |
|
437 | 442 | res.stringWidth = function(svg, line) {
|
438 | 443 | /* compute the bounding box of a string by using temporary svg:text */
|
|
6228 | 6233 | .style("stroke", lcolor.color);
|
6229 | 6234 |
|
6230 | 6235 | 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); |
6232 | 6239 | var nlines = pave.fPrimitives.arr.length;
|
6233 | 6240 | var font = JSROOT.Painter.getFontDetails(pave['fTextFont'], h / (nlines * 1.5));
|
6234 | 6241 |
|
6235 |
| - var max_len = 0, mul = 1.4; |
| 6242 | + var min_fact = 1.; |
6236 | 6243 | 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); |
6253 | 6255 |
|
| 6256 | + var step_y = (h - 2*padding_y)/nlines; |
| 6257 | + |
6254 | 6258 | for (var i = 0; i < nlines; ++i) {
|
6255 | 6259 | var leg = pave.fPrimitives.arr[i];
|
6256 | 6260 | var lopt = leg['fOption'].toLowerCase();
|
6257 | 6261 |
|
6258 | 6262 | var label = JSROOT.Painter.translateLaTeX(leg['fLabel']);
|
6259 | 6263 |
|
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 | + |
6267 | 6266 | var attfill = leg;
|
6268 | 6267 | var attmarker = leg;
|
6269 | 6268 | var attline = leg;
|
|
6279 | 6278 | var fill = this.createAttFill(attfill);
|
6280 | 6279 | var llll = JSROOT.Painter.createAttLine(attline);
|
6281 | 6280 |
|
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 |
| - |
6291 | 6281 | // Draw fill pattern (in a box)
|
6292 | 6282 | if (lopt.indexOf('f') != -1) {
|
6293 | 6283 | // box total height is yspace*0.7
|
6294 | 6284 | // 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 |
| - |
6315 | 6285 | 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) |
6320 | 6290 | .call(llll.func)
|
6321 | 6291 | .call(fill.func);
|
6322 | 6292 | }
|
6323 | 6293 | // Draw line
|
6324 | 6294 | 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; |
6329 | 6295 | p.append("svg:line")
|
6330 |
| - .attr("x1", pos_x) |
| 6296 | + .attr("x1", padding_x) |
6331 | 6297 | .attr("y1", pos_y)
|
6332 |
| - .attr("x2", pos_x + line_length) |
| 6298 | + .attr("x2", tpos_x - padding_x) |
6333 | 6299 | .attr("y2", pos_y)
|
6334 | 6300 | .call(llll.func);
|
6335 | 6301 | }
|
|
6338 | 6304 | }
|
6339 | 6305 | // Draw Polymarker
|
6340 | 6306 | if (lopt.indexOf('p') != -1) {
|
6341 |
| - |
6342 |
| - var line_length = (0.7 * pave['fMargin']) * w; |
6343 |
| - var pos_x = tpos_x / 2; |
6344 |
| - |
6345 | 6307 | var marker = JSROOT.Painter.createAttMarker(attmarker);
|
6346 | 6308 | 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 + ")"; }) |
6348 | 6310 | .call(marker.func);
|
6349 | 6311 | }
|
| 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 | + |
6350 | 6366 | }
|
6351 | 6367 | if (lwidth && lwidth > 1) {
|
6352 | 6368 | p.append("svg:line")
|
|
0 commit comments