Blame view

bower_components/charts-d3/dist/charts-d3.js 23.1 KB
752c1f5a   mwasiluk   updated d3-scatte...
1
  function D3ScatterPlotMatrix(placeholderSelector, data, config) {
e09a3d19   mwasiluk   charts-d3 update,...
2
      var self = this;
40c0e489   mwasiluk   charts-d3, d3 sc...
3
      this.utils = new ChartsD3Utils();
752c1f5a   mwasiluk   updated d3-scatte...
4
5
6
      this.placeholderSelector = placeholderSelector;
      this.svg = null;
      this.defaultConfig = {
40c0e489   mwasiluk   charts-d3, d3 sc...
7
8
9
          width: undefined, //svg width (default: computed using cell size and margins)
          size: 200, //scatter plot cell size
          padding: 20, //scatter plot cell padding
752c1f5a   mwasiluk   updated d3-scatte...
10
          brush: true,
40c0e489   mwasiluk   charts-d3, d3 sc...
11
12
13
          guides: true, //show axis guides
          tooltip: true, //show tooltip on dot hover
          ticks: undefined, //ticks number, (default: computed using cell size)
752c1f5a   mwasiluk   updated d3-scatte...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
          margin: {
              left: 30,
              right: 30,
              top: 30,
              bottom: 30
          },
          x: {// X axis config
              orient: "bottom",
              scale: "linear"
          },
          y: {// Y axis config
              orient: "left",
              scale: "linear"
          },
          dot: {
              radius: 2,
              color: null, // string or function returning color's value for color scale
              d3ColorCategory: 'category10'
          },
40c0e489   mwasiluk   charts-d3, d3 sc...
33
34
35
36
37
          variables: {
              labels: [], //optional array of variable labels (for the diagonal of the plot).
              keys: [], //optional array of variable keys
              value: function (d, variableKey) {// variable value accessor
                  return d[variableKey];
752c1f5a   mwasiluk   updated d3-scatte...
38
              }
40c0e489   mwasiluk   charts-d3, d3 sc...
39
40
41
          },
          groups:{
              key: undefined, //object property name or array index with grouping variable
e09a3d19   mwasiluk   charts-d3 update,...
42
43
44
              includeInPlot: false, //include group as variable in plot, boolean (default: false)
              value: function(d) { return d[self.config.groups.key] }, // grouping value accessor,
              label: ""
752c1f5a   mwasiluk   updated d3-scatte...
45
          }
40c0e489   mwasiluk   charts-d3, d3 sc...
46
  
752c1f5a   mwasiluk   updated d3-scatte...
47
48
      };
  
40c0e489   mwasiluk   charts-d3, d3 sc...
49
50
51
  
      this.setConfig(config||{});
  
752c1f5a   mwasiluk   updated d3-scatte...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  
      if (data) {
          this.setData(data);
      }
  
      this.init();
  }
  
  D3ScatterPlotMatrix.prototype.setData = function (data) {
      this.data = data;
  
  
      return this;
  };
  
  D3ScatterPlotMatrix.prototype.setConfig = function (config) {
      this.config = this.utils.deepExtend({}, this.defaultConfig, config);
      return this;
  };
  D3ScatterPlotMatrix.prototype.initPlot = function () {
  
  
      var self = this;
      var margin = this.config.margin;
      var conf = this.config;
      this.plot = {
          x: {},
          y: {},
          dot: {
              color: null//color scale mapping function
          }
      };
  
40c0e489   mwasiluk   charts-d3, d3 sc...
85
      this.setupVariables();
752c1f5a   mwasiluk   updated d3-scatte...
86
87
88
89
90
91
92
93
  
      this.plot.size = conf.size;
  
  
      var width = conf.width;
      var placeholderNode = d3.select(this.placeholderSelector).node();
  
      if (!width) {
40c0e489   mwasiluk   charts-d3, d3 sc...
94
          var maxWidth = margin.left + margin.right + this.plot.variables.length*this.plot.size;
752c1f5a   mwasiluk   updated d3-scatte...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
          width = Math.min(placeholderNode.getBoundingClientRect().width, maxWidth);
  
      }
      var height = width;
      if (!height) {
          height = placeholderNode.getBoundingClientRect().height;
      }
  
      this.plot.width = width - margin.left - margin.right;
      this.plot.height = height - margin.top - margin.bottom;
  
  
  
  
40c0e489   mwasiluk   charts-d3, d3 sc...
109
      if(conf.ticks===undefined){
752c1f5a   mwasiluk   updated d3-scatte...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
          conf.ticks = this.plot.size / 40;
      }
  
      this.setupX();
      this.setupY();
  
      if (conf.dot.d3ColorCategory) {
          this.plot.dot.colorCategory = d3.scale[conf.dot.d3ColorCategory]();
      }
      var colorValue = conf.dot.color;
      if (colorValue) {
          this.plot.dot.colorValue = colorValue;
  
          if (typeof colorValue === 'string' || colorValue instanceof String) {
              this.plot.dot.color = colorValue;
          } else if (this.plot.dot.colorCategory) {
              this.plot.dot.color = function (d) {
                  return self.plot.dot.colorCategory(self.plot.dot.colorValue(d));
              }
          }
  
  
40c0e489   mwasiluk   charts-d3, d3 sc...
132
      }else if(conf.groups.key){
752c1f5a   mwasiluk   updated d3-scatte...
133
          this.plot.dot.color = function (d) {
40c0e489   mwasiluk   charts-d3, d3 sc...
134
              return self.plot.dot.colorCategory(d[conf.groups.key]);
752c1f5a   mwasiluk   updated d3-scatte...
135
136
137
138
139
140
141
142
143
          }
      }
  
  
  
      return this;
  
  };
  
