weixin_33735077 2018-02-14 02:24 采纳率: 0%
浏览 154

chart.js数据不会更新

im trying to fetch data with ajax request to update chart.js. the ajax request is fine. but the response wont update the chart. this is how i fetch data :

<script type="text/javascript">
$(document).ready(function(){
    $("#data-row").hide();
    $("#btnCari").click(function(){
        var periode = $("select[name='tahun']").val();
        $.ajax({
            url:"<?php echo site_url('laporan/get_keuntungan/')?>"+periode,
            success:function(data){
                var graph = data;
                    data: {
                        labels: ["Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli","Agustus",'September','Oktober','November','Desember'],
                        datasets: [
                            {
                                label: "Penjualan",
                                fill: true,
                                lineTension: 0.3,
                                backgroundColor: gradient1,
                                borderColor: gradient1,
                                borderCapStyle: 'butt',
                                borderDash: [],
                                borderDashOffset: 0.0,
                                borderJoinStyle: 'miter',
                                borderWidth: 1,
                                pointBorderColor: gradient1,
                                pointBackgroundColor: "#fff",
                                pointBorderWidth: 1,
                                pointHoverRadius: 5,
                                pointHoverBackgroundColor: gradient1,
                                pointHoverBorderColor: "rgba(220,220,220,1)",
                                pointHoverBorderWidth: 2,
                                pointRadius: 1,
                                pointHitRadius: 10,
                                data: [graph],
                                spanGaps: false
                            }
                        ]
                    }
                });
            $("#data-row").fadeOut("slow");
            $("#data-row").fadeIn("slow");

            }
        })
    });
    })

the ajax response is look like this :

'10,20,30,40,50,10,20,30,30'

but it wont update the chart.

  • 写回答

1条回答 默认 最新

  • ??yy 2018-02-14 07:54
    关注

    Probally you're forgetting two things.

    First: You need select the chart, something like this:
    var ctx = document.getElementById("myChart").getContext('2d');

    for use:

    var myChart = new Chart(ctx, {
    type: 'bar',
    data: {//your code to build chart}
    

    Second: myChart.update();

    chartjs.org/docs/latest/
    chartjs.org/docs/latest/developers/updates.html

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号