0

I'm writing an application for plotting. To enter data, a table with forms is used and more data can be downloaded from the file. From the table everything works well (well, almost). But if you load from a file, the thread gives an error. I have been searching and writing for a long time at various forums. No one helped me, please help me. I think that for some reason in the variable date is written NAN suspect that the problem is in the data types and I need something to parse, but I can not understand what and how. Please help.


Here is a file structure, a standard JSON

[{"x": 1, "y": 2} ,{"x": 3, "y": 4} ,{"x": 5, "y": 6} ,{"x": 7, "y": 8},{"x": 9, "y": 10}];

Here is my 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 height = 500, 
    width = 500, 
    margin=30,
    data=[],
    numb;
    var osi = function () {
    var svg = d3.select("body").append("svg")
        .attr("class", "axis")
        .attr("width", width )
        .attr("height", height );

    var xAxisLength = width - 2 * margin;     
  
    var yAxisLength = height - 2 * margin;
  
    var scaleX = d3.scale.linear()
        .domain([0, numb])
        .range([0, xAxisLength]);
             
    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});

        
    var xAxis = d3.svg.axis()
        .scale(scaleX)
        .orient("bottom");
    
    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);
 
    svg.append("g")       
        .attr("class", "y-axis")
        .attr("transform", // сдвиг оси вниз и вправо на margin
            "translate(" + margin + "," + margin + ")")
        .call(yAxis);
   
    d3.selectAll("g.x-axis g.tick")
        .append("line")
        .classed("grid-line", true)
        .attr("x1", 0)
        .attr("y1", 0)
        .attr("x2", 0)
        .attr("y2", - (yAxisLength));
     
    d3.selectAll("g.y-axis g.tick")
        .append("line")
        .classed("grid-line", true)
        .attr("x1", 0)
        .attr("y1", 0)
        .attr("x2", xAxisLength)
        .attr("y2", 0);
    }


    var chart1 = function () {
    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 d.x;})
        .y(function(d){return d.y;});

    svg.append("g").append("path")
    .attr("d", line(data))
    .style("stroke", "steelblue")
    .style("stroke-width", 2);
    }

    var inputToMass = function () {
    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();
    });
    
    $.getJSON('ААА.json',function (info) {
    rawData = info;
    osi();
    chart1();
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

 <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>

  <button class="btnhelp">HELP</button>
 
</body>
Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
  • `numb` is equal to undefined when you create the scale. It should be max x for x scale and max y for y scale. – pmkro Mar 19 '18 at 15:25
  • Also this snippet does not reproduce the error, a lines is drawn but its not within the axis. – pmkro Mar 19 '18 at 16:12
  • but dont work, and show error – Сергей Матвеев Mar 21 '18 at 14:43
  • If you are reading from a file using one of the d3 methods, you will have to change the values to numbers as they are returned as strings. [See answer here](https://stackoverflow.com/questions/49406725/d3-max-not-returning-the-max-value-in-an-array) – pmkro Mar 21 '18 at 14:49
  • But if i redefine rawData on [{"x": 1, "y": 2} ,{"x": 3, "y": 4} ,{"x": 5, "y": 6} ,{"x": 7, "y": 8},{"x": 9, "y": 10}], application gives an error too I dont know why((( – Сергей Матвеев Mar 21 '18 at 14:55
  • If you put a `console.log` before here `rawData = info;` what does it output? Are the numbers coming in correctly? – pmkro Mar 21 '18 at 15:00
  • If i write console.log(rawData), i find empty array. Thats right! But if i write console.log (data); i find ([]0: {x: NaN, y: NaN}1: {x: NaN, y: NaN}2: {x: NaN, y: NaN}3: {x: NaN, y: NaN}4: {x: NaN, y: NaN}5: {x: NaN, y: NaN}6: {x: NaN, y: NaN}7: {x: NaN, y: NaN}8: {x: NaN, y: NaN}9: {x: NaN, y: NaN}length: 10__proto__: Array(0) ). This is strangely! Data must be defind in chart1(), but why is it works now? – Сергей Матвеев Mar 21 '18 at 15:40
  • Ya so its not getting the data correctly in the first place. – pmkro Mar 21 '18 at 15:42
  • All right, but haw fix it? – Сергей Матвеев Mar 21 '18 at 15:46
  • [Try here](http://api.jquery.com/jquery.getjson/) – pmkro Mar 21 '18 at 15:49
  • This is dont work( If i write rawData = [ {x: 10, y: 67}, {x: 20, y: 74}, {x: 30, y: 63}, {x: 40, y: 56}, {x: 50, y: 24}, {x: 60, y: 26}, {x: 70, y: 19}, {x: 80, y: 42}, {x: 90, y: 88} ]; output error too((( problem not in getjson metods. – Сергей Матвеев Mar 21 '18 at 15:57

0 Answers0