40c0e489   mwasiluk   charts-d3, d3 sc...
144
145
  D3ScatterPlotMatrix.prototype.setupVariables = function () {
      var variablesConf = this.config.variables;
752c1f5a   mwasiluk   updated d3-scatte...
146
147
148
  
      var data = this.data;
      var plot = this.plot;
40c0e489   mwasiluk   charts-d3, d3 sc...
149
150
151
152
      plot.domainByVariable = {};
      plot.variables = variablesConf.keys;
      if(!plot.variables || !plot.variables.length){
          plot.variables = this.utils.inferVariables(data, this.config.groups.key, this.config.includeInPlot);
752c1f5a   mwasiluk   updated d3-scatte...
153
154
155
      }
  
      plot.labels = [];
40c0e489   mwasiluk   charts-d3, d3 sc...
156
157
158
159
160
      plot.labelByVariable = {};
      plot.variables.forEach(function(variableKey, index) {
          plot.domainByVariable[variableKey] = d3.extent(data, function(d) { return variablesConf.value(d, variableKey) });
          var label = variableKey;
          if(variablesConf.labels && variablesConf.labels.length>index){
752c1f5a   mwasiluk   updated d3-scatte...
161
  
40c0e489   mwasiluk   charts-d3, d3 sc...
162
              label = variablesConf.labels[index];
752c1f5a   mwasiluk   updated d3-scatte...
163
164
          }
          plot.labels.push(label);
40c0e489   mwasiluk   charts-d3, d3 sc...
165
          plot.labelByVariable[variableKey] = label;
752c1f5a   mwasiluk   updated d3-scatte...
166
167
      });
  
40c0e489   mwasiluk   charts-d3, d3 sc...
168
      console.log(plot.labelByVariable);
752c1f5a   mwasiluk   updated d3-scatte...
169
170
171
172
173
174
175
176
177
178
  
      plot.subplots = [];
  };
  
  D3ScatterPlotMatrix.prototype.setupX = function () {
  
      var plot = this.plot;
      var x = plot.x;
      var conf = this.config;
  
40c0e489   mwasiluk   charts-d3, d3 sc...
179
      x.value = conf.variables.value;
752c1f5a   mwasiluk   updated d3-scatte...
180
      x.scale = d3.scale[conf.x.scale]().range([conf.padding / 2, plot.size - conf.padding / 2]);
40c0e489   mwasiluk   charts-d3, d3 sc...
181
182
      x.map = function (d, variable) {
          return x.scale(x.value(d, variable));
752c1f5a   mwasiluk   updated d3-scatte...
183
184
      };
      x.axis = d3.svg.axis().scale(x.scale).orient(conf.x.orient).ticks(conf.ticks);
40c0e489   mwasiluk   charts-d3, d3 sc...
185
      x.axis.tickSize(plot.size * plot.variables.length);
752c1f5a   mwasiluk   updated d3-scatte...
186
187
188
189
190
191
192
193
194
  
  };
  
  D3ScatterPlotMatrix.prototype.setupY = function () {
  
      var plot = this.plot;
      var y = plot.y;
      var conf = this.config;
  
40c0e489   mwasiluk   charts-d3, d3 sc...
195
      y.value = conf.variables.value;
752c1f5a   mwasiluk   updated d3-scatte...
196
      y.scale = d3.scale[conf.y.scale]().range([ plot.size - conf.padding / 2, conf.padding / 2]);
40c0e489   mwasiluk   charts-d3, d3 sc...
197
198
      y.map = function (d, variable) {
          return y.scale(y.value(d, variable));
752c1f5a   mwasiluk   updated d3-scatte...
199
200
      };
      y.axis= d3.svg.axis().scale(y.scale).orient(conf.y.orient).ticks(conf.ticks);
40c0e489   mwasiluk   charts-d3, d3 sc...
201
      y.axis.tickSize(-plot.size * plot.variables.length);
752c1f5a   mwasiluk   updated d3-scatte...
202
203
204
205
206
  };
  
  
  D3ScatterPlotMatrix.prototype.drawPlot = function () {
      var self =this;
40c0e489   mwasiluk   charts-d3, d3 sc...
207
      var n = self.plot.variables.length;
752c1f5a   mwasiluk   updated d3-scatte...
208
209
      var conf = this.config;
      self.svgG.selectAll(".mw-axis-x.mw-axis")
40c0e489   mwasiluk   charts-d3, d3 sc...
210
          .data(self.plot.variables)
752c1f5a   mwasiluk   updated d3-scatte...
211
212
213
          .enter().append("g")
          .attr("class", "mw-axis-x mw-axis"+(conf.guides ? '' : ' mw-no-guides'))
          .attr("transform", function(d, i) { return "translate(" + (n - i - 1) * self.plot.size + ",0)"; })
40c0e489   mwasiluk   charts-d3, d3 sc...
214
          .each(function(d) { self.plot.x.scale.domain(self.plot.domainByVariable[d]); d3.select(this).call(self.plot.x.axis); });
752c1f5a   mwasiluk   updated d3-scatte...
215
216
  
      self.svgG.selectAll(".mw-axis-y.mw-axis")
40c0e489   mwasiluk   charts-d3, d3 sc...
217
          .data(self.plot.variables)
752c1f5a   mwasiluk   updated d3-scatte...
218
219
220
          .enter().append("g")
          .attr("class", "mw-axis-y mw-axis"+(conf.guides ? '' : ' mw-no-guides'))
          .attr("transform", function(d, i) { return "translate(0," + i * self.plot.size + ")"; })
40c0e489   mwasiluk   charts-d3, d3 sc...
221
          .each(function(d) { self.plot.y.scale.domain(self.plot.domainByVariable[d]); d3.select(this).call(self.plot.y.axis); });
752c1f5a   mwasiluk   updated d3-scatte...
222
223
224
225
226
227
228
229
230
  
  
      if(conf.tooltip){
          self.plot.tooltip = this.utils.selectOrAppend(d3.select(self.placeholderSelector), 'div.mw-tooltip', 'div')
              .attr("class", "mw-tooltip")
              .style("opacity", 0);
      }
  
      var cell = self.svgG.selectAll(".mw-cell")
40c0e489   mwasiluk   charts-d3, d3 sc...
231
          .data(self.utils.cross(self.plot.variables, self.plot.variables))
752c1f5a   mwasiluk   updated d3-scatte...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
          .enter().append("g")
          .attr("class", "mw-cell")
          .attr("transform", function(d) { return "translate(" + (n - d.i - 1) * self.plot.size + "," + d.j * self.plot.size + ")"; });
  
      if(conf.brush){
          this.drawBrush(cell);
      }
  
      cell.each(plotSubplot);
  
  
  
      //Labels
      cell.filter(function(d) { return d.i === d.j; }).append("text")
          .attr("x", conf.padding)
          .attr("y", conf.padding)
          .attr("dy", ".71em")
40c0e489   mwasiluk   charts-d3, d3 sc...
249
          .text(function(d) { return self.plot.labelByVariable[d.x]; });
752c1f5a   mwasiluk   updated d3-scatte...
250
251
252
253
254
255
256
257
258
  
  
  
  
      function plotSubplot(p) {
          var plot = self.plot;
          plot.subplots.push(p);
          var cell = d3.select(this);
  
40c0e489   mwasiluk   charts-d3, d3 sc...
259
260
          plot.x.scale.domain(plot.domainByVariable[p.x]);
          plot.y.scale.domain(plot.domainByVariable[p.y]);
752c1f5a   mwasiluk   updated d3-scatte...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  
          cell.append("rect")
              .attr("class", "mw-frame")
              .attr("x", conf.padding / 2)
              .attr("y", conf.padding / 2)
              .attr("width", conf.size - conf.padding)
              .attr("height", conf.size - conf.padding);
  
  
          p.update = function(){
              var subplot = this;
              var dots = cell.selectAll("circle")
                  .data(self.data);
  
              dots.enter().append("circle");
  
              dots.attr("cx", function(d){return plot.x.map(d, subplot.x)})
                  .attr("cy", function(d){return plot.y.map(d, subplot.y)})
                  .attr("r", self.config.dot.radius);
  
              if (plot.dot.color) {
                  dots.style("fill", plot.dot.color)
              }
  
              if(plot.tooltip){
                  dots.on("mouseover", function(d) {
                      plot.tooltip.transition()
                          .duration(200)
                          .style("opacity", .9);
e09a3d19   mwasiluk   charts-d3 update,...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
                      var html = "(" + plot.x.value(d, subplot.x) + ", " +plot.y.value(d, subplot.y) + ")";
                      plot.tooltip.html(html)
                          .style("left", (d3.event.pageX + 5) + "px")
                          .style("top", (d3.event.pageY - 28) + "px");
  
                      var group = self.config.groups.value(d);
                      if(group || group===0 ){
                          html+="<br/>";
                          var label = self.config.groups.label;
                          if(label){
                              html+=label+": ";
                          }
                          html+=group
                      }
                      plot.tooltip.html(html)
752c1f5a   mwasiluk   updated d3-scatte...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
                          .style("left", (d3.event.pageX + 5) + "px")
                          .style("top", (d3.event.pageY - 28) + "px");
                  })
                      .on("mouseout", function(d) {
                          plot.tooltip.transition()
                              .duration(500)
                              .style("opacity", 0);
                      });
              }
  
              dots.exit().remove();
          };
  
          p.update();
      }
  
  
  };
  
  D3ScatterPlotMatrix.prototype.update = function () {
      this.plot.subplots.forEach(function(p){p.update()});
  };
  
  
  D3ScatterPlotMatrix.prototype.initSvg = function () {
  
      var self = this;
      var config = this.config;
  
  
  
      var width = self.plot.width+ config.margin.left + config.margin.right;
      var height =  self.plot.height+ config.margin.top + config.margin.bottom;
      var aspect = width / height;
  
      
      
      self.svg = d3.select(self.placeholderSelector).select("svg");
      if(!self.svg.empty()){
          self.svg.remove();
  
      }
      self.svg = d3.select(self.placeholderSelector).append("svg");
  
      self.svg
          .attr("width", width)
          .attr("height", height)
          .attr("viewBox", "0 0 "+" "+width+" "+height)
          .attr("preserveAspectRatio", "xMidYMid meet")
          .attr("class", "mw-d3-scatterplot-matrix");
  
      self.svgG = self.svg.append("g")
          .attr("class", "mw-container")
          .attr("transform", "translate(" + config.margin.left + "," + config.margin.top + ")");
  
  
      if(!config.width || config.height ){
          d3.select(window)
              .on("resize", function() {
                  //TODO add responsiveness if width/height not specified
              });
      }
  };
  
  D3ScatterPlotMatrix.prototype.init = function () {
      var self = this;
      self.initPlot();
      self.initSvg();
      self.drawPlot();
  };
  
  D3ScatterPlotMatrix.prototype.drawBrush = function (cell) {
      var self = this;
      var brush = d3.svg.brush()
          .x(self.plot.x.scale)
          .y(self.plot.y.scale)
              .on("brushstart", brushstart)
              .on("brush", brushmove)
              .on("brushend", brushend);
  
      cell.append("g").call(brush);
  
  
      var brushCell;
  
      // Clear the previously-active brush, if any.
      function brushstart(p) {
          if (brushCell !== this) {
              d3.select(brushCell).call(brush.clear());
40c0e489   mwasiluk   charts-d3, d3 sc...
394
395
              self.plot.x.scale.domain(self.plot.domainByVariable[p.x]);
              self.plot.y.scale.domain(self.plot.domainByVariable[p.y]);
752c1f5a   mwasiluk   updated d3-scatte...
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
              brushCell = this;
          }
      }
  
      // Highlight the selected circles.
      function brushmove(p) {
          var e = brush.extent();
          self.svgG.selectAll("circle").classed("hidden", function (d) {
              return e[0][0] > d[p.x] || d[p.x] > e[1][0]
                  || e[0][1] > d[p.y] || d[p.y] > e[1][1];
          });
      }
      // If the brush is empty, select all circles.
      function brushend() {
          if (brush.empty()) self.svgG.selectAll(".hidden").classed("hidden", false);
      }
  };
  
