【问题标题】:How to make jqPlot work with the bar chart example如何使 jqPlot 与条形图示例一起使用
【发布时间】:2014-02-10 01:06:24
【问题描述】:

我正在尝试运行来自 jqPlot 的示例。我将他们的代码改编为我的页面:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Tests</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
    </head>
    <body>
        <div>Testing a chart 2</div>

        <script type="text/javascript" src="jqplot.barRenderer.min.js"></script>
        <script type="text/javascript" src="jqplot.categoryAxisRenderer.min.js"></script>
        <script type="text/javascript" src="jqplot.pointLabels.min.js"></script>

        <script language=javascript type="text/javascript">
                $(document).ready(function() {
                    var s1 = [200, 600, 700, 1000];
                    var s2 = [460, -210, 690, 820];
                    var s3 = [-260, -440, 320, 200];
                    // Can specify a custom tick Array.
                    // Ticks should match up one for each y value (category) in the series.
                    var ticks = ['May', 'June', 'July', 'August'];

                    var plot1 = $.jqplot('chart1', [s1, s2, s3], {
                        // The "seriesDefaults" option is an options object that will
                        // be applied to all series in the chart.
                        seriesDefaults: {
                            renderer: $.jqplot.BarRenderer,
                            rendererOptions: {fillToZero: true}
                        },
                        // Custom labels for the series are specified with the "label"
                        // option on the series option.  Here a series option object
                        // is specified for each series.
                        series: [
                            {label: 'Hotel'},
                            {label: 'Event Regristration'},
                            {label: 'Airfare'}
                        ],
                        // Show the legend and put it outside the grid, but inside the
                        // plot container, shrinking the grid to accomodate the legend.
                        // A value of "outside" would not shrink the grid and allow
                        // the legend to overflow the container.
                        legend: {
                            show: true,
                            placement: 'outsideGrid'
                        },
                        axes: {
                            // Use a category axis on the x axis and use our custom ticks.
                            xaxis: {
                                renderer: $.jqplot.CategoryAxisRenderer,
                                ticks: ticks
                            },
                            // Pad the y axis just a little so bars can get close to, but
                            // not touch, the grid boundaries.  1.2 is the default padding.
                            yaxis: {
                                pad: 1.05,
                                tickOptions: {formatString: '$%d'}
                            }
                        }
                    });
                });
        </script>

        <div id="chart1" style="height:400px;width:300px; "></div>

        <br><a href="/Test/index.html">Home</a>
    </body>
</html>

请注意,我创建了div,图表名称为chart1,然后相应地添加了插件。

【问题讨论】:

  • 仅供参考:language=javascript 从来都不是真正需要的,而且type="text/javascript" 已经好几年没有需要了(当然在使用&lt;!DOCTYPE html&gt; 时不需要),所以你可以使用&lt;script&gt; :-)

标签: javascript jquery jqplot


【解决方案1】:

您需要添加主“jqplot”javascript 文件,而不仅仅是插件文件:

<script type="text/javascript" src="jquery.jqplot.js"></script>

请参阅工作示例here

PS:关于您的 Chart2.html 文件(在您的存档中)

  1. 您必须以精确的顺​​序加载文件:首先是 jquery,然后是 jqplot,最后是 jqlot 插件(它们之间没有任何特定的顺序)。
  2. 您拼错了 jqplot 文件:jquery.jqplot.min.js(不要忘记 'q' 字母)
  3. 您不需要同时加载 jquery.jqplot.min.js 和 jquery.jqplot.js,因为它们是相同的内容(区别在于缩小)。
  4. 如果您希望图表显示良好,请不要忘记加载 jqplot CSS。

因此正确的 javascript 和 css 包含是:

<script src="jquery.min.js"></script>
<script src="jquery.jqplot.min.js"></script>
<script src="jqplot.barRenderer.min.js"></script>
<script src="jqplot.categoryAxisRenderer.min.js"></script>
<script src="jqplot.pointLabels.min.js"></script>
<link href="jquery.jqplot.css" rel="stylesheet" type="text/css">

【讨论】:

  • 这已经在代码中了,但它不起作用。你还看到什么吗?谢谢
  • 我已经编辑了我的帖子。您需要添加主 jqplot javascript 文件 (jquery.jqplot.js),因为其他文件是“仅”插件
  • 哦,谢谢,我一回家就试一试,告诉你结果!
  • 它没有用。如果您能帮助我,我在此链接中添加了我的 NB 项目:dl.dropboxusercontent.com/u/30676916/Test.zip
  • 我已经处理了您的 Chart2.html 文件,请参阅上面编辑过的帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
相关资源
最近更新 更多