Parsing JSON data into Chart.js bar chartSerializing to JSON in jQueryHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?Why can't Python parse this JSON data?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?

Roll the carpet

Was any UN Security Council vote triple-vetoed?

What typically incentivizes a professor to change jobs to a lower ranking university?

Modeling an IP Address

Why doesn't H₄O²⁺ exist?

What defenses are there against being summoned by the Gate spell?

How is it possible to have an ability score that is less than 3?

Arrow those variables!

Perform and show arithmetic with LuaLaTeX

What does the "remote control" for a QF-4 look like?

Which country benefited the most from UN Security Council vetoes?

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?

Horror movie about a virus at the prom; beginning and end are stylized as a cartoon

Is it inappropriate for a student to attend their mentor's dissertation defense?

dbcc cleantable batch size explanation

What's that red-plus icon near a text?

Important Resources for Dark Age Civilizations?

Can I make popcorn with any corn?

How much of data wrangling is a data scientist's job?

Today is the Center

Did Shadowfax go to Valinor?

Do infinite dimensional systems make sense?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Can I ask the recruiters in my resume to put the reason why I am rejected?



Parsing JSON data into Chart.js bar chart


Serializing to JSON in jQueryHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?Why can't Python parse this JSON data?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I'm trying to use Chart.js to generate a bar chart using dynamically generated data from Oracle DB. I'm having trouble getting the data in the right format, I think I'm close but I've been stuck for a while.



My JSON file:



["REGION":"Poland","REV_VALUE":"2263","REGION":"United States","REV_VALUE":"1961","REGION":"Spain","REV_VALUE":"555","REGION":"United Kingdom","REV_VALUE":"380","REGION":"Germany","REV_VALUE":"314"]


And here is my barchar.js file:






$(document).ready(function()
$.ajax(
url: "http://localhost/DWH/dataJSON.php",
method: "GET",
dataType : 'JSON',
success: function(data)
console.log(data);
var region = [];
var value = [];

for(var i in data)
region.push(data[i].REGION);
value.push(data[i].REV_VALUE);


var chartdata =
labels: region,
datasets : [

label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: value.map(function(x) return x * 1;)

]
;

var ctx = $("#myChart");

var barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);
,
error: function(data)
console.log(data);

);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>





I tried to implement a solution that I found in other topics, but still without success.
Any help would be very much appreciated!










share|improve this question
























  • Thanks for providing the JSON data snippet, it made formulating an actual response a lot easier. Best of luck, let me know if there is any issues and if my answer was able to set you on the right track.

    – Raymond
    Mar 21 at 23:39












  • Thank you so much for your time and help. It works perfectly for me! Back on the right track thanks to you.

    – Jack84
    Mar 22 at 7:09

















2















I'm trying to use Chart.js to generate a bar chart using dynamically generated data from Oracle DB. I'm having trouble getting the data in the right format, I think I'm close but I've been stuck for a while.



My JSON file:



["REGION":"Poland","REV_VALUE":"2263","REGION":"United States","REV_VALUE":"1961","REGION":"Spain","REV_VALUE":"555","REGION":"United Kingdom","REV_VALUE":"380","REGION":"Germany","REV_VALUE":"314"]


And here is my barchar.js file:






$(document).ready(function()
$.ajax(
url: "http://localhost/DWH/dataJSON.php",
method: "GET",
dataType : 'JSON',
success: function(data)
console.log(data);
var region = [];
var value = [];

for(var i in data)
region.push(data[i].REGION);
value.push(data[i].REV_VALUE);


var chartdata =
labels: region,
datasets : [

label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: value.map(function(x) return x * 1;)

]
;

var ctx = $("#myChart");

var barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);
,
error: function(data)
console.log(data);

);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>





I tried to implement a solution that I found in other topics, but still without success.
Any help would be very much appreciated!










share|improve this question
























  • Thanks for providing the JSON data snippet, it made formulating an actual response a lot easier. Best of luck, let me know if there is any issues and if my answer was able to set you on the right track.

    – Raymond
    Mar 21 at 23:39












  • Thank you so much for your time and help. It works perfectly for me! Back on the right track thanks to you.

    – Jack84
    Mar 22 at 7:09













2












2








2








I'm trying to use Chart.js to generate a bar chart using dynamically generated data from Oracle DB. I'm having trouble getting the data in the right format, I think I'm close but I've been stuck for a while.



My JSON file:



["REGION":"Poland","REV_VALUE":"2263","REGION":"United States","REV_VALUE":"1961","REGION":"Spain","REV_VALUE":"555","REGION":"United Kingdom","REV_VALUE":"380","REGION":"Germany","REV_VALUE":"314"]


