简体   繁体   English

需要帮助将Chart.js图表​​添加到模态窗口(我正在使用AngularJS)

[英]Need help adding Chart.js chart to a Modal window (I'm Using AngularJS)

I'm new to most things web development related. 我是与Web开发相关的大多数事物的新手。 I've been building a prototype website for the past couple of weeks using AngularJS. 在过去的两周中,我一直在使用AngularJS建立一个原型网站。 Due to my flawed understanding of how angular works I was redirecting to certain webpages and so breaking the app; 由于我对angular的工作方式了解不足,因此我将重定向到某些网页,从而破坏了该应用程序; at the time this didn't matter as it was just a quick prototype to get some ideas down. 当时,这并不重要,因为这只是一个快速的原型,可以将一些想法记下来。
Now however, I want to build something more realistic, and so the app must run on the one page. 但是,现在,我想构建更实际的东西,因此该应用程序必须在一页上运行。 Instead of the redirects I was using in the original prototype I've decided to implement modals to display the data within the app. 我决定使用模式来在应用程序中显示数据,而不是在原始原型中使用重定向。 All has been going well so far, I can click on an item in the list and a modal window opens and displays the correct data as related to the item that was selected, except that is, for the graphs. 到目前为止,一切进展顺利,我可以单击列表中的一个项目,然后会打开一个模态窗口,并显示与所选项目相关的正确数据,但对于图形除外。

I'm not sure how I should implement the graphs in the model, or to be more accurate, how I can select the canvas element to add the graph to it. 我不确定如何在模型中实现图形,或更确切地说,如何选择canvas元素向其中添加图形,我不确定。

I'm using a templateURL for the modal, and a controller to handle the modal functionality. 我正在使用templateURL作为模态,并使用一个控制器来处理模态功能。

Since my knowledge of javascript is dodgy at best, I've spend most of today going over the basics of Javascript and JQuery (I've been working with Angular for a couple of weeks now). 由于我对javascript的知识充其量是狡猾的,所以我今天大部分时间都在研究Javascript和JQuery的基础知识(我已经与Angular一起工作了几周了)。

On my static pages I use the charts as described in the Chart.js documentation. 在我的静态页面上,我按照Chart.js文档中的描述使用图表。

<script>
                        var data = {
                            labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
                            datasets: [
                                {
                                    label: "My First dataset",
                                    fillColor: "rgba(220,220,220,0.2)",
                                    strokeColor: "rgba(220,220,220,1)",
                                    pointColor: "rgba(220,220,220,1)",
                                    pointStrokeColor: "#fff",
                                    pointHighlightFill: "#fff",
                                    pointHighlightStroke: "rgba(220,220,220,1)",
                                    data: [65, 59, 80, 81, 56, 55, 40, 80, 81, 56, 55, 40]
                                },
                                {
                                    label: "My Second dataset",
                                    fillColor: "rgba(151,187,205,0.2)",
                                    strokeColor: "rgba(151,187,205,1)",
                                    pointColor: "rgba(151,187,205,1)",
                                    pointStrokeColor: "#fff",
                                    pointHighlightFill: "#fff",
                                    pointHighlightStroke: "rgba(151,187,205,1)",
                                    data: [28, 48, 40, 19, 86, 27, 90, 86, 27, 90, 90, 2]
                                }
                            ]

                        }
                        var ctx = document.getElementById("myChart").getContext("2d");
                        var options = {responsive: true};
                        var myNewChart = new Chart(ctx).Line(data, options); 
</script>

This works fine when it's added to a html page that contains the <canvas> element, but it doesn't work if I include it in the modal template. 将其添加到包含<canvas>元素的html页面中时,此方法工作正常,但如果我将其包含在模式模板中,则该方法不起作用。 Here's my modal template (without the javascript): 这是我的模式模板(没有javascript):

