简体   繁体   English

将angularjs货币绑定到chart.js中的scaleLabel

[英]bind angularjs currency to scaleLabel in chart.js

I'm working with Angularjs and Chartjs is there a way to bind a currency to the scaleLabel. 我正在使用Angularjs,Chartjs可以将货币绑定到scaleLabel。 In Html I would normally do 在HTML中,我通常会

{{value | currency }} 

This is the label from chartjs graph options 这是来自chartjs图形选项的标签

scaleLabel: "<%= '$' + value  %>"

Is there a way to do this instead of having the currency hard coded please? 有没有办法代替硬编码货币呢?

This is my HTML in which i'm calling controller and $scopes 这是我在其中调用控制器和$ scopes的HTML

<div ng-controller="ChartJSController" class="container-fluid">
<div class="row mb-lg">
    <div class="col-lg-12">
        <div>
            <canvas linechart="" options="lineOptions" data="rev" height="667" responsive="true"></canvas>
        </div>
    </div>
</div>

This is the graph data 这是图形数据

    $scope.revenueToday = {
    labels: ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00'],
    datasets: [
      {
          label: 'My Second dataset',
          fillColor: 'rgba(35,183,229,0.2)',
          strokeColor: 'rgba(35,183,229,1)',
          pointColor: 'rgba(35,183,229,1)',
          pointStrokeColor: '#fff',
          pointHighlightFill: '#fff',
          pointHighlightStroke: 'rgba(35,183,229,1)',
          data: log

      }
    ]
};

And this is where I am setting graph options 这是我设置图形选项的地方

    $scope.lineOptions = {
    scaleShowGridLines: true,
    scaleGridLineColor: 'rgba(0,0,0,.05)',
    scaleGridLineWidth: 1,
    bezierCurve: true,
    bezierCurveTension: 0.4,
    pointDot: true,
    pointDotRadius: 4,
    pointDotStrokeWidth: 1,
    pointHitDetectionRadius: 20,
    datasetStroke: true,
    datasetStrokeWidth: 2,
    datasetFill: true,
    scaleLabel: "<%= '$' + value  %>"

};

You can do this, however you'd have to tack your bit of code that does the formatting to some global object (which IMO is not very maintainable). 您可以执行此操作,但是您必须添加一些代码来对某些全局对象(IMO不太易于维护)进行格式化。 This is how you do it 这就是你的做法

myApp.controller('myCtrlr', ['$filter', '$scope', function ($filter, $scope) {
    Chart.currencify = function(value) {
        return $filter('currency')(value);
    }
    ...
    ...
    var myChart = new Chart(ctx).Line(data, {
        scaleLabel: "<%=Chart.currencify(value)%>",
    });
}]);

You could tack the currencify function to any global object (I've added it to the Chart js global object). 您可以将currencify函数附加到任何全局对象(我已将其添加到Chart js全局对象)。 All you have to make sure is that currencify is defined before you initialize the chart (so you could also do it in myApp.run ). 您只需要确保在初始化图表之前就定义了currencify (因此也可以在myApp.run )。


Fiddle - http://jsfiddle.net/fntcdtve/ 小提琴-http: //jsfiddle.net/fntcdtve/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM