JavaScript-数组转SVG


function svgHtmlByArr(data, width, height, color, strokeWidth) {
    const maxValue = Math.max(...data);
    const minValue = Math.min(...data);
    const xStep = width / (data.length - 1);

    let svg = `<svg width="${width}" height="${height}">`;
    svg += `<polyline points="`;

    data.forEach((value, index) => {
        const y = ((value - minValue) / (maxValue - minValue)) * height;
        const x = index * xStep;
        svg += `${x},${height - y} `;
    });

    svg += `" style="fill:none;stroke:${color};stroke-width:${strokeWidth}" />`;
    svg += '</svg>';

    return svg
}

const svgHtml = svgHtmlByArr(arr, 90, 20, 'black', 0.6)

文章作者: 钱不寒
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 钱不寒 !
  目录