Changeset 10
- Timestamp:
- 17/03/06 04:43:04 (2 years ago)
- Files:
-
- plotkit/trunk/PlotKit/Base.js (modified) (1 diff)
- plotkit/trunk/PlotKit/Canvas.js (modified) (13 diffs)
- plotkit/trunk/PlotKit/SVG.js (modified) (1 diff)
- plotkit/trunk/PlotKit/SweetCanvas.js (modified) (7 diffs)
- plotkit/trunk/doc/PlotKit.QuickStart.html (modified) (3 diffs)
- plotkit/trunk/doc/PlotKit.QuickStart.txt (modified) (3 diffs)
- plotkit/trunk/tests/sweet.js (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plotkit/trunk/PlotKit/Base.js
r8 r10 231 231 "axisLabelColor": Color.grayColor(), 232 232 "axisLineColor": Color.whiteColor(), 233 "padding": {top: 5, bottom: 20, left: 40, right: 10}, 234 "shouldStroke": true 233 "padding": {top: 5, bottom: 30, left: 10, right: 10} 235 234 }; 236 235 plotkit/trunk/PlotKit/Canvas.js
r8 r10 101 101 "IECanvasHTC": "PlotKit/iecanvas.htc" 102 102 }; 103 104 103 MochiKit.Base.update(this.options, options ? options : {}); 104 105 // we need to refetch the element because of this horrible Canvas on IE 106 // crap 107 this.element_id = element.id ? element.id : element; 108 109 // Stuff relating to Canvas on IE support 110 var self = PlotKit.CanvasRenderer; 111 this.isIE = self.IECanvasEmulationIfNeeded(this.options.IECanvasHTC); 112 this.IEDelay = 0.5; 113 this.maxTries = 5; 114 this.renderDelay = null; 115 this.clearDelay = null; 116 105 117 this.layout = layout; 106 118 this.style = layout.style; 107 this.element = MochiKit.DOM.getElement(element); 119 this.element = MochiKit.DOM.getElement(this.element_id); 120 //this.element = element; 108 121 this.container = this.element.parentNode; 109 122 this.height = element.height; … … 115 128 throw "CanvasRenderer() - passed canvas is not found"; 116 129 117 this.isIE = this.IECanvasEmulationIfNeeded(this.element);118 this.IEDelay = 0.3;119 130 if (!this.isIE && !(PlotKit.CanvasRenderer.isSupported(this.element))) 120 131 throw "CanvasRenderer() - Canvas is not supported."; 121 132 122 if (isNil(this.container) || (this.container.nodeName == "div"))133 if (isNil(this.container) || (this.container.nodeName != "DIV")) 123 134 throw "CanvasRenderer() - <canvas> needs to be enclosed in <div>"; 124 135 … … 138 149 {"style":{ "position": "relative", "width": this.width + "px"}}); 139 150 151 152 140 153 // load event system if we have Signals 141 154 this.event_isinside = null; … … 146 159 }; 147 160 148 PlotKit.CanvasRenderer. prototype.IECanvasEmulationIfNeeded = function(elem) {161 PlotKit.CanvasRenderer.IECanvasEmulationIfNeeded = function(htc) { 149 162 var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); 150 163 var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); … … 152 165 return false; 153 166 154 this.element.style.width = this.element.width + "px";155 this.element.style.height = this.element.height + "px";156 157 167 if (isUndefinedOrNull(MochiKit.DOM.getElement('VMLRender'))) { 168 // before we add VMLRender, we need to recreate all canvas tags 169 // programmatically otherwise IE will not recognise it 170 171 var nodes = document.getElementsByTagName('canvas'); 172 for (var i = 0; i < nodes.length; i++) { 173 var node = nodes[i]; 174 if (node.getContext) { return; } // Other implementation, abort 175 var newNode = MochiKit.DOM.CANVAS( 176 {id: node.id, 177 width: "" + parseInt(node.width), 178 height: "" + parseInt(node.height)}, ""); 179 newNode.style.width = parseInt(node.width) + "px"; 180 newNode.style.height = parseInt(node.height) + "px"; 181 node.id = node.id + "_old"; 182 MochiKit.DOM.swapDOM(node, newNode); 183 } 184 158 185 document.namespaces.add("v"); 159 186 var vmlopts = {'id':'VMLRender', … … 162 189 var vml = MochiKit.DOM.createDOM('object', vmlopts); 163 190 document.body.appendChild(vml); 164 var htc = this.options.IECanvasHTC;165 191 var vmlStyle = document.createStyleSheet(); 166 192 vmlStyle.addRule("canvas", "behavior: url('" + htc + "');"); 167 193 vmlStyle.addRule("v\\:*", "behavior: url(#VMLRender);"); 168 194 } 169 170 195 return true; 171 196 }; … … 175 200 // VML takes a while to start up, so we just poll every this.IEDelay 176 201 try { 202 if (this.renderDelay) { 203 this.renderDelay.cancel(); 204 this.renderDelay = null; 205 } 177 206 var context = this.element.getContext("2d"); 178 207 } 179 208 catch (e) { 180 209 this.isFirstRender = false; 181 this.delay = MochiKit.Async.wait(this.IEDelay); 182 this.delay.addCallback(bind(this.render, this)); 210 if (this.maxTries-- > 0) { 211 log("trying again"); 212 this.renderDelay = MochiKit.Async.wait(this.IEDelay); 213 this.renderDelay.addCallback(bind(this.render, this)); 214 } 183 215 return; 184 216 } … … 303 335 PlotKit.CanvasRenderer.prototype._renderPieChart = function() { 304 336 var context = this.element.getContext("2d"); 305 context.save();306 307 337 var colorCount = this.options.colorScheme.length; 308 338 var slices = this.layout.slices; … … 312 342 var radius = Math.min(this.area.w * this.options.pieRadius, 313 343 this.area.h * this.options.pieRadius); 344 345 if (this.isIE) { 346 centerx = parseInt(centerx); 347 centery = parseInt(centery); 348 radius = parseInt(radius); 349 } 350 314 351 315 352 // NOTE NOTE!! Canvas Tag draws the circle clockwise from the y = 0, x = 1 … … 332 369 }; 333 370 334 if (this.options.shouldFill) { 335 makePath(); 336 context.fill(); 337 } 338 339 if (this.options.shouldStroke) { 340 makePath(); 341 context.lineWidth = this.options.strokeWidth; 342 if (this.options.strokeColor) 343 context.strokeStyle = this.options.strokeColor.toRGBString(); 344 else if (this.options.strokeColorTransform) 345 context.strokeStyle = color[this.options.strokeColorTransform]().toRGBString(); 346 context.stroke(); 347 } 371 if (Math.abs(slices[i].startAngle - slices[i].endAngle) > 0.001) { 372 if (this.options.shouldFill) { 373 makePath(); 374 context.fill(); 375 } 376 377 if (this.options.shouldStroke) { 378 makePath(); 379 context.lineWidth = this.options.strokeWidth; 380 if (this.options.strokeColor) 381 context.strokeStyle = this.options.strokeColor.toRGBString(); 382 else if (this.options.strokeColorTransform) 383 context.strokeStyle = color[this.options.strokeColorTransform]().toRGBString(); 384 context.stroke(); 385 } 386 } 348 387 context.restore(); 349 388 } … … 527 566 PlotKit.CanvasRenderer.prototype._renderBackground = function() { 528 567 var context = this.element.getContext("2d"); 568 context.save(); 529 569 context.fillStyle = this.options.backgroundColor.toString(); 530 570 context.fillRect(0, 0, this.width, this.height); 571 context.restore(); 531 572 }; 532 573 … … 535 576 // VML takes a while to start up, so we just poll every this.IEDelay 536 577 try { 578 if (this.clearDelay) { 579 this.clearDelay.cancel(); 580 this.clearDelay = null; 581 } 537 582 var context = this.element.getContext("2d"); 538 583 } 539 584 catch (e) { 540 585 this.isFirstRender = false; 541 this. delay = MochiKit.Async.wait(this.IEDelay);542 this. delay.addCallback(bind(this.clear, this));586 this.clearDelay = MochiKit.Async.wait(this.IEDelay); 587 this.clearDelay.addCallback(bind(this.clear, this)); 543 588 return; 544 589 } … … 578 623 var y = (e.mouse().page.y - PlotKit.Base.findPosY(this.element) - this.area.y) / this.area.h; 579 624 580 log(x, y);625 //log(x, y); 581 626 582 627 var isHit = this.layout.hitTest(x, y); plotkit/trunk/PlotKit/SVG.js
r8 r10 107 107 if (isNil(this.element)) 108 108 throw "SVGRenderer() - passed SVG object is not found"; 109 if (isNil(this.container)) 109 110 if (isNil(this.container) || this.container.nodeName != "DIV") 110 111 throw "SVGRenderer() - No DIV's around the SVG."; 111 112 plotkit/trunk/PlotKit/SweetCanvas.js
r8 r10 58 58 // --------------------------------------------------------------------- 59 59 60 PlotKit.SweetCanvasRenderer.prototype.__init__ = function(el ement, layout, options) {61 var additionalOptions = PlotKit.Base.officeBlue();62 MochiKit.Base.update( additionalOptions, options);63 PlotKit.SweetCanvasRenderer.__super__.__init__.call(this, el ement, layout, additionalOptions);60 PlotKit.SweetCanvasRenderer.prototype.__init__ = function(el, layout, opts) { 61 var moreOpts = PlotKit.Base.officeBlue(); 62 MochiKit.Base.update(moreOpts, opts); 63 PlotKit.SweetCanvasRenderer.__super__.__init__.call(this, el, layout, moreOpts); 64 64 }; 65 65 … … 105 105 context.shadowBlur = 5.0; 106 106 context.shadowColor = Color.fromHexString("#888888").toRGBString(); 107 108 prepareFakeShadow(context, x, y, w, h); 107 108 if (this.isIE) { 109 context.save(); 110 context.fillStyle = "#cccccc"; 111 context.fillRect(x-2, y-2, w+4, h+2); 112 context.restore(); 113 } 114 else { 115 prepareFakeShadow(context, x, y, w, h); 116 } 117 109 118 context.fillStyle = chooseColor(bar.name).toRGBString(); 110 119 context.fillRect(x, y, w, h); … … 157 166 // faux shadow for firefox 158 167 context.save(); 159 context.fillStyle = Color.blackColor().colorWithAlpha(0.2).toRGBString(); 168 if (this.isIE) { 169 context.fillStyle = "#cccccc"; 170 } 171 else { 172 context.fillStyle = Color.blackColor().colorWithAlpha(0.2).toRGBString(); 173 } 174 160 175 context.translate(-1, -2); 161 176 bind(makePath, this)(); … … 178 193 }; 179 194 180 181 195 PlotKit.CanvasRenderer.prototype._renderPieChart = function() { 182 196 var context = this.element.getContext("2d"); 183 context.save();184 197 185 198 var colorCount = this.options.colorScheme.length; … … 191 204 this.area.h * this.options.pieRadius); 192 205 206 if (this.isIE) { 207 centerx = parseInt(centerx); 208 centery = parseInt(centery); 209 radius = parseInt(radius); 210 } 211 193 212 // NOTE NOTE!! Canvas Tag draws the circle clockwise from the y = 0, x = 1 194 213 // so we have to subtract 90 degrees to make it start at y = 1, x = 0 195 196 context.save();197 context.fillStyle = Color.blackColor().colorWithAlpha(0.2).toRGBString();198 context.shadowBlur = 5.0;199 context.shadowColor = Color.fromHexString("#888888").toRGBString();200 201 context.translate(1, 1);202 context.beginPath();203 context.moveTo(centerx, centery);204 context.arc(centerx, centery, radius + 2, 0, Math.PI*2, false);205 context.closePath();206 context.fill();207 context.restore();208 214 if (!this.isIE) { 215 context.save(); 216 context.fillStyle = Color.blackColor().colorWithAlpha(0.2).toRGBString(); 217 context.shadowBlur = 5.0; 218 context.shadowColor = Color.fromHexString("#888888").toRGBString(); 219 220 context.translate(1, 1); 221 context.beginPath(); 222 context.moveTo(centerx, centery); 223 context.arc(centerx, centery, radius + 2, 0, Math.PI*2, false); 224 context.closePath(); 225 context.fill(); 226 context.restore(); 227 } 209 228 210 229 context.strokeStyle = Color.whiteColor().toRGBString(); 211 230 context.lineWidth = 2.0; 231 context.save(); 212 232 for (var i = 0; i < slices.length; i++) { 213 233 var color = this.options.colorScheme[i%colorCount]; 214 context.save();215 234 context.fillStyle = color.toRGBString(); 216 235 … … 225 244 context.closePath(); 226 245 }; 227 228 229 230 makePath(); 231 context.fill(); 232 makePath(); 233 context.stroke(); 234 235 context.restore(); 236 } 237 }; 238 246 247 if (Math.abs(slices[i].startAngle - slices[i].endAngle) > 0.0001) { 248 makePath(); 249 context.fill(); 250 makePath(); 251 context.stroke(); 252 } 253 } 254 context.restore(); 255 }; 239 256 240 257 PlotKit.SweetCanvasRenderer.prototype._renderBackground = function() { 241 258 var context = this.element.getContext("2d"); 242 //context.fillStyle = Color.whiteColor().toRGBString(); 243 //context.fillRect(0, 0, this.width, this.height); 244 245 context.fillStyle = this.options.backgroundColor.toRGBString(); 246 context.fillRect(this.area.x, this.area.y, this.area.w, this.area.h); 247 259 248 260 if (this.layout.style == "bar" || this.layout.style == "line") { 249 261 context.save(); 262 context.fillStyle = this.options.backgroundColor.toRGBString(); 263 context.fillRect(this.area.x, this.area.y, this.area.w, this.area.h); 250 264 context.strokeStyle = Color.whiteColor().toRGBString(); 251 265 context.lineWidth = 1.0; … … 264 278 PlotKit.SweetCanvasRenderer.__super__._renderBackground.call(this); 265 279 } 266 267 }; 280 }; plotkit/trunk/doc/PlotKit.QuickStart.html
r8 r10 181 181 </p> 182 182 <pre><code>var options = { 183 "IECanvasHTC": " ../PlotKit/iecanvas.htc",183 "IECanvasHTC": "/plotkit/iecanvas.htc", 184 184 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]), 185 185 "padding": {left: 0, right: 0, top: 10, bottom: 30}, … … 249 249 <script type="text/javascript"> 250 250 <!-- 251 251 252 var options = { 252 "IECanvasHTC": "../ PlotKit/iecanvas.htc",253 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]), 253 "IECanvasHTC": "../plotkit/iecanvas.htc", 254 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]), 254 255 "padding": {left: 10, right: 10, top: 10, bottom: 30}, 255 256 "xTicks": [{v:0, label:"zero"}, … … 261 262 "pieRadius": 0.35 262 263 }; 264 263 265 264 266 function drawBarGraph() { plotkit/trunk/doc/PlotKit.QuickStart.txt
r8 r10 148 148 149 149 var options = { 150 "IECanvasHTC": " ../PlotKit/iecanvas.htc",150 "IECanvasHTC": "/plotkit/iecanvas.htc", 151 151 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]), 152 152 "padding": {left: 0, right: 0, top: 10, bottom: 30}, … … 212 212 <script type="text/javascript"> 213 213 <!-- 214 214 215 var options = { 215 "IECanvasHTC": "../ PlotKit/iecanvas.htc",216 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]), 216 "IECanvasHTC": "../plotkit/iecanvas.htc", 217 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]), 217 218 "padding": {left: 10, right: 10, top: 10, bottom: 30}, 218 219 "xTicks": [{v:0, label:"zero"}, … … 225 226 }; 226 227 228 227 229 function drawBarGraph() { 228 230 var layout = new PlotKit.Layout("bar", options); plotkit/trunk/tests/sweet.js
r7 r10 1 1 /* actual tests */ 2 2 3 var options = {"IECanvasHTC":"../PlotKit/iecanvas.htc"}; 4 MochiKit.Base.update(options, PlotKit.Base.officePurple()); 3 var opts = { 4 "IECanvasHTC": "../plotkit/iecanvas.htc", 5 "enableEvents": false 6 }; 5 7 6 8 function genericTest(num, plotStyle) { … … 9 11 l.evaluate(); 10 12 var c = $("test" + num + "canvas"); 11 var g = new PlotKit.SweetCanvasRenderer(c, l, opt ions);13 var g = new PlotKit.SweetCanvasRenderer(c, l, opts); 12 14 g.render(); 13 15 } … … 17 19 l.addDatasetFromTable("data1." + num, $("test" + num), 0, 1); 18 20 l.addDatasetFromTable("data2." + num, $("test" + num), 0, 2); 19 l.addDatasetFromTable("data3." + num, $("test" + num), 0, 1);20 l.addDatasetFromTable("data4." + num, $("test" + num), 0, 2);21 l.addDatasetFromTable("data5." + num, $("test" + num), 0, 1);22 l.addDatasetFromTable("data6." + num, $("test" + num), 0, 2);23 21 l.evaluate(); 24 22 var c = $("test" + num + "canvas"); 25 var g = new PlotKit.SweetCanvasRenderer(c, l, opt ions);23 var g = new PlotKit.SweetCanvasRenderer(c, l, opts); 26 24 g.render(); 27 25 } … … 71 69 var missingData = [[0, 1], [1, 4], [3, 16], [5, 17]]; 72 70 73 var dualData = [[0,0,0], [1,2,1], [2,4,4], [3,8,9] ]; //, [4,16,16], [5,32,25], [6, 64, 36], [7, 128, 49]];71 var dualData = [[0,0,0], [1,2,1], [2,4,4], [3,8,9], [4,16,16], [5,32,25], [6, 64, 36], [7, 128, 49]]; 74 72 75 73 tests.appendChild(H2(null, "Simple Tests")); … … 87 85 tests.appendChild(generateUnitTest(5, genericTest, simpleData1, 88 86 "pie", "")); 87 89 88 } 90 89




Atom Feed for the Blog Entries