在页面加载之前/期间加载 Google 可视化 API

发布于 2025-01-14 11:47:32 字数 958 浏览 4 评论 0原文

我所处的场景是,我需要显示一个可呈现的谷歌图表,并准备好在整个页面加载“之前”显示。我尝试使用“ignoreWindowOnLoad”属性加载模块,混淆不同的异步调用方法和不同版本的 API。我还尝试通过以下方式加载脚本:

<script type="text/javascript" src='https://www.google.com/jsapi?autoload={"modules":[{"name":"visualization","version":"current","packages":["gauge"],"ignoreWindowOnLoad":true}]}'></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
google.load("visualization", "current", { packages: ["corechart"], ignoreWindowOnLoad: true});

问题是,当页面加载时,我尝试在加载后立即绘制图表,我得到一个异常:

Uncaught TypeError: Cannot read properties of undefined (reading 'arrayToDataTable')
    at drawChart

我使用 ASP.NET(不是 Core)作为在我的场景中,使用 Node.js 等 SSR 技术并不是一个可行的选择,但如果我可以在没有框架的情况下实现 SSR(如果它不是一个过度杀伤性的解决方案),那可能是可以接受的。如果我在页面完全加载后调用drawChart,图表加载正常。

基本上,我正在寻找一种方法来确保所有与可视化相关的 API 在页面呈现之前完全加载,因此单个 Web 请求可以拉取整个页面及其内容(包括图表)。任何有关如何实现这一目标的建议将不胜感激。

I am in a scenario where I need to display a google chart that is presentable and ready to display "before" the entire page loads. I've tried loading modules using the "ignoreWindowOnLoad" property, messing with different async call methodologies, and different versions of the API. I've also tried loading the scripts the following ways:

<script type="text/javascript" src='https://www.google.com/jsapi?autoload={"modules":[{"name":"visualization","version":"current","packages":["gauge"],"ignoreWindowOnLoad":true}]}'></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
google.load("visualization", "current", { packages: ["corechart"], ignoreWindowOnLoad: true});

The issue is that by the time the page loads and I try to draw the chart immediately after load, I get an exception:

Uncaught TypeError: Cannot read properties of undefined (reading 'arrayToDataTable')
    at drawChart

I am using ASP.NET (not Core) as the backend, and using an SSR technology such as node.js isn't a viable option in my scenario, but if I can achieve SSR without a framework (if it's not an overkill solution) that may be acceptable. The chart loads fine if I call drawChart after the page fully loads.

Basically I'm looking for a way to ensure all visualization-related APIs are fully loaded before the page presents itself, so a single web request can pull the entire page and its contents, charts included. Any advice on how to achieve this would be greatly appreciated.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文