0
votes

I´m trying to use in solid gauge a data array, created previously. The problem is: when I use the setInterval function to update the point´s values,dynamically, my array update it's first element to current value.

I've tried some solutions like duplicate my array, clone it, even reassignment the first value again but nothing work, the value always change, I think it's by refence but where? Example: The array is:

fullData=[[{y:25},{y:87}],[{y:73},{y:154}]]

when the setInterval run the array update it's first value position as:

fullData=[[{y:440},{y:239}],[{y:200},{y:320}]]

This is an example: https://jsfiddle.net/asonipse/nsvt0jb3/1/3

var fullData = [
    [{
      'name': 'A',
      'innerRadius': 50,
      'radius': 70,
      'y': 440
    }, {
      'name': 'E',
      'innerRadius': 70,
      'radius': 100,
      'y': 239
    }],
    [{
      'name': 'A',
      'innerRadius': 50,
      'radius': 70,
      'y': 200
    }, {
      'name': 'E',
      'innerRadius': 70,
      'radius': 100,
      'y': 320
    }]
  ],
  cont = 1,
  series = [];

series.push({
  'name': 'Name serie',
  'data': fullData[0],
  'dataLabels': false
});

var gaugeOptions = {
  chart: {
    type: 'solidgauge',
  },
  title: {
    text: 'Ejemplo'
  },
  tooltip: {
    enabled: true,
    pointFormat: '{point.name}: <b>{point.y}</b>'

  },
  pane: {
    center: ['50%', '85%'],
    size: '150%',
    startAngle: -90,
    endAngle: 90,
    background: {
      backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
      innerRadius: '50%',
      outerRadius: '100%',
      shape: 'arc'
    }
  },
  // the value axis
  yAxis: {
    min: 0,
    max: 440,
    lineWidth: 0,
    minorTickInterval: null,
    tickPixelInterval: 400,
    tickWidth: 0,
    tickAmount: null,
  },
  plotOptions: {
    solidgauge: {
      dataLabels: {
        y: 5,
        borderWidth: 0,
        useHTML: true
      }
    }
  },
};

$('#container-speed').highcharts(Highcharts.merge(gaugeOptions, {
  credits: {
    enabled: false
  },
  series: series
}), function(chart) {
  if (!chart.renderer.forExport) {
    setInterval(function() {
      if (chart && chart.series) {
        chart.series[0].setData(fullData[cont]);
        cont = (cont + 1 === 2) ? 0 : cont + 1;
      }
    }, 2000);
  }
});
1
The array description is: The array is: fullData=[[{y:440},{y:239}],[{y:200},{y:320}]] when the SetInterval run the array update it's first value position as: fullData=[[{y:200},{y:320}],[{y:200},{y:320}]] and the link is jsfiddle.net/asonipse/nsvt0jb3/1 - Angel Espinosa

1 Answers

0
votes

It occurs because the problem with mutating source arrays by Highcharts, namely the objects in arrays. This problem (and a few related ones) is reported on our GitHub repository. You can read more about it here: https://github.com/highcharts/highcharts/issues/4259

However all is not lost, because you can simply refactor your code, so that it brings you expected effect, just using Object.assign function.

First you need to use function mentioned above to make your fullData variable "independent" of chart. Change your code like that:

var fullData = [
  [{
    'name': 'A',
    'innerRadius': 50,
    'radius': 70,
    'y': 440
  }, {
    'name': 'E',
    'innerRadius': 70,
    'radius': 100,
    'y': 239
  }],
  [{
    'name': 'A',
    'innerRadius': 50,
    'radius': 70,
    'y': 200
  }, {
    'name': 'E',
    'innerRadius': 70,
    'radius': 100,
    'y': 320
  }]
];
var cont = 1;
var series = [];
series.push({
  'name': 'Name serie',
  'data': [
    Object.assign({}, fullData[0][0]),
    Object.assign({}, fullData[0][1]),
  ],
  'dataLabels': false
});

Then, update your points with new values:

 setInterval(function() {
      if (chart && chart.series) {
        chart.series[0].points.forEach((point, i) => {
            point.update(fullData[cont][i])
        })
        cont = (cont + 1 === 2) ? 0 : cont + 1;
      }
    }, 1000);

Now it should works as you expecting.

Live example: https://jsfiddle.net/xLcewn1z/