2

Currently i'm trying to learn Javascript, however I can't seem to get this problem fixed:

I have a Json file which is filled with data. I want the specific arrays of the first month August (Reports, Average, Global) and insert them (via a variable) into my chart. So I get a chart with:

August

  • Reports: 7
  • Average: 25
  • Global: 20

June ... ect.

Any help or tips are welcome!!

JSON Data

[{
    "month": "August",
    "reports": 7,
    "average": 25,
    "global": 20,
    "percentage": 14
}, {
    "month": "July",
    "reports": 22,
    "average": 25,
    "global": 20,
    "percentage": 44
}, {
    "month": "June",
    "reports": 12,
    "average": 25,
    "global": 20,
    "percentage": 24
}]

JS Code

window.onload = function() {

    var reports = [];

    $.getJSON("data.json", function(data) {
        reports = data;
    });


    var repaug = 7;
    var avg = 25;
    var global = 20;

    var ctx = document.getElementById("Chart1");
    var Chart1 = new Chart(ctx, {
        type: 'doughnut',
        data: {
            labels: ["Reports", "Average PSN", "Global Average"],
            datasets: [{
                label: '# of reports',
                data: [repaug, global, avg],
                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: {}
    })
}

Example

1 Answer 1

2

Inside of datasets you could add an array so first you need to loop for your response.

You could use map() method for array.

DEMO

let arrayData =[{"month":"August","reports":7,"average":25,"global":20,"percentage":14},{"month":"July","reports":22,"average":25,"global":20,"percentage":44},{"month":"June","reports":12,"average":25,"global":20,"percentage":24}],
    datasets = arrayData.map(item => {
        return {
            label: `${item.month}`,
            data: [item.reports, item.average, item.global],
            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
        }
    }),
    ctx = document.getElementById("myChart").getContext('2d');

new Chart(ctx, {
    type: 'doughnut',
    data: {
        labels: ["Reports", "Average PSN", "Global Average"],
        datasets: datasets
    },
    options: {
        responsive: true,
        legend: {
            position: 'top',
        },
        animation: {
            animateScale: true,
            animateRotate: true
        },
        tooltips: {
            callbacks: {
                label: function(item, data) {
                    let datasets = data.datasets,
                        dIndex = item.datasetIndex,
                        index = item.index;
                  
                    return `${datasets[dIndex].label} : ${data.labels[index]} : ${datasets[dIndex].data[index]}`;
                }
            }
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>

Sign up to request clarification or add additional context in comments.

7 Comments

Hi is it possible to change it to bar chart?
@Suvin94 Could you pls let me know what you need in bar chart ?
I am trying to make a same concept with stacked chart from mysql database. Is it possible? The array(from database) of labels which shows different colours and value array from database.
I still can't find a good tutorial for stacked chart with laravel mysql database
Yes you can. Please this working codepen. This will help you more to configure stacked chart.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.