40c0e489   mwasiluk   charts-d3, d3 sc...
414
415
  
  function D3ScatterPlot(placeholderSelector, data, config){
e09a3d19   mwasiluk   charts-d3 update,...
416
      var self =  this;
40c0e489   mwasiluk   charts-d3, d3 sc...
417
418
419
      this.utils = new ChartsD3Utils();
      this.placeholderSelector = placeholderSelector;
      this.svg=null;
e09a3d19   mwasiluk   charts-d3 update,...
420
      this.config = undefined;
40c0e489   mwasiluk   charts-d3, d3 sc...
421
422
423
424
425
426
427
428
429
430
431
432
433
      this.defaultConfig = {
          width: 0,
          height: 0,
          guides: false, //show axis guides
          tooltip: true, //show tooltip on dot hover
          margin:{
              left: 50,
              right: 30,
              top: 30,
              bottom: 50
          },
          x:{// X axis config
              label: 'X', // axis label
e09a3d19   mwasiluk   charts-d3 update,...
434
435
              key: 0,
              value: function(d) { return d[self.config.x.key] }, // x value accessor
40c0e489   mwasiluk   charts-d3, d3 sc...
436
437
438
439
              orient: "bottom",
              scale: "linear"
          },
          y:{// Y axis config
e09a3d19   mwasiluk   charts-d3 update,...
440
441
442
              label: 'Y', // axis label,
              key: 1,
              value: function(d) { return d[self.config.y.key] }, // y value accessor
40c0e489   mwasiluk   charts-d3, d3 sc...
443
444
445
              orient: "left",
              scale: "linear"
          },
e09a3d19   mwasiluk   charts-d3 update,...
446
447
448
449
450
          groups:{
              key: 2,
              value: function(d) { return d[self.config.groups.key] }, // grouping value accessor,
              label: ""
          },
40c0e489   mwasiluk   charts-d3, d3 sc...
451
452
          dot:{
              radius: 2,
e09a3d19   mwasiluk   charts-d3 update,...
453
              color: function(d) { return self.config.groups.value(d) }, // string or function returning color's value for color scale
40c0e489   mwasiluk   charts-d3, d3 sc...
454
455
              d3ColorCategory: 'category10'
          }
e09a3d19   mwasiluk   charts-d3 update,...
456
  
40c0e489   mwasiluk   charts-d3, d3 sc...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
      };
  
      if(data){
          this.setData(data);
      }
  
      if(config){
          this.setConfig(config);
      }
  
      this.init();
  
  }
  
  D3ScatterPlot.prototype.setData = function (data){
      this.data = data;
      return this;
  };
  
  D3ScatterPlot.prototype.setConfig = function (config){
      this.config = this.utils.deepExtend({}, this.defaultConfig, config);
      return this;
  };
  D3ScatterPlot.prototype.initPlot = function (){
      var self=this;
      var margin = this.config.margin;
      var conf = this.config;
      this.plot={
          x: {},
          y: {},
          dot: {
              color: null//color scale mapping function
          }
      };
  
      var width = conf.width;
      var placeholderNode = d3.select(this.placeholderSelector).node();
  
      if(!width){
          width =placeholderNode.getBoundingClientRect().width;
      }
      var height = conf.height;
      if(!height){
          height =placeholderNode.getBoundingClientRect().height;
      }
  
      this.plot.width = width - margin.left - margin.right;
      this.plot.height = height - margin.top - margin.bottom;
  
      this.setupX();
      this.setupY();
  
      if(conf.dot.d3ColorCategory){
          this.plot.dot.colorCategory = d3.scale[conf.dot.d3ColorCategory]();
      }
      var colorValue = conf.dot.color;
      if(colorValue){
          this.plot.dot.colorValue = colorValue;
  
          if (typeof colorValue === 'string' || colorValue instanceof String){
              this.plot.dot.color = colorValue;
          }else if(this.plot.dot.colorCategory){
              this.plot.dot.color = function(d){
                  return self.plot.dot.colorCategory(self.plot.dot.colorValue(d));
              }
          }
  
  
e09a3d19   mwasiluk   charts-d3 update,...
525
526
527
      }else{
  
  
40c0e489   mwasiluk   charts-d3, d3 sc...
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
      }
  
      return this;
  };
  
  D3ScatterPlot.prototype.setupX = function (){
  
      var plot = this.plot;
      var x = plot.x;
      var conf = this.config.x;
  
      /*
       * value accessor - returns the value to encode for a given data object.
       * scale - maps value to a visual display encoding, such as a pixel position.
       * map function - maps from data value to display value
       * axis - sets up axis
       */
      x.value = conf.value;
      x.scale = d3.scale[conf.scale]().range([0, plot.width]);
      x.map = function(d) { return x.scale(x.value(d));};
      x.axis = d3.svg.axis().scale(x.scale).orient(conf.orient);
      var data = this.data;
      plot.x.scale.domain([d3.min(data, plot.x.value)-1, d3.max(data, plot.x.value)+1]);
      if(this.config.guides) {
          x.axis.tickSize(-plot.height);
      }
  
  };
  
  D3ScatterPlot.prototype.setupY = function (){
  
      var plot = this.plot;
      var y = plot.y;
      var conf = this.config.y;
  
      /*
       * value accessor - returns the value to encode for a given data object.
       * scale - maps value to a visual display encoding, such as a pixel position.
       * map function - maps from data value to display value
       * axis - sets up axis
       */
      y.value = conf.value;
      y.scale = d3.scale[conf.scale]().range([plot.height, 0]);
      y.map = function(d) { return y.scale(y.value(d));};
      y.axis = d3.svg.axis().scale(y.scale).orient(conf.orient);
  
      if(this.config.guides){
          y.axis.tickSize(-plot.width);
      }
  
  
      var data = this.data;
      plot.y.scale.domain([d3.min(data, plot.y.value)-1, d3.max(data, plot.y.value)+1]);
  };
  
  D3ScatterPlot.prototype.draw = function (){
      this.drawAxisX();
      this.drawAxisY();
      this.update();
  };
  D3ScatterPlot.prototype.drawAxisX = function (){
      var self = this;
      var plot = self.plot;
      var axisConf = this.config.x;
      self.svgG.append("g")
          .attr("class", "mw-axis-x mw-axis"+(self.config.guides ? '' : ' mw-no-guides'))
          .attr("transform", "translate(0," + plot.height + ")")
          .call(plot.x.axis)
          .append("text")
          .attr("class", "mw-label")
          .attr("transform", "translate("+ (plot.width/2) +","+ (self.config.margin.bottom) +")")  // text is drawn off the screen top left, move down and out and rotate
          .attr("dy", "-1em")
          .style("text-anchor", "middle")
          .text(axisConf.label);
  };
  
  D3ScatterPlot.prototype.drawAxisY = function (){
      var self = this;
      var plot = self.plot;
      var axisConf = this.config.y;
      self.svgG.append("g")
          .attr("class", "mw-axis mw-axis-y"+(self.config.guides ? '' : ' mw-no-guides'))
          .call(plot.y.axis)
          .append("text")
          .attr("class", "mw-label")
          .attr("transform", "translate("+ -self.config.margin.left +","+(plot.height/2)+")rotate(-90)")  // text is drawn off the screen top left, move down and out and rotate
          .attr("dy", "1em")
          .style("text-anchor", "middle")
          .text(axisConf.label);
  };
  
  
  D3ScatterPlot.prototype.update = function (){
      var self = this;
      var plot = self.plot;
      var data = this.data;
      var dots = self.svgG.selectAll(".mw-dot")
          .data(data);
  
      dots.enter().append("circle")
          .attr("class", "mw-dot");
  
  
      dots.attr("r", self.config.dot.radius)
          .attr("cx", plot.x.map)
          .attr("cy", plot.y.map);
  
      if(plot.tooltip){
          dots.on("mouseover", function(d) {
              plot.tooltip.transition()
                  .duration(200)
                  .style("opacity", .9);
e09a3d19   mwasiluk   charts-d3 update,...
640
641
642
643
644
645
646
647
648
649
650
              var html = "(" + plot.x.value(d) + ", " +plot.y.value(d) + ")";
              var group = self.config.groups.value(d);
              if(group || group===0 ){
                  html+="<br/>";
                  var label = self.config.groups.label;
                  if(label){
                      html+=label+": ";
                  }
                  html+=group
              }
              plot.tooltip.html(html)
40c0e489   mwasiluk   charts-d3, d3 sc...
651
652
653
654
655
656
657
658
659
660
661
662
663
                  .style("left", (d3.event.pageX + 5) + "px")
                  .style("top", (d3.event.pageY - 28) + "px");
          })
              .on("mouseout", function(d) {
                  plot.tooltip.transition()
                      .duration(500)
                      .style("opacity", 0);
              });
      }
  
      if(plot.dot.color){
          dots.style("fill", plot.dot.color)
      }
e09a3d19   mwasiluk   charts-d3 update,...
664
  
40c0e489   mwasiluk   charts-d3, d3 sc...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
      dots.exit().remove();
  
  };
  
  D3ScatterPlot.prototype.initSvg = function (){
      var self = this;
      var config = this.config;
      
      var width = self.plot.width+ config.margin.left + config.margin.right;
      var height =  self.plot.height+ config.margin.top + config.margin.bottom;
      var aspect = width / height;
  
      self.svg = d3.select(self.placeholderSelector).select("svg");
      if(!self.svg.empty()){
          self.svg.remove();
  
      }
      self.svg = d3.select(self.placeholderSelector).append("svg");
  
      self.svg 
          .attr("width", width)
          .attr("height", height)
          .attr("viewBox", "0 0 "+" "+width+" "+height)
          .attr("preserveAspectRatio", "xMidYMid meet")
          .attr("class", "mw-d3-scatterplot");
      self.svgG = self.svg.append("g")
          .attr("transform", "translate(" + config.margin.left + "," + config.margin.top + ")");
  
      if(config.tooltip){
          self.plot.tooltip = this.utils.selectOrAppend(d3.select(self.placeholderSelector), 'div.mw-tooltip', 'div')
              .attr("class", "mw-tooltip")
              .style("opacity", 0);
      }
  
      if(!config.width || config.height ){
          d3.select(window)
              .on("resize", function() {
                  //TODO add responsiveness if width/height not specified
              });
      }
  
  };
  
  D3ScatterPlot.prototype.init = function (){
      var self = this;
      self.initPlot();
      self.initSvg();
      self.draw();
  
  };
  
  
  function ChartsD3Utils() {
  }
  
  // usage example deepExtend({}, objA, objB); => should work similar to $.extend(true, {}, objA, objB);
  ChartsD3Utils.prototype.deepExtend = function (out) { 
  
      var utils = this;
      var emptyOut = {};
  
  
      if (!out && arguments.length > 1 && Array.isArray(arguments[1])) {
          out = [];
      }
      out = out || {};
  
      for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          if (!source)
              continue;
  
          for (var key in source) {
              if (!source.hasOwnProperty(key)) {
                  continue;
              }
              var isArray = Array.isArray(out[key]);
              var isObject = utils.isObject(out[key]);
              var srcObj = utils.isObject(source[key]);
  
              if (isObject && !isArray && srcObj) {
                  utils.deepExtend(out[key], source[key]);
              } else {
                  out[key] = source[key];
              }
          }
      }
  
      return out;
  };
  
  ChartsD3Utils.prototype.cross = function (a, b) {
      var c = [], n = a.length, m = b.length, i, j;
      for (i = -1; ++i < n;) for (j = -1; ++j < m;) c.push({x: a[i], i: i, y: b[j], j: j});
      return c;
  };
  
  ChartsD3Utils.prototype.inferVariables = function (data, groupKey, includeGroup) {
      var res = [];
      if (data.length) {
          var d = data[0];
          if (d instanceof Array) {
              res=  d.map(function (v, i) {
                  return i;
              });
          }else if (typeof d === 'object'){
  
              for (var prop in d) {
                  if(!d.hasOwnProperty(prop)) continue;
  
                  res.push(prop);
              }
          }
      }
      if(!includeGroup){
          var index = res.indexOf(groupKey);
          if (index > -1) {
              res.splice(index, 1);
          }
      }
      return res
  };
  
  
  ChartsD3Utils.prototype.isObject = function(a) {
      return a !== null && typeof a === 'object';
  };
  ChartsD3Utils.prototype.isNumber = function(a) {
      return !isNaN(a) && typeof a === 'number';
  };
  ChartsD3Utils.prototype.isFunction = function(a) {
      return typeof a === 'function';
  };
  
  ChartsD3Utils.prototype.selectOrAppend = function (parent, selector, element) {
      var selection = parent.select(selector);
      if(selection.empty()){
          return parent.append(element || selector);
      }
      return selection;
  };