How to implement dynamic data on chartjs?

Mobile Technologies Mobile Computing 3 years ago

804 1 0 0 0

_x000D_ _x000D_ I need your help to learn how to dynamically implement data from a json file to Chartjs. The fields that I need to fill dynamically are labels and data. JSON File <<< [ { "EFICAZ_TAB_ITEM_ID":1, "EFICAZ_PERCENTS":99 }, { "EFICAZ_TAB_ITEM_ID":2, "EFICAZ_PERCENTS":99 }, { "EFICAZ_TAB_ITEM_ID":3, "EFICAZ_PERCENTS":99 } ] CHARTJS <<< var ctx = document.getElementById('myChart').getContext('2d'); var chart = new Chart(ctx, { // The type of chart we want to create type: 'line', // The data for our dataset data: { labels: [???], datasets: [{ label: "My First dataset", backgroundColor: 'rgb(255, 99, 132)', borderColor: 'rgb(255, 99, 132)', data: [???], }] }, // Configuration options go here options: {} });

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago
_x000D_ I have used $.ajax() to fetch JSON data, and save the data in two arrays, then you can use these arrays for your chart label and data. Hope this helps! var lbl = []; var dta = []; $.ajax({ url: "test.json", dataType: 'json', async: false, success: function(data) { $.each(data, function(i, field){ lbl.push(field.EFICAZ_TAB_ITEM_ID ); dta.push(field.EFICAZ_PERCENTS); }); } }); $.getJSON("test.json", function(result){ $.each(result, function(i, field){ lbl.push(field.EFICAZ_TAB_ITEM_ID ); dta.push(field.EFICAZ_PERCENTS); }); }); var ctx = document.getElementById("myCanvas").getContext('2d'); var chart = new Chart(ctx, { // The type of chart we want to create type: 'line', // The data for our dataset data: { labels: lbl, datasets: [{ label: "My First dataset", backgroundColor: 'rgb(255, 99, 132)', borderColor: 'rgb(255, 99, 132)', data: dta, }] }, // Configuration options go here options: {} });
3 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

Would Firebase be a good choice as a server for the Flutter mobile game I'm creating?

_x000D_ _x000D_ I am building a turn-based Flutter game. Would Firebase be a good choice as a server...
Q

Implement Bottom Navigation activity along with a VerticalViewPager with nested Cardview in Android...

_x000D_ _x000D_ I want to develop an Android App which will have a Bottom Navigation Bar and above i...
Q

(JS) For , determine if the device offers or not to capture a photo from the camera

_x000D_ _x000D_ When uploading a file, I need to give the user the possibility: upload the file fr...

Important Mobile Technologies Links