是否有可能获得图的基数64而不会在页面/画布上显示它(Graph.Js)

问题描述 投票:0回答:1

我只需要获取我提供的数据集的base-64并将其保存为图像。我面临的问题是我无法仅获得base-64。首先,我需要绘制画布,然后以64为底数。是否有可能在html页面上不显示图形的情况下得到它的base-64?请指教

javascript html jquery ruby-on-rails chart.js
1个回答
0
投票

您可以尝试使用red-agate-svg-canvas软件包,它允许您在服务器端呈现图表,但也可以在现代Web浏览器上使用。

const ctx = new SvgCanvas();

// SvgCanvas lacks the canvas property.
ctx.canvas = {
    width: 800,
    height: 400,
    style: {
        width: '800px',
        height: '400px',
    },
};

// SvgCanvas does not have font glyph information,
// so manually set the ratio of (font height / font width).
ctx.fontHeightRatio = 2;

// Chart.js needs a "HTMLCanvasElement"-like interface that has "getContext()" method.
// "getContext()" should returns a "CanvasRenderingContext2D"-compatible interface.
const el = { getContext: () => ctx };

// If "devicePixelRatio" is not set, Chart.js get the devicePixelRatio from "window" object.
// node.js environment has no window object.
opts.options.devicePixelRatio = 1;

// Disable animations.
opts.options.animation = false;
opts.options.events = [];
opts.options.responsive = false;

// Chart.js needs the "CanvasGradient" in the global scope.
const savedGradient = g.CanvasGradient;
g.CanvasGradient = SvgCanvas2DGradient;
try {
    const chart = new ChartJs.Chart(el, opts);
} finally {
    if (savedGradient) {
        g.CanvasGradient = savedGradient;
    }
}

// Render as SVG.
const svgString = ctx.render(new Rect2D(0, 0 , 800, 400), 'px');

希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.