Skip to content

Commit 3c25bd7

Browse files
author
linlj
committed
feat: 根据value显示不同宽度
1 parent 276882e commit 3c25bd7

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ migrationLayer.destroy();
6060
| pulseBorderWidth| pulse border width | 3 | any number>0 | no |
6161
| arcWidth | arc width | 1 | any number>0 | no |
6262
| arcLabel | show from and to label | true | Bool | no |
63-
| arcLabelFont | label font and size | '15px sans-serif'| 'size font' | no |
63+
| arcLabelFont | label font and size | '15px sans-serif'| 'size font' | no |
64+
| maxWidth | the max width of arc | 10 | any number>1 | no |
65+
66+
## data format
67+
68+
```js
69+
data = [{"from":[-118.2705,33.9984],"to":[-122.789336,37.920458],"labels":["Los Angeles","San Francisco"],"color":"#ff3a31","value":15}];
70+
```
71+
72+
If the value is not undefined, the width of arc will depend on the value.
6473

6574
## Leaflet Version
6675
Requires Leaflet 1.0.2 or newer

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@
4747
var lrmap = L.map('map').setView([35, -95], 5);
4848
L.tileLayer('https://api.tiles.mapbox.com/v4/mapbox.dark/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw')
4949
.addTo(lrmap);
50-
50+
5151
var data = [{"from":[-118.2705,33.9984],"to":[-122.789336,37.920458],"labels":["Los Angeles","San Francisco"],"color":"#ff3a31"},{"from":[-118.2705,33.9984],"to":[-80.247887,25.792296],"labels":[null,"Miami"],"color":"#ff7e2b"},{"from":[-118.2705,33.9984],"to":[-73.999705,40.795047],"labels":[null,"New York"],"color":"#ffc726"},{"from":[-118.2705,33.9984],"to":[-87.724088,41.917846],"labels":[null,"Chicago"],"color":"#e9ff20"},{"from":[-118.2705,33.9984],"to":[-92.145189,46.77372],"labels":[null,"Duluth"],"color":"#99ff1b"},{"from":[-118.2705,33.9984],"to":[-111.824547,40.788055],"labels":[null,"Salt Lake"],"color":"#45ff15"},{"from":[-118.2705,33.9984],"to":[-111.364615,47.536109],"labels":[null,"Great Falls"],"color":"#10ff33"},{"from":[-118.2705,33.9984],"to":[-97.585039,35.511099],"labels":[null,"Oklahoma"],"color":"#0aff84"},{"from":[-118.2705,33.9984],"to":[-115.157907,36.173032],"labels":[null,"Las Vegas"],"color":"#05ffd9"},{"from":[-118.2705,33.9984],"to":[-103.196215,34.418753],"labels":[null,"Clovis"],"color":"#00ccff"}];
5252

5353
var data2=[{"from":[-73.875523,40.781063],"to":[-80.247887,25.792296],"labels":["New York","Maima"],"color":"#05ffd9"},{"from":[-73.875523,40.781063],"to":[-118.2705,33.9984],"labels":[null,"Los Angeles"],"color":"#00ccff"},{"from":[-73.875523,40.781063],"to":[-87.724088,41.917846],"labels":[null,"Chicago"],"color":"#ffc726"},{"from":[-73.875523,40.781063],"to":[-71.058437,42.35902],"labels":[null,"Boston"],"color":"#e9ff20"},{"from":[-73.875523,40.781063],"to":[-75.683057,45.42172],"labels":[null,"Ottawa"],"color":"#99ff1b"}];
5454

55+
data = data.map(item => { return {...item, value: parseInt(Math.random()*20)}});
56+
data2 = data2.map(item => { return {...item, value: parseInt(Math.random()*20)}});
57+
5558
var migrationLayer = new L.migrationLayer({
5659
map: lrmap,
5760
data: data,
@@ -60,6 +63,7 @@
6063
arcWidth:1,
6164
arcLabel:true,
6265
arcLabelFont:'10px sans-serif',
66+
maxWidth:10
6367
}
6468
);
6569
migrationLayer.addTo(lrmap);

