2020-09-21

   <script src="/js/ChartJs/Chart.js"></script>

     <div id="canvas-holder" style="width:30%;margin:0 auto;">
        <canvas id="chart-area" width="300" height="300" />
    </div>
 
    <script>
    
var colors = [
"#F7464A",
"#46BFBD",
"#FDB45C",
"#949FB1",
"#4D5360",
];
    var config = {
        type: 'doughnut',
        data: {
            datasets: [{
                data: [
                    {{ implode(",",array_values($data)) }}
                ],
                backgroundColor: [
                     @for($i=0;$i<count($data);$i++)
                    colors[{{ $i }} % colors.length],
                     @endfor
                  
                ],
                
            }, {
                data: [
                    {{ implode(",",array_values($data1)) }}
                ],
                backgroundColor: [
                    @for($i=0;$i<count($data1);$i++)
                    colors[{{ $i }}% colors.length],
                     @endfor
                   
                ],
                
            }],
            labels: [
              "{!! implode('","',array_keys($data)) !!}"
            ]
        },
        options: {
            responsive: true,
            tooltips: { 
               callbacks: { 
               label: function(tooltipItem, data) { 
                    //get the concerned dataset 
                    var dataset = data.datasets[tooltipItem.datasetIndex]; 
                    //calculate the total of this data set 
                    // var total = dataset.data.reduce(function(previousValue, currentValue, currentIndex, array) { 
                    // return previousValue + currentValue; 
                    // }); 
                    //get the current items value 
                    var currentValue = dataset.data[tooltipItem.index]; 
                    //calculate the precentage based on the total and current item, also this does a rough rounding to give a whole number 
                    // var precentage = Math.floor(((currentValue/total) * 100)+0.5); 
                    // console.log(tooltipItem, data);
                    if(tooltipItem.datasetIndex == 0){
                         return data.labels[tooltipItem.index]+"作品数:"+currentValue+"条" ; 
                    }
                    return data.labels[tooltipItem.index]+"学者数:"+currentValue+"条" ; 
                    } 
               } 
               },
               onClick: function(event, legendItem) {
                                   // console.log(legendItem[0]);
                    showSchools(legendItem[0]._model.label);
               }
        }
    };

    window.onload = function() {
        var ctx = document.getElementById("chart-area").getContext("2d");
        window.myDoughnut = new Chart(ctx, config);
        console.log(window.myDoughnut);
    };
 function showSchools(country){
     console.log(country);
     $(".schools ul").empty();
     $.get("/api/get_school_by_country/"+country,function(data){
          // console.log(data);
          for(var i = 0; i<data.length;i++){
               $(".schools ul").append("<li>"+data[i]+"</li>");
          }
               
         
     });
 }
   
    </script>

双环图表,带点击事件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值