Amcharts 5 setInterval(function()) - intermittent 404 error

51 Views Asked by At

I'm writing a one line data file every 3 seconds on local pc and uploading to server where it is read by amcharts setInterval(function). It works well generating a "live" chart but it also causes a 404 error intermittently. I'm assuming the error is generated as the net.load is attemtping to read the file at the same time as it is being over written by the automatic upload of the file from the local pc to the server.

ERROR AS LOGGED TO CONSOLE
Ooops at time 10:51:56  {"xhr":{},"error":true,"type":"text/html"}
index.js:1     GET https://example.com/uploads/fetchData.json 404
(anonymous) @ index.js:1
we @ index.js:1
(anonymous) @ index.js:559
index.js:636 Ooops at time 10:52:4  {"xhr":{},"error":true,"type":"text/html"}
index.js:1     GET https://example.com/uploads/fetchData.json 404
(anonymous) @ index.js:1
we @ index.js:1
(anonymous) @ index.js:559
index.js:636 Ooops at time 10:52:8  {"xhr":{},"error":true,"type":"text/html"}
index.js:1     GET https://example.com/uploads/fetchData.json 404
(anonymous) @ index.js:1
we @ index.js:1
(anonymous) @ index.js:559
index.js:636 Ooops at time 10:52:12  {"xhr":{},"error":true,"type":"text/html"}
index.js:1     GET https://example.com/uploads/fetchData.json 404
(anonymous) @ index.js:1
    setInterval(function() {
        var valueSeries = stockChart.get("stockSeries");
        var date = Date.now();
        var dateMinutes = new Date(date).getMinutes();
        var lastDataObject = valueSeries.data.getIndex(valueSeries.data.length - 1);
        //console.log(lastDataObject);
        //console.log(dateMinutes);
        
        if (lastDataObject) {
            var previousDate = lastDataObject.Date;
            var previousValue = lastDataObject.Close;

            var high = lastDataObject.High;
            var low = lastDataObject.Low;
            var open = lastDataObject.Open;
            var close = lastDataObject.Close;
            var volume = lastDataObject.Volume;
            var columnSettings = lastDataObject.ColumnSettings;
            var pos = lastDataObject.Pos;       

            am5.net.load("https://mytrades.com/uploads/fetchData.json").then(function(result) {
            var jsdata = am5.JSONParser.parse(result.response);
            var valueOpen = jsdata.Open
            var valueHigh = jsdata.High
            var valueLow = jsdata.Low
            var valueClose = jsdata.Close
            var valueVolume = jsdata.Volume     
            var valueColumnString = jsdata.ColumnSettings
            if(valueColumnString == 0) var valueColumnSettings = {fill:am5.color(0x808080),stroke:am5.color(0x808080)}; //"No Pos"
            if(valueColumnString == 1) var valueColumnSettings = {fill:am5.color(0x382DFF),stroke:am5.color(0x382DFF)}; //"Long Pos"
            if(valueColumnString == 2) var valueColumnSettings = {fill:am5.color(0xFF16B5),stroke:am5.color(0xFF16B5)}; //"Short Pos"
            var valuePos = jsdata.Pos
                                
            if (am5.time.checkChange(date, previousDate, "minute") && (dateMinutes == 0 || dateMinutes == 15 || dateMinutes == 30 || dateMinutes == 45)) {

                var dObj1 = {
                    Date: date,
                    Close: valueClose,
                    Open: valueOpen,
                    Low: valueLow,
                    High: valueHigh,
                    Volume: valueVolume,
                    ColumnSettings: valueColumnSettings,
                    Pos: valuePos       
                };
                
                //console.log("New 15 Minute Candle at " + new Date(date));
                //console.log(dObj1);
                valueSeries.data.push(dObj1);
                volumeSeries.data.push(dObj1);
                sbSeries.data.push(dObj1);
                previousDate = date;
            } else {

                var dObj2 = {
                    Date: previousDate,
                    Close: valueClose,
                    Open: valueOpen,
                    Low: valueLow,
                    High: valueHigh,
                    Volume: valueVolume,
                    ColumnSettings: valueColumnSettings,
                    Pos: valuePos   
                };

                //console.log("Forming Candle at " + new Date(date));
                //console.log(dObj2);
                valueSeries.data.setIndex(valueSeries.data.length - 1, dObj2);
                volumeSeries.data.setIndex(valueSeries.data.length - 1, dObj2);
                sbSeries.data.setIndex(sbSeries.data.length - 1, dObj2);
            }
            // update current value
            if (currentLabel) {
                currentValueDataItem.animate({ key: "Close", to: valueClose, duration: 500, easing: am5.ease.out(am5.ease.cubic) });
                currentLabel.set("text", stockChart.getNumberFormatter().format(valueClose));
                var bg = currentLabel.get("background");
                if (bg) {
                    if(valueClose < open){      
                        bg.set("fill", root.interfaceColors.get("negative"));
                    }
                    else{
                        bg.set("fill", root.interfaceColors.get("positive"));
                    }
                }
            }                   
                    
            }).catch(err => {
                console.log('Ooops !! ' + JSON.stringify(err));
            });         
        }
    }, 1000);

I'm fairly new to this type of coding and am curious if this is something I need to be concerned about. It does not seem to affect the functioning of the chart. If it is important then I would like to fix it, is there another method I should be using to read a file to update my chart? Thanks for any assistance you might offer ...

I've tried the fetch API with similar results. I've scoured resources looking for a similar situation without finding one. Any assistance or thoughts would be greatly appreciated.

0

There are 0 best solutions below