Javascript 传单.js:将功能属性传递给类构造函数

Javascript 传单.js:将功能属性传递给类构造函数,javascript,d3.js,leaflet,Javascript,D3.js,Leaflet,我有很多使用定义的svg标记(glyph)(非常感谢名为rioV8的SO用户在这方面的帮助,不仅仅是这个…),理想情况下,我希望这些glyph能够从特征属性结构中获得它们的形状 //create feature properties var p = { "id": i, "popup": "Dot_" + i, "year": parseInt(data

我有很多使用定义的svg标记(glyph)(非常感谢名为rioV8的SO用户在这方面的帮助,不仅仅是这个…),理想情况下,我希望这些glyph能够从特征属性结构中获得它们的形状

            //create feature properties
            var p = {
                "id": i,
                "popup": "Dot_" + i,
                "year": parseInt(data[i].year),
                "glyphName": "square",
                "size": 500 // Fixed size circle radius=~13
            };
这些用户定义的图示符扩展了L.circleMarker,为了简单起见,我们假设它们的形状可以是正方形或菱形。目前,我正在扩展
L.Class
,并在构造函数中传递glyphName:(如果它看起来不好看,请随意批评)

当我需要绘制图示符时,我有类似的东西:

L.geoJson(myDots[i], {
    pointToLayer: function(feature, latlng) {
        var p = latlng;
        var myGlyph = new glyph('diamond')
        return new myGlyph.type(p, style(feature));
       },
       onEachFeature: onEachDot
   }).addTo(map);
我可以用特征属性来确定形状吗?最终,我试图实现的是合并这两条线

var myGlyph = new glyph('diamond')
return new myGlyph.type(p, style(feature));
差不多

return new myGlyph.type(p, style(feature));
这将使我能够绘制不同的形状,这些形状将由用于填充特征属性的输入数据确定。与这些属性用于颜色或大小的方式类似,它们现在可以用于设置形状

谢谢!(完整代码如下)


图表
html,
身体{
身高:100%;
保证金:0;
}
#地图{
宽度:600px;
高度:600px;
}
包括({
_updateMarkerDiamond:函数(层){
如果(!this.| | | layer._empty()){
返回;
}
var p=层.\u点,
ctx=这个,
r=数学最大值(数学圆(层半径),6);
此._drawnLayers[层._传单\u id]=层;
ctx.beginPath();
ctx.moveTo(p.x-r,p.y);
ctx.lineTo(p.x,p.y-r);
ctx.lineTo(p.x+r,p.y);
ctx.lineTo(p.x,p.y+r);
ctx.lineTo(p.x-r,p.y);
ctx.closePath();
这是一个填充冲程(ctx,层);
}
});
var MarkerDiamond=L.CircleMarker.extend({
_updatePath:函数(){
这个.\u渲染器.\u更新MarkerDiamond(这个);
}
});
包括({
_updateMarkerSquare:函数(层){
如果(!this.| | | layer._empty()){
返回;
}
var p=层.\u点,
ctx=这个,
r=数学最大值(数学圆(层半径),5);
此._drawnLayers[层._传单\u id]=层;
ctx.beginPath();
ctx.moveTo(p.x-r,p.y-r);
ctx.lineTo(p.x+r,p.y-r);
ctx.lineTo(p.x+r,p.y+r);
ctx.lineTo(p.x-r,p.y+r);
ctx.lineTo(p.x-r,p.y-r);
ctx.closePath();
这是一个填充冲程(ctx,层);
}
});
var MarkerSquare=L.CircleMarker.extend({
_updatePath:函数(){
此._渲染器._更新Markersquare(此);
}
});
var glyph=L.Class.extend({
初始化:函数(名称){
glyphName==“正方形”?this.type=MarkerSquare:
glyphName==“菱形”?this.type=MarkerDiamond:
this.type=L.circleMarker;
},
});
var数据=[];
变量NumOfPoints=100;
for(设i=0;i90?'#6068F0':
y>80?“#6B64DC”:
y>70?“#7660C9”:
y>60?“#815CB6”:
y>50?“#8C58A3”:
y>40?“#985490”:
y>30?“#A3507C”:
y>20?“#AE4C69”:
y>10?“#B94856”:
y>0?“#C44443”:
#d0430";
}
//计算半径,使生成的圆按面积成比例
函数getRadius(y){
r=Math.sqrt(y/Math.PI)
返回r;
}
var-myrender;
//使用从颜色渐变中拾取的fillColor创建样式
功能样式(特征){
返回{
半径:getRadius(feature.pr
return new myGlyph.type(p, style(feature));
var Marker = L.CircleMarker.extend({
     _updatePath: function() {
         if (this.options.shape === "square")
             this._renderer._updateMarkerSquare(this);
         if (this.options.shape === "diamond")
             this._renderer._updateMarkerDiamond(this);
     }
 });

    function style(feature) {
        return {
            radius: getRadius(feature.properties.size),
            shape: feature.properties.shape,
            fillColor: getColor(feature.properties.year),
            color: "#000",
            weight: 0,
            opacity: 1,
            fillOpacity: 0.9,
            renderer: myRenderer
        };
    }