简体   繁体   English

Chart.js没有显示在我的视图中

[英]Chart.js not showing in my view

I have an app running with angular.js and one of my view should load an chart. 我有一个运行angular.js的应用程序,我的一个视图应该加载图表。 I'm using chart.js for it, but for some reason it's not working and no error is shown on my console. 我正在使用chart.js,但由于某种原因,它不起作用,我的控制台上没有显示错误。 Here is my function that creates the chart: 这是我创建图表的函数:

$scope.writeBatteryChart = function(){
        console.log("Function was called");
        var data = {
            labels: ["10:30am", "11:00am", "11:30am", "12:00pm", "12:30pm", "01:00pm", "01:30pm"],
            datasets: [
                {
                    label: "Battery",
                    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]
                }
            ]
        };

        var options = {
            responsive: true,
            bezierCurve : false
        }

        var ctx = document.getElementById("myChart").getContext("2d");
        var myLineChart = new Chart(ctx).Line(data, options);

    }

Here's the HTML where the chart should appear: 这是图表应该出现的HTML:

               <div class="row">
                    <div class="col-md-8 col-sm-8 col-xs-8" style="padding-left: 0px;">
                        <div class="panel panel-info">
                          <div class="panel-heading">
                            <h3 class="panel-title">Battery <i class="fi-battery-half"></i></h3>
                          </div>
                          <div class="panel-body">
                            <canvas id="myChart" height="300"></canvas>
                          </div>
                        </div>
                    </div>
                    <div class="col-md-4 col-sm-4 col-xs-4" style="padding-left: 0px; padding-right: 0px;">

                    </div>
                </div>

and I'm also loading the latest version of the Chart.js: 我还加载了最新版本的Chart.js:

    <!--Chart plugin -->
    <script src="bower_components/Chart.js/Chart.js" language="javascript"></script>

One thing that I noticed is that the canvas is being rendered with 0 dimensions: 我注意到的一件事是画布正在渲染0维:

画布上有0个尺寸

Does someone can see where I'm doing something wrong? 有人能看到我做错了什么吗?

问题:我在画布加载到视图之前调用了函数,所以我使用setTimeout()来延迟调用writeBatteryChart()以等待画布呈现。

You have to add your rendered things inside of canvas. 您必须在画布中添加渲染的内容。 Hope it will help Link 希望它能帮到Link

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

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