gpt4 book ai didi

javascript - 来自 geojson 的传单格式值

转载 作者:行者123 更新时间:2023-12-03 09:27:22 24 4
gpt4 key购买 nike

有没有一种方法可以格式化 geojson 中包含的不同区域设置的值,就像它们在 Leaflet 中显示的那样?

在我正在开发的 Web 应用程序中,所有数据都包含在 geojson 中。我必须使用不同的语言版本,这也需要不同的数字和货币格式。这些值显示在弹出窗口、图例和 D3 图表中。在图表中,我可以使用 D3 中的格式化程序函数来获取正确的区域设置:逗号分隔的小数。

如何使用 JavaScript 和 geojson 中的传单将值显示为逗号分隔的小数以及不同格式的货币?

有什么建议吗?

编辑:Matthew Bloch 建议使用 d3.locale 来格式化客户端上的值。这很有道理。我会尝试找一个例子。 globalize.js 可以是一个解决方案,还是只是一个 JavaScript 函数?

这里是示例区域和代码: jsfiddle

示例代码:

var onEachFeature_LMA = function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature
});

var Popup = feature.properties.AreaName + '</b><br />No diploma: ' +
feature.properties.NoDipTot + '%' +
'<br />High school: ' +
feature.properties.HSTot + '%' +
'<br />College: ' +
feature.properties.ColTot + '%' +
'<br />University: ' +
feature.properties.UnTot + '%';
layer.bindPopup(Popup);
}

编辑2:

我认为这只是一个快速补充,对于研究这个问题的人们来说可能会很有趣。提供的答案有效,但我决定按照 Matthew 的建议使用 D3 进行本地化。使用 D3,我可以格式化我可能需要的所有内容:日期、货币、数字等。

定义要使用的区域设置,然后使用 numberFormat 或 timeFormat 等。它与当前的 D3 动态相关,不需要像以前那样重新编译。

var enCA = d3.locale({
decimal: ".",
thousands: ",",
grouping: [3],
currency: ["$", ""],
dateTime: "%a %b %e %X %Y",
date: "%Y-%m-%d",
time: "%H:%M:%S",
periods: ["AM", "PM"],
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
shortDays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
});

var format = enCA.numberFormat("n");
format(3,4) //3.4
format(10000) //10,000

最佳答案

如果这不符合您的要求,请纠正我,但是只需使用简单的字符串替换将点更改为逗号,如下所示:

//globally replace '.' in oldString by ',' and save it to formattedString string
formattedString = oldString.replace(/\./g, ',');

参见this fiddle .

您还可以扩展该函数以将位置作为输入,并根据 switch 语句格式化字符串。

关于javascript - 来自 geojson 的传单格式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31628656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com