绘制txt值(绘图表)

时间:2015-06-22 15:38:42

标签: javascript jquery html charts flot

我对这个问题感到有些困惑。

我有一个文件,其中要绘制的元素按列分隔,如下例所示:

ID Number Hour Name Mood Seconds Hour2
1 29.1 11:32:37 Tim Good 0954 11:32:34 PM
2 31.9 11:33:19 Tim Fine 1251 11:33:15 PM
3 32.0 11:54:16 Tim Excellent 1897 11:54:12 PM
4 36.6 12:00:43 Time Good 0997 12:00:37 PM

我想要的是能够从.txt文件中提取所有这些数据并将其存储在变量中,以便更容易地使用它们,不包括第一行。然后,我试图做的是绘制例如只是Seconds列与小时一,或Mood列与小时一(这可能更难,因为心情是一个字符串),所有这一切FLOT图表,但我无法做到,因为我不知道正确的命令,我在互联网上寻找它们,但我没有找到任何东西。我确实尝试从Stackoverflow帖子修改文件以查看它是否有效,但该页面只是空白。使用的代码是以下代码(头标记在那里,但是当我在这里输入时,帖子变得疯狂,当然.js文件放在与此代码相同的文件夹中,datafile.txt文件也是如此):

<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <!-- 1. Add JQuery and Highcharts in the head of your page -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>

    <!-- 2. You can add print and export feature by adding this line -->
    <script src="http://code.highcharts.com/modules/exporting.js"></script>


    <!-- 3. Add the JavaScript with the Highchart options to initialize the chart -->
    <script type="text/javascript">
    jQuery(document).ready(function() { 

        var options = {
            chart: {
                renderTo: 'container',
                type: 'column'
            },
            title: {
                text: 'Tiny Tool Monthly Sales'                 
            },
            subtitle: {
                text: '2014 Q1-Q2'
            },
            xAxis: {
                categories: []
            },
            yAxis: {
                title: {
                    text: 'Sales'
                }
            },
            series: []
        };
        // JQuery function to process the csv data
        $.get('datafile.txt', function(data) {
            $.each(data, function(lineNo, line) {
                var items = line.split(' ');

                // header line contains names of categories
                if (lineNo == 0) {
                    $.each(items, function(itemNo, item) {
                        //skip first item of first line
                        if (itemNo > 0) options.xAxis.categories.push(item);
                    });
                }

                // the rest of the lines contain data with their name in the first position
                else {
                    var series = { 
                        data: []
                    };
                    $.each(items, function(itemNo, item) {
                        series.data.push(parseFloat(item));
                    });

                    options.series.push(series);
                }

            });
            //putting all together and create the chart
            var chart = new Highcharts.Chart(options);
        });         

    });
    </script>

</head>
<body>

    <!-- 3. Add the container -->
    <div id="container" style="width: 600px; height: 400px; margin: 0 auto"></div>      

</body>

您是否知道哪里出错或如何完成?非常感谢你们!

1 个答案:

答案 0 :(得分:0)

The $.get() method returns a string and using the $.each() method with a string calls the callback function for each character in the string, not for each line. Change the call to $.each() to this:

$.each(data.split('\n'), function(lineNo, line) {