otsdaq_utilities  v2_05_02_indev
JSRootPainter.v7more.js
1 
4 (function( factory ) {
5  if ( typeof define === "function" && define.amd ) {
6  define( ['JSRootPainter', 'd3'], factory );
7  } else if (typeof exports === 'object' && typeof module !== 'undefined') {
8  factory(require("./JSRootCore.js"), require("d3"));
9  } else {
10  if (typeof d3 != 'object')
11  throw new Error('This extension requires d3.js', 'JSRootPainter.v7hist.js');
12  if (typeof JSROOT == 'undefined')
13  throw new Error('JSROOT is not defined', 'JSRootPainter.v7hist.js');
14  if (typeof JSROOT.Painter != 'object')
15  throw new Error('JSROOT.Painter not defined', 'JSRootPainter.v7hist.js');
16  factory(JSROOT, d3);
17  }
18 } (function(JSROOT, d3) {
19 
20  "use strict";
21 
22  JSROOT.sources.push("v7more");
23 
24  // =================================================================================
25 
26  function drawText() {
27  var text = this.GetObject(),
28  pp = this.pad_painter(),
29  w = this.pad_width(),
30  h = this.pad_height(),
31  use_frame = false,
32  p = pp.GetCoordinate(text.fPos),
33  text_size = this.v7EvalAttr( "text_size", 12),
34  text_angle = -1 * this.v7EvalAttr( "text_angle", 0),
35  text_align = this.v7EvalAttr( "text_align", 22),
36  text_color = this.v7EvalColor( "text_color", "black"),
37  text_font = this.v7EvalAttr( "text_font", 41);
38 
39  this.CreateG(use_frame);
40 
41  var arg = { align: text_align, x: p.x, y: p.y, text: text.fText, rotate: text_angle, color: text_color, latex: 1 };
42 
43  // if (text.fTextAngle) arg.rotate = -text.fTextAngle;
44  // if (text._typename == 'TLatex') { arg.latex = 1; fact = 0.9; } else
45  // if (text._typename == 'TMathText') { arg.latex = 2; fact = 0.8; }
46 
47  this.StartTextDrawing(text_font, text_size);
48 
49  this.DrawText(arg);
50 
51  this.FinishTextDrawing();
52  }
53 
54  // =================================================================================
55 
56  function drawLine() {
57 
58  var line = this.GetObject(),
59  pp = this.pad_painter(),
60  p1 = pp.GetCoordinate(line.fP1),
61  p2 = pp.GetCoordinate(line.fP2),
62  line_width = this.v7EvalAttr("line_width", 1),
63  line_style = this.v7EvalAttr("line_style", 1),
64  line_color = this.v7EvalColor("line_color", "black");
65 
66  this.CreateG();
67 
68  this.draw_g
69  .append("svg:line")
70  .attr("x1", p1.x)
71  .attr("y1", p1.y)
72  .attr("x2", p2.x)
73  .attr("y2", p2.y)
74  .style("stroke", line_color)
75  .attr("stroke-width", line_width)
76 // .attr("stroke-opacity", line_opacity)
77  .style("stroke-dasharray", JSROOT.Painter.root_line_styles[line_style]);
78  }
79 
80  // =================================================================================
81 
82  function drawBox() {
83 
84  var box = this.GetObject(),
85  pp = this.pad_painter(),
86  p1 = pp.GetCoordinate(box.fP1),
87  p2 = pp.GetCoordinate(box.fP2),
88  line_width = this.v7EvalAttr( "box_border_width", 1),
89  line_style = this.v7EvalAttr( "box_border_style", 1),
90  line_color = this.v7EvalColor( "box_border_color", "black"),
91  fill_color = this.v7EvalColor( "box_fill_color", "white"),
92  fill_style = this.v7EvalAttr( "box_fill_style", 1),
93  round_width = this.v7EvalAttr( "box_round_width", 0), // not yet exists
94  round_height = this.v7EvalAttr( "box_round_height", 0); // not yet exists
95 
96  this.CreateG();
97 
98  if (fill_style == 0) fill_color = "none";
99 
100  this.draw_g
101  .append("svg:rect")
102  .attr("x", p1.x)
103  .attr("width", p2.x-p1.x)
104  .attr("y", p2.y)
105  .attr("height", p1.y-p2.y)
106  .attr("rx", round_width)
107  .attr("ry", round_height)
108  .style("stroke", line_color)
109  .attr("stroke-width", line_width)
110  .attr("fill", fill_color)
111  .style("stroke-dasharray", JSROOT.Painter.root_line_styles[line_style]);
112  }
113 
114  // =================================================================================
115 
116  function drawMarker() {
117  var marker = this.GetObject(),
118  pp = this.pad_painter(),
119  p = pp.GetCoordinate(marker.fP),
120  marker_size = this.v7EvalAttr( "marker_size", 1),
121  marker_style = this.v7EvalAttr( "marker_style", 1),
122  marker_color = this.v7EvalColor( "marker_color", "black"),
123  att = new JSROOT.TAttMarkerHandler({ style: marker_style, color: marker_color, size: marker_size }),
124  path = att.create(p.x, p.y);
125 
126  this.CreateG();
127 
128  if (path)
129  this.draw_g.append("svg:path")
130  .attr("d", path)
131  .call(att.func);
132  }
133 
134  // =================================================================================
135 
136  function drawLegend(arg) {
137 
138  var legend = this.GetObject(),
139  pp = this.pad_painter(),
140  p1 = pp.GetCoordinate(legend.fP1),
141  p2 = pp.GetCoordinate(legend.fP2),
142  line_width = this.v7EvalAttr( "box_border_width", 1),
143  line_style = this.v7EvalAttr( "box_border_style", 1),
144  line_color = this.v7EvalColor( "box_border_color", "black"),
145  fill_color = this.v7EvalColor( "box_fill_color", "white"),
146  fill_style = this.v7EvalAttr( "box_fill_style", 1),
147  text_size = this.v7EvalAttr( "title_size", 20),
148  text_angle = -1 * this.v7EvalAttr( "title_angle", 0),
149  text_align = this.v7EvalAttr( "title_align", 22),
150  text_color = this.v7EvalColor( "title_color", "black"),
151  text_font = this.v7EvalAttr( "title_font", 41);
152 
153  //if (arg=="drag_resize") {
154  // p1.x = parseInt(this.draw_g.attr("x"));
155  // p2.y = parseInt(this.draw_g.attr("y"));
156  // p2.x = p1.x + parseInt(this.draw_g.attr("width"));
157  // p1.y = p2.y + parseInt(this.draw_g.attr("height"));
158  //}
159 
160  this.CreateG();
161 
162  if (fill_style == 0) fill_color = "none";
163 
164  // position and size required only for drag functions
165  // this.draw_g.attr("transfrom", "translate(" + p1.x + "," + p2.y + ")")
166  // .attr("x", p1.x)
167  // .attr("y", p2.y)
168  // .attr("width", p2.x-p1.x)
169  // .attr("height", p1.y-p2.y);
170 
171  this.draw_g
172  .append("svg:rect")
173  .attr("x", p1.x)
174  .attr("width", p2.x-p1.x)
175  .attr("y", p2.y)
176  .attr("height", p1.y-p2.y)
177  .style("stroke", line_color)
178  .attr("stroke-width", line_width)
179  .attr("fill", fill_color)
180  .style("stroke-dasharray", JSROOT.Painter.root_line_styles[line_style]);
181 
182  var nlines = legend.fEntries.length;
183 
184  if (legend.fTitle) nlines++;
185 
186  var arg = { align: text_align, rotate: text_angle, color: text_color, latex: 1 };
187 
188  this.StartTextDrawing(text_font, text_size);
189 
190  var cnt = 0;
191  if (legend.fTitle) {
192  this.DrawText(JSROOT.extend({ x: p1.x + (p2.x-p1.x)/2, y: p2.y - 0.5*(p2.y-p1.y)/(nlines+1), text: legend.fTitle }, arg));
193  cnt++;
194  }
195 
196  for (var i=0; i<legend.fEntries.length; ++i) {
197  var entry = legend.fEntries[i],
198  ypos = p2.y - (cnt+0.5)*(p2.y-p1.y)/(nlines+1);
199  this.DrawText(JSROOT.extend({ x: p1.x + (p2.x-p1.x)/4, y: ypos, text: entry.fLabel }, arg));
200 
201  var objp = this.FindPainterFor(entry.fDrawable.fIO);
202 
203  if (objp && objp.lineatt)
204  this.draw_g
205  .append("svg:line")
206  .attr("x1", p1.x + (p2.x-p1.x)*0.5)
207  .attr("y1", ypos)
208  .attr("x2", p1.x + (p2.x-p1.x)*0.8)
209  .attr("y2", ypos)
210  .call(objp.lineatt.func);
211 
212  cnt++;
213  }
214 
215  this.FinishTextDrawing();
216 
217  // this.AddDrag({ minwidth: 10, minheight: 20, canselect: false,
218  // redraw: this.Redraw.bind(this, "drag_resize"),
219  // ctxmenu: false /*JSROOT.touches && JSROOT.gStyle.ContextMenu && this.UseContextMenu */ });
220  }
221 
222  // ================================================================================
223 
224  JSROOT.v7.drawText = drawText;
225  JSROOT.v7.drawLine = drawLine;
226  JSROOT.v7.drawBox = drawBox;
227  JSROOT.v7.drawMarker = drawMarker;
228  JSROOT.v7.drawLegend = drawLegend;
229 
230  return JSROOT;
231 
232 }));