Highchart删除小数位

时间:2018-09-08 15:15:46

标签: highcharts

我正在使用图表图表,并通过中心显示数据。

function(chart1) { // on complete
  var xpos = '50%';
  var ypos = '50%';
  var circleradius = 40;

  // Render the circle
  chart1.renderer.circle(xpos, ypos, circleradius).attr({
    fill: '#fff',
  }).add();

  // Render the text 
  chart1.renderer.text(chart1.series[0].data[0].percentage + '%', 62, 80).css({
    width: circleradius * 2,
    color: '#4572A7',
    fontSize: '16px',
    textAlign: 'center'
  }).attr({
    // why doesn't zIndex get the text in front of the chart?
    zIndex: 999
  }).add();
});

但是我想防止显示任何小数位,目前为33.33333%。我想显示为33%

有可能吗,我知道有一个设置allowDecimals,但这没用。

1 个答案:

答案 0 :(得分:1)

您从点获得原始百分比值,该值未四舍五入。使用JS Math.round函数:

chart1.renderer.text(Math.round(chart1.series[0].data[0].percentage) + '%', 62, 80)
    .css({
        width: circleradius * 2,
        color: '#4572A7',
        fontSize: '16px',
        textAlign: 'center'
    })
    .attr({
        zIndex: 999
    })
    .add();

实时演示:https://jsfiddle.net/BlackLabel/0z4hLbaw/

相关问题