| 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 { WidgetView } from '@jupyter-widgets/base'; |
| 17 |
import * as d3 from 'd3'; |
| 18 |
// var d3 =Object.assign({}, require("d3-axis"), require("d3-scale"), require("d3-selection"), require("d3-selection-multi")); |
| 19 |
import { Axis } from './Axis'; |
| 20 |
import { applyAttrs } from './utils'; |
| 21 |
import { ColorScale } from './ColorScale'; |
| 22 |
|
| 23 |
class ColorBar extends Axis { |
| 24 |
render() { |
| 25 |
this.parent = this.options.parent; |
| 26 |
this.vertical = this.model.get('orientation') === 'vertical'; |
| 27 |
|
| 28 |
const scale_promise = this.set_scale(this.model.get('scale')); |
| 29 |
this.side = this.model.get('side'); |
| 30 |
this.x_offset = 100; |
| 31 |
this.y_offset = 40; |
| 32 |
this.bar_height = 20; |
| 33 |
this.d3el |
| 34 |
.attr('class', 'ColorBar') |
| 35 |
.attr('display', this.model.get('visible') ? 'inline' : 'none') |
| 36 |
.attr('transform', this.get_topg_transform()); |
| 37 |
|
| 38 |
this.ordinal = false; |
| 39 |
this.num_ticks = this.model.get('num_ticks'); |
| 40 |
const that = this; |
| 41 |
return scale_promise.then(() => { |
| 42 |
that.create_listeners(); |
| 43 |
that.tick_format = that.generate_tick_formatter(); |
| 44 |
that.set_scales_range(); |
| 45 |
that.append_axis(); |
| 46 |
}); |
| 47 |
} |
| 48 |
|
| 49 |
create_listeners() { |
| 50 |
this.listenTo(this.model, 'change:scale', function (model, value) { |
| 51 |
this.update_scale(model.previous('scale'), value); |
| 52 |
// TODO: rescale_axis does too many things. Decompose |
| 53 |
this.axis.scale(this.axis_scale.scale); // TODO: this is in redraw_axisline |
| 54 |
this.rescale_axis(); |
| 55 |
}); |
| 56 |
|
| 57 |
this.listenTo(this.model, 'change:tick_format', this.tickformat_changed); |
| 58 |
this.axis_scale.on('domain_changed', this.redraw_axisline, this); |
| 59 |
this.axis_scale.on('color_scale_range_changed', this.redraw_axis, this); |
| 60 |
this.axis_scale.on('highlight_axis', this.highlight, this); |
| 61 |
this.axis_scale.on('unhighlight_axis', this.unhighlight, this); |
| 62 |
|
| 63 |
this.listenTo(this.parent, 'margin_updated', this.parent_margin_updated); |
| 64 |
this.listenTo(this.model, 'change:visible', this.update_visibility); |
| 65 |
this.listenTo(this.model, 'change:label', this.update_label); |
| 66 |
this.model.on_some_change( |
| 67 |
['side', 'orientation'], |
| 68 |
this.update_display, |
| 69 |
this |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
update_display() { |
| 74 |
this.side = this.model.get('side'); |
| 75 |
this.vertical = this.model.get('orientation') === 'vertical'; |
| 76 |
if (this.vertical) { |
| 77 |
this.axis = |
| 78 |
this.side === 'right' |
| 79 |
? d3.axisRight(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>) |
| 80 |
: d3.axisLeft(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>); |
| 81 |
} else { |
| 82 |
this.axis = |
| 83 |
this.side === 'top' |
| 84 |
? d3.axisTop(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>) |
| 85 |
: d3.axisBottom(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>); |
| 86 |
} |
| 87 |
this.g_axisline.remove(); |
| 88 |
this.g_axisline = this.d3el |
| 89 |
.select('#colorBarG' + this.cid) |
| 90 |
.append('g') |
| 91 |
.attr('class', 'axis'); |
| 92 |
this.rescale_axis(); |
| 93 |
this.d3el |
| 94 |
.select('#colorBarG' + this.cid) |
| 95 |
.attr('transform', this.get_colorbar_transform()); |
| 96 |
this.d3el |
| 97 |
.select('#colorBarG' + this.cid) |
| 98 |
.select('.g-rect') |
| 99 |
.attr('transform', this.vertical ? 'rotate(-90)' : ''); |
| 100 |
this.redraw_axisline(); |
| 101 |
} |
| 102 |
|
| 103 |
set_scale(model) { |
| 104 |
// Sets the child scale |
| 105 |
const that = this; |
| 106 |
if (this.axis_scale) { |
| 107 |
this.axis_scale.remove(); |
| 108 |
} |
| 109 |
return this.create_child_view(model).then((view) => { |
| 110 |
// Trigger the displayed event of the child view. |
| 111 |
that.displayed.then(() => { |
| 112 |
view.trigger('displayed'); |
| 113 |
}); |
| 114 |
that.axis_scale = view as WidgetView as ColorScale; |
| 115 |
// TODO: eventually removes what follows |
| 116 |
if (that.axis_scale.model.type === 'date_color_linear') { |
| 117 |
that.axis_line_scale = d3.scaleUtc().nice(); |
| 118 |
} else if (that.axis_scale.model.type === 'ordinal') { |
| 119 |
that.axis_line_scale = d3.scaleBand(); |
| 120 |
that.ordinal = true; |
| 121 |
} else { |
| 122 |
that.axis_line_scale = d3.scaleLinear(); |
| 123 |
} |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
append_axis() { |
| 128 |
// The label is allocated a space of 100px. If the label |
| 129 |
// occupies more than 100px then you are out of luck. |
| 130 |
if ( |
| 131 |
this.model.get('label') !== undefined && |
| 132 |
this.model.get('label') !== null |
| 133 |
) { |
| 134 |
this.d3el |
| 135 |
.append('g') |
| 136 |
.attr('transform', this.get_label_transform()) |
| 137 |
.attr('class', 'axis label_g') |
| 138 |
.append('text') |
| 139 |
.append('tspan') |
| 140 |
.attr('id', 'text_elem') |
| 141 |
.attr('dy', '0.5ex') |
| 142 |
.attr('class', 'axislabel') |
| 143 |
.style('text-anchor', this.vertical ? 'middle' : 'end') |
| 144 |
.text(this.model.get('label')); |
| 145 |
} |
| 146 |
const colorBar = this.d3el.append('g').attr('id', 'colorBarG' + this.cid); |
| 147 |
|
| 148 |
this.draw_color_bar(); |
| 149 |
this.set_axisline_domain(); |
| 150 |
|
| 151 |
this.g_axisline = colorBar.append('g').attr('class', 'axis'); |
| 152 |
|
| 153 |
if (this.vertical) { |
| 154 |
this.axis = |
| 155 |
this.side === 'right' |
| 156 |
? d3.axisRight(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>) |
| 157 |
: d3.axisLeft(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>); |
| 158 |
} else { |
| 159 |
this.axis = |
| 160 |
this.side === 'top' |
| 161 |
? d3.axisTop(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>) |
| 162 |
: d3.axisBottom(this.axis_scale.scale as d3.AxisScale<d3.AxisDomain>); |
| 163 |
} |
| 164 |
this.axis = this.axis.tickFormat(this.tick_format); |
| 165 |
this.redraw_axisline(); |
| 166 |
} |
| 167 |
|
| 168 |
draw_color_bar() { |
| 169 |
const colorBar = this.d3el.select('#colorBarG' + this.cid); |
| 170 |
colorBar.attr('transform', this.get_colorbar_transform()); |
| 171 |
const that = this; |
| 172 |
colorBar.selectAll('.g-rect').remove(); |
| 173 |
colorBar.selectAll('.g-defs').remove(); |
| 174 |
|
| 175 |
this.colors = this.axis_scale.scale.range() as number[]; |
| 176 |
const colorSpacing = 100 / (this.colors.length - 1); |
| 177 |
|
| 178 |
if (this.ordinal) { |
| 179 |
const bar_width = this.get_color_bar_width() / this.colors.length; |
| 180 |
let rects = colorBar |
| 181 |
.append('g') |
| 182 |
.attr('class', 'g-rect axis') |
| 183 |
.selectAll('rect') |
| 184 |
.data(this.colors); |
| 185 |
|
| 186 |
rects = rects |
| 187 |
.enter() |
| 188 |
.append('rect') |
| 189 |
.attr('y', 0) |
| 190 |
.attr('height', this.bar_height) |
| 191 |
.attr('width', bar_width) |
| 192 |
.style('fill', (d) => { |
| 193 |
return d; |
| 194 |
}) |
| 195 |
.merge(rects as d3.Selection<SVGRectElement, number, SVGGElement, any>); |
| 196 |
|
| 197 |
if (this.vertical) { |
| 198 |
rects.attr('x', (d, i) => { |
| 199 |
return i * bar_width - (that.height - 2 * that.x_offset); |
| 200 |
}); |
| 201 |
} else { |
| 202 |
rects.attr('x', (d, i) => { |
| 203 |
return i * bar_width; |
| 204 |
}); |
| 205 |
} |
| 206 |
} else { |
| 207 |
const gradient = colorBar |
| 208 |
.append('g') |
| 209 |
.attr('class', 'g-defs') |
| 210 |
.append('defs') |
| 211 |
.append('linearGradient'); |
| 212 |
applyAttrs(gradient, { |
| 213 |
id: 'colorBarGradient' + this.cid, |
| 214 |
x1: '0%', |
| 215 |
y1: '0%', |
| 216 |
x2: '100%', |
| 217 |
y2: '0%', |
| 218 |
}); |
| 219 |
const stop = gradient |
| 220 |
.selectAll('stop') |
| 221 |
.data(this.colors) |
| 222 |
.enter() |
| 223 |
.append('stop'); |
| 224 |
applyAttrs(stop, { |
| 225 |
offset: function (d, i) { |
| 226 |
return colorSpacing * i + '%'; |
| 227 |
}, |
| 228 |
'stop-color': function (d, i) { |
| 229 |
return that.colors[i]; |
| 230 |
}, |
| 231 |
'stop-opacity': 1, |
| 232 |
}); |
| 233 |
|
| 234 |
const rect = colorBar |
| 235 |
.append('g') |
| 236 |
.attr('class', 'g-rect axis') |
| 237 |
.append('rect'); |
| 238 |
applyAttrs(rect, { |
| 239 |
width: this.get_color_bar_width(), |
| 240 |
height: this.bar_height, |
| 241 |
x: this.vertical ? -(this.height - 2 * this.x_offset) : 0, |
| 242 |
y: 0, |
| 243 |
'stroke-width': 1, |
| 244 |
}).style('fill', 'url(#colorBarGradient' + this.cid + ')'); |
| 245 |
} |
| 246 |
if (this.vertical) { |
| 247 |
colorBar.select('.g-rect').attr('transform', 'rotate(-90)'); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
get_topg_transform() { |
| 252 |
const em = 12; |
| 253 |
if (this.vertical) { |
| 254 |
if (this.side === 'right') { |
| 255 |
return ( |
| 256 |
'translate(' + |
| 257 |
String( |
| 258 |
this.get_basic_transform() + this.margin.right / 2 - this.bar_height |
| 259 |
) + |
| 260 |
', 0)' |
| 261 |
); |
| 262 |
} |
| 263 |
return ( |
| 264 |
'translate(' + |
| 265 |
String( |
| 266 |
this.get_basic_transform() - this.margin.left / 2 + this.bar_height |
| 267 |
) + |
| 268 |
', 0)' |
| 269 |
); |
| 270 |
} else { |
| 271 |
if (this.side === 'top') { |
| 272 |
return ( |
| 273 |
'translate(0, ' + |
| 274 |
String( |
| 275 |
this.get_basic_transform() - |
| 276 |
this.margin.top + |
| 277 |
this.bar_height + |
| 278 |
2 * em |
| 279 |
) + |
| 280 |
')' |
| 281 |
); |
| 282 |
} |
| 283 |
return ( |
| 284 |
'translate(0, ' + |
| 285 |
String( |
| 286 |
this.get_basic_transform() + |
| 287 |
this.margin.bottom - |
| 288 |
this.bar_height - |
| 289 |
2 * em |
| 290 |
) + |
| 291 |
')' |
| 292 |
); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
get_label_transform() { |
| 297 |
if (this.vertical) { |
| 298 |
return ( |
| 299 |
'translate(' + |
| 300 |
(this.side === 'right' ? this.bar_height / 2 : -this.bar_height / 2) + |
| 301 |
', ' + |
| 302 |
(this.x_offset - 15) + |
| 303 |
')' |
| 304 |
); |
| 305 |
} |
| 306 |
return ( |
| 307 |
'translate(' + (this.x_offset - 5) + ', ' + this.bar_height / 2 + ')' |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
get_colorbar_transform() { |
| 312 |
if (this.vertical) { |
| 313 |
return 'translate(0, ' + String(this.x_offset) + ')'; |
| 314 |
} |
| 315 |
return 'translate(' + String(this.x_offset) + ', 0)'; |
| 316 |
} |
| 317 |
|
| 318 |
set_axisline_scale_range() { |
| 319 |
const range = this.vertical |
| 320 |
? [this.height - 2 * this.x_offset, 0] |
| 321 |
: [0, this.width - 2 * this.x_offset]; |
| 322 |
if (this.ordinal) { |
| 323 |
(this.axis_line_scale as d3.ScaleBand<string>) |
| 324 |
.rangeRound(range as [number, number]) |
| 325 |
.padding(0.05); |
| 326 |
} else { |
| 327 |
const mid = this.axis_scale.model.mid; |
| 328 |
if (mid === undefined || mid === null) { |
| 329 |
this.axis_line_scale.range(range); |
| 330 |
} else { |
| 331 |
this.axis_line_scale.range([ |
| 332 |
range[0], |
| 333 |
(range[0] + range[1]) * 0.5, |
| 334 |
range[1], |
| 335 |
]); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
set_scales_range() { |
| 341 |
//Setting the range of the color scale |
| 342 |
this.axis_scale.set_range(); |
| 343 |
this.set_axisline_scale_range(); |
| 344 |
} |
| 345 |
|
| 346 |
get_color_bar_width() { |
| 347 |
return this.vertical |
| 348 |
? this.height - 2 * this.x_offset |
| 349 |
: this.width - 2 * this.x_offset; |
| 350 |
} |
| 351 |
|
| 352 |
update_label() { |
| 353 |
this.d3el.select('#text_elem').text(this.model.get('label')); |
| 354 |
} |
| 355 |
|
| 356 |
rescale_axis() { |
| 357 |
// rescale the axis |
| 358 |
this.set_axisline_scale_range(); |
| 359 |
// shifting the entire g of the color bar first. |
| 360 |
this.d3el.attr('transform', this.get_topg_transform()); |
| 361 |
const that = this; |
| 362 |
const bar_width = this.get_color_bar_width() / this.colors.length; |
| 363 |
if (this.ordinal) { |
| 364 |
const rectangles = this.d3el |
| 365 |
.select('#colorBarG' + this.cid) |
| 366 |
.select('.g-rect') |
| 367 |
.selectAll('rect') |
| 368 |
.attr('width', bar_width); |
| 369 |
if (this.vertical) { |
| 370 |
rectangles.attr('x', (d, i) => { |
| 371 |
return i * bar_width - (that.height - 2 * that.x_offset); |
| 372 |
}); |
| 373 |
} else { |
| 374 |
rectangles.attr('x', (d, i) => { |
| 375 |
return i * bar_width; |
| 376 |
}); |
| 377 |
} |
| 378 |
} else { |
| 379 |
this.d3el |
| 380 |
.select('#colorBarG' + this.cid) |
| 381 |
.select('.g-rect') |
| 382 |
.selectAll('rect') |
| 383 |
.attr('width', this.get_color_bar_width()) |
| 384 |
.attr('x', this.vertical ? -(this.height - 2 * this.x_offset) : 0); |
| 385 |
} |
| 386 |
if ( |
| 387 |
this.model.get('label') !== undefined && |
| 388 |
this.model.get('label') !== null |
| 389 |
) { |
| 390 |
this.d3el |
| 391 |
.select('.label_g') |
| 392 |
.attr('transform', this.get_label_transform()) |
| 393 |
.select('#text_elem') |
| 394 |
.style('text-anchor', this.vertical ? 'middle' : 'end'); |
| 395 |
} |
| 396 |
this.g_axisline.call(this.axis); |
| 397 |
} |
| 398 |
|
| 399 |
redraw_axisline() { |
| 400 |
if (this.axis) { |
| 401 |
this.set_axisline_domain(); |
| 402 |
// We need to set the range of the axis line scale here again. |
| 403 |
// Only because, if the domain has changed from a two element |
| 404 |
// array to a three element one, the range of the axis has to |
| 405 |
// be changed accordingly. |
| 406 |
this.set_axisline_scale_range(); |
| 407 |
this.axis.scale(this.axis_line_scale); |
| 408 |
this.set_tick_values(); |
| 409 |
|
| 410 |
let transform; |
| 411 |
if (this.vertical) { |
| 412 |
transform = |
| 413 |
'translate(' + (this.side === 'right' ? this.bar_height : 0) + ', 0)'; |
| 414 |
} else { |
| 415 |
transform = |
| 416 |
'translate(0, ' + (this.side === 'top' ? 0 : this.bar_height) + ')'; |
| 417 |
} |
| 418 |
this.g_axisline.attr('transform', transform).call(this.axis); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
set_axisline_domain() { |
| 423 |
const domain = this.axis_scale.scale.domain(); |
| 424 |
if (this.ordinal) { |
| 425 |
this.axis_line_scale.domain(domain); |
| 426 |
} else { |
| 427 |
const mid = this.axis_scale.model.mid; |
| 428 |
if (mid === undefined || mid === null) { |
| 429 |
this.axis_line_scale.domain([domain[0], domain[domain.length - 1]]); |
| 430 |
} else { |
| 431 |
this.axis_line_scale.domain([ |
| 432 |
domain[0], |
| 433 |
mid, |
| 434 |
domain[domain.length - 1], |
| 435 |
]); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
redraw_axis() { |
| 441 |
this.draw_color_bar(); |
| 442 |
this.redraw_axisline(); |
| 443 |
} |
| 444 |
|
| 445 |
axis_scale: ColorScale; |
| 446 |
axis_line_scale: |
| 447 |
| d3.ScaleLinear<number, number> |
| 448 |
| d3.ScaleTime<number, number> |
| 449 |
| d3.ScaleBand<string>; |
| 450 |
ordinal: boolean; |
| 451 |
bar_height: number; |
| 452 |
side: string; |
| 453 |
x_offset: number; |
| 454 |
y_offset: number; |
| 455 |
num_ticks: number; |
| 456 |
colors: number[]; |
| 457 |
} |
| 458 |
|
| 459 |
export { ColorBar as ColorAxis }; |