gpt4 book ai didi

javascript - 如何在 Plottable.js 中调整图表大小

转载 作者:行者123 更新时间:2023-11-28 06:49:02 25 4
gpt4 key购买 nike

我正在使用 Plottable.js 在我的页面上绘制图表。我想在调整窗口大小时调整图表的大小。

我尝试通过 css(以及 svg 宽度和高度属性)设置大小,但没有成功。

这是我尝试通过 svg 属性进行设置:

$('#svgSample').attr('width', w);
$('#svgSample').attr('height', h);

这是我通过 css 设置的尝试:

$('#svgSample').css({
'width': w + 'px',
'height': h + 'px'
});

有什么想法吗?

谢谢

最佳答案

我相信您需要做的就是在设置 svg 属性后调用 Plot.redraw()

这是一个例子:

window.onload = function() {
var data = [
{ x: 1, y: 1 },
{ x: 2, y: 1 },
{ x: 3, y: 2 },
{ x: 4, y: 3 },
{ x: 5, y: 5 },
{ x: 6, y: 8 }
];
var xScale = new Plottable.Scales.Linear();
var yScale = new Plottable.Scales.Linear();

var plot = new Plottable.Plots.Scatter()
.addDataset(new Plottable.Dataset(data))
.x(function(d) { return d.x; }, xScale)
.y(function(d) { return d.y; }, yScale);

plot.renderTo("#chart");
$('#chart').attr('width', 500);
$('#chart').attr('height', 400);
plot.redraw();
}
</style> <link rel="stylesheet" type="text/css" href="https://rawgithub.com/palantir/plottable/develop/plottable.css"> <style type="text/css">
body { background-color: #AAA; }
svg { background-color: #FFF; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://rawgithub.com/palantir/plottable/develop/plottable.js"></script>
<svg id="chart" width="100" height="100"></svg>

关于javascript - 如何在 Plottable.js 中调整图表大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33141679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com