Skip to content

Commit 3ddb55a

Browse files
authored
Merge pull request #306 from vizzuhq/docs_bgimage
Tutorial, add background image example
2 parents 47b0fe7 + 287d4bd commit 3ddb55a

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

docs/tutorial/events.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ Promise.all([dataLoaded, mdChartLoaded]).then((results) => {
1919
event.preventDefault();
2020
};
2121

22+
const backgroundImageHandler = (event) => {
23+
const bgImage = new Image();
24+
bgImage.src = "https://vizzuhq.com/images/logo/logo.svg";
25+
26+
const vizzuCanvasWidth = event.renderingContext.canvas.width;
27+
const vizzuCanvasHeight = event.renderingContext.canvas.height;
28+
29+
const imageAspectRatio = bgImage.width / bgImage.height;
30+
const canvasAspectRatio = vizzuCanvasWidth / vizzuCanvasHeight;
31+
let imageWidth;
32+
let imageHeight;
33+
if (imageAspectRatio > canvasAspectRatio) {
34+
imageWidth = vizzuCanvasWidth;
35+
imageHeight = vizzuCanvasWidth / imageAspectRatio;
36+
} else {
37+
imageHeight = vizzuCanvasHeight;
38+
imageWidth = vizzuCanvasHeight * imageAspectRatio;
39+
}
40+
const xOffset = (vizzuCanvasWidth - imageWidth) / 2;
41+
const yOffset = (vizzuCanvasHeight - imageHeight) / 2;
42+
43+
event.renderingContext.drawImage(
44+
bgImage,
45+
xOffset,
46+
yOffset,
47+
imageWidth,
48+
imageHeight
49+
);
50+
event.preventDefault();
51+
};
52+
2253
mdchart.create([
2354
{
2455
anims: [
@@ -95,5 +126,39 @@ Promise.all([dataLoaded, mdChartLoaded]).then((results) => {
95126
},
96127
],
97128
},
129+
{
130+
anims: [
131+
(chart) => {
132+
try {
133+
chart.off("background-draw", backgroundImageHandler);
134+
} catch (error) {
135+
if (!error.toString().includes("unknown event handler")) {
136+
throw error;
137+
}
138+
}
139+
return chart.animate({
140+
config: {
141+
title: "Add background image",
142+
},
143+
});
144+
},
145+
(chart) => {
146+
chart.on("background-draw", backgroundImageHandler);
147+
chart = chart.animate(
148+
{
149+
style: {
150+
plot: {
151+
xAxis: { interlacing: { color: "#ffffff00" } },
152+
yAxis: { interlacing: { color: "#ffffff00" } },
153+
},
154+
},
155+
},
156+
{ duration: 0 }
157+
);
158+
chart.then((chart) => chart.render.updateFrame(true));
159+
return chart;
160+
},
161+
],
162+
},
98163
]);
99164
});

docs/tutorial/events.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,75 @@ Unregistering the previously registered handler.
112112
chart.off('logo-draw', logoDrawHandler);
113113
```
114114

115+
You can also add a background image to the chart using the `preventDefault`
116+
method.
117+
118+
<div id="tutorial_04"></div>
119+
120+
```javascript
121+
function backgroundImageHandler(event) {
122+
const bgImage = new Image();
123+
bgImage.src = "https://vizzuhq.com/images/logo/logo.svg";
124+
125+
// Get the dimensions of the chart canvas
126+
const vizzuCanvasWidth = event.renderingContext.canvas.width;
127+
const vizzuCanvasHeight = event.renderingContext.canvas.height;
128+
129+
// Calculate the aspect ratios of the image and the canvas
130+
const imageAspectRatio = bgImage.width / bgImage.height;
131+
const canvasAspectRatio = vizzuCanvasWidth / vizzuCanvasHeight;
132+
133+
// Calculate the dimensions and position of the image on the canvas
134+
let imageWidth;
135+
let imageHeight;
136+
if (imageAspectRatio > canvasAspectRatio) {
137+
imageWidth = vizzuCanvasWidth;
138+
imageHeight = vizzuCanvasWidth / imageAspectRatio;
139+
} else {
140+
imageHeight = vizzuCanvasHeight;
141+
imageWidth = vizzuCanvasHeight * imageAspectRatio;
142+
}
143+
const xOffset = (vizzuCanvasWidth - imageWidth) / 2;
144+
const yOffset = (vizzuCanvasHeight - imageHeight) / 2;
145+
146+
// Draw the background image on the canvas
147+
event.renderingContext.drawImage(
148+
bgImage,
149+
xOffset,
150+
yOffset,
151+
imageWidth,
152+
imageHeight
153+
);
154+
event.preventDefault();
155+
}
156+
157+
chart.on('background-draw', backgroundImageHandler);
158+
```
159+
160+
??? info "Info - How to make interlacing transparent"
161+
```javascript
162+
chart.animate({
163+
style: {
164+
plot: {
165+
xAxis: {
166+
interlacing: {
167+
color: "#ffffff00"
168+
}
169+
},
170+
yAxis: {
171+
interlacing: {
172+
color: "#ffffff00"
173+
}
174+
},
175+
},
176+
},
177+
});
178+
```
179+
180+
Unregistering the previously registered handler.
181+
182+
```javascript
183+
chart.off('background-draw', backgroundImageHandler);
184+
```
185+
115186
<script src="../events.js"></script>

0 commit comments

Comments
 (0)