-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlaychart.js
145 lines (122 loc) · 4.49 KB
/
overlaychart.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use strict";
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['lodash','d3'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('lodash'),require('d3'));
} else {
// Browser globals (root is window)
root.overlaychart = factory(root._,root.d3);
}
}(this,realAction));
function realAction(_,d3){
return function(){
this.render=function(){
let placeholder=this.placeholder(),
dimensions=this.dimensions(),
current=this.current(),
search=this.search(),
searchAccessor=this.searchAccessor(),
uniqueAccessor=this.uniqueAccessor(),
data=this.data();
let ph = d3.select(placeholder);
let overlaychart = null;
let {width,height,margin}=dimensions;
if (ph.select("svg#overlaychart").size() > 0){
overlaychart = ph.select("svg#overlaychart");
}else {
overlaychart = ph.append("svg").attr({
id: "overlaychart",
width: width,
height: height,
fill: "white"
});
}
let gs=overlaychart.selectAll("g")
.data(this.data(),uniqueAccessor);
let availableArr=this.data().map(d=>d.index);
let xScale=d3.scale.linear().domain([_.head(availableArr),_.last(availableArr)]).range([margin.left,width-margin.right]);
let yScale=d3.scale.linear().domain([0,10]).range([height-margin.top,margin.bottom])
gs.exit().remove();
gs.transition().attr({
transform: (d)=>`translate(${xScale(d.index)},0)`
})
gs.select("path").attr({stroke:"#AAAAAA"})
let entered=gs.enter().append("g").attr({
transform: (d)=>`translate(${xScale(d.index)},0)`
})
let paths=entered.append("path").attr({
"stroke-width":1,
stroke:"#AAAAAA",
d: d=>{return `M 0 ${yScale(0)} L 0 ${yScale(d.value.reds)}`}
});
paths
.attr("stroke-dasharray", function() {
return this.getTotalLength() + " " + this.getTotalLength();
})
.attr("stroke-dashoffset", function() {
return this.getTotalLength()
})
.transition()
.attr("stroke-dashoffset", 0);
if(search && searchAccessor){
gs.filter(searchAccessor(search))
.select("path").attr({stroke:"red"});
}
}
this.data=function(){
if(arguments.length){
let data=arguments[0];
this.__data__=data;
return this;
}else return this.__data__;
}
this.placeholder=function(){
if(arguments.length){
let placeholder=arguments[0];
this.__placeholder__=placeholder;
return this;
}else return this.__placeholder__;
}
this.dimensions=function(){
if(arguments.length){
let dimensions=arguments[0];
this.__dimensions__=dimensions;
return this;
}else return this.__dimensions__;
}
this.current=function(){
if(arguments.length){
let current=arguments[0];
this.__current__=current;
return this;
}else return this.__current__;
}
this.uniqueAccessor=function(){
if(arguments.length){
let uniqueAccessor=arguments[0];
this.__uniqueAccessor__=uniqueAccessor;
return this;
}else return this.__uniqueAccessor__ || JSON.stringify;
}
this.search=function(){
if(arguments.length){
let search=arguments[0];
this.__search__=search;
return this;
}else return this.__search__;
}
this.searchAccessor=function(){
if(arguments.length){
let searchAccessor=arguments[0];
this.__searchAccessor__=searchAccessor;
return this;
}else return this.__searchAccessor__;
}
return this;
}
}