从异步 AJAX 响应加载 Google Chart
在旧的 Google Chart API 中,可以使用 PHP 来渲染图表,甚至有一个包装器可以做到这一点: http://code.google.com/p/gchartphp/
但有了新的 Chart API http://code.google.com/apis/chart/ 它会生成更精美的图表,但仅在浏览器中使用 JavaScript 加载。
我想要实现的效果是通过AJAX向服务器提交多项选择表单,让PHP更新数据库服务器端,然后返回更新后的图表。
用旧的 API 方式我可以做到这一点。但以新的方式,我会将 javascript 返回到浏览器并将其附加到文档中以呈现图表。因此它不会执行。我相信我可以 eval() 这个 javascript,但这是一种不好的形式,不是因为有人可以用它做一些令人讨厌的事情,他们不能 - eval()ing 服务器端响应吗?
我怎样才能克服这个问题?有 PHP 包装器来帮助解决这个问题吗?还是还有其他我忽略的原因?
非常感谢
In the old Google Chart API it was possible to use PHP to render a chart, there was even a wrapper to do it:
http://code.google.com/p/gchartphp/
But with the new Chart API
http://code.google.com/apis/chart/
which produces much fancier chart, but loads only with javascript in the browser.
The effect I am trying to achieve is submitting a multiple choice form to the server via AJAX, have the PHP update the DB serverside, then return the updated chart.
In the old API way I could have done this. But in the new way I would be returning javascript to the browser and appending it to the document in order to render the chart. It wouldn't execute because of this. I believe I could eval() this javascript but that is bad form isn't it because someone could do some nasty stuff with that couldn't they - eval()ing a serverside response?
How can I overcome this? Is there a PHP wrapper to help this? Or is there another why which I have overlooked?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后我解决这个问题的方法非常明显。我只需要跳出自己的想法,从不同的角度来看待它。
我正在尝试使用 PHP 在服务器端处理数据库信息并创建 Google Chart(在本例中为饼图)的所有跑腿工作。然后将其作为 AJAX 响应返回。
实际上,我所需要做的就是返回创建饼图所需的数据(以及一些额外的元数据,例如目标元素 ID)。我是用 JSON 来做的。然后,通过浏览 Google Charts 文档,我能够找到如何使用客户端 javascript 触发 API 来加载返回的 JSON 数据。然后让 Google 代码在客户端渲染图表 - 所以我基本上将所有渲染责任转移到客户端浏览器。这就是新旧图表 API 的不同之处。
这需要大量的尝试和错误,并需要 Firebug 的大量帮助。值得一提的一个大问题是,您需要在收到 JSON 响应之前加载所有 Google JSAPI - 以便在初始页面呈现时 - 您必须做的一件事是:
确保它在页面呈现时加载。如果您在页面完全呈现后调用此方法,则页面将重新加载。
希望这对某人有帮助。
The way I solved this issue was pretty obvious in the end. I just needed to step outside my thoughts and approach it from a different angle.
I was trying to all the leg work of processing the database information AND creating the Google Chart (a Pie Chart in this case) on the server side, using PHP. Then return that as the AJAX response.
Actually all I needed to do was return the data needed to create the Pie Chart (along with some extra meta data such as target element id). I did this as JSON. Then by picking through the Google Charts documentation I was able to find out how to trigger the API using client side javascript to load the returned JSON data. Then let the Google code render the chart client side - so I basically shifted all the rendering responsibility to the client browser. This is where the old and new chart APIs differ.
It took a lot of trial and error and plenty of help from Firebug. One big tripping point which is worth mentioning is that you need to load all the Google JSAPI before you receive the JSON response - so as the initial page renders - and one thing you must do with:
is make sure it loads as the page is rendering. If you call this after the page has completely rendered then the page will reload.
Hope this helps someone.
感谢 andyg1 和 Ascendant,我能够让它像这样工作(使用 PrototypeJS 而不是 jQuery,但想法是相同的)。由于这并不明显,我将展示所有步骤。
Ajax 端点仅返回 json,如下所示(.NET MVC 模板)。请注意,我发现我必须引用 Google 文档 未建议的所有内容是必要的:
然后母版页包含以下内容:
然后在 myJavascriptFile.js 中(注意初始化方法的最后一行是 google.setOnLoadCallback ,它调用我的类中的方法而不是drawChart):
我确信它可以进一步改进,但它作品!
Thanks to andyg1 and Ascendant, I was able to make it work like this (using PrototypeJS rather than jQuery but the idea is the same). Since this is not at all obvious, I'm going to show all the steps.
The Ajax endpoint just returns json and looks like this (a .NET MVC template). Note that I found I had to quote everything which Google's documentation does not suggest is necessary:
Then the master page contained this:
Then in myJavascriptFile.js (note the last line of the initialize method is google.setOnLoadCallback which calls a method in my class not drawChart):
I'm sure it could be further improved but it works!
我过去曾使用隐藏字段处理过类似的情况,该隐藏字段作为请求的一部分返回,并以您想要的任何格式提供相关数据。没有返回 JavaScript。在 ajax 的成功回调中,您将获取并处理来自该输入的数据。
编辑:
基本上,如果您直接通过 ajax html 更新页面,并且您不想更改其功能,请将隐藏输入作为 ajax 响应的一部分
然后, >一旦 html 被注入到您的页面,您会执行类似“
这是最好的选择吗?”之 类的操作。我不知道。这实际上取决于您如何处理 ajax。我最初的评论来自 ASP.NET 背景,在这种背景下,您通常依赖某些类实例来为您生成 html,并且您有点从正在生成的实际 html(和其他内容)中抽象出来。当然还有其他方法可以处理这个问题,特别是如果您可以完全控制 AJAX 响应的呈现和处理方式。
I've handled situations like this in the past using a hidden field that's returned as part of the request with the relevant data in whatever format you want. No javascript is returned. On the ajax's success callback, you'd grab and handle the data from that input.
EDIT:
basically, if you're directly getting via ajax html to update the page and you don't want to change how that functions, put as part of the ajax response a hidden input
Then, once the html is injected into your page, you would do something like a
Is that the best option? I'm not sure. It really depends upon how you're handing your ajax. I made my original comments coming from an ASP.NET background, where, often, you are counting on certain class instances to generate html for you and you are kinda abstracted from the actual html (and other things) being generated. There are certainly other ways you could handle this, especially if you have full control as to how the AJAX response is rendered and handled.