Chart.js - Line Type - JSON - SQL Server

1

I'm setting up my first Chart. When I use constants in the date the chart renders normally, but when I load sql database information I do not have the same success. Follow the code ...

<div id="canvas-holder1" style="width: 100%;">
    <canvas id="chart1" />
</div>
<script language="javascript" type="text/javascript">
    $(function() {
        $.ajax({
            type: "POST",
            url: "Entrada.aspx/getChartData",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnErrorCall
        });

        function OnSuccess(response) {
            var lstReceita = eval(response.d[0]);
            var lstDespesa = eval(response.d[1]);

            var lineChartData = {
                labels: ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
                datasets: [{
                    label: "Receitas",
                    data: lstReceita
                }, {
                    label: "Despesas",
                    data: lstDespesa
                }]
            };

            window.onload = function() {
                var chartEl = document.getElementById("chart1");
                window.myLine = new Chart(chartEl, {
                    type: 'line',
                    data: lineChartData,
                    options: {
                        title: {
                            display: true,
                            text: 'Comparativo Gráfico de Performance Financeira'
                        },
                        tooltips: {
                            enabled: true
                        }
                    }
                });
            };
        };

        function OnErrorCall(response) {
            alert('error');
        }
    });

</script>
    
asked by anonymous 06.11.2016 / 00:47

1 answer

0

The $(function() { line is the jQuery way of calling window.load . Code that is within $(function() {...}); will only be run when the DOM is loaded. So you have the line window.onload = function() {...}; is a redundancy and in some cases (eg when there are other window.load ) this will prevent the code from running.

Remove window.onload = function() {...}; and leave only what's inside, this should solve your problem.

    
07.11.2016 / 09:51