How can i launch a modal after clicking each bar in Bar chart in Chartjs and also how can i get its data?
So im creating charts in javascript using chartjs , i have a problem that how can i select each bar in bar chart and launch a modal with its data.
I already tried the onClick event after options , but i think i have a wrong pattern. I dont know much more about chartjs and i think i need help. Here's my code :
var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: outlet,
datasets: [{
label: "Players",
data: total_outlet,
backgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
hoverBackgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
ticks: {
min: 0,
fontColor: "white"
},
gridLines: {
display:false
}
}],
yAxes:[{
ticks: {
fontColor: "white"
},
gridLines: {
display:false
}
}]
},
title: {
display: true,
text: 'Top 10 Outlet with Active UGL Terminals',
fontColor: "white"
},
legend: {
display:false,
labels: {
fontColor: "white"
}
}
}
});
javascript chart.js
add a comment |
So im creating charts in javascript using chartjs , i have a problem that how can i select each bar in bar chart and launch a modal with its data.
I already tried the onClick event after options , but i think i have a wrong pattern. I dont know much more about chartjs and i think i need help. Here's my code :
var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: outlet,
datasets: [{
label: "Players",
data: total_outlet,
backgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
hoverBackgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
ticks: {
min: 0,
fontColor: "white"
},
gridLines: {
display:false
}
}],
yAxes:[{
ticks: {
fontColor: "white"
},
gridLines: {
display:false
}
}]
},
title: {
display: true,
text: 'Top 10 Outlet with Active UGL Terminals',
fontColor: "white"
},
legend: {
display:false,
labels: {
fontColor: "white"
}
}
}
});
javascript chart.js
add a comment |
So im creating charts in javascript using chartjs , i have a problem that how can i select each bar in bar chart and launch a modal with its data.
I already tried the onClick event after options , but i think i have a wrong pattern. I dont know much more about chartjs and i think i need help. Here's my code :
var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: outlet,
datasets: [{
label: "Players",
data: total_outlet,
backgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
hoverBackgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
ticks: {
min: 0,
fontColor: "white"
},
gridLines: {
display:false
}
}],
yAxes:[{
ticks: {
fontColor: "white"
},
gridLines: {
display:false
}
}]
},
title: {
display: true,
text: 'Top 10 Outlet with Active UGL Terminals',
fontColor: "white"
},
legend: {
display:false,
labels: {
fontColor: "white"
}
}
}
});
javascript chart.js
So im creating charts in javascript using chartjs , i have a problem that how can i select each bar in bar chart and launch a modal with its data.
I already tried the onClick event after options , but i think i have a wrong pattern. I dont know much more about chartjs and i think i need help. Here's my code :
var ctx = document.getElementById("myChart2").getContext('2d');
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: outlet,
datasets: [{
label: "Players",
data: total_outlet,
backgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
hoverBackgroundColor: [
"#f38b4a",
"#56d798",
"#ff8397",
"#6970d5",
"#ff0000",
"#339966",
"#004d00",
"#003366",
"#e62e00",
"#666633",
"#996633"
],
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
ticks: {
min: 0,
fontColor: "white"
},
gridLines: {
display:false
}
}],
yAxes:[{
ticks: {
fontColor: "white"
},
gridLines: {
display:false
}
}]
},
title: {
display: true,
text: 'Top 10 Outlet with Active UGL Terminals',
fontColor: "white"
},
legend: {
display:false,
labels: {
fontColor: "white"
}
}
}
});
javascript chart.js
javascript chart.js
asked Jan 20 at 9:24
Reymond JaronReymond Jaron
1313
1313
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is quite straightforward and is documented at the Chart.js API page. Below is a working snippet adapted from the official example:
let canvas = document.getElementById('canvas');
let myChart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addEventListener('click', function(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert('Label: ' + label + "nValue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275079%2fhow-can-i-launch-a-modal-after-clicking-each-bar-in-bar-chart-in-chartjs-and-als%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is quite straightforward and is documented at the Chart.js API page. Below is a working snippet adapted from the official example:
let canvas = document.getElementById('canvas');
let myChart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addEventListener('click', function(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert('Label: ' + label + "nValue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
add a comment |
This is quite straightforward and is documented at the Chart.js API page. Below is a working snippet adapted from the official example:
let canvas = document.getElementById('canvas');
let myChart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addEventListener('click', function(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert('Label: ' + label + "nValue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
add a comment |
This is quite straightforward and is documented at the Chart.js API page. Below is a working snippet adapted from the official example:
let canvas = document.getElementById('canvas');
let myChart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addEventListener('click', function(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert('Label: ' + label + "nValue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
This is quite straightforward and is documented at the Chart.js API page. Below is a working snippet adapted from the official example:
let canvas = document.getElementById('canvas');
let myChart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addEventListener('click', function(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert('Label: ' + label + "nValue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
let canvas = document.getElementById('canvas');
let myChart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addEventListener('click', function(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert('Label: ' + label + "nValue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
let canvas = document.getElementById('canvas');
let myChart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['a', 'b', 'c', 'd'],
datasets: [{
data: [0, 2, 4, 8]
}]
}
});
canvas.addEventListener('click', function(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert('Label: ' + label + "nValue: " + value);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>
answered Jan 21 at 11:59
timcluttontimclutton
7,80031933
7,80031933
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275079%2fhow-can-i-launch-a-modal-after-clicking-each-bar-in-bar-chart-in-chartjs-and-als%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown