简体   繁体   English

HighCharts未定义,因为Javascript文件中有多个Html页面

[英]HighCharts is undefined because multiple Html pages in Javascript file

I'm trying to get a div id called "container" like this: 我试图获得一个名为“容器”的div id,如下所示:

var chart = $(#container).highcharts() ; var chart = $(#container).highcharts() ;

It works perfectly when I call by a controller that is defined in my html page. 当我通过我的html页面中定义的控制器调用时,它完美地工作。 But When I try to call this function by another controller which is not referred by my Html page, it isn't working. 但是当我试图通过我的Html页面没有引用的另一个控制器调用此函数时,它无法正常工作。

SO, How can I call my div inside other controller which is associate in other html page? 那么,如何在其他html页面中关联的其他控制器中调用我的div?

Example: 例:

It works and is defined in my controller "main" which is defined in my html page "main.html" 它工作并在我的控制器“main”中定义,它在我的html页面“main.html”中定义

    Item.Rest("datePerUser",null).then(function(totalDatePerUser){

        var objectDate = totalDatePerUser.data;

        var femDate = objectDate.Chart1Fem;
        var mascDate = objectDate.Chart1Masc;    

        loadDataChart1(mascDate, femDate);            

    });

It isn't work and is defined in my controller "menu" which is defined in my html page "menu.html" 它不起作用,并在我的控制器“菜单”中定义,该菜单在我的html页面“menu.html”中定义

    PeriodItem.Rest("datePerUser_",Obj).then(function(datePerUserTeste){

                      var objectDate = datePerUserTeste.data;

                      newFemDate = objectDate.Chart1Fem;
                      newMaleDate = objectDate.Chart1Masc;    

                      loadDataChart1(newMaleDate, newFemDate);

                  }); 

A both call the follow function 两者都调用了跟随功能

function loadDataChart1(dataMale, dataFem){

    var chartProfiles = $('#container').highcharts();

    obj_female =  JSON.parse('{ "data":' + dataFem + ' }');
    obj_male =  JSON.parse('{ "data":' + dataMale + ' }');

    chartProfiles.series[0].update(obj_female);    
    chartProfiles.series[1].update(obj_male);

} 

Just to information, I'm using angularJS with Rest Service, to get data from DB, and I have multiple Html pages. 仅仅是为了获取信息,我正在使用angularJS和Rest Service,从数据库获取数据,我有多个Html页面。

A super simple option for your case is a 'common' controller with 'loadDataChart1' functionality. 对于您的案例,一个超级简单的选项是具有'loadDataChart1'功能的'通用'控制器。

 //Step 1 app.controller('CommonChartsController', function($scope) { $scope.createCharts = function (dataMale, dataFem) { var chartProfiles = $('#container').highcharts(); obj_female = JSON.parse('{ "data":' + dataFem + ' }'); obj_male = JSON.parse('{ "data":' + dataMale + ' }'); chartProfiles.series[0].update(obj_female); chartProfiles.series[1].update(obj_male); }; } //Step 2 app.controller('MainController', function($scope, $controller) { $controller('CommonChartsController', { $scope: $scope }); Item.Rest("datePerUser",null).then(function(totalDatePerUser){ var objectDate = totalDatePerUser.data; var femDate = objectDate.Chart1Fem; var mascDate = objectDate.Chart1Masc; $scope.createCharts(mascDate, femDate); }); } //Step 3 app.controller('MenuController', function($scope, $controller) { $controller('CommonChartsController', { $scope: $scope }); PeriodItem.Rest("datePerUser_",Obj).then(function(datePerUserTeste){ var objectDate = datePerUserTeste.data; newFemDate = objectDate.Chart1Fem; newMaleDate = objectDate.Chart1Masc; $scope.createCharts(newMaleDate, newFemDate); }); } 

You can create a directive and use that directive in your htmls..sample code provided here 您可以在此处提供的htmls..sample代码中创建指令并使用该指令

Directive: 指示:

  angularAMD.directive('graphDirective', function () { return { restrict: 'E', template: '<div></div>', scope: { chartData: "=value", chartObj: "=?", page: "=", }, transclude: true, replace: true, link: function ($scope, $element, $attrs) { // Update when charts data changes $scope.$watch('chartData', function (value) { if (!value) { return; } var graphData = { chart: { type: 'area', marginLeft: 65, marginRight: 1, marginBottom: 40, plotBackgroundColor: '#FFF', backgroundColor: 'transparent', plotBorderWidth: 1, plotBorderColor: '#CCC', animation: false, renderTo: $element[0] }, legend: { enabled: true, align: 'right', verticalAlign: 'top', layout: 'horizontal', backgroundColor: '#FFF', borderColor: '#FFF', borderWidth: 1, symbolHeight: 20, symbolWidth:20 }, title: { text: '' }, credits: { enabled: false }, xAxis: { min: 0, categories: [], title: { margin: 0, }, tickInterval: 1, tickmarkPlacement: 'on', gridLineWidth: 1, gridLineColor: '#bbb', alternateGridColor: '#FFF', gridZIndex: 1, startOnTick: true, endOnTick: false, minPadding: 0, maxPadding: 0, lineWidth: 1 }, yAxis: { // min: 0, title: { margin: 10, text: 'Target' }, labels: { formatter: function () { return this.value; } }, gridLineWidth: 1, gridLineColor: '#ddd', lineWidth: 1, tickInterval: 10 }, plotOptions: { series: { states: { hover: '' }, }, area: { //pointStart: 5, marker: { enabled: true, symbol: 'circle', radius: 4, lineWidth: 2, lineColor: '#FFF', states: { hover: { enabled: true } } } } }, series: [ { animation: false, fillOpacity: 0.4, name: 'Actual', color: "#5F8605", data: [], connectNulls: true }, { animation: false, fillOpacity: 0.4, name: 'Strech Goal', color: "#61DDD3", data: [], connectNulls: true }, { animation: false, fillOpacity: 0.4, name: 'Goal', color: "#31ABEA", data: [], connectNulls: true } ] }; graphData.series[0].data = $scope.chartData.series[0].data; graphData.series[2].data = $scope.chartData.series[2].data; graphData.series[1].data = $scope.chartData.series[1].data; graphData.yAxis.min = $scope.chartData.yAxis.min; graphData.yAxis.max = $scope.chartData.yAxis.max; graphData.xAxis.categories = $scope.chartData.xAxis.categories; graphData.yAxis.title.align = 'high'; graphData.yAxis.title.offset = -15; graphData.yAxis.title.rotation = 0; graphData.yAxis.title.y = -10; graphData.xAxis.min = graphData.xAxis.min; graphData.xAxis.max = $scope.chartData.xAxis.max; graphData.xAxis.labels = { formatter: function () {...} }; graphData.tooltip = { formatter: function () {..} }; $scope.chartObj = new Highcharts.Chart(graphData); } }, true); } }; }); In HTML: 

angularAMD.directive('graphDirective', function () { return { restrict: 'E', template: '<div></div>', scope: { chartData: "=value", chartObj: "=?", page: "=", }, transclude: true, replace: true, link: function ($scope, $element, $attrs) { // Update when charts data changes $scope.$watch('chartData', function (value) { if (!value) { return; } var graphData = { chart: { type: 'area', marginLeft: 65, marginRight: 1, marginBottom: 40, plotBackgroundColor: '#FFF', backgroundColor: 'transparent', plotBorderWidth: 1, plotBorderColor: '#CCC', animation: false, renderTo: $element[0] }, legend: { enabled: true, align: 'right', verticalAlign: 'top', layout: 'horizontal', backgroundColor: '#FFF', borderColor: '#FFF', borderWidth: 1, symbolHeight: 20, symbolWidth:20 }, title: { text: '' }, credits: { enabled: false }, xAxis: { min: 0, categories: [], title: { margin: 0, }, tickInterval: 1, tickmarkPlacement: 'on', gridLineWidth: 1, gridLineColor: '#bbb', alternateGridColor: '#FFF', gridZIndex: 1, startOnTick: true, endOnTick: false, minPadding: 0, maxPadding: 0, lineWidth: 1 }, yAxis: { // min: 0, title: { margin: 10, text: 'Target' }, labels: { formatter: function () { return this.value; } }, gridLineWidth: 1, gridLineColor: '#ddd', lineWidth: 1, tickInterval: 10 }, plotOptions: { series: { states: { hover: '' }, }, area: { //pointStart: 5, marker: { enabled: true, symbol: 'circle', radius: 4, lineWidth: 2, lineColor: '#FFF', states: { hover: { enabled: true } } } } }, series: [ { animation: false, fillOpacity: 0.4, name: 'Actual', color: "#5F8605", data: [], connectNulls: true }, { animation: false, fillOpacity: 0.4, name: 'Strech Goal', color: "#61DDD3", data: [], connectNulls: true }, { animation: false, fillOpacity: 0.4, name: 'Goal', color: "#31ABEA", data: [], connectNulls: true } ] }; graphData.series[0].data = $scope.chartData.series[0].data; graphData.series[2].data = $scope.chartData.series[2].data; graphData.series[1].data = $scope.chartData.series[1].data; graphData.yAxis.min = $scope.chartData.yAxis.min; graphData.yAxis.max = $scope.chartData.yAxis.max; graphData.xAxis.categories = $scope.chartData.xAxis.categories; graphData.yAxis.title.align = 'high'; graphData.yAxis.title.offset = -15; graphData.yAxis.title.rotation = 0; graphData.yAxis.title.y = -10; graphData.xAxis.min = graphData.xAxis.min; graphData.xAxis.max = $scope.chartData.xAxis.max; graphData.xAxis.labels = { formatter: function () {...} }; graphData.tooltip = { formatter: function () {..} }; $scope.chartObj = new Highcharts.Chart(graphData); } }, true); } }; }); In HTML:

 <graph-directive class="cls" page="ctrl.graphPlottingPage" value='ctrl.graphData'></graph-directive > 

each graphData you can set via your controllers 您可以通过控制器设置每个graphData

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

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