src/src.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@
6666
if (this.style === 'circle') {
6767
context.arc(0, 0, this.size, 0, Math.PI * 2, false);
6868
} else if (this.style === 'arrow') {
69-
context.moveTo(-this.size, -this.size);
70-
context.lineTo(this.size, 0);
71-
context.lineTo(-this.size, this.size);
72-
context.lineTo(-this.size / 4, 0);
73-
context.lineTo(-this.size, -this.size);
69+
// 将箭头后退,让箭头的尖头指向终点
70+
context.moveTo(-this.size*2, -this.size);
71+
context.lineTo(-this.size*5/4, 0);
72+
context.lineTo(-this.size*2, this.size);
73+
context.lineTo(0, 0);
74+
context.lineTo(-this.size*2, -this.size);
7475
}
7576
context.closePath();
7677
context.stroke();
@@ -117,6 +118,7 @@
117118
this.label = options.label;
118119
this.font = options.font;
119120
this.shadowBlur = options.shadowBlur;
121+
this.endAngle = endAngle - this.lineWidth / radius; // 让线后退,与箭头结合
120122
};
121123

122124
A.prototype.draw = function (context) {
@@ -236,14 +238,15 @@
236238

237239
this.animateBlur = true;
238240

241+
const size = options.size? options.size/2:1
239242
this.marker = new Marker({
240243
x: 50,
241244
y: 80,
242245
rotation: 50 * Math.PI / 180,
243246
style: 'arrow',
244247
color: 'rgb(255, 255, 255)',
245-
size: 3,
246-
borderWidth: 0,
248+
size: size+1,
249+
borderWidth: size ,
247250
borderColor: this.strokeStyle
248251
});
249252
};
@@ -367,7 +370,7 @@
367370
labels: element.labels,
368371
label: this.style.arc.label,
369372
font: this.style.arc.font,
370-
width: this.style.arc.width,
373+
width: element.arcWidth || this.style.arc.width,
371374
color: element.color
372375
});
373376
var marker = new Marker({
@@ -376,7 +379,7 @@
376379
rotation: arc.endAngle + Math.PI / 2,
377380
style: 'arrow',
378381
color: element.color,
379-
size: 4,
382+
size: element.arcWidth || this.style.arc.width+3,
380383
borderWidth: 0,
381384
borderColor: element.color
382385
});
@@ -393,7 +396,8 @@
393396
endX: element.to[0],
394397
endY: element.to[1],
395398
width: 15,
396-
color: element.color
399+
color: element.color,
400+
size: element.arcWidth
397401
});
398402

399403
this.store.arcs.push(arc);
@@ -515,9 +519,37 @@
515519
},
516520
_convertData: function () {
517521
var bounds = this._map.getBounds();
518-
522+
let maxValue;
523+
let minValue
519524
if (this._data && bounds) {
525+
arrayUtils.forEach(this._data, function (d) {
526+
if(d.value){
527+
if(!maxValue){
528+
maxValue = d.value;
529+
minValue = d.value;
530+
}
531+
if(maxValue<d.value){
532+
maxValue = d.value;
533+
}
534+
if(minValue>d.value){
535+
minValue = d.value;
536+
}
537+
}
538+
});
539+
var maxWidth = this.options.maxWidth || 10;
520540
var data = arrayUtils.map(this._data, function (d) {
541+
if(d.value){
542+
if(!maxValue){
543+
maxValue = d.value;
544+
minValue = d.value;
545+
}
546+
if(maxValue<d.value){
547+
maxValue = d.value;
548+
}
549+
if(minValue>d.value){
550+
minValue = d.value;
551+
}
552+
}
521553

522554
var fromPixel = this._map.latLngToContainerPoint(new L.LatLng(d.from[1], d.from[0]));
523555
var toPixel = this._map.latLngToContainerPoint(new L.LatLng(d.to[1], d.to[0]));
@@ -526,10 +558,14 @@
526558
to: [toPixel.x, toPixel.y],
527559
labels: d.labels,
528560
value: d.value,
529-
color: d.color
561+
color: d.color,
562+
arcWidth: d.value? parseInt((d.value - minValue) * (maxWidth-1)/(maxValue - minValue)) + 1:this.options.arcWidth
530563
}
531564
}, this);
532565

566+
567+
568+
533569
return data;
534570
}
535571
},

0 commit comments

Comments
 (0)