【问题标题】:clear cache from Highchart when loading data from a json file从 json 文件加载数据时从 Highchart 清除缓存
【发布时间】:2014-09-16 07:37:36
【问题描述】:

我正在做以下事情:

  1. 使用python django中的一些逻辑创建一个json文件
  2. 这个json文件现在被high chart js代码用来渲染饼图

highchart js 代码如下:

//高图json饼图

$(document).ready(function() {
console.log("hi from high chart from json PIE")

  $.getJSON("{% static 'money/data/highchartpie.json' %}", function(json) {
    console.log("haha i have read the json")
   $('#containerHighChartJSONPie').highcharts({
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: 1,//null,
        plotShadow: false
    },
    title: {
        text: 'Expenses per Types of Expenditures'
    },
    tooltip: {
        pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    },
    series: [{
        type: 'pie',
        name: 'Type of Expenditure',
        data: json
    }]
});
});

});

json 中的数据是: [["杂货店",50.0],["杂项",30.0]]

问题 以下会根据需要生成良好的饼图,如果更改了任何数据,则图表也会更改,但有时图表会在其中显示更新的数据。我试过了:

  1. 从另一个浏览器运行它 - 它显示带有新值的更新图表
  2. 已清除缓存并在同一浏览器中再次尝试,现在它显示更新的代码

所以这似乎是一个缓存问题,但是有什么办法可以在 highchart 代码中解决这个问题?

【问题讨论】:

    标签: javascript jquery json highcharts


    【解决方案1】:

    您可以通过调用以下命令将缓存设置为false,它将为您的所有请求禁用:

    $(document).ready(function() {
      $.ajaxSetup({ cache: false });
    });
    

    看到这个问题:How to set cache false for getJSON in JQuery?

    为了更精确的缓存处理,将getJSON 更改为ajax jquery 调用,将datatype 设置为JSON:事实上,$.getJSON 是以下操作的快捷方式:

    $.ajax({
        dataType: "json",
        url: url,
        data: data,
        success: success
    });
    

    所以如果你想在你的请求中设置cache: false,你应该这样添加:

    $.ajax({
        cache: false,
        dataType: "json",
        url: url,
        data: data,
        success: success
    });
    

    【讨论】:

    • 设置缓存效果很好,甚至用 ajax 替换 getJSON 效果很好。谢谢:)
    • 正如我所说,一个是全局的,另一个是本地的(只有这个请求)所以使用正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    相关资源
    最近更新 更多