And here is my barchar.js file:






$(document).ready(function()
$.ajax(
url: "http://localhost/DWH/dataJSON.php",
method: "GET",
dataType : 'JSON',
success: function(data)
console.log(data);
var region = [];
var value = [];

for(var i in data)
region.push(data[i].REGION);
value.push(data[i].REV_VALUE);


var chartdata =
labels: region,
datasets : [

label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: value.map(function(x) return x * 1;)

]
;

var ctx = $("#myChart");

var barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);
,
error: function(data)
console.log(data);

);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>





I tried to implement a solution that I found in other topics, but still without success.
Any help would be very much appreciated!










share|improve this question
















I'm trying to use Chart.js to generate a bar chart using dynamically generated data from Oracle DB. I'm having trouble getting the data in the right format, I think I'm close but I've been stuck for a while.



My JSON file:



["REGION":"Poland","REV_VALUE":"2263","REGION":"United States","REV_VALUE":"1961","REGION":"Spain","REV_VALUE":"555","REGION":"United Kingdom","REV_VALUE":"380","REGION":"Germany","REV_VALUE":"314"]


And here is my barchar.js file:






$(document).ready(function()
$.ajax(
url: "http://localhost/DWH/dataJSON.php",
method: "GET",
dataType : 'JSON',
success: function(data)
console.log(data);
var region = [];
var value = [];

for(var i in data)
region.push(data[i].REGION);
value.push(data[i].REV_VALUE);


var chartdata =
labels: region,
datasets : [

label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: value.map(function(x) return x * 1;)

]
;

var ctx = $("#myChart");

var barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);
,
error: function(data)
console.log(data);

);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>





I tried to implement a solution that I found in other topics, but still without success.
Any help would be very much appreciated!






$(document).ready(function()
$.ajax(
url: "http://localhost/DWH/dataJSON.php",
method: "GET",
dataType : 'JSON',
success: function(data)
console.log(data);
var region = [];
var value = [];

for(var i in data)
region.push(data[i].REGION);
value.push(data[i].REV_VALUE);


var chartdata =
labels: region,
datasets : [

label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: value.map(function(x) return x * 1;)

]
;

var ctx = $("#myChart");

var barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);
,
error: function(data)
console.log(data);

);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>





$(document).ready(function()
$.ajax(
url: "http://localhost/DWH/dataJSON.php",
method: "GET",
dataType : 'JSON',
success: function(data)
console.log(data);
var region = [];
var value = [];

for(var i in data)
region.push(data[i].REGION);
value.push(data[i].REV_VALUE);


var chartdata =
labels: region,
datasets : [

label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: value.map(function(x) return x * 1;)

]
;

var ctx = $("#myChart");

var barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);
,
error: function(data)
console.log(data);

);
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>






javascript json ajax chart.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 22:55









Dean Meehan

1,8701426




1,8701426










asked Mar 21 at 22:42









Jack84Jack84

225




225












  • Thanks for providing the JSON data snippet, it made formulating an actual response a lot easier. Best of luck, let me know if there is any issues and if my answer was able to set you on the right track.

    – Raymond
    Mar 21 at 23:39












  • Thank you so much for your time and help. It works perfectly for me! Back on the right track thanks to you.

    – Jack84
    Mar 22 at 7:09

















  • Thanks for providing the JSON data snippet, it made formulating an actual response a lot easier. Best of luck, let me know if there is any issues and if my answer was able to set you on the right track.

    – Raymond
    Mar 21 at 23:39












  • Thank you so much for your time and help. It works perfectly for me! Back on the right track thanks to you.

    – Jack84
    Mar 22 at 7:09
















Thanks for providing the JSON data snippet, it made formulating an actual response a lot easier. Best of luck, let me know if there is any issues and if my answer was able to set you on the right track.

– Raymond
Mar 21 at 23:39






Thanks for providing the JSON data snippet, it made formulating an actual response a lot easier. Best of luck, let me know if there is any issues and if my answer was able to set you on the right track.

– Raymond
Mar 21 at 23:39














Thank you so much for your time and help. It works perfectly for me! Back on the right track thanks to you.

– Jack84
Mar 22 at 7:09





Thank you so much for your time and help. It works perfectly for me! Back on the right track thanks to you.

– Jack84
Mar 22 at 7:09












2 Answers
2






active

oldest

votes


















2














I made this quickly to give you an example to help you out, you were in the right direction. I have the snippet using hardcoded data for example purposes, and then at the bottom I used the Ajax method.



Parsed the data as so, similar to what you were doing by using data.map().



let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);
catch (error)
console.log(error);



