如何将高图转换为二进制图像

时间:2013-10-08 10:36:15

标签: javascript highcharts

我正在使用高图来绘制带角度的图形:

HTML:

<highchart id="chart" class="plotchartCntr" config="chart"></highchart>

JS:

$scope.chart = {
    options: {
        chart: {
            type: 'column'
        }
    },
    xAxis: {
        categories: [] // dynamically loaded after search
    },
    yAxis: {
        title: {
            text: 'Counts'
        },
        labels: {
            formatter: function() {
                return this.value
            }
        }
    },
    tooltip: {
        crosshairs: true,
        shared: true
    },
    series: [] // dynamically loaded after search
}

我必须将其保存为图像。

我的代码(来自堆栈溢出):

var canvas = document.createElement("chart");
canvas.width = 200;
canvas.height = 500;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
console.log(dataURL);

但我收到错误canvas.getContext is not a function

请建议

2 个答案:

答案 0 :(得分:2)

谢谢,最后我得到了答案:

HTML:

<img id="mock" />

JS:

var svg = document.getElementById('chart').children[0].innerHTML;
//svg = chart.getSVG(); 

var base_image = new Image();
svg = "data:image/svg+xml," + svg;
base_image.src = svg;
$('#mock').attr('src', svg);

我们也可以直接在DB中保存图像标签。参考。 Save highchart to binary image

答案 1 :(得分:0)

 var canvas = document.createElement("chart");

替换为:

var canvas = document.getElementById("chart");