forked from arbelzinger/ng-d3-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3-slider.directive.ts
208 lines (184 loc) · 5.67 KB
/
d3-slider.directive.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
Directive, OnInit, OnChanges, SimpleChanges, Input, Output, EventEmitter, ElementRef,
ViewContainerRef
} from '@angular/core';
import * as d3 from 'd3'
@Directive({
selector: '[ngD3Slider]'
})
export class D3SliderDirective implements OnInit, OnChanges{
id:string;
@Input() disable:string;
@Input() length:number;
@Input() maxValue:number;
@Input() minValue:number;
@Input() initialValue:number;
@Input() step:number;
@Input() lineWidth:number;
@Input() color:string;
@Input() emptyColor:string;
@Input() thumbColor:string;
@Input() thumbSize:number;
@Input() direction:string;
@Input() vertical:string;
@Output() selectedValue = new EventEmitter();
private value;
constructor(slider:ViewContainerRef) {
this.maxValue = 1;
this.minValue = 0;
this.value;
this.initialValue = null;
this.step = 1;
this.color = "#51CB3F";
this.emptyColor = "#ECECEC";
this.thumbColor = "white";
this.lineWidth = 6;
this.thumbSize = 6;
this.direction = "LTR";
this.id = slider.element.nativeElement.id;
}
ngOnInit() {
}
ngOnChanges(changes: SimpleChanges){
let selection;
if(!this.initialValue&&changes['initialValue']&&changes['initialValue'].firstChange){
this.initialValue = this.minValue;
}
if(!this.value){
this.value = this.initialValue;
}
d3.select("#"+this.id).selectAll("*").remove();
if(this.vertical=="true"){
selection = d3.select("#"+this.id).append("svg").attr('height',this.length+20).attr("viewBox", '0,-10,'+'30,'+(this.length+20));
}else{
selection = d3.select("#"+this.id).append("svg").attr('width',this.length+20).attr("viewBox", '-10,0,'+(this.length+20)+',30');
}
this.createSlider(selection)
}
createSlider(selection){
let that= this;
var direction = this.direction;
var width = this.length;
var maxValue = this.maxValue;
var minValue = this.minValue;
if(minValue>maxValue){
maxValue = minValue*2
}
var value = this.value;
var color;
var emptyColor;
if(direction=='RTL'){
emptyColor = this.color;
color = this.emptyColor;
}else{
emptyColor = this.emptyColor;
color = this.color;
}
var thumbColor = this.thumbColor;
var lineWidth = this.lineWidth;
var thumbSize = this.thumbSize;
var normStep = this.step;
if(normStep>maxValue){
normStep = 1
}
var normValue = this.setNormValue(value,minValue,maxValue,direction);// value normalized between 0-1
var mainAxis;
var secondaryAxis;
if(this.vertical=="true"){
mainAxis = "y";
secondaryAxis = "x"
}else{
mainAxis = "x";
secondaryAxis = "y"
}
var selectedValue;
function dragStart(){
valueCircle.attr("r", thumbSize+1);
}
function drag() {
selectedValue = d3.event[mainAxis];
if (selectedValue < 0)
selectedValue = 0;
else if (selectedValue > width)
selectedValue = width;
selectedValue= selectedValue - (selectedValue%normStep);
normValue = selectedValue/ width;
valueCircle.attr("c"+mainAxis, selectedValue);
valueLine.attr(mainAxis+"2",selectedValue);
emptyLine.attr(mainAxis+"1", selectedValue);
if (event)
event(normValue);
d3.event.sourceEvent.stopPropagation();
}
function dragEnd(){
valueCircle.attr("r", thumbSize);
}
//Line to represent the current value
var valueLine = selection.append("line")
.attr(mainAxis+"1", 0)
.attr(mainAxis+"2", width * normValue)
.attr(secondaryAxis+"1", 10)
.attr(secondaryAxis+"2", 10)
.style("stroke", color)
.style("stroke-linecap", "round")
.style("stroke-width", lineWidth);
//Line to show the remaining value
var emptyLine = selection.append("line")
.attr(mainAxis+"1", width * normValue)
.attr(mainAxis+"2", width)
.attr(secondaryAxis+"1", 10)
.attr(secondaryAxis+"2", 10)
.style("stroke", emptyColor)
.style("stroke-linecap", "round")
.style("stroke-width", lineWidth);
//Draggable circle to represent the current value
var valueCircle = selection.append("circle")
.attr("c"+mainAxis, width * normValue)
.attr("c"+secondaryAxis, 10)
.attr("r", thumbSize)
.style("stroke", "black")
.style("stroke-width",1)
.style("fill", thumbColor);
if(that.disable!="disable"){
valueCircle.call(d3.drag()
.on("start", dragStart)
.on("drag", drag)
.on("end", dragEnd))
.style("cursor","hand");
}
function event(iNewValue){
that.value = that.setDenormValue(iNewValue,minValue,maxValue,direction);
that.selectedValue.emit(that.value)
}
}
/**
* Normalizes the values to a range between 0 to 1
* @param iValue
* @param iMinValue
* @param iMaxValue
* @param iDirection
* @returns {number}
*/
setNormValue(iValue:number,iMinValue:number,iMaxValue:number,iDirection:string){
if(iDirection==="LTR"){
return (iValue-iMinValue)/(iMaxValue-iMinValue);
}else if(iDirection==="RTL"){
return 1-(iValue-iMinValue)/(iMaxValue-iMinValue);
}
}
/**
* Converts to normalized value according to the min-max range given
* @param iValue
* @param iMinValue
* @param iMaxValue
* @param iDirection
* @returns {Number}
*/
setDenormValue(iValue:number,iMinValue:number,iMaxValue:number,iDirection:string){
if(iDirection=="LTR"){
return Number((iValue*(iMaxValue-iMinValue)+iMinValue).toFixed(2));
}else if(iDirection==="RTL"){
return Number(((1-iValue)*(iMaxValue-iMinValue)+iMinValue).toFixed(2));
}
}
}