2017-07-29 78 views
0

我用Chart.js创建了一些图表,它们在我的网站上[http://projetoplenario.com/proposicoes.html]。我试图在我的网站的其他html页面插入相同的图表,但它不起作用。为什么我的圆环图不是用Chart.js创建的?

例如,我们考虑第一个proposicoes.html图表。它被命名为“reformaTrabalhistaChart”。在reformaTrabalhista.html [http://projetoplenario.com/reformaTrabalhista.html]也有代码来插入图表。

但是,我不知道为什么,它不能在reformaTrabalhista.html上工作,并且对proposicoes.html起作用。

如何在reformaTrabalhista.html上插入相同的图表?

<div class="boxChart"> 
 
    <a href="reformaTrabalhista.html"> 
 
    <canvas id="reformaTrabalhistaChart"></canvas> 
 
    </a> 
 
</div>

<div class="boxChart"> 
 
    <canvas id="reformaTrabalhistaChart"></canvas> 
 
</div>

var reformaTrabalhistaChart; 
 
var data = [ 
 
    { 
 
    value: 50, 
 
    color: "green" 
 
    }, { 
 
    value: 26, 
 
    color: "red" 
 
    }, { 
 
    value: 1, 
 
    color: "orange" 
 
    }, { 
 
    value: 1, 
 
    color: "purple" 
 
    }, { 
 
    value: 3, 
 
    color: "gray" 
 
    } 
 
]; 
 

 
var options = { 
 
    animation: true, 
 
    animationEasing: 'easeInOutQuart', 
 
    animationSteps: 80 
 
}; 
 

 
//Get the context of the canvas element we want to select 
 
var ctx = document.getElementById("reformaTrabalhistaChart") 
 
        .getContext("2d"); 
 

 
reformaTrabalhistaChart = new Chart(ctx).Doughnut(data, options);

+0

你的代码在哪里? – hurricane

+0

我刚才包括在内 – agccaesar

回答

0

这是因为,你试图让其他画布元素,这是不存在于reformaTrabalhista.html

你应该更好地得到帆布背景下,所有的画布,就像这样:

var canvas = document.getElementById("canvas-id-here"); 
var ctx = canvas && canvas.getContext("2d"); 

和定义图表实例,因为这样的:

chart-variable-name = ctx && new Chart(ctx).Doughnut(data, options); 

下面是修改版本mycharts.js - https://kopy.io/0HiRj

相关问题