【问题标题】:redraw method is not refreshing polar chart重绘方法不刷新极坐标图
【发布时间】:2017-07-26 06:49:56
【问题描述】:

我有一个类似的问题,就像在highchart chart redraw method is not refreshing the chart 上发布的一样,但我正在使用极坐标图,所以那里给出的解决方案不能解决我的问题。

所以,下面的代码正确显示了高图,但没有刷新数据。现在我正在寻求建议/帮助如何解决它。

$(function() {

$.getJSON('wind_graph.php?callback=?', function(dataWind) {

 var direction = Wind_direction;

  var polarOptions = {
    chart: {
      polar: true,
      events : {
          load : function () {
              setInterval(function(){
                RefreshDataWind();
                }, 1000);
          }
      }
    },
    title: {
      text: 'Wind Direction'
    },
    pane: {
      startAngle: 0,
    },
    tooltip: {
      enabled: false
    },
    legend: {
      enabled: false
    },
    // the value axis
    xAxis: {
      tickInterval: 15,
      min: 0,
      max: 360,
      labels: {
        formatter: function() {
          return this.value + '°';
        }
      }
    },

    plotOptions: {
      series: {
        pointStart: 0,
        pointInterval: 30,
        marker: {
          enabled: false
        },
      },
    }
  };

  // The polar chart
  $('#graph-1').highcharts(Highcharts.merge(polarOptions, {
    yAxis: {
      tickInterval: 5,
      min: 0,
      max: 25,
      visible: false
    },
    credits: {
      enabled: false
    },
    series: [{
        type: 'line',
        name: 'Direction',
        data: [
          [0, 0],
          [direction, 20]
        ],
        lineColor: '#7cb5ec',
        enableMouseTracking: false,
        visible: true,
        lineWidth: 2,
        zIndex: 8,
      }
    ]
  }));

  function RefreshDataWind()
  {
    var chart = $('#graph-1').highcharts();

    $.getJSON('wind_graph.php?callback=?', function(dataWind)
    {
        var direction = Wind_direction;
        chart.redraw();
    }); 

    chart.redraw();
  }
});
});

更准确地说:如果给定的 Wind_direction 值等于 0(零),那么我需要在图表上显示以下“样条线”:

{
        type: 'spline',
        name: 'CentralCicrleCalmWind1',
        data: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
        pointInterval: 30,
        pointStart: 0,
        lineColor: windLineColor,
        enableMouseTracking: false,
        lineWidth: windLineWidth,
        visible: showCentralCicrleCalmWind,
      }, {
        type: 'spline',
        name: 'CentralCicrleCalmWind2',
        data: [2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5],
        pointInterval: 30,
        pointStart: 0,
        lineColor: windLineColor,
        enableMouseTracking: false,
        lineWidth: windLineWidth,
        visible: showCentralCicrleCalmWind,
      }

如您所见,我将“showCentralCircleCalmWind”等附加参数设置为 TRUE 或 FALSE,这取决于我在代码顶部准备的给定“Wind_direction”值和逻辑(此处未粘贴)。 我需要的是:

  1. 读取给定 JSON 中变量的值
  2. 在javascript代码的开头设置变量“direction”
  3. 使用 higcharts 库显示图表
  4. 从 JSON 中读取新值
  5. 将变量“direction”更改为新值。
  6. 显示给定值的新图表
  7. 回到第 4 点...

【问题讨论】:

  • 最后一个chart.redraw(); 似乎没有必要。去掉它 。你的浏览器控制台有什么错误吗?
  • 我在浏览器控制台中没有任何错误。只有关于长时间运行的 JavaScirpt 任务的违规警告需要 369 毫秒。也许我应该改变jquery版本?实际上我使用的是 jquery 1.11.3
  • 更换最新的并检查。还有为什么要花这么多时间?
  • 更改为 3.1.1.min 并且 Violation 错误不再显示,但刷新仍然无效。

标签: javascript jquery charts highcharts data-visualization


【解决方案1】:

使用setData() 函数可能会解决您的问题(请参阅http://api.highcharts.com/highcharts/Series.setData)。

在您的示例中,我建议如下:

function RefreshDataWind()
{
    var chart = $('#graph-1').highcharts();

    $.getJSON('wind_graph.php?callback=?', function(dataWind)
    {
        var direction = Wind_direction;
        chart.series[0].setData(direction);
        /* assuming "direction" to be an array like [1, 2, 3] */
    }); 

}

以下 Highcharts 演示向您展示了其工作原理:http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/members/series-setdata/

根据您的“Wind_direction”变量的格式,您可能需要在setData() 之前有一个声明,明确将其设为数组,因为这是函数所期望的。

我还建议您删除 chart.redraw() 的第二个实例,因为 setData() 使这变得不必要。

希望对你有帮助!

【讨论】:

  • 好的,我们走的很好,但更准确地说 - 我需要更新代码第 5 行中的 var 方向值,因为我的代码比我的代码复杂得多在这里张贴。还有一些数据要绘制在同一张图表上,它们取决于这个变量。
  • 那么,您的变量“Wind_direction”在此图表中有多个系列的数据?
  • 不,变量“Wind_direction”用于多个系列(我有大约 20 个)。另外 - 我正在使用函数 chart.renderer.text() 在图表上渲染文本以​​显示“Wind_direction”变量的当前值,并且该渲染器也应该刷新。
  • 您需要遍历系列/数据并更改应该更改的内容。不要更新“变量”,更新数据/系列属性。如果您有自定义文本,则需要通过 text.attr({text: 'new text'}) 手动更改它
猜你喜欢
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
相关资源
最近更新 更多