可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
hi i was trying to use chartjs
can be found in this link www.chartjs.org
i tried to draw two chart in the same page using the samples code
i created two different div with two different ids
like this
<div id="chart1"></div>
<div id="chart2"></div>
then after including this line :
i created the first chart this way:
window.onload = function(){
var ctx1 = document.getElementById("chart1").getContext("2d");
window.myLine = new Chart(ctx1).Line(lineChartData, {
responsive: true
});
}
and the second chart this way :
window.onload = function(){
var ctx2 = document.getElementById("chart2").getContext("2d");
window.myPie = new Chart(ctx2).Pie(pieData);
};
data used for both chart is the same like as samples , so nothing changed
but if i draw just one chart by itself it works great
if i put the two chart at the same time i get only the pie chart
can you tell me where is the problem please
i think it is some sort of conflict , but i can't find it
回答1:
Use only one window.onload
window.onload = function () {
window.myRadar = new Chart(document.getElementById("canvas1").getContext("2d")).Radar(radarChartData, {
responsive: true
});
var G2 = document.getElementById("canvas2").getContext("2d");
window.myBar = new Chart(G2).Bar(barChartData, {
responsive: true
});
}
回答2:
First off, you only need one window.onload event. No reason to have two separate instances of that.
Second, the data sets for Pie chart and Line chart are actually very different.
Pie chart sample data:
self.pieData= [
{
value: 65,
color:"#F7464A",
highlight: "#FF5A5E",
label: "New Scenarios"
},
{
value: 297,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Responses Submitted"
},
{
value: 225,
color: "#64a789",
highlight: "#5AD3D1",
label: "Responses Graded"
}
]
Line chart sample data:
self.lineData= {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "New Tests",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "#64a789",
pointColor: "#64a789",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: "Responses",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [128, 148, 140, 119, 186, 127, 190]
},
{
label: "Responses Graded",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "#41e498",
pointColor: "#41e498",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [108, 116, 120, 112, 136, 121, 111]
}
]
};
The line chart is likely not initializing because you are feeding it the wrong kind of data.
回答3:
I have not worked on different type of charts but I worked on an example and created two bar chart in a page using this code:
<div style="width: 50%">
<canvas id="canvas" height="450" width="600"></canvas>
</div>
<div style="width: 50%">
<canvas id="canvas2" height="450" width="600"></canvas>
</div>
and in script part I do like this:
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var barChartData = {
labels : ["January","February","March","April","May","June","July"],
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 : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
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 : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
]
}
var barChartData2 = {
labels : ["January","February","March","April","May","June","July"],
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 : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
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 : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
]
}
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
responsive : true
});
var ctx2 = document.getElementById("canvas2").getContext("2d");
window.myBar = new Chart(ctx2).Bar(barChartData2, {
responsive : true
});
}
回答4:
Use only 1 window.onload
function myfunc1{
}
function myfunc2{
}
function start(){
myfunc1();
myfunc1();
}
window.onload = start();
Ref:
http://www.htmlgoodies.com/beyond/javascript/article.php/3724571/Using-Multiple-JavaScript-Onload-Functions.htm
回答5:
I try this code ... this solve your problem
var barChartData = {
labels: [<?php echo $str_indiv_value; ?>],
datasets: [{
label: 'Dataset 1',
backgroundColor: [
window.chartColors.red,
window.chartColors.orange,
window.chartColors.yellow,
window.chartColors.green,
window.chartColors.blue,
window.chartColors.purple,
window.chartColors.red
],
yAxisID: 'y-axis-1',
data: [
<?php echo $str_indiv_requred;?>
]
}]
};
var barChartData_2 = {
labels: [<?php echo $str_indiv_value; ?>],
datasets: [{
label: 'Dataset 1',
backgroundColor: [
window.chartColors.red,
window.chartColors.orange,
window.chartColors.yellow,
window.chartColors.green,
window.chartColors.blue,
window.chartColors.purple,
window.chartColors.red
],
yAxisID: 'y-axis-1',
data: [
<?php echo $str_indiv_requred;?>
]
}]
};
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Bar Chart - Multi Axis'
},
tooltips: {
mode: 'index',
intersect: true
},
scales: {
yAxes: [{
type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
display: true,
position: 'left',
id: 'y-axis-1',
}],
}
}
});
var ctx_2 = document.getElementById('canvas2').getContext('2d');
window.myBar = new Chart(ctx_2, {
type: 'bar',
data: barChartData_2,
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Bar Chart - Multi Axis'
},
tooltips: {
mode: 'index',
intersect: true
},
scales: {
yAxes: [{
type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
display: true,
position: 'left',
id: 'y-axis-1',
}],
}
}
});
};
回答6:
You could rather user jQuery to get the canvas object
var ctx = $parent.find('#' + idOfCanvas).get(0).getContext("2d");
now make sure the code below is not throwing js error at all
window.onload = function(){
var ctx2 = document.getElementById("chart2").getContext("2d");
window.myPie = new Chart(ctx2).Pie(pieData);
};
as you know, if any error in js occurs the js-runtime halts all the latter lines of code