简体   繁体   English

使用传单的Choropleth地图未显示正确的样式

[英]Choropleth map using leaflet not displaying correct style

I created a map of the 2016 election at the county level, where if one party gets more votes than the other in one county, that county gets the color of said party. 我在县一级创建了一张2016年选举的地图,如果一个政党在一个县中获得的选票多于另一政党,则该县将获得该政党的颜色。 So obviously, I use a conditional statement. 所以很明显,我使用条件语句。 Almost all counties are looking fine, they're following the rule, but some are NOT. 几乎所有县都很好,它们都遵守规定,但有些不满意。 For example, Gray County, TX, where the dems only got around 9%, while the gop got 88%. 例如,得克萨斯州格雷县(Gray County)的抽样率仅约为9%,而实际税率仅为88%。 For some reason, the map displays this county in blue, not the gop color scheme: 出于某种原因,地图以蓝色显示该县,而不是gop配色方案:

You can find the map here: https://sushirittoman.github.io/maps/counties_project/index.html 您可以在这里找到地图: https : //sushirittoman.github.io/maps/counties_project/index.html

And here is the code I wrote for the map, borrowing heavily from the leaflet tutorial for choropleth maps ( https://leafletjs.com/examples/choropleth/ ): 这是我为地图编写的代码,大量借鉴了Choropleth地图的传单教程( https://leafletjs.com/examples/choropleth/ ):

//Red palette for Gop 
function getColorGop(v) {
            return v > 70 ? '#800026' :
                   v > 50 ? '#BD0026' :
                   v > 40 ? '#E31A1C' :
                   v > 30 ? '#FC4E2A' :
                   v > 20 ? '#FD8D3C' :
                   v > 10 ? '#FEB24C' :
                            '#FED976' ;                                  

}

//Blue palette for Dems
function getColorDem(v) {
            return v > 70 ? '#08589e':
                   v > 50 ? '#2b8cbe':
                   v > 40 ? '#4eb3d3':
                   v > 30 ? '#7bccc4':
                   v > 20 ? '#a8ddb5':
                   v > 10 ? '#ccebc5':
                            '#e0f3db';                               

}

//Ultimate style, where we insert the conditional statement
function style(feature) {
            if ([feature.properties.per_dem * 100] > [feature.properties.per_gop * 100]) {
                return {
                    fillColor: getColorDem(feature.properties.per_dem * 100),
                    weight: 1,
                    opacity: 1,
                    color: 'black',                    
                    fillOpacity: 1                        
                }                                        
            } 
            else {
                return {
                    fillColor: getColorGop(feature.properties.per_gop * 100),
                    weight: 1,
                    opacity: 1,
                    color: 'black',                   
                    fillOpacity: 1                        
                };
}        

I am stumped. 我感到难过。 I was hoping this could be my first official non-tutorial webmap. 我希望这可以成为我的第一个官方非教程网络地图。 This issue seems to be an anomaly. 这个问题似乎是反常的。 I would very much appreciate your thoughts. 我非常感谢您的想法。

Your comparison is written as 您的比较写为

[feature.properties.per_dem * 100] > [feature.properties.per_gop * 100]

[...] in Javascript is the syntax for declaring an array, which means that you are trying to determine if an array is bigger than an other and that's probably not what you want (given how type casting in JS works, predicting the outcome can be tricky). [...] Javascript是用于声明数组的语法,这意味着您正在尝试确定数组是否大于另一个数组,而这可能不是您想要的(鉴于JS中的类型转换如何工作,可以预测结果可能很棘手)。

Try 尝试

if (feature.properties.per_dem * 100 > feature.properties.per_gop * 100)

or ( + are there to cast the strings in your geoJSON into numbers) 或( +可以将geoJSON中的字符串转换为数字)

if (+feature.properties.per_dem > +feature.properties.per_gop)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM