| 1 |
/* Copyright 2015 Bloomberg Finance L.P. |
| 2 |
* |
| 3 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
* you may not use this file except in compliance with the License. |
| 5 |
* You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software |
| 10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
* See the License for the specific language governing permissions and |
| 13 |
* limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
import * as d3 from 'd3'; |
| 17 |
// var d3 =Object.assign({}, require("d3")); |
| 18 |
import { Mark } from './Mark'; |
| 19 |
import * as _ from 'underscore'; |
| 20 |
|
| 21 |
export class Image extends Mark { |
| 22 |
render() { |
| 23 |
const base_render_promise = super.render(); |
| 24 |
const el = this.d3el || this.el; |
| 25 |
this.im = ( |
| 26 |
el.append('image') as d3.Selection<SVGImageElement, any, any, any> |
| 27 |
) |
| 28 |
.attr('x', 0) |
| 29 |
.attr('y', 0) |
| 30 |
.attr('width', 1) |
| 31 |
.attr('height', 1) |
| 32 |
.attr('preserveAspectRatio', 'none') |
| 33 |
.classed('image_pixelated', this.model.get('pixelated')); |
| 34 |
this.update_image(); |
| 35 |
|
| 36 |
this.event_metadata = { |
| 37 |
mouse_over: { |
| 38 |
msg_name: 'hover', |
| 39 |
lookup_data: false, |
| 40 |
hit_test: true, |
| 41 |
}, |
| 42 |
legend_clicked: { |
| 43 |
msg_name: 'legend_click', |
| 44 |
hit_test: true, |
| 45 |
}, |
| 46 |
element_clicked: { |
| 47 |
msg_name: 'element_click', |
| 48 |
lookup_data: false, |
| 49 |
hit_test: false, |
| 50 |
}, |
| 51 |
parent_clicked: { |
| 52 |
msg_name: 'background_click', |
| 53 |
hit_test: false, |
| 54 |
}, |
| 55 |
}; |
| 56 |
|
| 57 |
const that = this; |
| 58 |
return base_render_promise.then(() => { |
| 59 |
that.event_listeners = {}; |
| 60 |
that.reset_click(); |
| 61 |
that.create_listeners(); |
| 62 |
that.listenTo(that.parent, 'margin_updated', () => { |
| 63 |
that.draw(false); |
| 64 |
}); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
set_positional_scales() { |
| 69 |
const x_scale = this.scales.x, |
| 70 |
y_scale = this.scales.y; |
| 71 |
this.listenTo(x_scale, 'domain_changed', function () { |
| 72 |
if (!this.model.dirty) { |
| 73 |
this.draw(); |
| 74 |
} |
| 75 |
}); |
| 76 |
this.listenTo(y_scale, 'domain_changed', function () { |
| 77 |
if (!this.model.dirty) { |
| 78 |
this.draw(); |
| 79 |
} |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
set_ranges() { |
| 84 |
const x_scale = this.scales.x, |
| 85 |
y_scale = this.scales.y; |
| 86 |
if (x_scale) { |
| 87 |
x_scale.set_range(this.parent.padded_range('x', x_scale.model)); |
| 88 |
} |
| 89 |
if (y_scale) { |
| 90 |
y_scale.set_range(this.parent.padded_range('y', y_scale.model)); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
create_listeners() { |
| 95 |
super.create_listeners(); |
| 96 |
this.listenTo(this.model, 'change:image', this.update_image); |
| 97 |
this.listenTo(this.model, 'data_updated', function () { |
| 98 |
//animate on data update |
| 99 |
const animate = true; |
| 100 |
this.draw(animate); |
| 101 |
}); |
| 102 |
this.listenTo(this.model, 'change:pixelated', () => { |
| 103 |
this.im.classed('image_pixelated', this.model.get('pixelated')); |
| 104 |
}); |
| 105 |
} |
| 106 |
|
| 107 |
update_image() { |
| 108 |
if (this.im.attr('href')) { |
| 109 |
URL.revokeObjectURL(this.im.attr('href')); |
| 110 |
} |
| 111 |
const image = this.model.get('image'); |
| 112 |
const format = image.get('format'); |
| 113 |
if (format === 'url') { |
| 114 |
const url = new TextDecoder('utf-8').decode(image.get('value')); |
| 115 |
this.im.attr('href', url); |
| 116 |
} else { |
| 117 |
const blob = new Blob([image.get('value')], { |
| 118 |
type: 'image/' + image.get('format'), |
| 119 |
}); |
| 120 |
const url = URL.createObjectURL(blob); |
| 121 |
this.im.attr('href', url); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
remove() { |
| 126 |
URL.revokeObjectURL(this.im.attr('href')); |
| 127 |
super.remove(); |
| 128 |
} |
| 129 |
|
| 130 |
relayout() { |
| 131 |
this.draw(true); |
| 132 |
} |
| 133 |
|
| 134 |
img_send_message(event_name, data) { |
| 135 |
// For the moment, use a custom function instead of overriding the |
| 136 |
// event_dispatcher from Mark.js. The data you want from an image |
| 137 |
// click is very different than other marks. We are not trying to |
| 138 |
// to find out which image was clicked in the way that Scatter |
| 139 |
// or Lines returns returns the position of the dot or line on |
| 140 |
// (or near) which the user clicked. |
| 141 |
// |
| 142 |
// Here we want to return the location of the mouse click. |
| 143 |
const event_data = this.event_metadata[event_name]; |
| 144 |
const data_message = { |
| 145 |
click_x: this.scales.x.invert(d3.mouse(this.el)[0]), |
| 146 |
click_y: this.scales.y.invert(d3.mouse(this.el)[1]), |
| 147 |
}; |
| 148 |
// how to get access to raw event: data.data.clientY}; |
| 149 |
// for (var datum in data.data) { |
| 150 |
// data_message[datum] = data.data[datum]; |
| 151 |
// } |
| 152 |
this.send({ event: event_data.msg_name, data: data_message }); |
| 153 |
} |
| 154 |
|
| 155 |
draw(animate?) { |
| 156 |
this.set_ranges(); |
| 157 |
|
| 158 |
const x_scale = this.scales.x ? this.scales.x : this.parent.scale_x; |
| 159 |
const y_scale = this.scales.y ? this.scales.y : this.parent.scale_y; |
| 160 |
|
| 161 |
const animation_duration = animate |
| 162 |
? this.parent.model.get('animation_duration') |
| 163 |
: 0; |
| 164 |
const el = (this.d3el || this.el) as d3.Selection<any, any, any, any>; |
| 165 |
const x_scaled = this.model.mark_data['x'].map(x_scale.scale), |
| 166 |
y_scaled = this.model.mark_data['y'].map(y_scale.scale); |
| 167 |
|
| 168 |
el.selectAll('image') |
| 169 |
.transition() |
| 170 |
.duration(animation_duration) |
| 171 |
.attr('transform', (d) => { |
| 172 |
const tx = x_scaled[0] + x_scale.offset; |
| 173 |
const ty = y_scaled[1] + y_scale.offset; |
| 174 |
const sx = x_scaled[1] - x_scaled[0]; |
| 175 |
const sy = y_scaled[0] - y_scaled[1]; |
| 176 |
return 'translate(' + tx + ',' + ty + ') scale(' + sx + ', ' + sy + ')'; |
| 177 |
}); |
| 178 |
el.on( |
| 179 |
'click', |
| 180 |
_.bind(function (d, i) { |
| 181 |
this.img_send_message('element_clicked', { data: d3.event, index: i }); |
| 182 |
}, this) |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
clear_style(style_dict, indices?, elements?) {} |
| 187 |
|
| 188 |
compute_view_padding() {} |
| 189 |
|
| 190 |
set_default_style(indices, elements?) {} |
| 191 |
|
| 192 |
set_style_on_elements(style, indices, elements?) {} |
| 193 |
|
| 194 |
im: d3.Selection<SVGImageElement, any, any, any>; |
| 195 |
} |