Javascript 如何动态填充我的图表

Javascript 如何动态填充我的图表,javascript,chart.js,pie-chart,adminlte,Javascript,Chart.js,Pie Chart,Adminlte,请帮助我如何从数据库数据中动态填充此图表JS piechart,以便用piechart显示。我的意思是从我的数据库中获取一个查询,并将数据库提供的数据转换为与chartjs格式所需的数据兼容的数据。请帮帮我 我正在使用ajax从数据库中获取值: function getpieclinic() { $.ajax({ url: siteurl+"patients_report/piedataclinic", type: "GET", dataType

请帮助我如何从数据库数据中动态填充此图表JS piechart,以便用piechart显示。我的意思是从我的数据库中获取一个查询,并将数据库提供的数据转换为与chartjs格式所需的数据兼容的数据。请帮帮我

我正在使用ajax从数据库中获取值:

function getpieclinic() {
    $.ajax({
       url: siteurl+"patients_report/piedataclinic",
       type: "GET",
       dataType: "JSON",
        success:function(response) {
            alert(response);
        }
    });
}
下面的代码有一个JSON对象值,该值包含以下查询结果(响应值):

以下是piechart示例中的数据:

var pieChartCanvas = $("#pieChart").get(0).getContext("2d");
    var pieChart = new Chart(pieChartCanvas);
    var PieData = [
      {
        value: 700,
        color: "#f56954",
        highlight: "#f56954",
        label: "Chrome"
      },
      {
        value: 500,
        color: "#00a65a",
        highlight: "#00a65a",
        label: "IE"
      },
      {
        value: 400,
        color: "#f39c12",
        highlight: "#f39c12",
        label: "FireFox"
      },
      {
        value: 600,
        color: "#00c0ef",
        highlight: "#00c0ef",
        label: "Safari"
      },
      {
        value: 300,
        color: "#3c8dbc",
        highlight: "#3c8dbc",
        label: "Opera"
      },
      {
        value: 100,
        color: "#d2d6de",
        highlight: "#d2d6de",
        label: "Navigator"
      }
    ];
    var pieOptions = {
      //Boolean - Whether we should show a stroke on each segment
      segmentShowStroke: true,
      //String - The colour of each segment stroke
      segmentStrokeColor: "#fff",
      //Number - The width of each segment stroke
      segmentStrokeWidth: 2,
      //Number - The percentage of the chart that we cut out of the middle
      percentageInnerCutout: 50, // This is 0 for Pie charts
      //Number - Amount of animation steps
      animationSteps: 100,
      //String - Animation easing effect
      animationEasing: "easeOutBounce",
      //Boolean - Whether we animate the rotation of the Doughnut
      animateRotate: true,
      //Boolean - Whether we animate scaling the Doughnut from the centre
      animateScale: false,
      //Boolean - whether to make the chart responsive to window resizing
      responsive: true,
      // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
      maintainAspectRatio: true,
      //String - A legend template
      legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
    };
    //Create pie or douhnut chart
    // You can switch between pie and douhnut using the method below.
    pieChart.Doughnut(PieData, pieOptions);
var pieChartCanvas=$(“#pieChart”).get(0.getContext(“2d”);
var pieChart=新图表(pieChartCanvas);
变量数据=[
{
价值:700,
颜色:“f56954”,
亮点:“f56954”,
标签:“铬”
},
{
价值:500,
颜色:“00a65a”,
亮点:“00a65a”,
标签:“IE”
},
{
价值:400,
颜色:“f39c12”,
亮点:“f39c12”,
标签:“FireFox”
},
{
价值:600,
颜色:“00c0ef”,
亮点:“00c0ef”,
标签:“狩猎”
},
{
价值:300,
颜色:“3c8dbc”,
亮点:“3c8dbc”,
标签:“歌剧”
},
{
数值:100,
颜色:“d2d6de”,
亮点:“d2d6de”,
标签:“导航器”
}
];
变量选项={
//布尔-我们是否应该在每个线段上显示笔划
这是真的,
//字符串-每段笔划的颜色
segmentStrokeColor:#fff“,
//数字-每个线段笔划的宽度
分段行程宽度:2,
//数字-我们从中间剪下的图表百分比
percentageInnerCutout:50,//对于饼图,这是0
//Number-动画步数
动画步骤:100,
//字符串动画效果
动画化:“easeOutBounce”,
//布尔-是否设置圆环旋转的动画
动画片:对,
//布尔值-是否设置从中心缩放圆环的动画
动画缩放:错误,
//布尔值-是否使图表响应窗口大小调整
回答:是的,
//布尔值-响应时是否保持起始纵横比,如果设置为false,将占用整个容器
维护Aspectratio:是的,
//字符串-图例模板
legendTemplate:“
    ” }; //创建饼图或双圆图 //您可以使用下面的方法在pie和douhnut之间切换。 pieChart.甜甜圈(PieData,pieOptions);
    我认为您在为图表表单JSON创建数据方面遇到了困难。以下是解决方案:

    您的JSON字符串:

    $json = '[{"clinic_name":"Clinic 1","total_checked_up":"4"},{"clinic_name":"Clinic 2","total_checked_up":"0"},{"clinic_name":"Clinic 3","total_checked_up":"0"},{"clinic_name":"Clinic 4","total_checked_up":"0"}]';
    
    首先将其转换为数组:

    $array = json_decode($json);
    
    声明空变量:

    $string_array = '';
    
    循环抛出数组:

    foreach($array as $single_array){
    
        $string_array[] = array(
        'value'=>$single_array->total_checked_up,
        'color'=>'#f56954',
        'highlight'=>'#f56954',
        'label'=>$single_array->clinic_name
        );  
    }
    
    再次将数组转换为JSON以获取所需数据:

    var PieData  = json_encode($string_array);
    
    这就是您的数据的外观

    [
        {
            "value": "4",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 1"
        },
        {
            "value": "0",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 2"
        },
        {
            "value": "0",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 3"
        },
        {
            "value": "0",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 4"
        }
    ]
    

    我认为您在为图表表单JSON创建数据方面遇到了困难。以下是解决方案:

    您的JSON字符串:

    $json = '[{"clinic_name":"Clinic 1","total_checked_up":"4"},{"clinic_name":"Clinic 2","total_checked_up":"0"},{"clinic_name":"Clinic 3","total_checked_up":"0"},{"clinic_name":"Clinic 4","total_checked_up":"0"}]';
    
    首先将其转换为数组:

    $array = json_decode($json);
    
    声明空变量:

    $string_array = '';
    
    循环抛出数组:

    foreach($array as $single_array){
    
        $string_array[] = array(
        'value'=>$single_array->total_checked_up,
        'color'=>'#f56954',
        'highlight'=>'#f56954',
        'label'=>$single_array->clinic_name
        );  
    }
    
    再次将数组转换为JSON以获取所需数据:

    var PieData  = json_encode($string_array);
    
    这就是您的数据的外观

    [
        {
            "value": "4",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 1"
        },
        {
            "value": "0",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 2"
        },
        {
            "value": "0",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 3"
        },
        {
            "value": "0",
            "color": "#f56954",
            "highlight": "#f56954",
            "label": "Clinic 4"
        }
    ]
    

    任何答案请告诉我需要它任何答案请告诉我需要它