使用AngularJS更改呈现HighChart的指令

时间:2017-07-10 21:46:54

标签: angularjs highcharts angularjs-directive

我使用Angular Directives渲染Highcharts视觉效果,并尝试更改基于用户输入使用的指令。进行测试,尝试在线图和柱形图之间切换。 指令是:

myApp.directive('columnChart', function () {
return function (scope, element, attrs) {
    initChart();
    function initChart() {
        var chartConfig = {
            chart: {
                type: 'column',
                zoomType: 'xy'
            },
            title: {
                text: ''
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: [{
                min: 0,
                title: {
                    text: 'Y Axis Label'
                    },
            }],
                series: [{
                    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
                }]
        };
        var chart = $(element).highcharts(chartConfig);
    }
};
});

myApp.directive('lineChart', function () {
return function (scope, element, attrs) {
     initChart();

 function initChart() {
        var chartConfig = {
            chart: {
                type: 'line',
                zoomType: 'xy'
            },
            title: {
                text: ''
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: [{
                min: 0,
                //max:200,
                title: {
                    text: 'Y Axis Label'
                    },
            }],
                series: [{
                    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
                }]
        };
        var chart = $(element).highcharts(chartConfig);
    }
};
});

我有一个简单的测试页面,其中包含可更改指令的按钮:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div ng-controller="MyCtrl">
    <button ng-click="setLineChart()">Set Line Chart</button>
    <button ng-click="setColumnChart()">Set Column Chart</button>
    <div id="container" column-Chart style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</div>

以及设置指令的代码:

$scope.setLineChart = function() {
    var docElement = document.getElementById("container");
    var scope = angular.element(docElement).scope();
    var element = angular.element(docElement);
    element.html('<div id="chartContainer" line-Chart style="min-width: 400px; height: 400px; margin: 0 auto"></div>');
    console.log("element: " , element);
    $compile(element)(scope);
};

单击“设置折线图”按钮将导致调用折线图的指令,但图表仍然以折线图的形式解析。

这是jsfiddle:https://jsfiddle.net/MFRTest/xsadzv4f/12/

我很欣赏任何我缺少的东西

1 个答案:

答案 0 :(得分:0)

使用AngularJS框架,避免在控制器中操纵DOM。框架separates concerns使应用程序更易于理解,调试,测试和维护。

取而代之的是根据Controller提供的模型创建highcharts的指令:

app.directive("highchart", function() {
  return {
    link: postLink
  }
  function postLink(scope, elem, attrs) {
    scope.$watch(attrs.highchart, function (chartConfig) {
      elem.highcharts(chartConfig);
    }, true)
  }
})

用法:

<button ng-click="$ctrl.chart = $ctrl.lineChart">Set Line Chart</button>
<button ng-click="$ctrl.chart = $ctrl.colChart">Set Column Chart</button>

<div highchart="$ctrl.chart" 
     style="min-width: 300px; height: 300px; margin: 0 auto">
</div>

The DEMO

angular.module("app",[])

.directive("highchart", function() {
  return {
    link: postLink
  }
  function postLink(scope, elem, attrs) {
    scope.$watch(attrs.highchart, function (chartConfig) {
      elem.highcharts(chartConfig);
    })
  }
})

.controller("ctrl", function() {
  this.colChart = {
        chart: {
          type: 'column',
          zoomType: 'xy'
        },
        title: {
          text: ''
        },
        xAxis: {
          categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
          ]
        },
        yAxis: [{
          min: 0,
          //max:200,
          title: {
            text: 'Y Axis Label'
          },
        }],
        series: [{
          data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
        }]
      };
  this.lineChart = {
            chart: {
                type: 'line',
                zoomType: 'xy'
            },
            title: {
                text: ''
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: [{
                min: 0,
                //max:200,
                title: {
                    text: 'Y Axis Label'
                    },
            }],
                series: [{
                    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
                }]
        };
})
    <script src="//unpkg.com/jquery"></script>
    <script src="//unpkg.com/highcharts"></script>
    <script src="//unpkg.com/angular/angular.js"></script>
    
  <body ng-app="app" ng-controller="ctrl as $ctrl">
    <h3>Highchart Directive DEMO</h3>
    <button ng-click="$ctrl.chart = $ctrl.lineChart">
      Set Line Chart
    </button>
    <button ng-click="$ctrl.chart = $ctrl.colChart">
      Set Column Chart
    </button>
    <div highchart="$ctrl.chart" 
         style="min-width: 300px; height: 180px; margin: 0 auto">
    </div>
  </body>