[Chartjs]-How can I show JSON data in Chart.js with Javascript if static values are working but dynamic from mySQL are not?

0👍

I now managed to resolve this on my own, code is below:

<!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="/node_modules/chart.js/dist/Chart.bundle.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <style>
        canvas{
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
        }
    </style>
    <title>Temperatur und Feuchtigkeit</title>
    </head>
    <body>
        <div style="width: 100%;"> 
            <canvas id="canvas"></canvas>
        </div>
        <script>
            var data = [], labels = [], temperature=[], humidity=[];
            $.get('GetTestData.php', function(dataGet) {
                data = JSON.parse(dataGet);
                data['labels'].forEach(function(singleResult) {
                    labels.push(singleResult);
                });
                data['temperature'].forEach(function(singleResult) {
                    temperature.push(singleResult);
                });
                data['humidity'].forEach(function(singleResult) {
                    humidity.push(singleResult);
                });
                var ctx = document.getElementById("canvas").getContext("2d");
                window.myLine = new Chart(ctx, config);
            });
            var randomScalingFactor = function() {
                return Math.round(Math.random() * 100);
            };
            var randomColorFactor = function() {
                return Math.round(Math.random() * 255);
            };
            var randomColor = function(opacity) {
                return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.9') + ')';
            };

            var config = {
                type: 'line',
                data: {
                    labels: labels,
                    datasets: [{
                        label: "Temperatur",
                        data: temperature,
                        fill: false
                    },
                    {
                        label: "Feuchtigkeit",
                        data: humidity,
                        fill: false
                    }]
                },
                options: {
                    responsive: true,
                    title:{
                        display:true,
                        text:'Temperatur und Feuchtigkeit'
                    },
                    tooltips: {
                        mode: 'label'
                    },
                    hover: {
                        mode: 'dataset'
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Datum'
                            }
                        }],
                        yAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Wert'
                            },
                            ticks: {
                                suggestedMin: -20,
                                suggestedMax: 250,
                            }
                        }],
                    }
                }
            };

            $.each(config.data.datasets, function(i, dataset) {
                dataset.borderColor = randomColor(1.0);
                dataset.backgroundColor = randomColor(1.0);
                dataset.pointBorderColor = randomColor(1.0);
                dataset.pointBackgroundColor = randomColor(1.0);
                dataset.pointBorderWidth = 1;
            });

            window.onload = function() {
                var ctx = document.getElementById("canvas").getContext("2d");
                window.myLine = new Chart(ctx, config);
            };
        </script>
    </body>
    </html>

Result looks as following: result

Leave a comment