在javascript中压缩两个数组无法绘制高图表

时间:2016-07-08 11:03:27

标签: highcharts

我将在文本区域中取x,y值。为xy cordinates创建另一个数组以绘制高图。但它不显示graph.i无法找到error.please帮助。

     <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script type="text/javascript">



    $(function () {
        $('#container').highcharts({
            chart: {},
            plotOptions: {
                series: {
                    allowPointSelect: true
                }
            },
            series: [{
                data: []
            }]
        });


        $('#button').click(function () {

      xval = $('#hi').val().split('\n');
    var lines=[];
    for (var i = 0; i < xval.length; i++)
        if (xval[i]) lines.push(xval[i]);
    lines.toString();
    document.getElementById("demo").innerHTML=lines;


    yval = $('#hii').val().split('\n');
     var lines1 = [];
    for (var i = 0; i < yval.length; i++)
        if (yval[i]) lines1.push(yval[i]);// to remove white space

    lines1.toString();    
            var mySeries = [];
            for (var i = 0; i < lines.length; i++) {
                mySeries.push([lines[i], lines1[i]]);


            }


            var chart = $('#container').highcharts();
            chart.series[0].setData(mySeries);
        });
    });



    </script>
    </head>
    <body>
    <div id="container" style="height: 400px"></div>
    <textarea id="hi" name="Text1" cols="40" rows="5" placeholder="enter one wdg in one line"></textarea>

    <textarea id="hii" name="Text2" cols="40" rows="5" placeholder="enter one wdg in one line"></textarea>
    <p id="demo"></p>
<button id="button">Add My Data</button>
    </body>
    </html> 

检查这个小提琴。 http://jsfiddle.net/Cm3Ps/443/

1 个答案:

答案 0 :(得分:0)

问题是由error #14引起的,这意味着您使用字符串而不是值。您应该将所有x / y值(即逗号)拆分并将它们中的每一个推送到数组。你不能像#10; 10,20,30&#39;

那样推动字符串
for (var i = 0; i < lines.length; i++) {
    var itemsX = lines[i].split(','),
        itemsY = lines1[i].split(',');

  for(var j = 0; j < itemsX.length; j++) {
    mySeries.push([parseFloat(itemsX[j]), parseFloat(itemsY[j])]);
  }
}
var chart = $('#container').highcharts();

修正演示: