2017-07-29 75 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

相關問題