How to stack bar from data with x and y in Chart JS












1















I am trying to plot a stack bar with data having the x and y values, eg {x: 1, y:1}.
But it seems that Chart JS follows indexing of array passed in the data with the index in the labels not the x.
I also tried using ticks: {source: 'data'} but no use.



I had to uncomment the labels: on and add index 1 and 2 in Africa dataset to see desired results.



Is there some way?



<canvas id="barchart" width="800" height="450"></canvas>
<script>
var ctx = document.getElementById("barchart").getContext('2d');
var barchart = new Chart(ctx, {
type: 'bar',
data: {
//labels: [0,1,2,3],
datasets: [{
data: [{x: 0, y: 2}, {x: 3, y: 1}],
label: "Africa",
backgroundColor: "#3e95cd",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 2}],
label: "Asia",
backgroundColor: "#8e5ea2",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Europe",
backgroundColor: "#3cba9f",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Latin America",
backgroundColor: "#e8c3b9",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "North America",
backgroundColor: "#c45850",
fill: false
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
},
responsive: false,
maintainAspectRatio: false,
scales: {
xAxes: [{
stacked: true,
ticks: {source: 'data'}
}],
yAxes: [{
stacked: true
}]
}
}
});
</script>









share|improve this question

























  • what is the problem you are facing?

    – Aroon
    Nov 15 '18 at 6:14
















1















I am trying to plot a stack bar with data having the x and y values, eg {x: 1, y:1}.
But it seems that Chart JS follows indexing of array passed in the data with the index in the labels not the x.
I also tried using ticks: {source: 'data'} but no use.



I had to uncomment the labels: on and add index 1 and 2 in Africa dataset to see desired results.



Is there some way?



<canvas id="barchart" width="800" height="450"></canvas>
<script>
var ctx = document.getElementById("barchart").getContext('2d');
var barchart = new Chart(ctx, {
type: 'bar',
data: {
//labels: [0,1,2,3],
datasets: [{
data: [{x: 0, y: 2}, {x: 3, y: 1}],
label: "Africa",
backgroundColor: "#3e95cd",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 2}],
label: "Asia",
backgroundColor: "#8e5ea2",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Europe",
backgroundColor: "#3cba9f",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Latin America",
backgroundColor: "#e8c3b9",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "North America",
backgroundColor: "#c45850",
fill: false
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
},
responsive: false,
maintainAspectRatio: false,
scales: {
xAxes: [{
stacked: true,
ticks: {source: 'data'}
}],
yAxes: [{
stacked: true
}]
}
}
});
</script>









share|improve this question

























  • what is the problem you are facing?

    – Aroon
    Nov 15 '18 at 6:14














1












1








1








I am trying to plot a stack bar with data having the x and y values, eg {x: 1, y:1}.
But it seems that Chart JS follows indexing of array passed in the data with the index in the labels not the x.
I also tried using ticks: {source: 'data'} but no use.



I had to uncomment the labels: on and add index 1 and 2 in Africa dataset to see desired results.



Is there some way?



<canvas id="barchart" width="800" height="450"></canvas>
<script>
var ctx = document.getElementById("barchart").getContext('2d');
var barchart = new Chart(ctx, {
type: 'bar',
data: {
//labels: [0,1,2,3],
datasets: [{
data: [{x: 0, y: 2}, {x: 3, y: 1}],
label: "Africa",
backgroundColor: "#3e95cd",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 2}],
label: "Asia",
backgroundColor: "#8e5ea2",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Europe",
backgroundColor: "#3cba9f",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Latin America",
backgroundColor: "#e8c3b9",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "North America",
backgroundColor: "#c45850",
fill: false
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
},
responsive: false,
maintainAspectRatio: false,
scales: {
xAxes: [{
stacked: true,
ticks: {source: 'data'}
}],
yAxes: [{
stacked: true
}]
}
}
});
</script>









share|improve this question
















I am trying to plot a stack bar with data having the x and y values, eg {x: 1, y:1}.
But it seems that Chart JS follows indexing of array passed in the data with the index in the labels not the x.
I also tried using ticks: {source: 'data'} but no use.



I had to uncomment the labels: on and add index 1 and 2 in Africa dataset to see desired results.



Is there some way?



<canvas id="barchart" width="800" height="450"></canvas>
<script>
var ctx = document.getElementById("barchart").getContext('2d');
var barchart = new Chart(ctx, {
type: 'bar',
data: {
//labels: [0,1,2,3],
datasets: [{
data: [{x: 0, y: 2}, {x: 3, y: 1}],
label: "Africa",
backgroundColor: "#3e95cd",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 2}],
label: "Asia",
backgroundColor: "#8e5ea2",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Europe",
backgroundColor: "#3cba9f",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "Latin America",
backgroundColor: "#e8c3b9",
fill: false
}, {
data: [{x: 0, y: 1}, {x: 1, y: 1}, {x: 2, y: 1}, {x: 3, y: 1}],
label: "North America",
backgroundColor: "#c45850",
fill: false
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
},
responsive: false,
maintainAspectRatio: false,
scales: {
xAxes: [{
stacked: true,
ticks: {source: 'data'}
}],
yAxes: [{
stacked: true
}]
}
}
});
</script>






javascript chart.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:29







Nikko

















asked Nov 14 '18 at 9:00









NikkoNikko

1147




1147













  • what is the problem you are facing?

    – Aroon
    Nov 15 '18 at 6:14



















  • what is the problem you are facing?

    – Aroon
    Nov 15 '18 at 6:14

















what is the problem you are facing?

– Aroon
Nov 15 '18 at 6:14





what is the problem you are facing?

– Aroon
Nov 15 '18 at 6:14












0






active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296336%2fhow-to-stack-bar-from-data-with-x-and-y-in-chart-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296336%2fhow-to-stack-bar-from-data-with-x-and-y-in-chart-js%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()