I am writing an application using D3. It should build charts from input forms on a page and from a file. I have problems with the latter, I can’t read the file in any way and write it to a variable. The essence of the problem is that for reading I use the function and the value is stored in it, and the variable will need to be passed to other functions. Please help. There was of course the option of calling functions from the read function, but for some reason d3 gives an error.

Thank you for attention.


My program code.

//ДобавлСния строки Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ $('.btn').click(function () { var ind = $('tr:last').index(); $('.hide').clone().appendTo('table').addClass('row').removeClass('hide'); $('tr:last>td:nth-child(2)>input').addClass('x'); $('tr:last>td:nth-child(3)>input').addClass('y'); $('tr:last>td:first').text(ind); }); var rawData = []; var z = 0; var height = 500, width = 500, margin=30, data=[]; var numb; //функция построСния Π³Ρ€Π°Ρ„ΠΈΠΊΠ° var chart1 = function () { // созданиС ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° svg var svg = d3.select("body").append("svg") .attr("class", "axis") .attr("width", width) .attr("height", height); // функция, ΡΠΎΠ·Π΄Π°ΡŽΡ‰Π°Ρ ΠΏΠΎ массиву Ρ‚ΠΎΡ‡Π΅ΠΊ Π»ΠΈΠ½ΠΈΠΈ var line = d3.svg.line() .x(function(d){return dx;}) .y(function(d){return dy;}); // добавляСм ΠΏΡƒΡ‚ΡŒ svg.append("g").append("path") .attr("d", line(data)) .style("stroke", "steelblue") .style("stroke-width", 2); } //функция построСния сСтки ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ var osi = function () { // созданиС ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° svg var svg = d3.select("body").append("svg") .attr("class", "axis") .attr("width", width + 10) .attr("height", height + 10); // Π΄Π»ΠΈΠ½Π° оси X= ΡˆΠΈΡ€ΠΈΠ½Π° ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° svg - отступ слСва ΠΈ справа var xAxisLength = width - 2 * margin; // Π΄Π»ΠΈΠ½Π° оси Y = высота ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π° svg - отступ свСрху ΠΈ снизу var yAxisLength = height - 2 * margin; // функция интСрполяции Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Π½Π° ось Π₯ var scaleX = d3.scale.linear() .domain([0, numb]) .range([0, xAxisLength]); // функция интСрполяции Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Π½Π° ось Y var scaleY = d3.scale.linear() .domain([numb, 0]) .range([0, yAxisLength]); // ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ Ρ€Π΅Π°Π»ΡŒΠ½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… Π² Π΄Π°Π½Π½Ρ‹Π΅ для нашСй ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π½ΠΎΠΉ систСмы for(i=0; i<rawData.length; i++) data.push({x: scaleX(rawData[i].x)+margin, y: scaleY(rawData[i].y) + margin}); // создаСм ось X var xAxis = d3.svg.axis() .scale(scaleX) .orient("bottom"); // создаСм ось Y var yAxis = d3.svg.axis() .scale(scaleY) .orient("left"); for(i=0; i<rawData.length; i++) data.push({x: scaleX(rawData[i].x)+margin, y: scaleY(rawData[i].y) + margin}); // отрисовка оси Π₯ svg.append("g") .attr("class", "x-axis") .attr("transform", // сдвиг оси Π²Π½ΠΈΠ· ΠΈ Π²ΠΏΡ€Π°Π²ΠΎ "translate(" + margin + "," + (height - margin) + ")") .call(xAxis); // отрисовка оси Y svg.append("g") .attr("class", "y-axis") .attr("transform", // сдвиг оси Π²Π½ΠΈΠ· ΠΈ Π²ΠΏΡ€Π°Π²ΠΎ Π½Π° margin "translate(" + margin + "," + margin + ")") .call(yAxis); // создаСм Π½Π°Π±ΠΎΡ€ Π²Π΅Ρ€Ρ‚ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… Π»ΠΈΠ½ΠΈΠΉ для сСтки d3.selectAll("gx-axis g.tick") .append("line") .classed("grid-line", true) .attr("x1", 0) .attr("y1", 0) .attr("x2", 0) .attr("y2", - (yAxisLength)); // рисуСм Π³ΠΎΡ€ΠΈΠ·ΠΎΠ½Ρ‚Π°Π»ΡŒΠ½Ρ‹Π΅ Π»ΠΈΠ½ΠΈΠΈ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π½ΠΎΠΉ сСтки d3.selectAll("gy-axis g.tick") .append("line") .classed("grid-line", true) .attr("x1", 0) .attr("y1", 0) .attr("x2", xAxisLength) .attr("y2", 0); } //функция которая Ρ‡ΠΈΡ‚Π°Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… ΠΈ записываСт ΠΈΠ· Π² масив. //[{x: 1, y: 2} ,{x: 3, y: 4} ,{x: 5, y: 6} ,{x: 7, y: 8},{x: 9, y: 10}] Ρ‚Π°ΠΊ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Ρ‹Π³Π»ΡΠ΄Π΅Ρ‚ΡŒ масив Ρ‡Ρ‚ΠΎΠ±Ρ‹ D3 Π΅Π³ΠΎ ΠΌΠΎΠ³Π»Π° ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ. function inputToMass() { var inpx = $('.x'); var inpy = $('.y'); var x, y; var massx = []; var massy = []; var mass = {}; var cur = 0; for (i = 0; i < inpx.length; i++) { mass = {}; x = inpx[i].value; x = parseInt(x); mass.x = x; y = inpy[i].value; y = parseInt(y); mass.y = y; rawData[i] = mass; } for (i = 0; inpx.length > i; i++) { x = inpx[i].value; x = parseInt(x); massx[i] = x; y = inpy[i].value; y = parseInt(y); massy[i] = y; } for (var i = 0; i < massx.length; i++) { if (cur < massx[i]) { cur = massx[i]; } if (cur < massy[i]) { cur = massy[i]; } } var ax = cur/100*7; numb = cur + ax; } // общая функция которая ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ Π²Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ $('.btn2').click(function () { $('svg').remove(); inputToMass(); osi(); chart1() console.log(rawData); }); // функция чтСния ΠΈΠ· Ρ„Π°ΠΉΠ»Π° $('.file').change(function () { var fr = new FileReader(); fr.onload = function(info) { var rezf = info.target.result; rawData = rezf; return rezf; } fr.readAsText(this.files[0]); }); 

D3 error code

 d3.v3.min.js:1 Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…". 

HTML code

  <div class="table"> <table border="1"> <tr> <th>#</th> <th>X</th> <th>Y</th> </tr> <tr class="hide"> <td></td> <td><input type="number"></td> <td><input type="number"></td> </tr> <tr class="row"> <td>1</td> <td><input class="x" type="number"></td> <td><input class="y" type="number"></td> </tr> </table> </div> <br><br> <button class="btn">Add Row</button> <button class="btn2">generate</button> <input class="file" type="file"> <pre class="cont"></pre> 

    0