从PHP回应的d3.js边距值不起作用

时间:2017-07-05 08:26:51

标签: javascript d3.js

我将PHP(Laravel)中的图形边缘回显到这样的JavaScript:

margin = { 
    top: {!! $margins !!}[ 0 ],
    bottom: {!! $margins !!}[ 1 ], 
    left: {!! $margins !!}[ 2 ],
    right: {!! $margins !!}[ 3 ]
},

$margins是一个包含许多值的数组。 Console.log( {!! $margins !!}[ 0 ] )会为保证金返回正确的值,即30。当我看到来源时,我看到了:

margin = { 
    top: ["30","30","50","20","30","30","50","20"][ 0 ],
    bottom: ["30","30","50","20","30","30","50","20"][ 1 ], 
    left: ["30","30","50","20","30","30","50","20"][ 2 ],
    right: ["30","30","50","20","30","30","50","20"][ 3 ]
},

问题是d3似乎总结了数组中的所有边距值。即而不是30,它返回30 + 30 + 50 + 20 + 30 + 30 + 50 + 20 = 260。

有谁知道为什么会这样做以及如何解决它?

编辑:我有点不对劲。它没有总结数组值,而是在我从JSON数组设置边距时编辑SVG高度。

1 个答案:

答案 0 :(得分:-1)

可能存在错误。截至d3.js 4.9.1,它仍然存在。必须将顶部和底部边距强制为数字数据类型,否则它们将JSON字符串作为输入。左右边距自动转换为整数。所以这有效:

margin = { 
    top: {!! $margins !!}[ 0 ] * 1,
    bottom: {!! $margins !!}[ 1 ] * 1, 
    left: {!! $margins !!}[ 2 ],
    right: {!! $margins !!}[ 3 ]
},