最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Php array and chartjs - Stack Overflow

programmeradmin1浏览0评论

i need help with chart.js (www.chartjs.or).

I've got an array in php, lets say

    $array_with_data = array(1,1,1,1,1,1,2,1,2);

this array is from mysql database. And now I want to use this chart.js, where in this line

      data: [12, 19, 3, 5, 2, 3, 2,3,1,2,4,5],

i want replace this array data with my array in php. How can I do this? :) The full code of this chart is at the bottom.

<script src=".js/2.6.0/Chart.js"></script>
<script>
    var myChart = new Chart(ctx, {...});
</script>


<canvas id="myChart" width="400" height="400"></canvas>
<script>



	
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange", "Orange", "Orange", "Orange", "Orange", "Orange", "Orange"],
        datasets: [{
            label: '# of Votes',
		
            data: [12, 19, 3, 5, 2, 3, 2,3,1,2,4,5],
            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: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});
</script>

i need help with chart.js (www.chartjs.or).

I've got an array in php, lets say

    $array_with_data = array(1,1,1,1,1,1,2,1,2);

this array is from mysql database. And now I want to use this chart.js, where in this line

      data: [12, 19, 3, 5, 2, 3, 2,3,1,2,4,5],

i want replace this array data with my array in php. How can I do this? :) The full code of this chart is at the bottom.

<script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.6.0/Chart.js"></script>
<script>
    var myChart = new Chart(ctx, {...});
</script>


<canvas id="myChart" width="400" height="400"></canvas>
<script>



	
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange", "Orange", "Orange", "Orange", "Orange", "Orange", "Orange"],
        datasets: [{
            label: '# of Votes',
		
            data: [12, 19, 3, 5, 2, 3, 2,3,1,2,4,5],
            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: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});
</script>

Share Improve this question edited Jul 21, 2017 at 11:11 kasiulks asked Jul 21, 2017 at 11:05 kasiulkskasiulks 311 silver badge3 bronze badges 2
  • Sorry! Wrong code! – kasiulks Commented Jul 21, 2017 at 11:10
  • Try this var data = <?php echo $data = json_encode($array_with_data); ?> – Anand Pandey Commented Jul 21, 2017 at 11:19
Add a ment  | 

1 Answer 1

Reset to default 5

In php

$data = json_encode($array_with_data);

In your js

var data = <?php echo $data ?>;
发布评论

评论列表(0)

  1. 暂无评论