0

this is the ajax method to get data from restcontroller

function get_data() {
var datas = '';
$.ajax({
    type: 'GET',
    url: 'http://localhost:8080/tower/getDataTower',
    contentType: "application/json",
    dataType: 'json',
    data: datas,
    success: function(datas) {
        oneTowerChart(datas); //put data into highchart
    },
    error: function(a, b, c) {
        console.log(a, b, c + 'error ******')
    }

});

}

i put data into highchart using this method

dsi[0],dsi1 // here is the probleme when i use a foreach it's doesn't work but like that it's working

console.log(dsi) // this is the data i want to put into my chart its an array you find the result in a picture

function oneTowerChart(datas) {
var dateNow = Date.now();
var day = dateNow.getUTCDate();
var month = dateNow.getUTCMonth();
var year = dateNow.getUTCFullYear();

console.log(datas)
var dsi = [];

datas.forEach(
    data => {
            switch(data.uetype){
                case "During service":
                    console.log(data.uetype);
                    break;
                case "Out of service":
                    console.log(data.uetype);
                    break;
                case "During service (internal root cause)":
                    dsi.push([Date.UTC(data.uestart.year, (data.uestart.monthOfYear-1) , data.uestart.dayOfMonth, data.uestart.hourOfDay,data.uestart.minuteOfHour),10]);
                    break;
                case "During service (external root cause)":
                    console.log(data.uetype);
                    break;
                case "Out of service (internal or external root cause)":
                    console.log(data.uetype);
                    break;
            }
        }
)

console.log(dsi) // this is the data i want to put into my chart its an array you find the 
                    result in a picture
[![enter image description here][1]][1]
Highcharts.chart('graphOnDemand',{
    
    //highchart code...

    series: [{
        name: 'during service internal',
        data: [
            dsi[0],dsi[1] // here is the probleme when i use a foreach it's doesn't work but like that it's working
            ],
        color: {
            linearGradient: [0, 0, 0, 300],
            stops: [
                [0, '#BFBFBF'],
                [1, '#E5E5E5']
            ]
        }
    }, {
        name: 'during service external',
        data: [
            [Date.UTC(2021, 6, 14, 12, 05), 2],

        ],
        color: {
            linearGradient: [0, 0, 0, 300],
            stops: [
                [0, '#2E75B6'],
                [1, '#B5CEE5']
            ]
        }
    }, {
        name: 'out of service internal or external',
        data: [
            [Date.UTC(2021, 5, 1, 12, 05), 5],

        ],
        color: {
            linearGradient: [0, 0, 0, 300],
            stops: [
                [0, '#FFC000'],
                [1, '#FFE8A2']
            ]
        }
    }, {
        name: 'during service',
        data: [
            [Date.UTC(2021, 4, 17, 12, 05), 29.9],

        ],
        color: {
            linearGradient: [0, 0, 0, 300],
            stops: [
                [0, '#ED7D31'],
                [1, '#F9D0B4']
            ]
        }
    }, {
        name: 'out of service',
        data: [
            [Date.UTC(2021, 5, 3, 12, 05), 29.9],

        ],
        color: {
            linearGradient: [0, 0, 0, 300],
            stops: [
                [0, '#C00000'],
                [1, '#E49595']
            ]
        }
    }]
});

}

console.log(dsi)

NTIC TECH
  • 116
  • 1
  • 9

2 Answers2

1

The variable dsi is local to the oneTowerChart function and as such, is out of scope when you try to access it later. One way you can fix it is to make dsi a global variable by moving its definition outside of your function

var dsi = [];
function oneTowerChart(datas) {
  // your code, without var dsi = [];
}
James
  • 20,957
  • 5
  • 26
  • 41
0

i modified my code like that and it works

Highcharts.chart('graphOnDemand',{

//highchart code...

series: [{
    name: 'during service internal',
    data:dsi,

    //code...
NTIC TECH
  • 116
  • 1
  • 9