-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (66 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
'use strict';
var LightningVisualization = require('lightning-visualization');
var d3 = require('d3');
var moment = require('moment');
var _ = require('lodash');
var LightningAxis = LightningVisualization.extend({
axisPadding: 5,
coerceValue: function(val, scaleType) {
scaleType = scaleType || 'linear';
if(scaleType === 'time') {
return moment(val).toDate();
}
return val;
},
getXScale: function(domain) {
var scaleType = this.data.xscale || 'linear';
var scale;
if(scaleType === 'time') {
scale = d3.time.scale();
} else {
scale = d3.scale[scaleType]();
}
return scale.domain(this.data.xlim || [domain[0], domain[1]])
.range([this.axisPadding, this.width - this.margin.left - this.margin.right - this.axisPadding]);
},
getYScale: function(domain) {
var scaleType = this.data.yscale || 'linear';
var scale;
if(scaleType === 'time') {
scale = d3.time.scale();
} else {
scale = d3.scale[scaleType]();
}
return scale.domain(this.data.ylim || [domain[0], domain[1]])
.range([this.height - this.margin.top - this.margin.bottom - this.axisPadding, this.axisPadding]);
},
getXAxis: function(scale) {
var tickFormat = null;
if(this.data.xTickFormat) {
var format = this.data.xTickFormat;
tickFormat = function(d) {
return parseFloat(d3.format(format)(d))
}
}
return d3.svg.axis()
.scale(scale)
.orient('bottom')
.ticks(this.data.xticks || 5)
.tickFormat(tickFormat);
},
getYAxis: function(scale) {
var tickFormat = null;
if(this.data.yTickFormat) {
var format = this.data.yTickFormat;
tickFormat = function(d) {
return parseFloat(d3.format(format)(d))
}
}
return d3.svg.axis()
.scale(scale)
.orient('left')
.ticks(this.data.yticks || 5)
.tickFormat(tickFormat);
}
});
module.exports = LightningAxis;