dqmq0654 2015-08-11 12:34
浏览 76
已采纳

统计页面 - 逐个加载每个统计信息<div>

I want to show some apache statistics within my webpage (cumulated accesses per hour). Therefore I use jQuery with chart.js.

Now I want to fill every chart via ajax (with a loading icon).

First I create alle the div's via php while:

    <?php
        $SQL = $mysqli->query("SELECT * FROM server ORDER BY vname ASC");

        $count = 0;

        while ($row = $SQL->fetch_assoc()){
             $count++;
    ?>
        <div class="col-lg-6">
             <!-- Lines Chart -->
                  <div class="block">
                       <div class="block-header bg-primary">
                           <ul class="block-options">
                               <li>
                                  <button type="button" data-toggle="block-option" data-action="refresh_toggle" data-action-mode="demo"><i class="si si-refresh" style="color:white;"></i></button>
                                </li>
                           </ul>
                           <h3 class="block-title"><?php echo $row['name'];?> <small style="color: white;"><?php echo $row['vname'];?></small></h3>
                            </div>
                       <div class="block-content block-content-full text-center">
                       <!-- Lines Chart Container -->
                       <div style="height: 330px;"><canvas class="js-chartjs-lines-<?php echo $row['HSID'];?>"></canvas></div>
                        </div>
                   </div>
          </div>
<?php
    }
?>

Now I want to fill every div with the chart.js properties like this:

<script>
$(document).ready(function(){


    var $chartLinesCon  = jQuery('.js-chartjs-lines-2')[0].getContext('2d');

    var $globalOptions = {
        scaleFontFamily: "'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif",
        scaleFontColor: '#999',
        scaleFontStyle: '600',
        tooltipTitleFontFamily: "'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif",
        tooltipCornerRadius: 3,
        maintainAspectRatio: false,
        responsive: true
    };

    // Chart Data
    var $chartLinesBarsRadarData = {
        labels: [<?php for ($i = 0; $i <= 24; $i++){ echo $i.','; }?>],
        datasets: [
            {
                label: 'This Week',
                fillColor: 'rgba(171, 227, 125, .3)',
                strokeColor: 'rgba(171, 227, 125, 1)',
                pointColor: 'rgba(171, 227, 125, 1)',
                pointStrokeColor: '#fff',
                pointHighlightFill: '#fff',
                pointHighlightStroke: 'rgba(171, 227, 125, 1)',
                data: [15, 16, 20, 25, 0, 0, 23, 25, 32]
            }
        ]
    };

    $chartLines = new Chart($chartLinesCon).Line($chartLinesBarsRadarData, $globalOptions);


});

How to do this the best way?

A javascript 'for loop' (+ hidden input element in the div for giving the serverID to the ajax script)?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongrouli2667 2015-08-15 04:56
    关注

    You need to

    1. Move / copy the block you have in your $(document).ready above into your AJAX callback.

    2. Replace the (currently hardcoded) array with your AJAX result (reformatting if necessary into a flat array)

    3. Attach an onclick event handler for your refresh button that

      1. Calls a .destroy() on the corresponding chart instance

      2. Replaces the canvas element with your loading icon

      3. Does the AJAX call (that has the callback from Step 1 above)

    4. In Your AJAX callback handler, you replace the loading icon with the canvas before doing the remaining steps (from Step 1 above) to (re)initialize the chart

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真