IT/PHP

[PHP] 구글 파이 차트 사용 및 mysql DB 연동, mysql 배열 출력

월공 2020. 8. 26. 10:50
728x90
300x250

위와 같은 파이차트를 출력했다

사실 이전에 포스팅한 컬럼 차트와 크게 다를게 없고, 그냥 자바스크립트 내부만 좀 바꿔주면 된다

https://bug41.tistory.com/32

 

javascript 부분

$.ajax({
  type: "POST",
  url : "ajax.php",
  data	:{ 각종 데이터 seq, id 등등 },  
  success	: function (res) {
  
    var pieData = google.visualization.arrayToDataTable(JSON.parse(res));

    var options = {
      title: '파이통계',
      colors: ['#3F6D98','#D26812']
    };

    $("#piechart_material").show();
    var chart = new google.visualization.PieChart(document.getElementById('piechart_material'));
    chart.draw(pieData, options);    

  }
});

 

res 가져오는 데이터 형식이 아래와 같아야하고 , 저대로 출력되게끔 ajax.php 에서 DB 를 출력하고 배열을 요리조리 조합하고 마지막에 json_encode 로 return 해줘야한다

[["\uc77c\ubcc4","\uacac\uc801\uac74\uc218"],["\uc790\uc720\ud1b5\ud654",80],["\uc77c\ubc18\uc694\uae08\uc81c",39]]

 

728x90
300x250