首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改使用HighChart呈现AngularJS的指令

更改使用HighChart呈现AngularJS的指令
EN

Stack Overflow用户
提问于 2017-07-10 21:46:53
回答 1查看 403关注 0票数 0

我正在使用角指令渲染高图表可视化,并试图改变使用的指令,根据用户的输入。用于测试,尝试在行图和列图之间切换。这些指令是:

代码语言:javascript
复制
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);
    }
};
});

代码语言:javascript
复制
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);
    }
};
});

我有一个简单的测试页面,它的按钮可以更改指令:

代码语言:javascript
复制
<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>

以及设置指令的代码:

代码语言:javascript
复制
$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/

我很感激任何对我所缺少的东西的洞察力

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-11 03:13:21

使用AngularJS框架,避免在控制器中操作DOM。框架分离关注点使应用程序更容易理解、调试、测试和维护。

相反,有一个基于Controller提供的模型创建高图集的指令:

代码语言:javascript
复制
app.directive("highchart", function() {
  return {
    link: postLink
  }
  function postLink(scope, elem, attrs) {
    scope.$watch(attrs.highchart, function (chartConfig) {
      elem.highcharts(chartConfig);
    }, true)
  }
})

用法:

代码语言:javascript
复制
<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>

演示

代码语言:javascript
复制
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]
                }]
        };
})
代码语言:javascript
复制
    <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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45022163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档