从使用 ajax 调用获取的数据加载图表

发布于 2024-12-04 13:12:11 字数 1848 浏览 0 评论 0原文

我正在使用 jqPlot javascript 库( http://www.jqplot.com/ )将图形和图表合二为一我的申请。

在我的应用程序中,有 5-6 个页面使用了该库。但我想在这里讨论一个具体案例。

在第 1 页上,我正在加载 3 个图表。这 3 个图表的数据是从数据库表中填充的。

每个图表都有不同的查询集。因此,每个图表的填充数据也不同。

填充数据后,我必须在将其输入提供给图表之前对其进行处理。

那么问题是什么: 我面临的问题是页面在浏览器上渲染需要花费大量时间(这很明显,因为首先它将形成查询,然后针对数据库表触发查询,获取数据,处理数据并提供给图表)

我的一位朋友建议使用 ajax 来实现以下内容。我真的很喜欢他的解决方案。

这就是我打算做的:

我将创建一个页面,它将加载 jqPlot 库所需的所有 js/css 文件。 该页面上将有 3 个部分,我将在其中放置一些 GIF 图像,指示某些进程正在进行(例如 ajax-loader.gif)。

一旦页面加载,它将触发 3 个 ajax 调用,一次一个,以获取每个图表。

我的问题是如何从ajax调用收到的数据加载图表?

jqplot 按以下方式放置数据并创建图表(请看下面的示例)

<script class="code" type="text/javascript"> 
$(document).ready(function(){
  var plot2 = $.jqplot ('chart2', [[3,7,9,1,4,6,8,2,5]], {
      // Give the plot a title.
      title: 'Plot With Options',
      // You can specify options for all axes on the plot at once with
      // the axesDefaults object.  Here, we're using a canvas renderer
      // to draw the axis label which allows rotated text.
      axesDefaults: {
        labelRenderer: $.jqplot.CanvasAxisLabelRenderer
      },
      // An axes object holds options for all axes.
      // Allowable axes are xaxis, x2axis, yaxis, y2axis, y3axis, ...
      // Up to 9 y axes are supported.
      axes: {
        // options for each axis are specified in seperate option objects.
        xaxis: {
          label: "X Axis",
          // Turn off "padding".  This will allow data point to lie on the
          // edges of the grid.  Default padding is 1.2 and will keep all
          // points inside the bounds of the grid.
          pad: 0
        },
        yaxis: {
          label: "Y Axis" 
        }
      }
    });
});
</script> 

I am using jqPlot javascript library ( http://www.jqplot.com/ ) for graphs and charts in one of my application.

In my application, there are 5-6 pages where this library is used. But I would like to discuss one particular case here.

On 1 of page, I am loading 3 charts. Data for these 3 charts is populated from database tables.

There is different set of queries for each chart. So, populated data for each chart is different too.

Once I have populated data, I have to process it, before providing its input to chart.

What is the problem then:
Problem that I am facing is it takes lots of time for page to render on browser (which is quiet obvious, as first it will form query, then fire that query against database tables, get the data, process on data and give to chart)

One of my friend suggested to implement following thing using ajax. I really liked his solution.

This is what I intend to do:

I would create a page, which will load all the required js/css files for jqPlot library.
There will be 3 sections on that page, where I would put some GIF images indicating that some process is going on (say ajax-loader.gif)

Once page is loaded, it will fire 3 ajax call, one at a time, to fetch each chart.

My Question Is how can I load chart from data received from ajax-call?

jqplot puts data and creates chart in following way (look at example below)

<script class="code" type="text/javascript"> 
$(document).ready(function(){
  var plot2 = $.jqplot ('chart2', [[3,7,9,1,4,6,8,2,5]], {
      // Give the plot a title.
      title: 'Plot With Options',
      // You can specify options for all axes on the plot at once with
      // the axesDefaults object.  Here, we're using a canvas renderer
      // to draw the axis label which allows rotated text.
      axesDefaults: {
        labelRenderer: $.jqplot.CanvasAxisLabelRenderer
      },
      // An axes object holds options for all axes.
      // Allowable axes are xaxis, x2axis, yaxis, y2axis, y3axis, ...
      // Up to 9 y axes are supported.
      axes: {
        // options for each axis are specified in seperate option objects.
        xaxis: {
          label: "X Axis",
          // Turn off "padding".  This will allow data point to lie on the
          // edges of the grid.  Default padding is 1.2 and will keep all
          // points inside the bounds of the grid.
          pad: 0
        },
        yaxis: {
          label: "Y Axis" 
        }
      }
    });
});
</script> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-12-11 13:12:11

由于您使用的是 jQuery,因此您可以使用 jQuery Ajax 方法 来获取图表数据页面加载后。

在您的 success 函数中,您的 JS 代码(在浏览器上)从服务器接收数据。获得数据后,调用 $.jqplot ——传入您刚刚收到的数据。

要最初显示繁忙的 gif,只需使用 img 元素作为 chart2 div 的静态内容,该 div 稍后将成为图形的容器。

一些提示:

  • 某些浏览器在运行 js 程序时不能很好地处理动画 gif。因此,除了旋转 gif 之外,您可能还想尝试短信(“正在加载图表...”)。 -- 或者更新短信。例如,从“从服务器获取图表数据”开始,然后在调用 success 函数后更新为“处理图表数据”。

  • 不要同时启动所有 3 个 Ajax 调用,而是尝试为第一个图表使用 success 函数来启动第二个 Ajax 调用。 (除了绘制数据图表之外。)

如果您的 Ajax 调用有问题,请搜索 Google 示例,如果仍然有问题,请就 SO 提出单独的问题。

Since you're using jQuery, you'd use the jQuery Ajax method to fetch the chart data after the page has loaded.

In your success function, your JS code (on the browser) receives the data from your server. Once you have the data, make the call to $.jqplot -- passing in the data you've just received.

To initially show the busy gif, just use the img element as the static content of the chart2 div which will later be the graph's container.

Some tips:

  • Some browsers don't do well at handling an animated gif while running a js program. So you may want to try a text message ("Loading chart...") in addition to the rotating gif. -- Or update the text messages. Eg start with "Fetching chart data from server" then update to "Processing chart data" once your success function has been called.

  • Rather than starting all 3 Ajax calls at once, experiment with having the success function for the first chart initiating the second Ajax call. (In addition to it charting the data.)

If you have problems with your Ajax calls, Google for examples and ask a separate question on SO if you still have problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文