<div id="deviceModal">
    <div class="modal-header">
        <div class="divBar" >
            <div class="{{device.status}}"><img src="{{(device.type == 'type1') ? 'img/type1white.png' : 'img/type2white.png'}}"> </div>
            <div class="title">
                <h3>{{device.id}}</h3><br />

            </div>
            <div class="{{device.status}}Text"><b>STATUS: {{device.status}}</b> - {{device.action}}</div>
        </div> 
        <div class="divBar">
            <table>
                <tr><td class="dataLabel">Location: </td><td class="dataContent">{{device.location}}</td></tr>
                <tr><td class="dataLabel">Running Time: </td><td class="dataContent">3 days</td></tr>
                <tr><td class="dataLabel">Number of Starts: </td><td class="dataContent">60</td></tr>
            </table>
        </div>
        <div class="divBar deviceStatistics">
            <canvas id="myChart" width="400" height="200"></canvas>
        </div>
    </div>
</div>

And here's the modal controller built with Angular: 这是用Angular构建的模态控制器:

app.controller('modalController', function ($scope, $modal) {

    $scope.open = function (device) {
        $scope.device = device;
        var modalInstance = $modal.open({
            templateUrl: 'add_modal.html',
            windowClass: 'app-modal-window',
            scope: $scope
        });

        console.log('modal opened');

        modalInstance.result.then(function () {

            console.log('you can check user selections here');

        }, function () {

            console.log('Modal dismissed at: ' + new Date());

        });

    };
});

Is it possible for me to add the chart and data in the Modal controller? 我可以在Modal控制器中添加图表和数据吗? or by using a directive maybe? 还是通过使用指令? Eventually the graphs will be displaying data that's queried from a database when the user selects an item that opens the modal window. 最终,当用户选择打开模式窗口的项目时,图形将显示从数据库查询的数据。

I've been looking at using javascript and JQuery to add the chart and data, but they require the element to be available on the page at the time of execution and that's not the case here. 我一直在使用javascript和JQuery添加图表和数据,但是它们要求元素在执行时在页面上可用,而事实并非如此。 I've tried adding javascript to the templateURL but that doesn't work either. 我尝试将javascript添加到templateURL,但这也不起作用。 Also, I'd imagine I should be able to do this the Angular way, can anyone suggest how that might be possible? 另外,我想我应该能够以Angular的方式做到这一点,有人可以建议这怎么可能吗?

With my answer I assume the modal is open correctly. 根据我的回答,我认为模态已正确打开。

We need to have 2 controllers: 我们需要2个控制器:

  1. Hold the $modal service and control on opening the modal (We'll use your with rename) 持有$ modal服务并控制打开模式(我们将使用您的带有更名的名称)

 app.controller('myCtrl', function ($scope, $modal) { $scope.open = function (device) { $scope.device = device; var modalInstance = $modal.open({ templateUrl: 'add_modal.html', controller: 'myCtrl' windowClass: 'app-modal-window', }); modalInstance.result.then(function () { console.log('you can check user selections here'); }, function () { console.log('Modal dismissed at: ' + new Date()); }); }; }); 

  1. A controller who control's the modal DOM when it opens 打开时控制模态DOM的控制器

 app.controller('modalCtrl', function($scope) { $scope.myData = [/*your data array*/]; $scope.myLabels = [/*your labels array*/]; }); 

And your canvas whitin the modal is defined using the $scope's vars 然后使用$ scope的vars定义模态的画布

<canvas class="chart chart-line" data="myData" labels="myLabels"></canvas>

PS - please pay attention to where I 'open' the modal I declare a controller for it PS-请注意在“打开”模态的地方,我为此声明了一个控制器

it will be in your best interest to create a directive for this chart, and that way you can also use it anywhere you like. 为该图表创建指令最符合您的利益,这样您就可以在任何喜欢的地方使用它。

https://github.com/miganga/angularMeetupTwo/tree/master/app https://github.com/miganga/angularMeetupTwo/tree/master/app

I did something similar above. 我在上面做了类似的事情。

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

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