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>