Then to use the data I just simply used a spread operator for the array contents [...].



labels: [...region],
data: [...rev_value],



Example 1 using Canvas.js simple example with your data hardcoded.







var ctx = document.getElementById('myChart').getContext('2d');

let data = [
"REGION": "Poland",
"REV_VALUE": "2263"
,
"REGION": "United States",
"REV_VALUE": "1961"
,
"REGION": "Spain",
"REV_VALUE": "555"
,
"REGION": "United Kingdom",
"REV_VALUE": "380"
,
"REGION": "Germany",
"REV_VALUE": "314"
];

let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);

var myChart = new Chart(ctx,
type: 'bar',
data:
labels: [...region],
datasets: [
label: 'Regions',
data: [...rev_value],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
]
,
options:
scales:
yAxes: [
ticks:
beginAtZero: true

]


);

catch (error)
console.log(error);

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>






Example 2 using your template and an Ajax call, change the URL for the request.




<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
function grab()
/* Promise to make sure data loads */
return new Promise((resolve, reject) =>
$.ajax(
url: "/data.json",
method: "GET",
dataType: 'JSON',
success: function(data)
resolve(data)
,
error: function(error)
reject(error);

)
)


$(document).ready(function()
grab().then((data) =>
console.log('Recieved our data', data);
let regions = [];
let value = [];

try
data.forEach((item) =>
regions.push(item.REGION)
value.push(item.REV_VALUE)
);

let chartdata =
labels: [...regions],
datasets: [
label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: [...value]
]
;

let ctx = $("#myChart");

let barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);

catch (error)
console.log('Error parsing JSON data', error)


).catch((error) =>
console.log(error);
)
);
</script>





share|improve this answer

























  • Example 2 is what I was looking for. Thank you very much for your support and prompt answer.

    – Jack84
    Mar 22 at 7:10


















-2














You can try something like this :



 dataPoints: variable ? variable.map((v) => (x: (v.region), y: v.value)) : []





share|improve this answer























  • Try updating your answer to show how it can be used in the OPs code.

    – quicklikerabbit
    Mar 22 at 0:17











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%2f55290321%2fparsing-json-data-into-chart-js-bar-chart%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I made this quickly to give you an example to help you out, you were in the right direction. I have the snippet using hardcoded data for example purposes, and then at the bottom I used the Ajax method.



Parsed the data as so, similar to what you were doing by using data.map().



let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);
catch (error)
console.log(error);



Then to use the data I just simply used a spread operator for the array contents [...].



labels: [...region],
data: [...rev_value],



Example 1 using Canvas.js simple example with your data hardcoded.







var ctx = document.getElementById('myChart').getContext('2d');

let data = [
"REGION": "Poland",
"REV_VALUE": "2263"
,
"REGION": "United States",
"REV_VALUE": "1961"
,
"REGION": "Spain",
"REV_VALUE": "555"
,
"REGION": "United Kingdom",
"REV_VALUE": "380"
,
"REGION": "Germany",
"REV_VALUE": "314"
];

let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);

var myChart = new Chart(ctx,
type: 'bar',
data:
labels: [...region],
datasets: [
label: 'Regions',
data: [...rev_value],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
]
,
options:
scales:
yAxes: [
ticks:
beginAtZero: true

]


);

catch (error)
console.log(error);

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>






Example 2 using your template and an Ajax call, change the URL for the request.




<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
function grab()
/* Promise to make sure data loads */
return new Promise((resolve, reject) =>
$.ajax(
url: "/data.json",
method: "GET",
dataType: 'JSON',
success: function(data)
resolve(data)
,
error: function(error)
reject(error);

)
)


$(document).ready(function()
grab().then((data) =>
console.log('Recieved our data', data);
let regions = [];
let value = [];

try
data.forEach((item) =>
regions.push(item.REGION)
value.push(item.REV_VALUE)
);

let chartdata =
labels: [...regions],
datasets: [
label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: [...value]
]
;

let ctx = $("#myChart");

let barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);

catch (error)
console.log('Error parsing JSON data', error)


).catch((error) =>
console.log(error);
)
);
</script>





share|improve this answer

























  • Example 2 is what I was looking for. Thank you very much for your support and prompt answer.

    – Jack84
    Mar 22 at 7:10















2














I made this quickly to give you an example to help you out, you were in the right direction. I have the snippet using hardcoded data for example purposes, and then at the bottom I used the Ajax method.



Parsed the data as so, similar to what you were doing by using data.map().



let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);
catch (error)
console.log(error);



Then to use the data I just simply used a spread operator for the array contents [...].



labels: [...region],
data: [...rev_value],



