Chart JS:使用工具提示模板中的函数

时间:2015-10-01 22:32:30

标签: javascript jquery chart.js

我有一系列代表金钱的数据,我需要工具提示来显示数据系列并格式化数据。我有一个工作函数,将“123456.78”转换为“123,456.78”,但我不能在工具提示中应用它。我的代码如下:

function formatMoney(n, c, d, t){
  var c = isNaN(c = Math.abs(c)) ? 2 : c,
      d = d == undefined ? "." : d,
      t = t == undefined ? "," : t,
      s = n < 0 ? "-" : "",
      i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
      j = (j = i.length) > 3 ? j % 3 : 0;
     return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
   };
  var lineChartData = {
        labels : ['January', 'Ferbruary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    datasets : [
      {
        label: "Value",
        fillColor : "rgba(32,78,158,0.2)",
        strokeColor : "rgba(32,78,158,1)",
        pointColor : "rgba(32,78,158,1)",
        pointStrokeColor : "#fff",
        pointHighlightFill : "#fff",
        pointHighlightStroke : "rgba(32,78,158,1)",
        data : ['20000','22000','35000','17000','18000','47500','20000','22000','35000','17000','18000','47500']
      },{
        label: "Budget",
        fillColor: "rgba(47,116,234,0.3)",
        strokeColor: "rgba(47,116,234,1)",
        pointColor: "rgba(47,116,234,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(47,116,234,1)",
        data : ['10000','20000','30000','7000','17000','40500','19000','20000','33000','12000','17500','40500']
    },
    ]
  }
  var ctx = document.getElementById("money-graph").getContext("2d");
  window.myLine = new Chart(ctx).Line(lineChartData, {
    responsive: true,
    maintainAspectRatio: true,
    multiTooltipTemplate: "<%= datasetLabel %> £<%= formatMoney(value,2) %>"
  });

我在控制台中收到的错误是ReferenceError: Can't find variable: formatMoney

我还尝试将函数定义为var formatMoney = function(n, c, d, t)

1 个答案:

答案 0 :(得分:1)

我正在回答这个问题,以便更容易找到以供将来参考。问题是模板引擎无法处理功能,或者是那种性质的东西。但是使用另一种模板方式似乎有效:

ICollection<>