我使用Highcharts SVG渲染API(渲染器)绘制自定义图表,我想为rect的stroke-width属性设置动画。 Here is Highcharts文档中提供的示例,但似乎无法正常工作 - 除了stroke-width之外,所有属性都会更改。
如果我在Chrome DevTools中打开HTML,我可以看到类似的内容:
<rect x="50" y="50" width="200" height="20" rx="5" ry="5" stroke="gray"
stroke-width="2" fill="silver" zIndex="3" strokeWidth="10"></rect>
使用驼峰式名称设置笔划宽度,而不是使用短划线样式名称。
可能有一些解决方法吗?
答案 0 :(得分:2)
是的,有一个解决方法。您可以使用jQuery的attr()函数来实现此目的。单击矩形时,更改stroke-width属性。
我认为这可能是一个很好的报告,可能是API的错误吗?
无论如何,JS代码将如下所示:
$(function () {
var renderer = new Highcharts.Renderer(
$('#container')[0],
400,
300
),
rect = renderer.rect(100, 100, 100, 100, 5)
.attr({
'stroke-width': 2,
stroke: 'gray',
fill: 'silver',
zIndex: 3
})
.on('click', function () {
rect.animate({
x: 50,
y: 50,
width: 200,
height: 20
})
$("rect").attr("stroke-width", "30"); // here you can change the stroke-width
})
.add();
});
要在此处查看已调整的JSFIDDLE
版本2
根据您的评论我编辑代码。在这种情况下,您还设置了笔画宽度的动画。这是一个简单的解决方案。另一个解决方案是调整我不推荐的原始Highcharts库。无论如何,希望它有所帮助:
$(function () {
var renderer = new Highcharts.Renderer(
$('#container')[0],
400,
300
),
rect = renderer.rect(100, 100, 100, 100, 5)
.attr({
'stroke-width': 2,
stroke: 'gray',
fill: 'silver',
zIndex: 3
})
.on('click', function () {
rect.animate({
x: 50,
y: 50,
width: 200,
height: 20
})
$("rect").animate(
{ "stroke-width": "10" },
{
duration: 500,
step: function(now) { $(this).attr("stroke-width", now); }
});
})
.add();
});
持续时间可以调整为适合您的时间。 在此处查看此操作:JSFIDDLE