Advertisement
kberg

prototype new drawing callbacks

Jan 22nd, 2012
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var RegularConvex = function(sides, rotation) {
  2.   this.sides = sides;
  3.   this.rotation = rotation ? rotation : 0;
  4.   this.delta = Math.PI * 2 / sides;
  5. }
  6. RegularConvex.prototype.draw = function(ctx, cx, cy, radius) {
  7.    ctx.beginPath();
  8.    var first = true;
  9.    var angle = this.rotation;
  10.  
  11.    var x = cx + (Math.sin(angle) * radius);
  12.    var y = cy + (-Math.cos(angle) * radius);
  13.    ctx.moveTo(x, y);
  14.  
  15.    for (var idx = 0; idx < this.sides; idx++) {
  16.      angle = (idx == this.sides - 1) ? this.rotation : (angle + this.delta);
  17.      var x = cx + (Math.sin(angle) * radius);
  18.      var y = cy + (-Math.cos(angle) * radius);
  19.      ctx.lineTo(x, y);
  20.    }
  21.    ctx.stroke();
  22.    ctx.closePath();
  23. }
  24.  
  25. g = new Dygraph(
  26.         document.getElementById("demodiv"),
  27.         function() {
  28.           var zp = function(x) { if (x < 10) return "0"+x; else return x; };
  29.           var r = "date,parabola,line,another line,sine wave\n";
  30.           for (var i=1; i<=31; i++) {
  31.           r += "200610" + zp(i);
  32.           r += "," + 10*(i*(31-i));
  33.           r += "," + 10*(8*i);
  34.           r += "," + 10*(250 - 8*i);
  35.           r += "," + 10*(125 + 125 * Math.sin(0.3*i));
  36.           r += "\n";
  37.           }
  38.           return r;
  39.         },
  40.         {
  41.           strokeWidth: 2,
  42.           'parabola': {
  43.             strokeWidth: 0.0,
  44.             drawPoints: true,
  45.             pointSize: 4,
  46.             highlightCircleSize: 6,
  47.             drawPointCallback: function(g, series, ctx, cx, cy, color, radius) {
  48.               ctx.strokeStyle = color;
  49.               ctx.lineWidth = 1;
  50.               new RegularConvex(4, Math.PI / 4).draw(ctx, cx, cy, radius);
  51.             },
  52.             highlightCircleCallback:  function(g, series, ctx, cx, cy, color, radius) {
  53.               ctx.fillStyle = "#FFFF00";
  54.               ctx.beginPath();
  55.               ctx.arc(cx, cy, radius, Math.PI*2, false);
  56.               ctx.closePath();
  57.               ctx.stroke();
  58.               ctx.fill();
  59.  
  60.               ctx.fillStyle = "#000000";
  61.               ctx.beginPath();
  62.               ctx.arc(cx - (radius / 3) , cy - (radius / 4), 1, Math.PI*2, false);
  63.               ctx.closePath();
  64.               ctx.stroke();
  65.               ctx.fill();
  66.  
  67.               ctx.fillStyle = "#000000";
  68.               ctx.beginPath();
  69.               ctx.arc(cx + (radius / 3) , cy - (radius / 4), 1, Math.PI*2, false);
  70.               ctx.closePath();
  71.               ctx.stroke();
  72.               ctx.fill();
  73.  
  74.               ctx.fillStyle = "#000000";
  75.               ctx.beginPath();
  76.               ctx.arc(cx, cy, radius - 2, .3, Math.PI - .3, false);
  77.               ctx.stroke();
  78.             }
  79.           },
  80.           'line': {
  81.             strokeWidth: 1.0,
  82.             drawPoints: true,
  83.             pointSize: 2.5,
  84.             drawPointCallback: function(g, series, ctx, cx, cy, color, radius) {
  85.               ctx.beginPath();
  86.               ctx.strokeStyle = color;
  87.              
  88.               ctx.arc(cx, cy, radius, 0, 2 * Math.PI, false);
  89.               ctx.closePath();
  90.               ctx.fillStyle = "#FFF";
  91.               ctx.fill();
  92.  
  93.               ctx.beginPath();
  94.               ctx.arc(cx, cy, radius, 0, 2 * Math.PI, false);
  95.               ctx.closePath();
  96.               ctx.strokeStyle = color;
  97.               ctx.stroke();
  98.             }
  99.           },
  100.           'sine wave': {
  101.             strokeWidth: 3,
  102.             highlightCircleSize: 10,
  103.             drawPoints: true,
  104.             pointSize: 6,
  105.             drawPointCallback: function(g, series, ctx, cx, cy, color, radius) {
  106.               ctx.lineWidth = 1;
  107.               ctx.strokeStyle = "#f00";
  108.               new RegularConvex(5, Math.PI / 5).draw(ctx, cx, cy, radius);
  109.             },
  110.             highlightCircleCallback:  function(g, series, ctx, cx, cy, color, radius) {
  111.               ctx.lineWidth = 1;
  112.               ctx.strokeStyle = "#444";
  113.               new RegularConvex(5, Math.PI / 5).draw(ctx, cx, cy, radius);
  114.             }
  115.           }
  116.         }
  117.     );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement