Jac*_*ski 7 javascript r leaflet r-leaflet leaflet.markercluster
我对分组标记和以小饼图形式按组呈现计数的解决方案着迷/sf/answers/4236759621/ 我只是 R,不懂 JS。我希望代码对每个数据点的值求和而不是计数(每个单独的数据点可能已经代表一个计数)。我想根据值控制更多气泡的大小。你能帮助我并展示如何更改 js 代码,以便它对数据点的值进行求和,以及如何增加/控制气泡的大小?
这里有一个所需的解决方案,对非饼图标记进行求和而不是计数:How to display the value (sum)rather than count of labels in a dc.leaflet.js 这里 有一个解决方案,它还可以控制气泡的大小:带有总和(不是计数)的聚类传单标记:如何获得一致的圆形红色形状和标签格式,就像非聚类标记一样
原始代码来自/sf/users/189819871/ @DanielBonnery
library(leaflet)
library(dplyr)
#Creates data
data("breweries91",package="leaflet")
#set.seed(1);
breweries91$goodbear<-sample(as.factor(c("terrific","marvelous","culparterretaping")),nrow(breweries91),replace=T)
#Colors
joliepalette<-c("red","green","blue")[1:nlevels(breweries91$goodbear)]
getColor <- function(breweries91) {joliepalette[breweries91$goodbear]}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(breweries91)
)
#Generate the javascript
jsscript3<-
paste0(
"function(cluster) {
const groups= [",paste("'",levels(breweries91$goodbear),"'",sep="",collapse=","),"];
const colors= {
groups: [",paste("'",joliepalette,"'",sep="",collapse=","),"],
center:'#ddd',
text:'black'
};
const markers= cluster.getAllChildMarkers();
const proportions= groups.map(group => markers.filter(marker => marker.options.group === group).length / markers.length);
function sum(arr, first= 0, last) {
return arr.slice(first, last).reduce((total, curr) => total+curr, 0);
}
const cumulativeProportions= proportions.map((val, i, arr) => sum(arr, 0, i+1));
cumulativeProportions.unshift(0);
const width = 2*Math.sqrt(markers.length);
const radius= 15+width/2;
const arcs= cumulativeProportions.map((prop, i) => { return {
x : radius*Math.sin(2*Math.PI*prop),
y : -radius*Math.cos(2*Math.PI*prop),
long: proportions[i-1] >.5 ? 1 : 0
}});
const paths= proportions.map((prop, i) => {
if (prop === 0) return '';
else if (prop === 1) return `<circle cx='0' cy='0' r='${radius}' fill='none' stroke='${colors.groups[i]}' stroke-width='${width}' stroke-alignment='center' stroke-linecap='butt' />`;
else return `<path d='M ${arcs[i].x} ${arcs[i].y} A ${radius} ${radius} 0 ${arcs[i+1].long} 1 ${arcs[i+1].x} ${arcs[i+1].y}' fill='none' stroke='${colors.groups[i]}' stroke-width='${width}' stroke-alignment='center' stroke-linecap='butt' />`
});
return new L.DivIcon({
html: `
<svg width='60' height='60' viewBox='-30 -30 60 60' style='width: 60px; height: 60px; position: relative; top: -24px; left: -24px;' >
<circle cx='0' cy='0' r='15' stroke='none' fill='${colors.center}' />
<text x='0' y='0' dominant-baseline='central' text-anchor='middle' fill='${colors.text}' font-size='15'>${markers.length}</text>
${paths.join('')}
</svg>
`,
className: 'marker-cluster'
});
}")
# Generates the map.
leaflet() %>%
addTiles() %>%
addAwesomeMarkers(data=breweries91,
group=~goodbear,
icon = icons,
clusterOptions = markerClusterOptions(
iconCreateFunction =
JS(jsscript3)))
Run Code Online (Sandbox Code Playgroud)
小智 0
请注意您的代码中的这一行
const arcs= cumulativeProportions.map((prop, i) => { return {
Run Code Online (Sandbox Code Playgroud)
据我所知,用 所创建的路径的大小是由SVG的值控制的prop,我猜这意味着“比例”。
我将 javascript 复制到vim并搜索设置的位置prop。在我看来,它prop从未被赋予价值。
他们一直告诉我,保持事物的“比例”总是有帮助的。