CSV data only working in preview
-
Hi,
I’m loading external CSV data using the example code from https://www.amcharts.com/tutorials/loading-external-data/ and the data only displays when I’m previewing the chart or the page. When I publish the page, the chart shows up blank.
Page: https://leaderboard.puppod.com/chart-sample/
I get one error in the debug console:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience. For more help https://xhr.spec.whatwg.org/
Resources:
https://www.amcharts.com/lib/3/amcharts.js https://www.amcharts.com/lib/3/serial.js
HTML:
<div id="%CHART%" style="width: 640px; height: 400px;"> </div>
JS:
var chart; var chartData = []; var chartCursor; AmCharts.ready(function() { // SERIAL CHART chart = new AmCharts.AmSerialChart(); chart.pathToImages = "wp-content/plugins/amcharts-charts-and-maps/images/"; chart.dataProvider = chartData; chart.categoryField = "date"; chart.dataDateFormat = "YYYY-MM-DD"; // listen for "dataUpdated" event (fired when chart is rendered) and call zoomChart method when it happens chart.addListener("dataUpdated", zoomChart); // AXES // category var categoryAxis = chart.categoryAxis; categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true categoryAxis.minPeriod = "DD"; // our data is daily, so we set minPeriod to DD categoryAxis.dashLength = 1; categoryAxis.gridAlpha = 0.15; categoryAxis.minorGridEnabled = false; categoryAxis.axisColor = "#DADADA"; // value var valueAxis = new AmCharts.ValueAxis(); valueAxis.axisAlpha = 0.2; valueAxis.dashLength = 1; chart.addValueAxis(valueAxis); // GRAPH var graph = new AmCharts.AmGraph(); graph.title = "red line"; graph.valueField = "visits"; graph.type = "column"; chart.addGraph(graph); // CURSOR chartCursor = new AmCharts.ChartCursor(); chartCursor.cursorPosition = "mouse"; chart.addChartCursor(chartCursor); // SCROLLBAR /*var chartScrollbar = new AmCharts.ChartScrollbar(); chartScrollbar.graph = graph; chartScrollbar.scrollbarHeight = 40; chartScrollbar.color = "#FFFFFF"; chartScrollbar.autoGridCount = true; chart.addChartScrollbar(chartScrollbar);*/ // WRITE chart.write("%CHART%"); loadCSV("wp-content/plugins/amcharts-charts-and-maps/data.txt"); }); function loadCSV(file) { if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari var request = new XMLHttpRequest(); } else { // code for IE6, IE5 var request = new ActiveXObject('Microsoft.XMLHTTP'); } // load request.open('GET', file, false); request.send(); parseCSV(request.responseText); } function parseCSV(data) { //replace UNIX new lines data = data.replace(/\r\n/g, "\n"); //replace MAC new lines data = data.replace(/\r/g, "\n"); //split into rows var rows = data.split("\n"); // loop through all rows for (var i = 0; i < rows.length; i++) { // this line helps to skip empty rows if (rows[i]) { // our columns are separated by comma var column = rows[i].split(","); // column is array now // first item is date var date = column[0]; // second item is value of the second column var value = column[1]; // create object which contains all these items: var dataObject = { date: date, visits: value }; // add object to chartData array chartData.push(dataObject); } } chart.validateData(); } // this method is called when chart is first inited as we listen for "dataUpdated" event function zoomChart() { // different zoom methods can be used - zoomToIndexes, zoomToDates, zoomToCategoryValues chart.zoomToIndexes(chartData.length - 40, chartData.length - 1); } // changes cursor mode from pan to select function setPanSelect() { if (document.getElementById("rb1").checked) { chartCursor.pan = false; chartCursor.zoomable = true; } else { chartCursor.pan = true; } chart.validateNow(); }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘CSV data only working in preview’ is closed to new replies.