multiple chartjs in the same page

后端 未结 6 496
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 11:04

hi i was trying to use chartjs can be found in this link www.chartjs.org

i tried to draw two chart in the same page using the samples code

i created two dif

相关标签:
6条回答
  • 2021-01-01 11:43

    You could rather user jQuery to get the canvas object

    var ctx = $parent.find('#' + idOfCanvas).get(0).getContext("2d");
    

    now make sure the code below is not throwing js error at all

    window.onload = function(){
            var ctx2 = document.getElementById("chart2").getContext("2d");
            window.myPie = new Chart(ctx2).Pie(pieData);
        };
    

    as you know, if any error in js occurs the js-runtime halts all the latter lines of code

    0 讨论(0)
  • 2021-01-01 11:47

    I have not worked on different type of charts but I worked on an example and created two bar chart in a page using this code:

    <div style="width: 50%">
        <canvas id="canvas" height="450" width="600"></canvas>
    </div>
    
    <div style="width: 50%">
        <canvas id="canvas2" height="450" width="600"></canvas>
    </div>
    

    and in script part I do like this:

    var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
    
    var barChartData = {
        labels : ["January","February","March","April","May","June","July"],
        datasets : [
            {
                fillColor : "rgba(220,220,220,0.5)",
                strokeColor : "rgba(220,220,220,0.8)",
                highlightFill: "rgba(220,220,220,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            },
            {
                fillColor : "rgba(151,187,205,0.5)",
                strokeColor : "rgba(151,187,205,0.8)",
                highlightFill : "rgba(151,187,205,0.75)",
                highlightStroke : "rgba(151,187,205,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            }
        ]
    
    }
        var barChartData2 = {
        labels : ["January","February","March","April","May","June","July"],
        datasets : [
            {
                fillColor : "rgba(220,220,220,0.5)",
                strokeColor : "rgba(220,220,220,0.8)",
                highlightFill: "rgba(220,220,220,0.75)",
                highlightStroke: "rgba(220,220,220,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            },
            {
                fillColor : "rgba(151,187,205,0.5)",
                strokeColor : "rgba(151,187,205,0.8)",
                highlightFill : "rgba(151,187,205,0.75)",
                highlightStroke : "rgba(151,187,205,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            }
        ]
    
    }
    window.onload = function(){
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myBar = new Chart(ctx).Bar(barChartData, {
            responsive : true
        });
        var ctx2 = document.getElementById("canvas2").getContext("2d");
        window.myBar = new Chart(ctx2).Bar(barChartData2, {
            responsive : true
        });
    }
    
    0 讨论(0)
  • 2021-01-01 11:51

    I try this code ... this solve your problem

     var barChartData = {
                labels: [<?php echo $str_indiv_value; ?>],
                datasets: [{
                    label: 'Dataset 1',
                    backgroundColor: [
                        window.chartColors.red,
                        window.chartColors.orange,
                        window.chartColors.yellow,
                        window.chartColors.green,
                        window.chartColors.blue,
                        window.chartColors.purple,
                        window.chartColors.red
                    ],
                    yAxisID: 'y-axis-1',
                    data: [
                       <?php echo $str_indiv_requred;?>
                    ]
                }]
    
            };
    
            var barChartData_2 = {
                labels: [<?php echo $str_indiv_value; ?>],
                datasets: [{
                    label: 'Dataset 1',
                    backgroundColor: [
                        window.chartColors.red,
                        window.chartColors.orange,
                        window.chartColors.yellow,
                        window.chartColors.green,
                        window.chartColors.blue,
                        window.chartColors.purple,
                        window.chartColors.red
                    ],
                    yAxisID: 'y-axis-1',
                    data: [
                        <?php echo $str_indiv_requred;?>
                    ]
                }]
    
            };
    
    
            window.onload = function() {
                var ctx = document.getElementById('canvas').getContext('2d');
                window.myBar = new Chart(ctx, {
                    type: 'bar',
                    data: barChartData,
                    options: {
                        responsive: true,
                        title: {
                            display: true,
                            text: 'Chart.js Bar Chart - Multi Axis'
                        },
                        tooltips: {
                            mode: 'index',
                            intersect: true
                        },
                        scales: {
                            yAxes: [{
                                type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
                                display: true,
                                position: 'left',
                                id: 'y-axis-1',
                            }],
                        }
                    }
                });
    
    
    
    
                var ctx_2 = document.getElementById('canvas2').getContext('2d');
                window.myBar = new Chart(ctx_2, {
                    type: 'bar',
                    data: barChartData_2,
                    options: {
                        responsive: true,
                        title: {
                            display: true,
                            text: 'Chart.js Bar Chart - Multi Axis'
                        },
                        tooltips: {
                            mode: 'index',
                            intersect: true
                        },
                        scales: {
                            yAxes: [{
                                type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
                                display: true,
                                position: 'left',
                                id: 'y-axis-1',
                            }],
                        }
                    }
                });
    
    
    
            };
    
    0 讨论(0)
  • 2021-01-01 11:53

    Use only 1 window.onload

    function myfunc1{
    }
    function myfunc2{
    }
    function start(){
    myfunc1();
    myfunc1();
    }
    window.onload = start(); 
    

    Ref: http://www.htmlgoodies.com/beyond/javascript/article.php/3724571/Using-Multiple-JavaScript-Onload-Functions.htm

    0 讨论(0)
  • 2021-01-01 12:07

    First off, you only need one window.onload event. No reason to have two separate instances of that.

    Second, the data sets for Pie chart and Line chart are actually very different.

    Pie chart sample data:

            self.pieData=  [
            {
                value: 65,
                color:"#F7464A",
                highlight: "#FF5A5E",
                label: "New Scenarios"
            },
            {
                value: 297,
                color: "#46BFBD",
                highlight: "#5AD3D1",
                label: "Responses Submitted"
            },
            {
                value: 225,
                color: "#64a789",
                highlight: "#5AD3D1",
                label: "Responses Graded"
            }
    
        ]
    

    Line chart sample data:

    self.lineData= {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
            {
                label: "New Tests",
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "#64a789",
                pointColor: "#64a789",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [65, 59, 80, 81, 56, 55, 40]
            },
            {
                label: "Responses",
                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: [128, 148, 140, 119, 186, 127, 190]
            },
            {
                label: "Responses Graded",
                fillColor: "rgba(151,187,205,0.2)",
                strokeColor: "#41e498",
                pointColor: "#41e498",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(151,187,205,1)",
                data: [108, 116, 120, 112, 136, 121, 111]
            }
        ]
    };
    

    The line chart is likely not initializing because you are feeding it the wrong kind of data.

    0 讨论(0)
  • 2021-01-01 12:09

    Use only one window.onload

    window.onload = function () {
            window.myRadar = new Chart(document.getElementById("canvas1").getContext("2d")).Radar(radarChartData, {
                responsive: true
            });
            var G2 = document.getElementById("canvas2").getContext("2d");
            window.myBar = new Chart(G2).Bar(barChartData, {
                responsive: true
            });
        }
    
    0 讨论(0)
提交回复
热议问题