/    Sign up×
Community /Pin to ProfileBookmark

Javascript open 2 chart but show only one

Dear All
I try to insert 2 chart on my website but it always show only one. I don’t sure why it show like that

“`
<!DOCTYPE html>
<html>

<head>
<meta charset=”utf-8″ />
<title></title>
<script data-require=”jquery” data-semver=”2.1.4″ src=”https://code.jquery.com/jquery-2.1.4.js”></script>
<script src=”./chart2.0.js”></script>
<style>
.container {
width: 50%;
overflow: hidden;
padding: 10px;
}
</style>
</head>
<body>
<div class=”container”>
<canvas id=”chart_Number_IN_QF” width=”500″ height=”500″></canvas>
<br>
<br>
<canvas id=”chart_Number_IN_QF2″ width=”500″ height=”500″></canvas>
</div>
<script>
var barChartData = {
labels: [“2020/05”, “2020/06”, “2020/07”, “2020/08”, “2020/09”, “2020/10”],
datasets: [{
label: “Summary 1”,
type: ‘line’,
data: [3102, 8693, 16116, 22630, 27893, 34403],
fill: false,
borderColor: ‘#EC932F’,
backgroundColor: ‘#EC932F’,
pointBorderColor: ‘#EC932F’,
pointBackgroundColor: ‘#EC932F’,
pointHoverBackgroundColor: ‘#EC932F’,
pointHoverBorderColor: ‘#EC932F’,
yAxisID: ‘y-axis-2’
}, {
type: ‘bar’,
label: “People in QF”,
data: [3102, 5591, 7423, 6514, 5263, 6510],
fill: false,
backgroundColor: ‘#71B37C’,
borderColor: ‘#71B37C’,
hoverBackgroundColor: ‘#71B37C’,
hoverBorderColor: ‘#71B37C’,
yAxisID: ‘y-axis-1’
}]
};

window.onload = function () {
var ctx = document.getElementById(“chart_Number_IN_QF”).getContext(“2d”);
window.myBar = new Chart(ctx, {
type: ‘bar’,
data: barChartData,
options: {
responsive: true,
tooltips: {
mode: ‘label’
},
elements: {
line: {
fill: false
}
},
scales: {
xAxes: [{
display: true,
gridLines: {
display: false
},
labels: {
show: true,
}
}],
yAxes: [{
type: “linear”,
display: true,
position: “left”,
id: “y-axis-1”,
gridLines: {
display: false
},
labels: {
show: true,

}
}, {
type: “linear”,
display: true,
position: “right”,
id: “y-axis-2”,
gridLines: {
display: true
},
labels: {
show: true,
}
}]
}
}
});
};
var barChartData2 = {
labels: [“2020/05”, “2020/06”, “2020/07”, “2020/08”, “2020/09”, “2020/10”],
datasets: [{
label: “Summary2”,
type: ‘line’,
data: [3102, 8693, 16116, 22630, 27893, 34403],
fill: false,
borderColor: ‘#EC932F’,
backgroundColor: ‘#EC932F’,
pointBorderColor: ‘#EC932F’,
pointBackgroundColor: ‘#EC932F’,
pointHoverBackgroundColor: ‘#EC932F’,
pointHoverBorderColor: ‘#EC932F’,
yAxisID: ‘y-axis-2’
}, {
type: ‘bar’,
label: “People in QF”,
data: [3102, 5591, 7423, 6514, 5263, 6510],
fill: false,
backgroundColor: ‘#71B37C’,
borderColor: ‘#71B37C’,
hoverBackgroundColor: ‘#71B37C’,
hoverBorderColor: ‘#71B37C’,
yAxisID: ‘y-axis-1’
}]
};

window.onload = function () {
var ctx2 = document.getElementById(“chart_Number_IN_QF2”).getContext(“2d”);
window.myBar = new Chart(ctx2, {
type: ‘bar’,
data: barChartData2,
options: {
responsive: true,
tooltips: {
mode: ‘label’
},
elements: {
line: {
fill: false
}
},
scales: {
xAxes: [{
display: true,
gridLines: {
display: false
},
labels: {
show: true,
}
}],
yAxes: [{
type: “linear”,
display: true,
position: “left”,
id: “y-axis-1”,
gridLines: {
display: false
},
labels: {
show: true,

}
}, {
type: “linear”,
display: true,
position: “right”,
id: “y-axis-2”,
gridLines: {
display: true
},
labels: {
show: true,
}
}]
}
}
});
};
</script>
</body>

</html>
“`

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 25.2020 — You assign a function to window.onload twice. The second one will overwrite the first one so that only the second one will be executed.

BTW: Single backtics won't work reliably for formatting code. You better use code tags: `your code here` or triple backtics.

I edited your posting accordingly.
Copy linkTweet thisAlerts:
@NitiphoneauthorOct 25.2020 — @Sempervivum#1624558 so what should event I set to make it show on finish load page?
Copy linkTweet thisAlerts:
@NachfolgerOct 25.2020 — @Nitiphone#1624559

Use addEventListener instead.

Eg.

window.onload = function() { ... }

Becomes

window.addEventListener('load', function() { ... }
Copy linkTweet thisAlerts:
@NitiphoneauthorOct 25.2020 — @Nachfolger#1624560 Thank you very much
Copy linkTweet thisAlerts:
@NitiphoneauthorOct 25.2020 — @Nachfolger#1624560 Do you have any example about this your code here?
Copy linkTweet thisAlerts:
@NachfolgerOct 25.2020 — > @Nitiphone#1624563 Do you have any example about this

> your code here

> ?


What?
Copy linkTweet thisAlerts:
@SempervivumOct 25.2020 — Using `window.onload = function () {` is fine, however you have to use it only once, i. e. place all of your javascript, for both charts, inside one function.

There is no such restriction when using addEventListener as recommended by Nachfolger. "add" has to be perceived literally, the listener is added without removing or overwriting a previous one.

On the other hand using an event listener is not really necessary for you as you placed the javascript at the end of the body and it is ensured that the elements you are referencing are already defined.
×

Success!

Help @Nitiphone spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...