jquery - 使用 jQuery Flot 绘制外部数据

标签 jquery json plot flot

我正在尝试使用flot插件来绘制一些写入JSON文件的数据。 这看起来并不难,但我找不到有用的东西......你能帮助我吗?

这是我写的页面:

$(function () {  
    var data;
    $.getJSON("1.json", function(json) {
        var data = json;
    });

    var options = {  
            legend: {  
                show: true,  
                margin: 10,  
                backgroundOpacity: 0.5  
            },  
            points: {  
                show: true,  
                radius: 3  
            },  
            lines: {  
                show: true  
            }
    };

    var plotarea = $("#placeholder");  

    $.plot(plotarea , data, options);  
});

而 1.json 文件包含以下所有内容:

{  label: "Values",  
    data:   [   
        [1, 50.026],
        [2, 50.028],
        [3, 50.029],
        [4, 50.026],
        [5, 50.025],
        [6, 50.016]
        ]
}
<小时/>

@MarcoJohannesen 即使我在 JSON 调用后编写“console.log(data)”,脚本仍然无法工作,并且屏幕上不会出现任何消​​息。使用 Chrome 实用程序(我不记得名称了;-))我可以看到 hte 文件 1.json 已正确加载。我认为问题在于首先执行脚本,然后加载文件 1.json。我在页面上做了一些编辑。 您可以看到a demo on this page 这是页面“1.htm”代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <script language="javascript" type="text/javascript" src="jquery.js"></script>
    <script language="javascript" type="text/javascript" src="jquery.flot.js"></script>
 </head>
    <body>
    <h1>Graph</h1>

    <div id="placeholder" style="width:600px;height:300px;"></div>
<script language="javascript" type="text/javascript">  
$(function () {  
    var data;
    $.getJSON("1.json", function(json) {
        var data = json;
    });
    console.log(data);

    var plotarea = $("#placeholder");  

    $.plot(plotarea , data);  
});
</script>   
</body>
</html>

这是 1.json(我添加了方括号)

[{  label: "Values",  
    data:   [   
        [1, 50.026],
        [2, 50.028],
        [3, 50.029],
        [4, 50.026],
        [5, 50.025],
        [6, 50.016]
        ]
}}
<小时/>

我确实找到了一种制作工作页面的方法。这是我使用的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <script language="javascript" type="text/javascript" src="jquery.js"></script>
    <script language="javascript" type="text/javascript" src="jquery.flot.js"></script>
 </head>
    <body>
    <h1>Graph</h1>
<script language="javascript" type="text/javascript">  
$(document).ready(function(){
    $.getJSON("1.json", function(json) {
       //succes - data loaded, now use plot:
       var plotarea = $("#placeholder");
       var data=[json.data];
       $.plot(plotarea , data);  
    });
});

</script>       
    <div id="placeholder" style="width:600px;height:300px;"></div>

</body>
</html>

这就是 json 文件(取自 flot 官方示例,以确保格式正确)

{
    "label": "Europe (EU27)",
    "data": [[1999, 1], [2000, 0.23], [2001, 3], [2002, 4], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
}

现在我要 sleep 了,但明天我们应该尝试将标签添加到绘图中,并尝试使用多个值系列。

最佳答案

我用它来生成项目进度的自动更新信息网站。

我的小例子使用了不止一种类型的图表:折线图和条形图。

我发现如果有一个正在运行的示例会更容易理解,所以我们在这里:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <script language="javascript" type="text/javascript" src="jquery.js"></script>
    <script language="javascript" type="text/javascript" src="jquery.flot.js"></script>
 </head>
<body>
<h1>Graph</h1>
<script language="javascript" type="text/javascript">  
$(document).ready(function(){
    $.getJSON("barLine.json", function(json) {
       //succes - data loaded, now use plot:
           var plotarea = $("#placeholder");
           var dataBar=json.dataBar;
            var dataLine=json.dataLine;
            $.plot(plotarea , [
                {
                    data: dataBar,
                    bars: {show: true}
                },
                {
                    data: dataLine,
                    lines: { show: true, steps: false }
                }               
            ]
        );
    });
});

</script>       
    <div id="placeholder" style="width:600px;height:300px;"></div>
</body>
</html>

和数据(barLine.json):

{
"label": "Europe (EU27)",
"dataBar": [[1999, 1], [2000, 0.23], [2001, 3], [2002, 4], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]],
"dataLine": [[1999, 2], [2000, 3.23], [2001, 1], [2002, 5], [2003, 2.3], [2004, 6.5], [2005, 4.0], [2006, 3.1], [2007, 0.9], [2008, 6.9], [2009, 9.9] ]
}

关于jquery - 使用 jQuery Flot 绘制外部数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7859974/

相关文章:

jquery - 带有滚动控件的 Div 溢出滚动 - JQuery

jquery - .replace() 出现问题 - 仅适用于第一个匹配的字符?

php - jQuery 无法处理超链接点击

python - 如何使用 python 格式化 JSON 样式文本?

json - Gson转换为JSON时再次转义转义序列-Android

javascript - asp.net 下拉列表始终选择带有回发的 0 索引

java - Spring 4,JSON - HTTP 错误 406 Not Acceptable

python - 如何在Python中绘制从最大范围到最小范围的图

matlab - 如何在 MATLAB 中设置绘图的默认线型?

r - 在 R 中使用日期设置 xlim