Yan*_*aya 0 javascript variables global-variables chart.js
由于某种原因,我似乎在 Chart.JS 中遇到错误。它与您可以设置的全局选项有关。
var chart1 = document.getElementById("chart1").getContext("2d"),
chart2 = document.getElementById("chart2").getContext("2d"),
chart3 = document.getElementById("chart3").getContext("2d"),
datatest1 = document.getElementById("datatest1").value,
datatest2 = document.getElementById("datatest2").value,
color_bg = "#00b5e4",
color_fg = "#007799",
data1 = [{ value: Math.floor(Math.random() * 100), color: color_bg}, { value: Math.floor(Math.random() * 100), color: color_fg}],
data2 = [{ value: datatest1, color: color_bg}, { value: datatest2, color: color_fg}],
data3 = {
    labels: ["Jan", "Feb", "Mar"],
    datasets: [
        {
            fillColor: "rgba(220,220,220,0.5)",
            strokeColor: "rgba(220,220,220,0.8)",
            highlightFill: "rgba(220,220,220,0.75)",
            highlightStroke: "rgba(220,220,220,1)",
            data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
            fillColor: "rgba(151,187,205,0.5)",
            strokeColor: "rgba(151,187,205,0.8)",
            highlightFill: "rgba(151,187,205,0.75)",
            highlightStroke: "rgba(151,187,205,1)",
            data: [28, 48, 40, 19, 86, 27, 90]
        }
    ]
};
//
// #Global Chart Settings
var options = Chart.defaults.global = {
animation: true,
animationSteps: 160,
animationEasing: "easeOutQuart",
responsive: true,
showTooltips: true,
segmentShowStroke: false,
maintainAspectRatio: true,
percentageInnerCutout: 70,
onAnimationComplete: function () {
    "use strict";
    //console.log("Animation Done");
}
};
$(document).ready(function () {
"use strict";
//
// #Initialise and bind to data and global options
new Chart(chart1).Doughnut(data1, options);
new Chart(chart2).Doughnut(data2, options);
new Chart(chart3).Radar(data3);       
});
如果您从它们工作的图表中删除选项,如果您根据他们的文档添加选项并全局设置它们,则会出现我提到的错误。我错过了一些明显的东西还是这里有问题?
当你做
var options = Chart.defaults.global = {
   ...
您正在将 COMPLETE Chart 全局默认设置为您的对象。除非您的对象中有所有 Chart 全局选项,否则这将导致许多选项以undefined. 设置全局选项的正确方法是这样
Chart.defaults.global.animation = true;
Chart.defaults.global.animationSteps = 160;
...
即更改单个属性的值global而不是设置整个global属性。