如何将背景图像破解到 Google 图表工具中?动态(JavaScript/SVG)图表?

发布于 2024-12-26 02:00:45 字数 725 浏览 4 评论 0原文

我正在更新一个使用 Google Image Chart API 的旧数据可视化模块。现在,我们将切换到新的可视化 API< /a>,它使用 JavaScript 在浏览器中指定的

我们需要为图表提供背景图像(用于品牌推广目的)。然而,鉴于API参考中的配置选项,是改变背景颜色

如果我将背景图像嵌入到绘制图表的

中,则图表(显然)只会覆盖它。

是否有我完全不知道的 JavaScript 或 HTML5 解决方案可以解决我的问题?

(我对 JavaScript 有点陌生,感谢您忍受这个实际上可能是微不足道的——或者显然不可能的——问题!)

I'm updating an older data visualization module which used the Google Image Chart API. Now, we're switching to the new(er) Visualization API, which uses JavaScript to generate dynamic charts in the browser, within a specified <div> container.

We have a need to present the chart with a background image (for branding purposes). However, the only thing I've been able to accomplish, given the configuration options in API reference, is to change the background colour.

If I embed a background image into the <div> the Chart is drawn into, the chart (obviously) just overlays it.

Is there a JavaScript or HTML5 solution to my problem that I'm totally unaware of?

(I'm somewhat new to JavaScript, to thank you for bearing with what may actually be a trivial -- or clearly impossible -- problem!)

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

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

发布评论

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

评论(2

微凉徒眸意 2025-01-02 02:00:45

您可能希望用另一个图表的目标元素包裹图表的目标元素,并将父级的背景更改为您想要的图像,同时删除子级的背景。

例如,HTML

<div id='brand_div'>
    <div id='chart_div'></div>
</div>

和 CSS

#brand_div{ 
    background: url(<SOME-URL>) no-repeat;
}

当然,在 JavaScript 中的图表选项中,

var options = {
    ...
    backgroundColor: 'none'
};

我在这里放了一个示例 http://jsfiddle.net/jaimem/jaxfz /

You might want to wrap your chart's target element with another one and change the parent's background to the image you want, while removing the background of the child.

for example, HTML

<div id='brand_div'>
    <div id='chart_div'></div>
</div>

and CSS

#brand_div{ 
    background: url(<SOME-URL>) no-repeat;
}

And of course in your chart options in JavaScript

var options = {
    ...
    backgroundColor: 'none'
};

I put an example here http://jsfiddle.net/jaimem/jaxfz/

浅沫记忆 2025-01-02 02:00:45

以防万一有人遇到和我一样的问题,您也可以仅修复图表的背景,而不是整个 div(这会将背景添加到标题、轴等),

您可以执行以下操作:

在 Html 中,正如 jaime 建议的那样:

<div id='brand_div'>
    <div id='chart_div'></div>
</div>

在 css 中,正如 jaime 建议的那样:

#brand_div{ 
    background: url(<SOME-URL>) no-repeat;
}

在 javascript 中:

google.charts.load("current", {packages:["corechart"]});

google.charts.setOnLoadCallback(drawChart);

function drawChart() {
   var data = google.visualization.arrayToDataTable([
     ['ID', 'X', 'Y', 'Temperature'],
     ['',   80,  167,      120],
     ['',   79,  136,      130],
     ['',   78,  184,      50],
     ['',   72,  278,      230],
     ['',   81,  200,      210],
     ['',   72,  170,      100],
     ['',   68,  477,      80]
   ]);

   var options = {
     colorAxis: {colors: ['yellow', 'red']},
     width: 450,
     height: 300,
     title: 'My Daily Activities',
     backgroundColor: 'none' // this is important!
   };

   var chart = new google.visualization.BubbleChart(document.getElementById('chart_div'));
   chart.draw(data, options);

   // this two lines are the ones that do the magic
   var boundingBox = chart.getChartLayoutInterface().getChartAreaBoundingBox(); 
   $('#brand_div').css('background-position', boundingBox.left + "px " + boundingBox.top + "px").css('background-size', boundingBox.width + "px " + boundingBox.height + "px");
}

所有这些代码都是使用 jaime 的建议编写的,Google 图表文档 以及 此堆栈溢出问题

Just in case someone has the same problem as me, you can also fix the background to the chart only, and not the entire div (which will add background to the title, axis, etc.)

You can do this doing something like this:

In Html, as jaime suggested:

<div id='brand_div'>
    <div id='chart_div'></div>
</div>

In css, as jaime suggested:

#brand_div{ 
    background: url(<SOME-URL>) no-repeat;
}

In javascript:

google.charts.load("current", {packages:["corechart"]});

google.charts.setOnLoadCallback(drawChart);

function drawChart() {
   var data = google.visualization.arrayToDataTable([
     ['ID', 'X', 'Y', 'Temperature'],
     ['',   80,  167,      120],
     ['',   79,  136,      130],
     ['',   78,  184,      50],
     ['',   72,  278,      230],
     ['',   81,  200,      210],
     ['',   72,  170,      100],
     ['',   68,  477,      80]
   ]);

   var options = {
     colorAxis: {colors: ['yellow', 'red']},
     width: 450,
     height: 300,
     title: 'My Daily Activities',
     backgroundColor: 'none' // this is important!
   };

   var chart = new google.visualization.BubbleChart(document.getElementById('chart_div'));
   chart.draw(data, options);

   // this two lines are the ones that do the magic
   var boundingBox = chart.getChartLayoutInterface().getChartAreaBoundingBox(); 
   $('#brand_div').css('background-position', boundingBox.left + "px " + boundingBox.top + "px").css('background-size', boundingBox.width + "px " + boundingBox.height + "px");
}

All this code was written using jaime's suggestion, google chart's documentation and the comments on this stack overflow question

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