highcharts - Highcharts 将小数的 numberformat 设置为逗号
问题描述
我有一个人口金字塔的数据集,我使用 highcharts,但我对小数运算符有一些问题。Highcharts 接受分隔符为“。” 但是数据集有“,”所以在逗号之后它被视为一个新值。我的意思是,对于“5,12”,它不接受 5.12,它分别接受 5、12 两个值。我该如何解决这个问题?(我不能将“,”更改为“。”,因为它取自数据库)。
<script>
{% for item in dataset %}
// Age categories
var categories = [
'0-4', '5-9', '10-14', '15-19',
'20-24', '25-29', '30-34', '35-39', '40-44',
'45-49', '50-54', '55-59', '60-64', '65-69',
'70-74', '75-79', '80-84', '85-89', '90+'
];
Highcharts.chart('container', {
chart: {
type: 'bar'
},
title: {
text: 'POPULATION PYRAMID'
},
subtitle: {
text: 'Source: <a href="#">#</a>'
},
xAxis: [{
categories: categories,
reversed: false,
labels: {
step: 1
}
}, { // mirror axis on right side
opposite: true,
reversed: false,
categories: categories,
linkedTo: 0,
labels: {
step: 1
}
}],
yAxis: {
title: {
text: null
},
labels: {
formatter: function () {
return Math.abs(this.value) + '%';
}
}
},
plotOptions: {
series: {
stacking: 'normal'
}
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' +
'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y),
}
},
series: [{
name: 'Men',
data: [
-{{ item.men_population_0_4_percentage }},
-{{ item.men_population_5_9_percentage }},
-{{ item.men_population_10_14_percentage }},
-{{ item.men_population_15_19_percentage }},
-{{ item.men_population_20_24_percentage }},
-{{ item.men_population_25_29_percentage }},
-{{ item.men_population_30_34_percentage }},
-{{ item.men_population_35_39_percentage }},
-{{ item.men_population_40_44_percentage }},
-{{ item.men_population_45_49_percentage }},
-{{ item.men_population_50_54_percentage }},
-{{ item.men_population_55_59_percentage }},
-{{ item.men_population_60_64_percentage }},
-{{ item.men_population_65_69_percentage }},
-{{ item.men_population_70_74_percentage }},
-{{ item.men_population_75_79_percentage }},
-{{ item.men_population_80_84_percentage }},
-{{ item.men_population_85_89_percentage }},
-{{ item.men_population_90_over_percentage }},
]
}, {
name: 'Women',
data: [
{{ item.women_population_0_4_percentage }},
{{ item.women_population_5_9_percentage }},
{{ item.women_population_10_14_percentage }},
{{ item.women_population_15_19_percentage }},
{{ item.women_population_20_24_percentage }},
{{ item.women_population_25_29_percentage }},
{{ item.women_population_30_34_percentage }},
{{ item.women_population_35_39_percentage }},
{{ item.women_population_40_44_percentage }},
{{ item.women_population_45_49_percentage }},
{{ item.women_population_50_54_percentage }},
{{ item.women_population_55_59_percentage }},
{{ item.women_population_60_64_percentage }},
{{ item.women_population_65_69_percentage }},
{{ item.women_population_70_74_percentage }},
{{ item.women_population_75_79_percentage }},
{{ item.women_population_80_84_percentage }},
{{ item.women_population_85_89_percentage }},
{{ item.women_population_90_over_percentage }},
]
}]
});
{% endfor %}
</script>
当逗号设置为十进制分隔符时,值之间的逗号会出错,例如“{{ item.women_population_0_4_percentage }}, {{ item.women_population_5_9_percentage }}, ...”那么我该如何解决这个问题呢?
解决方案
您可以通过 JS 将逗号更改为点。例如:
var data = ['5,12', '10,4'];
data.forEach(function(el, i) {
data[i] = parseFloat(el.replace(/,/g, '.'))
});
Highcharts.chart('container', {
series: [{
data: data
}]
});
现场演示:http: //jsfiddle.net/BlackLabel/62b1vwje/
推荐阅读
- spring-boot - Spring Boot 应用程序无法连接到在 Docker 容器中运行的 MySQL 数据库
- laravel - PHPunit 在 Laravel 中忽略 onDelete 设置 null
- php - Laravel - 如何将作业从一个redis队列移动到另一个redis队列?
- python - Python父类init调用child的覆盖方法
- angular - Ionic 3 在第一次构建时出现错误
- python - TypeError:预期的 str、字节或 os.PathLike 对象,而不是 ImageFieldFile
- typescript - 从继承的类定义动态生成的属性
- python - Python Flask:将 Jinja 变量传递给后端
- apache-kafka - Google Cloud Dataflow 的反序列化 Avro
- react-native - 切换横屏时,界面装竖屏,怎么解决