饼图Highcharts使用setData不工作

问题描述 投票:1回答:2

我显示使用Highchart库的饼图。我有两组数据我想说明的,我的两个集合之间切换每5秒无限。当我加载的初始组数据,在图表显示,它应。并且当我加载所述第二组数据(使用setData),图表的变化,它应。然而,setData所有后续调用似乎什么也不做。我希望图表改变一次又一次地对应于新的数据。但没有任何反应,并且没有记录在控制台的错误。还有什么比我做错了?

这里是我的代码:

var chart24HoursTop5;
var lastSet = 1;
var set1 = [{
    "seconds": 754,
    "y": 754,
    "downtime": "00:05:07",
    "siteNumber": "13",
    "siteName": "Preston",
    "name": "Preston",
    "motorolaNumber": "SZ092C113",
    "system": "SZ Quantar",
    "channels": "5",
    "towerOwner": "Wynn Communications",
    "shelterOwner": "Wynn Communications",
    "equipmentOwner": "DPS",
    "latitude": "35.724833",
    "longitude": "-95.988333",
    "color": "#348AA7"
}, {
    "seconds": 20,
    "y": 20,
    "downtime": "00:00:20",
    "siteNumber": "29",
    "siteName": "Bakers Peak",
    "name": "Bakers Peak",
    "motorolaNumber": "SZ092C129",
    "system": "SZ Quantar",
    "channels": "5",
    "towerOwner": "FBI",
    "shelterOwner": "DPS",
    "equipmentOwner": "DPS",
    "latitude": "34.839722",
    "longitude": "-98.803333",
    "color": "#ffa600"
}, {
    "seconds": 19,
    "y": 19,
    "downtime": "00:00:19",
    "siteNumber": "30",
    "siteName": "Walters",
    "name": "Walters",
    "motorolaNumber": "SZ092C130",
    "system": "SZ Quantar",
    "channels": "5",
    "towerOwner": "Cotton Electric",
    "shelterOwner": "DPS",
    "equipmentOwner": "DPS",
    "latitude": "34.358583",
    "longitude": "-98.321583",
    "color": "#cb5464"
}, {
    "seconds": 12,
    "y": 12,
    "downtime": "00:00:12",
    "siteNumber": "69",
    "siteName": "Hominy",
    "name": "Hominy",
    "motorolaNumber": "SZ092C169",
    "system": "P25 GTR",
    "channels": "",
    "towerOwner": "Grand River Dam Authority",
    "shelterOwner": "Grand River Dam Authority",
    "equipmentOwner": "GRDA",
    "latitude": "36.4",
    "longitude": "-96.4863888888889",
    "color": "#82F2C0"
}];
var set2 = [{
    "seconds": 691,
    "y": 691,
    "downtime": "691 Sec",
    "siteNumber": "13",
    "siteName": "Preston 2",
    "name": "Preston 2",
    "motorolaNumber": "SZ092C113",
    "system": "SZ Quantar",
    "channels": "5",
    "towerOwner": "Wynn Communications",
    "shelterOwner": "Wynn Communications",
    "equipmentOwner": "DPS",
    "latitude": "35.724833",
    "longitude": "-95.988333",
    "color": "#348AA7"
}, {
    "seconds": 10,
    "y": 10,
    "downtime": "10 Sec",
    "siteNumber": "29",
    "siteName": "Bakers Peak 2",
    "name": "Bakers Peak 2",
    "motorolaNumber": "SZ092C129",
    "system": "SZ Quantar",
    "channels": "5",
    "towerOwner": "FBI",
    "shelterOwner": "DPS",
    "equipmentOwner": "DPS",
    "latitude": "34.839722",
    "longitude": "-98.803333",
    "color": "#ffa600"
}, {
    "seconds": 1,
    "y": 1,
    "downtime": "1 Sec",
    "siteNumber": "30",
    "siteName": "Walters 2",
    "name": "Walters 2",
    "motorolaNumber": "SZ092C130",
    "system": "SZ Quantar",
    "channels": "5",
    "towerOwner": "Cotton Electric",
    "shelterOwner": "DPS",
    "equipmentOwner": "DPS",
    "latitude": "34.358583",
    "longitude": "-98.321583",
    "color": "#cb5464"
}, {
    "seconds": 5,
    "y": 5,
    "downtime": "5 Sec",
    "siteNumber": "69",
    "siteName": "Hominy 2",
    "name": "Hominy 2",
    "motorolaNumber": "SZ092C169",
    "system": "P25 GTR",
    "channels": "",
    "towerOwner": "Grand River Dam Authority",
    "shelterOwner": "Grand River Dam Authority",
    "equipmentOwner": "GRDA",
    "latitude": "36.4",
    "longitude": "-96.4863888888889",
    "color": "#82F2C0"
}];

$(document).ready(function () {
    var optionsFor24HoursTop5 = {
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        credits: {
            enabled: false
        },
        title: {
            text: 'Last 24 Hours'
        },
        tooltip: {
            pointFormat: '{series.name}: {point.downtime}'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '{point.name}: {point.downtime}'
                }
            }
        },
        series: [{
            name: 'DropSum',
            colorByPoint: true,
            data: set1
        }]
    };
    chart24HoursTop5 = Highcharts.chart("chartContainer", optionsFor24HoursTop5);
    setTimeout(changeData, 5000);
});

function changeData() {
    if (lastSet == 1) {
        chart24HoursTop5.series[0].setData(set2);
        lastSet = 2;
    } else {
        chart24HoursTop5.series[0].setData(set1);
        lastSet = 1;
    }

这里是一个完全正常的jsfiddle例如:

https://jsfiddle.net/mspinks/ksd8gjec/23/

您会在例如在图表显示最初注意到,然后在5秒钟后,数据的变化,并反映在图表显示。然而,数据再次改变每5秒,并且没有这些变化将反映在图表显示。为什么不会Highcharts显示第一个变化之后更新的数据?

javascript jquery highcharts
2个回答
0
投票

这确实出现了某种错误(?),而是一个解决办法似乎是指定设置数据时点应不会被更新。例如(JSFiddle):

function changeData() {
  if (lastSet == 1){
    // The fourth parameter makes or breaks the update
    chart24HoursTop5.series[0].setData(set2, true, true, false);
    lastSet = 2;
  } else {
    // The fourth parameter makes or breaks the update
    chart24HoursTop5.series[0].setData(set1, true, true, false);
    lastSet = 1;
  }
  setTimeout(changeData, 5000);
}

这也意味着,没有动画,点报废并创建新的点。但它确实会出现,这是一个点的这一更新,似乎情况发生在所有停止更新,所以希望主要的问题就可以解决为好。


0
投票

对于服务表现Highcharts变异的原始数据阵列,这引起该变量set1set2是第一更新后的相同。

该解决方案可通过函数返回的数据:

function set1() {
    return [...];
}

function set2() {
    return [...];
}

function changeData() {
    if (lastSet == 1) {
        chart24HoursTop5.series[0].setData(set2());
        lastSet = 2;
    } else {
        chart24HoursTop5.series[0].setData(set1());
        lastSet = 1;
    }
    setTimeout(changeData, 2000);
}

现场演示:https://jsfiddle.net/BlackLabel/0smxjLac/

© www.soinside.com 2019 - 2024. All rights reserved.