- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 cordova/phonegap 页面上有多个 D3 图表,我想让它们缩放以适应水平/垂直屏幕。只要我注释掉早期的宽度和高度属性,添加“Viewbox”和“presereAspectRatio”的属性就可以做到这一点。
图表在页面上定义为:
<div id="graph1"></div>
<div id="graph2"></div>
<div id="graph3"></div>
etc....
并且可以很好地处理“宽度”和“高度”的静态属性
但是当我添加“viewbox”属性时,它们的缩放比例非常好,但现在彼此之间的间距约为 15 厘米,导致您必须长时间向下滚动手机才能看到它们。
如果我注释掉“viewbox”和“preserveAspectRatio”并取消注释原始静态“width”和“height”属性,图形将像以前一样一个接一个地出现在屏幕上。但它们是静态的。我不确定每个内部或外部的哪个方面会受此影响。
下面是我用于选择方面等的代码。
var margin = {
top: 40,
right: 20,
bottom: 35,
left: 40
},
width = 475 - margin.left - margin.right,
height = 205 - margin.top - margin.bottom;
var svg = d3.select("#graph1")
.append("svg")
.attr("viewBox","0 0 475 205")
.attr("preserveAspectRatio", "xMinYMin")
// .attr("width", width + margin.left + margin.right)
// .attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
对此有任何想法将不胜感激。
谢谢;)
最佳答案
当你离开 width
和 height
,它们被设置为 100%,这意味着高度将是视口(viewport)的 100%。这意味着额外的空间会添加到 svg 的底部。您可以通过创建 <rect>
来测试它这是您的 viewBox 的大小,并在 svg 元素上放置一个边框。
HERE是一个例子。如您所见,作为 svg 元素一部分的 viewBox 之外(未被矩形覆盖)有额外的空间。
不幸的是,您可能需要使用脚本来调整 svg 的大小。您可以创建一个函数来根据容器的宽度(在您的情况下可能是 body 元素)以及 viewBox 中的高度和宽度的比率来设置 height 属性。这是一种方法:
function resizeAll() {
d3.selectAll('svg').call(scaleSvg);
}
function scaleSvg(sel) {
sel.each(function() {
// split the viewbox into its component parts
var vbArray = d3.select(this).attr('viewBox').split(' ');
// find the ratio of height to width
var heightWidthRatio = +vbArray[3] / +vbArray[2];
// get the width of the body (or you could use some other container)
var w = document.body.offsetWidth;
// set the width and height of the element
d3.select(this)
.attr('width', w)
.attr('height', w * heightWidthRatio);
});
}
然后您只需调用 resizeAll()
页面加载时以及窗口调整大小时。
HERE是一个例子。
关于javascript - viewbox SVG 属性导致图形间隔很远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844355/
我试图理解在 svg 中定位。 假设你有... ,其中 viewBox 的大小正确,以适合给定(已知)字体、字体粗细和字体大小的文本。没有给出一个x="..." y="..."翻译后,它不会在 vie
我使用 viewBox 来模拟路径的百分比值,如下所示: 这意味着一个单位有 5px。除了笔画始终为 5px 宽度外,这按预期工作。如何将笔画的宽度设置为始终 1px ?我已经试过了 strok
是否可以链接两个 ViewBox,以便它们始终使用相同的比例因子? 或者是否有可能提取 ViewBox 的当前比例因子以在另一个元素的 ScaleTransform 中使用? 谢谢你。 最佳答案 我想
我想知道 svg 元素的 View 框是否有任何硬性限制。当我达到非常低的值(比如当 vb 宽度约为 .002 时)或非常大的值时,我看到奇怪的剪裁,firefox 开始在 200000000 宽度左
我正在使用 View 框来缩放文本 block 。但是当文本 block 的字体大小低于某个字体大小时,我想用...修剪文本并停止缩放。 我怎样才能做到这一点?
假设我有以下简单的 SVN: 现在我想在圆旁边添加一个文本,它与圆的高度相同,在所选坐标系中为 0.2。 我怎样才能做到这一点? 最佳答案 嗯,天真的答案是使用圆的直径作为您的尺寸。即,f
在这个例子中,绿色圆圈被切断 见:http://jsfiddle.net/sCzZT/ 请注意,每个圆圈都包裹在自己的 svg 中 在这个例子中(没有嵌套的 svgs),绿色圆
我有一个带有 Canvas Child 的 Viewbox, 我有要填充的 Viewbox 的 Stretch 属性, 我更改了 Viewbox 的宽度和高度, 我需要获取 Canvas 中子项相对于
我有一个带有 Viewbox 的 WPF 窗口。我希望此 Viewbox 的其中一个子元素在任何屏幕分辨率下都保持相同的大小。 我的 XAML: Vi
这个问题在这里已经有了答案: Content of a Button Style appears only in one Button instance (3 个回答) 关闭4年前。 运行 Visua
This question有助于理解 viewBox 的用途,但并不完全相同。 目标是用相应的 viewBox 属性替换 SVG 元素的宽度和高度属性。我们的代码需要设置 viewBox,而不是宽度和
这个问题已经有答案了: How to parse SVG element's viewBox x, y, width and height values? (3 个回答) 已关闭 3 年前。 我是一个
我尝试使用 viewBox 在拉斐尔纸上用鼠标滚轮进行缩放。 Here是 JSFiddle 代码。 它有效,但现在我想放大中心,但我不知道从哪里开始。我想我应该更改 viewBox x 和 y 坐标。
我想创建可重复使用的形状,这些形状在使用时会自动缩放以适应给定视口(viewport)的大小。 我的方法是将形状包含在“symbol”元素中,并为其提供一个与形状本身大小相同的 viewBox。 这似
我想创建可重复使用的形状,这些形状在使用时会自动缩放以适应给定视口(viewport)的大小。 我的方法是将形状包含在“symbol”元素中,并为其提供一个与形状本身大小相同的 viewBox。 这似
如何通过vue.js自动评估SVG的viewbox? 我的代码: Vue.component('icon', { props: { width: { type: Number,
如何使用 XAML 和 UWP 将 Image 在 Viewbox 中垂直和水平居中?给定一个可变尺寸的图像,我需要将其缩放到容器的宽度,同时将其裁剪到特定的纵横比。 我尝试将图像的宽度和高度设置为所
我在我的项目中基于 d3.v2 库创建了力布局图。它在几乎所有部分都运行良好,但有一件事我想改变。因此,当我之后将节点拖出图形 block 时,我无法到达它。为了解决这个问题,我为我的图形添加了 vi
很久以前我做了一个 jsfiddle 来演示如何缩放到中心 ( http://jsfiddle.net/Y69nm/1/ )。现在我想缩放到给定点(光标),就像 googleMap 一样,但不知道该怎
我很惊讶我无法通过谷歌搜索找到它,但我在 WPF 窗口中有一个 Viewbox,我想通过代码向它添加一个 UserControl。 View 框是空的,它只是在某些事件期间弹出一个“等待微调器”(我厌
我是一名优秀的程序员,十分优秀!