热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

图表饼图中的千位分隔符

如何解决《图表饼图中的千位分隔符》经验,为你挑选了1个好方法。

我正在使用Chart.js绘制饼图.

画我用的图

var ctx = document.getElementById("myChart").getContext('2d');
      var myChart = new Chart(ctx, {
        type: 'pie',
        data: {
          labels: #{raw @labels.to_json},
          datasets: [{
          backgroundColor: #{raw @colors.to_json},
            data: #{@followers}
          }]
        }
      });

澄清这个数据的事情是这样的

data: {
      labels: ["FAISAL","muhammadfaisali","faisaliqbal3699"],
      datasets: [{
      backgroundColor: ["#00b638","#efaa30","#50c8ea"],
        data: [500000, 75000, 100000]
      }]
    }

我需要将这些显示data: [500000, 75000, 100000]为千分隔符["500,000", "75,000", "100,000"]

我尝试了不同的

包括写这种方法的东西

function separator(numbers)
      { data = []
        for (i = 0; i 

并试图像这样使用它

data: separator(#{@followers})

它按我的要求格式化数据但是给出了错误 Cannot read property 'custom' of undefined

在这里以千位分隔符显示数据的方法是什么

现在就这样表现出来



1> jordanwillis..:

要在chart.js中执行此操作,您需要使用tooltips.callbacks.labelcallback属性.从此回调返回的值是用于生成工具提示文本的值.

以下是使用工具提示回调配置的图表,该回调使用数据值的本地字符串表示形式.

var ctx = document.getElementById("chart-area").getContext("2d");
var myPie = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: ["FAISAL","muhammadfaisali","faisaliqbal3699"],
    datasets: [{
      backgroundColor: ["#00b638","#efaa30","#50c8ea"],
      data: [500000, 75000, 100000]
    }],
  },
  options: {
    title: {
      display: true,
      text: 'Employee Overview',
      fontStyle: 'bold',
      fontSize: 20
    },
    tooltips: {
      callbacks: {
        // this callback is used to create the tooltip label
        label: function(tooltipItem, data) {
          // get the data label and data value to display
          // convert the data value to local string so it uses a comma seperated number
          var dataLabel = data.labels[tooltipItem.index];
          var value = ': ' + data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index].toLocaleString();

          // make this isn't a multi-line label (e.g. [["label 1 - line 1, "line 2, ], [etc...]])
          if (Chart.helpers.isArray(dataLabel)) {
            // show value on first line of multiline label
            // need to clone because we are changing the value
            dataLabel = dataLabel.slice();
            dataLabel[0] += value;
          } else {
            dataLabel += value;
          }

          // return the text to display on the tooltip
          return dataLabel;
        }
      }
    }
  }
});

您可以在此codepen中看到它的运行情况.


推荐阅读
author-avatar
蕶ok薍
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有