Example 1 using Canvas.js simple example with your data hardcoded.







var ctx = document.getElementById('myChart').getContext('2d');

let data = [
"REGION": "Poland",
"REV_VALUE": "2263"
,
"REGION": "United States",
"REV_VALUE": "1961"
,
"REGION": "Spain",
"REV_VALUE": "555"
,
"REGION": "United Kingdom",
"REV_VALUE": "380"
,
"REGION": "Germany",
"REV_VALUE": "314"
];

let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);

var myChart = new Chart(ctx,
type: 'bar',
data:
labels: [...region],
datasets: [
label: 'Regions',
data: [...rev_value],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
]
,
options:
scales:
yAxes: [
ticks:
beginAtZero: true

]


);

catch (error)
console.log(error);

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>






Example 2 using your template and an Ajax call, change the URL for the request.




<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
function grab()
/* Promise to make sure data loads */
return new Promise((resolve, reject) =>
$.ajax(
url: "/data.json",
method: "GET",
dataType: 'JSON',
success: function(data)
resolve(data)
,
error: function(error)
reject(error);

)
)


$(document).ready(function()
grab().then((data) =>
console.log('Recieved our data', data);
let regions = [];
let value = [];

try
data.forEach((item) =>
regions.push(item.REGION)
value.push(item.REV_VALUE)
);

let chartdata =
labels: [...regions],
datasets: [
label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: [...value]
]
;

let ctx = $("#myChart");

let barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);

catch (error)
console.log('Error parsing JSON data', error)


).catch((error) =>
console.log(error);
)
);
</script>





share|improve this answer

























  • Example 2 is what I was looking for. Thank you very much for your support and prompt answer.

    – Jack84
    Mar 22 at 7:10













2












2








2







I made this quickly to give you an example to help you out, you were in the right direction. I have the snippet using hardcoded data for example purposes, and then at the bottom I used the Ajax method.



Parsed the data as so, similar to what you were doing by using data.map().



let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);
catch (error)
console.log(error);



Then to use the data I just simply used a spread operator for the array contents [...].



labels: [...region],
data: [...rev_value],



Example 1 using Canvas.js simple example with your data hardcoded.







var ctx = document.getElementById('myChart').getContext('2d');

let data = [
"REGION": "Poland",
"REV_VALUE": "2263"
,
"REGION": "United States",
"REV_VALUE": "1961"
,
"REGION": "Spain",
"REV_VALUE": "555"
,
"REGION": "United Kingdom",
"REV_VALUE": "380"
,
"REGION": "Germany",
"REV_VALUE": "314"
];

let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);

var myChart = new Chart(ctx,
type: 'bar',
data:
labels: [...region],
datasets: [
label: 'Regions',
data: [...rev_value],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
]
,
options:
scales:
yAxes: [
ticks:
beginAtZero: true

]


);

catch (error)
console.log(error);

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>






Example 2 using your template and an Ajax call, change the URL for the request.




<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
function grab()
/* Promise to make sure data loads */
return new Promise((resolve, reject) =>
$.ajax(
url: "/data.json",
method: "GET",
dataType: 'JSON',
success: function(data)
resolve(data)
,
error: function(error)
reject(error);

)
)


$(document).ready(function()
grab().then((data) =>
console.log('Recieved our data', data);
let regions = [];
let value = [];

try
data.forEach((item) =>
regions.push(item.REGION)
value.push(item.REV_VALUE)
);

let chartdata =
labels: [...regions],
datasets: [
label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: [...value]
]
;

let ctx = $("#myChart");

let barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);

catch (error)
console.log('Error parsing JSON data', error)


).catch((error) =>
console.log(error);
)
);
</script>





share|improve this answer















I made this quickly to give you an example to help you out, you were in the right direction. I have the snippet using hardcoded data for example purposes, and then at the bottom I used the Ajax method.



Parsed the data as so, similar to what you were doing by using data.map().



let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);
catch (error)
console.log(error);



Then to use the data I just simply used a spread operator for the array contents [...].



labels: [...region],
data: [...rev_value],



Example 1 using Canvas.js simple example with your data hardcoded.







var ctx = document.getElementById('myChart').getContext('2d');

let data = [
"REGION": "Poland",
"REV_VALUE": "2263"
,
"REGION": "United States",
"REV_VALUE": "1961"
,
"REGION": "Spain",
"REV_VALUE": "555"
,
"REGION": "United Kingdom",
"REV_VALUE": "380"
,
"REGION": "Germany",
"REV_VALUE": "314"
];

let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);

var myChart = new Chart(ctx,
type: 'bar',
data:
labels: [...region],
datasets: [
label: 'Regions',
data: [...rev_value],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
]
,
options:
scales:
yAxes: [
ticks:
beginAtZero: true

]


);

