2

I've been looking for a long time here in StackOverflow, searching for a solution to my problem. I already know that it is about window.onload that's been opened two times in my script, but I can't fix it. I'm looking it for days. Could you guys please help me?

Here's my chartBarSample.php code:

<script>
    var optionsBar = {
        responsive: true
    };

    var dataBar = {
        labels: ["Outubro", "Novembro", "Dezembro"],
        datasets: [
            {
                label: "CPF's Enviados",
                fillColor: "#00335A",
                strokeColor: "#00243F",
                highlightFill: "#00243F",
                highlightStroke: "#00192B",
                data: [6920, 7226, 7969]
            },
            {
                label: "Propostas Finalizadas",
                fillColor: "#007CDA",
                strokeColor: "#0066B4",
                highlightFill: "#0066B4",
                highlightStroke: "#005290",
                data: [6325, 6825, 7586]
            },
            {
                label: "Propostas Aprovadas",
                fillColor: "#2B8B4A",
                strokeColor: "#196431",
                highlightFill: "#196431",
                highlightStroke: "#114622",
                data: [5463, 6606, 6501]
            }
        ]
    };

    var optionsLine = {
        responsive: true
    };

    var dataLine = {
        labels: ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
        datasets: [
            {
                label: "Dados primários",
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(220,220,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb(), randomnb()]
            },
            {
                label: "Dados secundários",
                fillColor: "rgba(151,187,205,0.2)",
                strokeColor: "rgba(151,187,205,1)",
                pointColor: "rgba(151,187,205,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(151,187,205,1)",
                data: [28, 48, 40, 19, 86, 27, 90, 200, 87, 20, 50, 20]
            }
        ]
    };     

    function start(){
        var ctx = document.getElementById("graficoBarra").getContext("2d");
        var BarChart = new Chart(ctx).Bar(dataBar, optionsBar);

        var ctx2 = document.getElementById("graficoLinha").getContext("2d");
        var LineChart = new Chart(ctx2).Line(dataLine, optionsLine);
    }

    window.onload = start;
</script>
2
  • What is not working? Commented Nov 4, 2016 at 20:11
  • @TheValyreanGroup only one charts is opening (the second one). Commented Nov 7, 2016 at 10:31

1 Answer 1

2

Your chart definitions are incorrect, try this:

function start(){
    var ctx = document.getElementById("graficoBarra").getContext("2d");
    var BarChart = new Chart(ctx, {
          type: 'bar',
          data: dataBar,
          options: optionsBar          
    });

    var ctx2 = document.getElementById("graficoLinha").getContext("2d");
    var LineChart = new Chart(ctx2, {
          type: 'line',
          data: dataLine,
          options: optionsLine          
    });
}

Full Code in Codepen: 2 Chart.js Charts Result:

enter image description here

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

4 Comments

It worked man, thanks! But do you know why the charts are both white? @Keno
@João you can change the background color in the css: #graficoBarra, #graficoLinha{background-color: white;} also, please accept the answer so I can get my imaginary points : )
Already accepted, but do you know why it's not working inside the script, instead of using css to paint the charts? @Keno
@João: Thx, by default they render transparent and you usually use css, there is a way with plugins, check the Codepen Example or this:[stackoverflow.com/questions/30464750/… set background)

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.