JacobsMeng 2019-06-07 00:38 采纳率: 50%
浏览 417

#Leaflet#用Chart.js在JavaScript生成的canvas里面绘制饼图 获取ElementID后出错

一个基于Leaflet开发的小小小项目

问题大概只这样的:
首先是我用L.control()生成一个控件(info),通过.update()方法对控件进行更新。在.update()中我创建了一个新的canvas元素,之后通过document.getElementById()获取该元素,并使用Chart.js库创建一个饼图。在调用Chart.js库的时候就报错了(TypeError: item is null)。

相关资源:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js"></script>

<style>
    #map{ height: 70% }
    .info {
            padding: 16px 10px;
            font: 14px/16px Arial, Helvetica, sans-serif;
            background: white;
            background: rgba(255,255,255,0.8);
            box-shadow: 0 0 15px rgba(0,0,0,0.2);
            border-radius: 5px;
        } 
    .info h4 {
            margin: 0 0 5px;
            color: 'white';
        }

  </style>
报错内容如下:
TypeError: item is nullChart.bundle.js:6904:14
acquireContext https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js:6904
construct https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js:8423
Chart https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js:8410
update http://127.0.0.1:5500/static/js/load_tiff.js:85
onAdd http://127.0.0.1:5500/static/js/load_tiff.js:76
addTo http://127.0.0.1:5500/static/js/leaflet/leaflet.js:5
<anonymous> http://127.0.0.1:5500/static/js/load_tiff.js:90
相关代码如下:
var randomScalingFactor = function() {
        return Math.round(Math.random() * 100);
};

var config = {
        type: 'pie',
        data: {
                datasets: [{
                        data: [
                                randomScalingFactor(),
                                randomScalingFactor(),
                                randomScalingFactor(),
                                randomScalingFactor(),
                                randomScalingFactor(),
                        ],
                        backgroundColor: [
                                "rgba(230, 14, 14, 1)",
                                "rgba(230, 122, 14, 1)",
                                "rgba(230, 230, 14, 1)",
                                "rgba(122, 230, 14, 1)",
                                "rgba(14, 122, 230, 1)",

                        ],
                        label: 'Dataset 1'
                }],
                labels: [
                        'Red',
                        'Orange',
                        'Yellow',
                        'Green',
                        'Blue'
                ]
        },
        options: {
                responsive: true
        }
};

var info = L.control();

info.onAdd = function(mymap) {
        this._div = L.DomUtil.create('div', 'info');
        this.update();
        return this._div;
};


info.update = function() {
        var content = '<h4>Statistic Info</h4>' + '<br />';
        content += '<canvas id="myChart" width="10" height="10"></canvas>';
        this._div.innerHTML = content;
        var piechart = new Chart(document.getElementById('myChart'),config)
};

info.addTo(mymap);
  • 写回答

1条回答 默认 最新

  • kotoriyou 2019-06-09 12:10
    关注

    你应该把你的html代码放出来,只有css和js看不出来什么

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)