mth*_*pvg 170 javascript d3.js
我正在用d3.js画一个散点图.借助此问题:
获取屏幕大小,当前网页和浏览器窗口
我正在使用这个答案:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
Run Code Online (Sandbox Code Playgroud)
所以我能够将我的情节适合用户的窗口,如下所示:
var svg = d3.select("body").append("svg")
.attr("width", x)
.attr("height", y)
.append("g");
Run Code Online (Sandbox Code Playgroud)
现在我想在用户调整窗口大小时,需要注意调整绘图的大小.
PS:我的代码中没有使用jQuery.
cmi*_*tti 277
寻找"响应式SVG"使SVG响应变得非常简单,您不必再担心大小了.
我是这样做的:
d3.select("div#chartId")
.append("div")
// Container class to make it responsive.
.classed("svg-container", true)
.append("svg")
// Responsive SVG needs these 2 attributes and no width and height attr.
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", "0 0 600 400")
// Class to make it responsive.
.classed("svg-content-responsive", true)
// Fill with a rectangle for visualization.
.append("rect")
.classed("rect", true)
.attr("width", 600)
.attr("height", 400);
Run Code Online (Sandbox Code Playgroud)
CSS代码:
.svg-container {
display: inline-block;
position: relative;
width: 100%;
padding-bottom: 100%; /* aspect ratio */
vertical-align: top;
overflow: hidden;
}
.svg-content-responsive {
display: inline-block;
position: absolute;
top: 10px;
left: 0;
}
svg .rect {
fill: gold;
stroke: steelblue;
stroke-width: 5px;
}
Run Code Online (Sandbox Code Playgroud)
更多信息/教程:
http://thenewcode.com/744/Make-SVG-Responsive
http://soqr.fr/testsvg/embed-svg-liquid-layout-responsive-web-design.php
Ada*_*rce 43
function updateWindow(){
x = w.innerWidth || e.clientWidth || g.clientWidth;
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
svg.attr("width", x).attr("height", y);
}
d3.select(window).on('resize.updatesvg', updateWindow);
Run Code Online (Sandbox Code Playgroud)
slf*_*slf 32
更新只是使用@cminatti的新方法
历史目的的旧答案
IMO最好使用select()和on(),因为这样你可以拥有多个resize事件处理程序......只是不要太疯狂
d3.select(window).on('resize', resize);
function resize() {
// update width
width = parseInt(d3.select('#chart').style('width'), 10);
width = width - margin.left - margin.right;
// resize the chart
x.range([0, width]);
d3.select(chart.node().parentNode)
.style('height', (y.rangeExtent()[1] + margin.top + margin.bottom) + 'px')
.style('width', (width + margin.left + margin.right) + 'px');
chart.selectAll('rect.background')
.attr('width', width);
chart.selectAll('rect.percent')
.attr('width', function(d) { return x(d.percent); });
// update median ticks
var median = d3.median(chart.selectAll('.bar').data(),
function(d) { return d.percent; });
chart.selectAll('line.median')
.attr('x1', x(median))
.attr('x2', x(median));
// update axes
chart.select('.x.axis.top').call(xAxis.orient('top'));
chart.select('.x.axis.bottom').call(xAxis.orient('bottom'));
}
Run Code Online (Sandbox Code Playgroud)
http://eyeseast.github.io/visible-data/2013/08/28/responsive-charts-with-d3/
Rai*_*aik 12
如果调整大小代码几乎与首先构建图形的代码一样长,那就太难看了.因此,为什么不简单地重新加载它,而不是调整现有图表的每个元素的大小?以下是它对我有用的方式:
function data_display(data){
e = document.getElementById('data-div');
var w = e.clientWidth;
// remove old svg if any -- otherwise resizing adds a second one
d3.select('svg').remove();
// create canvas
var svg = d3.select('#data-div').append('svg')
.attr('height', 100)
.attr('width', w);
// now add lots of beautiful elements to your graph
// ...
}
data_display(my_data); // call on page load
window.addEventListener('resize', function(event){
data_display(my_data); // just call it again...
}
Run Code Online (Sandbox Code Playgroud)
至关重要的是d3.select('svg').remove();
.否则,每次调整大小都会在前一个之后添加另一个SVG元素.
在强制布局中,仅设置"高度"和"宽度"属性将无法将绘图重新居中/移动到svg容器中.然而,不是这样的发现力布局工作很简单的答案在这里.综上所述:
使用你喜欢的相同(任何)事件.
window.on('resize', resize);
Run Code Online (Sandbox Code Playgroud)
假设你有svg和force变量:
var svg = /* D3 Code */;
var force = /* D3 Code */;
function resize(e){
// get width/height with container selector (body also works)
// or use other method of calculating desired values
var width = $('#myselector').width();
var height = $('#myselector').height();
// set attrs and 'resume' force
svg.attr('width', width);
svg.attr('height', height);
force.size([width, height]).resume();
}
Run Code Online (Sandbox Code Playgroud)
这样,你不会完全重新渲染图形,我们设置属性,d3根据需要重新计算.这至少在您使用重力点时起作用.我不确定这是否是此解决方案的先决条件.任何人都可以确认或否认吗?
干杯,克