catch (error)
console.log(error);

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>






Example 2 using your template and an Ajax call, change the URL for the request.




<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
function grab()
/* Promise to make sure data loads */
return new Promise((resolve, reject) =>
$.ajax(
url: "/data.json",
method: "GET",
dataType: 'JSON',
success: function(data)
resolve(data)
,
error: function(error)
reject(error);

)
)


$(document).ready(function()
grab().then((data) =>
console.log('Recieved our data', data);
let regions = [];
let value = [];

try
data.forEach((item) =>
regions.push(item.REGION)
value.push(item.REV_VALUE)
);

let chartdata =
labels: [...regions],
datasets: [
label: 'Region',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: [...value]
]
;

let ctx = $("#myChart");

let barGraph = new Chart(ctx,
type: 'bar',
data: chartdata
);

catch (error)
console.log('Error parsing JSON data', error)


).catch((error) =>
console.log(error);
)
);
</script>





var ctx = document.getElementById('myChart').getContext('2d');

let data = [
"REGION": "Poland",
"REV_VALUE": "2263"
,
"REGION": "United States",
"REV_VALUE": "1961"
,
"REGION": "Spain",
"REV_VALUE": "555"
,
"REGION": "United Kingdom",
"REV_VALUE": "380"
,
"REGION": "Germany",
"REV_VALUE": "314"
];

let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);

var myChart = new Chart(ctx,
type: 'bar',
data:
labels: [...region],
datasets: [
label: 'Regions',
data: [...rev_value],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
]
,
options:
scales:
yAxes: [
ticks:
beginAtZero: true

]


);

catch (error)
console.log(error);

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>





var ctx = document.getElementById('myChart').getContext('2d');

let data = [
"REGION": "Poland",
"REV_VALUE": "2263"
,
"REGION": "United States",
"REV_VALUE": "1961"
,
"REGION": "Spain",
"REV_VALUE": "555"
,
"REGION": "United Kingdom",
"REV_VALUE": "380"
,
"REGION": "Germany",
"REV_VALUE": "314"
];

let region = [];
let rev_value = [];

try
data.map((item) =>
rev_value.push(item.REV_VALUE);
region.push(item.REGION);
);

var myChart = new Chart(ctx,
type: 'bar',
data:
labels: [...region],
datasets: [
label: 'Regions',
data: [...rev_value],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
]
,
options:
scales:
yAxes: [
ticks:
beginAtZero: true

]


);

catch (error)
console.log(error);

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0/dist/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 23:35

























answered Mar 21 at 22:57









RaymondRaymond

1,012214




1,012214












  • Example 2 is what I was looking for. Thank you very much for your support and prompt answer.

    – Jack84
    Mar 22 at 7:10

















  • Example 2 is what I was looking for. Thank you very much for your support and prompt answer.

    – Jack84
    Mar 22 at 7:10
















Example 2 is what I was looking for. Thank you very much for your support and prompt answer.

– Jack84
Mar 22 at 7:10





Example 2 is what I was looking for. Thank you very much for your support and prompt answer.

– Jack84
Mar 22 at 7:10













-2














You can try something like this :



 dataPoints: variable ? variable.map((v) => (x: (v.region), y: v.value)) : []





share|improve this answer























  • Try updating your answer to show how it can be used in the OPs code.

    – quicklikerabbit
    Mar 22 at 0:17















-2














You can try something like this :



 dataPoints: variable ? variable.map((v) => (x: (v.region), y: v.value)) : []





share|improve this answer























  • Try updating your answer to show how it can be used in the OPs code.

    – quicklikerabbit
    Mar 22 at 0:17













-2












-2








-2







You can try something like this :



 dataPoints: variable ? variable.map((v) => (x: (v.region), y: v.value)) : []





share|improve this answer













You can try something like this :



 dataPoints: variable ? variable.map((v) => (x: (v.region), y: v.value)) : []






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 23:01









Ilias KorompilisIlias Korompilis

1




1












  • Try updating your answer to show how it can be used in the OPs code.

    – quicklikerabbit
    Mar 22 at 0:17

















  • Try updating your answer to show how it can be used in the OPs code.

    – quicklikerabbit
    Mar 22 at 0:17
















Try updating your answer to show how it can be used in the OPs code.

– quicklikerabbit
Mar 22 at 0:17





Try updating your answer to show how it can be used in the OPs code.

– quicklikerabbit
Mar 22 at 0:17

















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%2f55290321%2fparsing-json-data-into-chart-js-bar-chart%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







Popular posts from this blog

Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