Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Correct on exporting custom points #739

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions spec/api/api.export-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import util from "../assets/util";

describe("API export", () => {
let chart;
let args = {
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
["data2", 5000, 2000, 1000, 4000, 1500, 2500]
],
types: {
data1: "bar",
data2: "area"
}
}
};

beforeEach(() => {
chart = util.generate({
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
["data2", 5000, 2000, 1000, 4000, 1500, 2500]
],
types: {
data1: "bar",
data2: "area"
}
}
});
chart = util.generate(args);
});

it("should invoke a callback when ready", done => {
Expand All @@ -46,4 +47,40 @@ describe("API export", () => {

chart.export("image/png", exportCallback);
});

it("set options", () => {
args = {
size: {
width: 100,
height: 150
},
data: {
columns: [
["data1", 3]
]
},
point: {
pattern: [
"<g><circle cx='10' cy='10' r='10'></circle><rect x='5' y='5' width='10' height='10' style='fill:#fff'></rect></g>"
]
},
axis: {
y: {
min: 2.5,
max: 3
}
}
}
})

it("shoul exported custom points properly?", done => {
const expectedDataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAACWCAYAAAAouC1GAAALQklEQVR4Xu1dTYhlRxX+6nUmnYSgImRGA9M9OgsxKhqiu4BoFILTHcRFgjgbF3EjGiWZ7iEoiegi3Z0QFAQlCw0kKLgYsHvEv1HBhRhMBpII2WRId5tkkrgQRjvDpN+rcHpu4enKvbdO1a3785LzNtPz3qmqU99Xp/6+W3UN9DMoBMygvFFn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OI1xNNmJKSLEnfA6HiA6GYtQwmkjxAFGd3A9zyLEPwx0AsCjAF4tAKi6s4vs6Npa+lC3SkR8GcAvlBAAgTGE1DwCa4Edl3MtmUSlhwGcKd4E9wwAen/JIQD0t2t4VXavsZZLNkoIARIgxO/bq/p6/kIZnxDeY/gvnnG/KSEOiQAhPlD8XDy/+MZlVxYhVXYaIWWDW4MIof7fHQCqi5AqOyUkgRDXumk8cKDTGPL7YvB2hLgTXn6E+OciuZ0SkkAIJeHH6WiGRJdr0jqEf09vXfg2G+TdopSIrLN754whobm3/v5/BDpZhyjgcgSUEDlWnVgqIZ3ALC9ECZFj1YmlEtIJzPJClBA5Vp1YKiGdwCwvRAmRY9WJpRLSCczyQpQQOVadWL4JQPC8xLbzUzgAAAAASUVORK5CYII=";

setTimeout(() => {
chart.export("image/png", data => {
expect(data).to.be.equal(expectedDataURL);
done();
});
}, 500);
});
});
22 changes: 10 additions & 12 deletions src/api/api.export.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const b64EncodeUnicode = str => btoa(
* @return {String}
* @private
*/
const nodeToSvgDataUrl = node => {
const {width, height} = node.getBoundingClientRect();
const nodeToSvgDataUrl = (node, size) => {
const clone = node.cloneNode(true);
const styleSheets = toArray(document.styleSheets);
const cssRules = getCssRules(styleSheets);
Expand All @@ -37,12 +36,11 @@ const nodeToSvgDataUrl = node => {

// foreignObject not supported in IE11 and below
// https://msdn.microsoft.com/en-us/library/hh834675(v=vs.85).aspx
const dataStr = `<svg xmlns="${d3Namespaces.svg}" width="${width}" height="${height}">
const dataStr = `<svg xmlns="${d3Namespaces.svg}" width="${size.width}" height="${size.height}">
<foreignObject width="100%" height="100%">
<style>${cssText.join("\n")}</style>
${nodeXml}
${nodeXml.replace(/(url\()[^#]+/g, "$1")}
</foreignObject></svg>`
.replace(/#/g, "%23")
.replace("/\n/g", "%0A");

return `data:image/svg+xml;base64,${b64EncodeUnicode(dataStr)}`;
Expand Down Expand Up @@ -75,8 +73,10 @@ extend(Chart.prototype, {
* document.body.appendChild(link);
* });
*/
export(mimeType = "image/png", callback) {
const svgDataUrl = nodeToSvgDataUrl(this.element);
export(mimeType, callback) {
const $$ = this.internal;
const size = {width: $$.currentWidth, height: $$.currentHeight};
const svgDataUrl = nodeToSvgDataUrl(this.element, size);

if (isFunction(callback)) {
const img = new Image();
Expand All @@ -86,13 +86,11 @@ extend(Chart.prototype, {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");

canvas.width = img.width;
canvas.height = img.height;
canvas.width = size.width;
canvas.height = size.height;
ctx.drawImage(img, 0, 0);

canvas.toBlob(blob => {
callback(window.URL.createObjectURL(blob));
}, mimeType);
callback(canvas.toDataURL(mimeType));
};

img.src = svgDataUrl;
Expand Down