| 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 _ from 'underscore'; |
| 17 |
import * as d3 from 'd3'; |
| 18 |
import * as d3Shape from 'd3-shape'; |
| 19 |
// var d3 =Object.assign({}, require("d3-array"), require("d3-selection"), require("d3-shape"), require("d3-transition")); |
| 20 |
import { Mark } from './Mark'; |
| 21 |
import { LinesModel } from './LinesModel'; |
| 22 |
import { BaseSelector } from './Selector'; |
| 23 |
import { SelectorModel } from './SelectorModel'; |
| 24 |
import * as markers from './Markers'; |
| 25 |
|
| 26 |
const bqSymbol = markers.symbol; |
| 27 |
|
| 28 |
export class Lines extends Mark { |
| 29 |
async render() { |
| 30 |
const baseRenderPromise = super.render(); |
| 31 |
this.dot = bqSymbol().size(this.model.get('marker_size')); |
| 32 |
if (this.model.get('marker')) { |
| 33 |
this.dot.type(this.model.get('marker')); |
| 34 |
} |
| 35 |
|
| 36 |
// TODO: create_listeners is put inside the promise success handler |
| 37 |
// because some of the functions depend on child scales being |
| 38 |
// created. Make sure none of the event handler functions make that |
| 39 |
// assumption. |
| 40 |
this.displayed.then(() => { |
| 41 |
this.parent.tooltip_div.node().appendChild(this.tooltip_div.node()); |
| 42 |
this.create_tooltip(); |
| 43 |
}); |
| 44 |
|
| 45 |
this.display_el_classes = ['line', 'legendtext', 'dot']; |
| 46 |
|
| 47 |
await baseRenderPromise; |
| 48 |
|
| 49 |
this.event_listeners = {}; |
| 50 |
this.process_interactions(); |
| 51 |
this.create_listeners(); |
| 52 |
this.compute_view_padding(); |
| 53 |
this.draw(false); |
| 54 |
} |
| 55 |
|
| 56 |
set_ranges(): void { |
| 57 |
const xScale = this.scales.x; |
| 58 |
if (xScale) { |
| 59 |
xScale.set_range(this.parent.padded_range('x', xScale.model)); |
| 60 |
} |
| 61 |
const yScale = this.scales.y; |
| 62 |
if (yScale) { |
| 63 |
yScale.set_range(this.parent.padded_range('y', yScale.model)); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
set_positional_scales(): void { |
| 68 |
const xScale = this.scales.x, |
| 69 |
yScale = this.scales.y; |
| 70 |
this.listenTo(xScale, 'domain_changed', function () { |
| 71 |
if (!this.model.dirty) { |
| 72 |
this.update_line_xy(); |
| 73 |
} |
| 74 |
}); |
| 75 |
this.listenTo(yScale, 'domain_changed', function () { |
| 76 |
if (!this.model.dirty) { |
| 77 |
this.update_line_xy(); |
| 78 |
} |
| 79 |
}); |
| 80 |
} |
| 81 |
|
| 82 |
initialize_additional_scales(): void { |
| 83 |
const colorScale = this.scales.color; |
| 84 |
if (colorScale) { |
| 85 |
this.listenTo(colorScale, 'domain_changed', function () { |
| 86 |
this.update_style(); |
| 87 |
}); |
| 88 |
colorScale.on('color_scale_range_changed', this.update_style, this); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
create_listeners(): void { |
| 93 |
super.create_listeners(); |
| 94 |
this.d3el |
| 95 |
.on( |
| 96 |
'mouseover', |
| 97 |
_.bind(() => { |
| 98 |
this.event_dispatcher('mouse_over'); |
| 99 |
}, this) |
| 100 |
) |
| 101 |
.on( |
| 102 |
'mousemove', |
| 103 |
_.bind(() => { |
| 104 |
this.event_dispatcher('mouse_move'); |
| 105 |
}, this) |
| 106 |
) |
| 107 |
.on( |
| 108 |
'mouseout', |
| 109 |
_.bind(() => { |
| 110 |
this.event_dispatcher('mouse_out'); |
| 111 |
}, this) |
| 112 |
); |
| 113 |
|
| 114 |
this.listenTo(this.model, 'change:tooltip', this.create_tooltip); |
| 115 |
|
| 116 |
// FIXME: multiple calls to update_path_style. Use on_some_change. |
| 117 |
this.listenTo(this.model, 'change:interpolation', this.update_path_style); |
| 118 |
this.listenTo(this.model, 'change:close_path', this.update_path_style); |
| 119 |
|
| 120 |
// FIXME: multiple calls to update_style. Use on_some_change. |
| 121 |
this.listenTo(this.model, 'change:colors', this.update_style); |
| 122 |
this.listenTo(this.model, 'change:opacities', this.update_style); |
| 123 |
this.listenTo(this.model, 'change:fill_opacities', this.update_style); |
| 124 |
this.listenTo(this.model, 'change:fill_colors', this.update_style); |
| 125 |
|
| 126 |
this.listenTo(this.model, 'change:fill', this.update_fill); |
| 127 |
|
| 128 |
this.listenTo(this.model, 'data_updated', function () { |
| 129 |
const animate = true; |
| 130 |
this.draw(animate); |
| 131 |
}); |
| 132 |
this.listenTo(this.model, 'labels_updated', this.update_labels); |
| 133 |
this.listenTo(this.model, 'change:stroke_width', this.update_stroke_width); |
| 134 |
this.listenTo( |
| 135 |
this.model, |
| 136 |
'change:labels_visibility', |
| 137 |
this.update_legend_labels |
| 138 |
); |
| 139 |
this.listenTo( |
| 140 |
this.model, |
| 141 |
'change:curves_subset', |
| 142 |
this.update_curves_subset |
| 143 |
); |
| 144 |
this.listenTo(this.model, 'change:line_style', this.update_line_style); |
| 145 |
this.listenTo(this.model, 'change:interactions', this.process_interactions); |
| 146 |
this.listenTo(this.parent, 'bg_clicked', function () { |
| 147 |
this.event_dispatcher('parent_clicked'); |
| 148 |
}); |
| 149 |
|
| 150 |
this.listenTo(this.model, 'change:marker', this.update_marker); |
| 151 |
this.listenTo(this.model, 'change:marker_size', this.update_marker_size); |
| 152 |
} |
| 153 |
|
| 154 |
update_legend_labels(): void { |
| 155 |
if (this.model.get('labels_visibility') === 'none') { |
| 156 |
this.d3el.selectAll('.legend').attr('display', 'none'); |
| 157 |
this.d3el.selectAll('.curve_label').attr('display', 'none'); |
| 158 |
} else if (this.model.get('labels_visibility') === 'label') { |
| 159 |
this.d3el.selectAll('.legend').attr('display', 'none'); |
| 160 |
this.d3el.selectAll('.curve_label').attr('display', 'inline'); |
| 161 |
} else { |
| 162 |
this.d3el.selectAll('.legend').attr('display', 'inline'); |
| 163 |
this.d3el.selectAll('.curve_label').attr('display', 'none'); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
update_labels(): void { |
| 168 |
this.d3el |
| 169 |
.selectAll('.curve') |
| 170 |
.data(this.model.mark_data) |
| 171 |
.select('.curve_label') |
| 172 |
.text((d: any) => { |
| 173 |
return d.name; |
| 174 |
}); |
| 175 |
} |
| 176 |
|
| 177 |
get_line_style(): string { |
| 178 |
switch (this.model.get('line_style')) { |
| 179 |
case 'solid': |
| 180 |
return 'none'; |
| 181 |
case 'dashed': |
| 182 |
return '10,10'; |
| 183 |
case 'dotted': |
| 184 |
return '2,10'; |
| 185 |
case 'dash_dotted': |
| 186 |
return '10,5,2,5'; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
// Updating the style of the curve, stroke, colors, dashed etc... |
| 191 |
// Could be fused in a single function for increased readability |
| 192 |
// and to avoid code repetition |
| 193 |
update_line_style(): void { |
| 194 |
this.d3el |
| 195 |
.selectAll('.curve') |
| 196 |
.select('.line') |
| 197 |
.style('stroke-dasharray', _.bind(this.get_line_style, this)); |
| 198 |
if (this.legendEl) { |
| 199 |
this.legendEl |
| 200 |
.select('path') |
| 201 |
.style('stroke-dasharray', _.bind(this.get_line_style, this)); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
update_stroke_width(model, strokeWidth): void { |
| 206 |
this.compute_view_padding(); |
| 207 |
this.d3el |
| 208 |
.selectAll('.curve') |
| 209 |
.select('.line') |
| 210 |
.style('stroke-width', strokeWidth); |
| 211 |
if (this.legendEl) { |
| 212 |
this.legendEl.select('path').style('stroke-width', strokeWidth); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
update_style(): void { |
| 217 |
const fill = this.model.get('fill'), |
| 218 |
fillColor = this.model.get('fill_colors'), |
| 219 |
fillOpacities = this.model.get('fill_opacities'); |
| 220 |
// update curve colors |
| 221 |
const curves = this.d3el.selectAll('.curve'); |
| 222 |
curves |
| 223 |
.select('.line') |
| 224 |
.style('opacity', this.get_mark_opacity.bind(this)) |
| 225 |
.style('stroke', (d, i) => { |
| 226 |
return this.get_mark_color(d, i) || fillColor[i]; |
| 227 |
}) |
| 228 |
.style('fill', (d, i) => { |
| 229 |
return fill === 'inside' ? this.get_fill_color(d, i) : ''; |
| 230 |
}) |
| 231 |
.style('fill-opacity', (d, i) => { |
| 232 |
return fill === 'inside' ? fillOpacities[i] : ''; |
| 233 |
}); |
| 234 |
curves |
| 235 |
.select('.area') |
| 236 |
.style('fill', (d, i) => { |
| 237 |
return this.get_fill_color(d, i); |
| 238 |
}) |
| 239 |
.style('opacity', (d, i) => { |
| 240 |
return fillOpacities[i]; |
| 241 |
}); |
| 242 |
this.update_marker_style(); |
| 243 |
// update legend style |
| 244 |
if (this.legendEl) { |
| 245 |
this.legendEl |
| 246 |
.select('.line') |
| 247 |
.style('stroke', (d, i) => { |
| 248 |
return this.get_mark_color(d, i) || fillColor[i]; |
| 249 |
}) |
| 250 |
.style('opacity', this.get_mark_opacity.bind(this)) |
| 251 |
.style('fill', (d, i) => { |
| 252 |
return this.model.get('fill') === 'none' |
| 253 |
? '' |
| 254 |
: this.get_fill_color(d, i); |
| 255 |
}); |
| 256 |
this.legendEl |
| 257 |
.select('.dot') |
| 258 |
.style('stroke', (d, i) => { |
| 259 |
return this.get_mark_color(d, i) || fillColor[i]; |
| 260 |
}) |
| 261 |
.style('opacity', this.get_mark_opacity.bind(this)) |
| 262 |
.style('fill', (d, i) => { |
| 263 |
return this.get_mark_color(d, i) || fillColor[i]; |
| 264 |
}); |
| 265 |
this.legendEl |
| 266 |
.select('text') |
| 267 |
.style('fill', (d, i) => { |
| 268 |
return this.get_mark_color(d, i) || fillColor[i]; |
| 269 |
}) |
| 270 |
.style('opacity', this.get_mark_opacity.bind(this)); |
| 271 |
} |
| 272 |
this.update_stroke_width(this.model, this.model.get('stroke_width')); |
| 273 |
this.update_line_style(); |
| 274 |
} |
| 275 |
|
| 276 |
path_closure(): string { |
| 277 |
return this.model.get('close_path') ? 'Z' : ''; |
| 278 |
} |
| 279 |
|
| 280 |
update_path_style(): void { |
| 281 |
const interpolation = this.get_interpolation(); |
| 282 |
this.line.curve(interpolation); |
| 283 |
this.area.curve(interpolation); |
| 284 |
this.d3el |
| 285 |
.selectAll('.curve') |
| 286 |
.select('.line') |
| 287 |
.attr('d', (d: any) => { |
| 288 |
return this.line(d.values) + this.path_closure(); |
| 289 |
}); |
| 290 |
this.d3el |
| 291 |
.selectAll('.curve') |
| 292 |
.select('.area') |
| 293 |
.transition('update_path_style') |
| 294 |
.duration(0) //FIXME |
| 295 |
.attr('d', (d: any) => { |
| 296 |
return this.area(d.values); |
| 297 |
}); |
| 298 |
if (this.legendEl) { |
| 299 |
this.legendLine.curve(interpolation); |
| 300 |
this.legendEl |
| 301 |
.selectAll('path') |
| 302 |
.attr('d', this.legendLine(this.legendPathData) + this.path_closure()); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
relayout(): void { |
| 307 |
this.set_ranges(); |
| 308 |
this.update_line_xy(false); |
| 309 |
} |
| 310 |
|
| 311 |
selector_changed(pointSelector, rectSelector): [] { |
| 312 |
if (pointSelector === undefined) { |
| 313 |
this.model.set('selected', null); |
| 314 |
this.touch(); |
| 315 |
return []; |
| 316 |
} |
| 317 |
const pixels = this.pixelCoords; |
| 318 |
const indices = new Uint32Array(_.range(pixels.length)); |
| 319 |
const selected = indices.filter((index) => { |
| 320 |
return pointSelector(pixels[index]); |
| 321 |
}); |
| 322 |
this.model.set('selected', selected); |
| 323 |
this.touch(); |
| 324 |
} |
| 325 |
|
| 326 |
invert_point(pixel): void { |
| 327 |
if (pixel === undefined) { |
| 328 |
this.model.set('selected', null); |
| 329 |
this.touch(); |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
const index = Math.min( |
| 334 |
this.bisect(this.xPixels, pixel), |
| 335 |
Math.max(this.xPixels.length - 1, 0) |
| 336 |
); |
| 337 |
this.model.set('selected', new Uint32Array([index])); |
| 338 |
this.touch(); |
| 339 |
} |
| 340 |
|
| 341 |
update_multi_range(brushExtent): void { |
| 342 |
const xStart = brushExtent[0]; |
| 343 |
const xEnd = brushExtent[1]; |
| 344 |
const data = |
| 345 |
this.model.x_data[0] instanceof Array |
| 346 |
? this.model.x_data[0] |
| 347 |
: this.model.x_data; |
| 348 |
const idXStart = this.bisect(data, xStart); |
| 349 |
const idXEnd = Math.min( |
| 350 |
this.bisect(data, xEnd), |
| 351 |
Math.max(data.length - 1, 0) |
| 352 |
); |
| 353 |
|
| 354 |
this.selectorModel.set('selected', [idXStart, idXEnd]); |
| 355 |
this.selector.touch(); |
| 356 |
} |
| 357 |
|
| 358 |
draw_legend(elem, xDisp, yDisp, interXDisp, interYDisp: number): number[] { |
| 359 |
const curveLabels = this.model.get_labels(); |
| 360 |
const legendData = this.model.mark_data.map((d) => { |
| 361 |
return { index: d.index, name: d.name, color: d.color }; |
| 362 |
}); |
| 363 |
this.legendEl = elem.selectAll('.legend' + this.uuid).data(legendData); |
| 364 |
|
| 365 |
const rectDim = interYDisp * 0.8, |
| 366 |
fillColors = this.model.get('fill_colors'); |
| 367 |
|
| 368 |
this.legendLine = d3 |
| 369 |
.line() |
| 370 |
.curve(this.get_interpolation()) |
| 371 |
.x((d) => { |
| 372 |
return d[0]; |
| 373 |
}) |
| 374 |
.y((d) => { |
| 375 |
return d[1]; |
| 376 |
}); |
| 377 |
|
| 378 |
this.legendPathData = [ |
| 379 |
[0, rectDim], |
| 380 |
[rectDim / 2, 0], |
| 381 |
[rectDim, rectDim / 2], |
| 382 |
]; |
| 383 |
|
| 384 |
const legend = this.legendEl |
| 385 |
.enter() |
| 386 |
.append('g') |
| 387 |
.attr('class', 'legend' + this.uuid) |
| 388 |
.attr('transform', (d, i) => { |
| 389 |
return 'translate(0, ' + (i * interYDisp + yDisp) + ')'; |
| 390 |
}) |
| 391 |
.on( |
| 392 |
'mouseover', |
| 393 |
_.bind(() => { |
| 394 |
this.event_dispatcher('legend_mouse_over'); |
| 395 |
}, this) |
| 396 |
) |
| 397 |
.on( |
| 398 |
'mouseout', |
| 399 |
_.bind(() => { |
| 400 |
this.event_dispatcher('legend_mouse_out'); |
| 401 |
}, this) |
| 402 |
) |
| 403 |
.on( |
| 404 |
'click', |
| 405 |
_.bind(() => { |
| 406 |
this.event_dispatcher('legend_clicked'); |
| 407 |
}, this) |
| 408 |
); |
| 409 |
|
| 410 |
legend |
| 411 |
.append('path') |
| 412 |
.attr('class', 'line') |
| 413 |
.attr('fill', 'none') |
| 414 |
.attr('d', this.legendLine(this.legendPathData) + this.path_closure()) |
| 415 |
.style('stroke', (d, i) => { |
| 416 |
return this.get_mark_color(d, i) || fillColors[i]; |
| 417 |
}) |
| 418 |
.style('fill', (d, i) => { |
| 419 |
return this.model.get('fill') === 'none' |
| 420 |
? '' |
| 421 |
: this.get_fill_color(d, i); |
| 422 |
}) |
| 423 |
.style('opacity', this.get_mark_opacity.bind(this)) |
| 424 |
.style('stroke-width', this.model.get('stroke_width')) |
| 425 |
.style('stroke-dasharray', _.bind(this.get_line_style, this)); |
| 426 |
|
| 427 |
if (this.model.get('marker')) { |
| 428 |
legend |
| 429 |
.append('path') |
| 430 |
.attr('class', 'dot') |
| 431 |
.attr('transform', 'translate(' + rectDim / 2 + ',0)') |
| 432 |
.attr('d', this.dot.size(25)) |
| 433 |
.style('fill', (d, i) => { |
| 434 |
return this.get_mark_color(d, i); |
| 435 |
}); |
| 436 |
} |
| 437 |
|
| 438 |
legend |
| 439 |
.append('text') |
| 440 |
.attr('class', 'legendtext') |
| 441 |
.attr('x', rectDim * 1.2) |
| 442 |
.attr('y', rectDim / 2) |
| 443 |
.attr('dy', '0.35em') |
| 444 |
.text((d, i) => { |
| 445 |
return curveLabels[i]; |
| 446 |
}) |
| 447 |
.style('fill', (d, i) => { |
| 448 |
return this.get_mark_color(d, i) || fillColors[i]; |
| 449 |
}) |
| 450 |
.style('opacity', this.get_mark_opacity.bind(this)); |
| 451 |
|
| 452 |
this.legendEl = legend.merge(this.legendEl); |
| 453 |
|
| 454 |
const maxLength = d3.max(curveLabels, (d: any) => { |
| 455 |
return d.length; |
| 456 |
}); |
| 457 |
this.legendEl.exit().remove(); |
| 458 |
return [this.model.mark_data.length, maxLength]; |
| 459 |
} |
| 460 |
|
| 461 |
update_curves_subset(): void { |
| 462 |
const displayLabels = this.model.get('labels_visibility') === 'label'; |
| 463 |
// Show a subset of the curves |
| 464 |
const curvesSubset = this.model.get('curves_subset'); |
| 465 |
if (curvesSubset.length > 0) { |
| 466 |
this.d3el |
| 467 |
.selectAll('.curve') |
| 468 |
.attr('display', (d, i) => { |
| 469 |
return curvesSubset.indexOf(i) !== -1 ? 'inline' : 'none'; |
| 470 |
}) |
| 471 |
.select('.curve_label') |
| 472 |
.attr('display', (d, i) => { |
| 473 |
return curvesSubset.indexOf(i) !== -1 && displayLabels |
| 474 |
? 'inline' |
| 475 |
: 'none'; |
| 476 |
}); |
| 477 |
if (this.legendEl) { |
| 478 |
this.legendEl.attr('display', (d, i) => { |
| 479 |
return curvesSubset.indexOf(i) !== -1 ? 'inline' : 'none'; |
| 480 |
}); |
| 481 |
} |
| 482 |
this.d3el.selectAll('.curve'); |
| 483 |
} else { |
| 484 |
//make all curves visible |
| 485 |
this.d3el |
| 486 |
.selectAll('.curve') |
| 487 |
.attr('display', 'inline') |
| 488 |
.select('.curve_label') |
| 489 |
.attr('display', (d) => { |
| 490 |
return displayLabels ? 'inline' : 'none'; |
| 491 |
}); |
| 492 |
if (this.legendEl) { |
| 493 |
this.legendEl.attr('display', 'inline'); |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
update_fill(): void { |
| 499 |
const fill = this.model.get('fill'), |
| 500 |
area = fill === 'top' || fill === 'bottom' || fill === 'between'; |
| 501 |
|
| 502 |
const yScale = this.scales.y; |
| 503 |
|
| 504 |
this.area.defined((d: any) => { |
| 505 |
return area && d.y !== null && isFinite(yScale.scale(d.y)); |
| 506 |
}); |
| 507 |
|
| 508 |
if (fill == 'bottom') { |
| 509 |
this.area.y0(this.parent.plotarea_height); |
| 510 |
} else if (fill == 'top') { |
| 511 |
this.area.y0(0); |
| 512 |
} else if (fill == 'between') { |
| 513 |
this.area.y0((d: any) => { |
| 514 |
return yScale.scale(d.y0) + yScale.offset; |
| 515 |
}); |
| 516 |
} |
| 517 |
this.d3el |
| 518 |
.selectAll('.curve') |
| 519 |
.select('.area') |
| 520 |
.attr('d', (d: any) => { |
| 521 |
return this.area(d.values); |
| 522 |
}); |
| 523 |
this.d3el |
| 524 |
.selectAll('.curve') |
| 525 |
.select('.line') |
| 526 |
.style('fill', (d, i) => { |
| 527 |
return fill === 'inside' ? this.get_fill_color(d, i) : ''; |
| 528 |
}); |
| 529 |
// update legend fill |
| 530 |
if (this.legendEl) { |
| 531 |
this.legendEl.select('path').style('fill', (d, i) => { |
| 532 |
return fill === 'none' ? '' : this.get_fill_color(d, i); |
| 533 |
}); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
get_fill_color(data, index): string { |
| 538 |
const fillColors: string = this.model.get('fill_colors'); |
| 539 |
return fillColors.length === 0 |
| 540 |
? this.get_mark_color(data, index) |
| 541 |
: fillColors[index]; |
| 542 |
} |
| 543 |
|
| 544 |
update_line_xy(animate): void { |
| 545 |
const xScale = this.scales.x, |
| 546 |
yScale = this.scales.y; |
| 547 |
const animationDuration = |
| 548 |
animate === true ? this.parent.model.get('animation_duration') : 0; |
| 549 |
|
| 550 |
this.line |
| 551 |
.x((d: any) => { |
| 552 |
return xScale.scale(d.x) + xScale.offset; |
| 553 |
}) |
| 554 |
.y((d: any) => { |
| 555 |
return yScale.scale(d.y) + yScale.offset; |
| 556 |
}); |
| 557 |
|
| 558 |
const fill = this.model.get('fill'); |
| 559 |
this.area |
| 560 |
.x((d: any) => { |
| 561 |
return xScale.scale(d.x) + xScale.offset; |
| 562 |
}) |
| 563 |
.y1((d: any) => { |
| 564 |
return yScale.scale(d.y) + yScale.offset; |
| 565 |
}); |
| 566 |
|
| 567 |
if (fill == 'bottom') { |
| 568 |
this.area.y0(this.parent.plotarea_height); |
| 569 |
} else if (fill == 'top') { |
| 570 |
this.area.y0(0); |
| 571 |
} else if (fill == 'between') { |
| 572 |
this.area.y0((d: any) => { |
| 573 |
return yScale.scale(d.y0) + yScale.offset; |
| 574 |
}); |
| 575 |
} |
| 576 |
|
| 577 |
const curvesSel = this.d3el.selectAll('.curve'); |
| 578 |
|
| 579 |
curvesSel |
| 580 |
.select('.line') |
| 581 |
.transition('update_line_xy') |
| 582 |
.attr('d', (d: any) => { |
| 583 |
return this.line(d.values) + this.path_closure(); |
| 584 |
}) |
| 585 |
.duration(animationDuration); |
| 586 |
|
| 587 |
curvesSel |
| 588 |
.select('.area') |
| 589 |
.transition('update_line_xy') |
| 590 |
.attr('d', (d: any, i) => { |
| 591 |
return this.area(d.values); |
| 592 |
}) |
| 593 |
.duration(animationDuration); |
| 594 |
|
| 595 |
curvesSel |
| 596 |
.select('.curve_label') |
| 597 |
.transition('update_line_xy') |
| 598 |
.attr('transform', (d: any) => { |
| 599 |
const lastXy = d.values[d.values.length - 1]; |
| 600 |
return ( |
| 601 |
'translate(' + |
| 602 |
xScale.scale(lastXy.x) + |
| 603 |
',' + |
| 604 |
yScale.scale(lastXy.y) + |
| 605 |
')' |
| 606 |
); |
| 607 |
}) |
| 608 |
.duration(animationDuration); |
| 609 |
|
| 610 |
this.update_dots_xy(animate); |
| 611 |
this.xPixels = |
| 612 |
this.model.mark_data.length > 0 |
| 613 |
? this.model.mark_data[0].values.map((el) => { |
| 614 |
return xScale.scale(el.x) + xScale.offset; |
| 615 |
}) |
| 616 |
: []; |
| 617 |
this.yPixels = |
| 618 |
this.model.mark_data.length > 0 |
| 619 |
? this.model.mark_data[0].values.map((el) => { |
| 620 |
return yScale.scale(el.y) + yScale.offset; |
| 621 |
}) |
| 622 |
: []; |
| 623 |
this.pixelCoords = |
| 624 |
this.model.mark_data.length > 0 |
| 625 |
? this.model.mark_data[0].values.map((el) => { |
| 626 |
return [ |
| 627 |
xScale.scale(el.x) + xScale.offset, |
| 628 |
yScale.scale(el.y) + yScale.offset, |
| 629 |
]; |
| 630 |
}) |
| 631 |
: []; |
| 632 |
} |
| 633 |
|
| 634 |
get_interpolation(): d3Shape.CurveFactory { |
| 635 |
const curveTypes = { |
| 636 |
linear: d3.curveLinear, |
| 637 |
basis: d3.curveBasis, |
| 638 |
'basis-open': d3.curveBasisOpen, |
| 639 |
'basis-closed': d3.curveBasisClosed, |
| 640 |
bundle: d3.curveBundle, |
| 641 |
cardinal: d3.curveCardinal, |
| 642 |
'cardinal-open': d3.curveCardinalOpen, |
| 643 |
'cardinal-closed': d3.curveCardinalClosed, |
| 644 |
monotone: d3.curveMonotoneY, |
| 645 |
'step-before': d3.curveStepBefore, |
| 646 |
'step-after': d3.curveStepAfter, |
| 647 |
}; |
| 648 |
|
| 649 |
return curveTypes[this.model.get('interpolation')]; |
| 650 |
} |
| 651 |
|
| 652 |
draw(animate): void { |
| 653 |
this.set_ranges(); |
| 654 |
const curvesSel = this.d3el.selectAll('.curve').data(this.model.mark_data); |
| 655 |
|
| 656 |
const yScale = this.scales.y; |
| 657 |
|
| 658 |
const newCurves = curvesSel.enter().append('g').attr('class', 'curve'); |
| 659 |
newCurves |
| 660 |
.append('path') |
| 661 |
.attr('class', 'line') |
| 662 |
.attr('fill', 'none') |
| 663 |
.attr('id', (d, i) => 'curve' + (i + 1)) |
| 664 |
.on('click', () => { |
| 665 |
this.event_dispatcher('element_clicked'); |
| 666 |
}); |
| 667 |
newCurves.append('path').attr('class', 'area'); |
| 668 |
newCurves |
| 669 |
.append('text') |
| 670 |
.attr('class', 'curve_label') |
| 671 |
.attr('x', 3) |
| 672 |
.attr('dy', '.35em') |
| 673 |
.attr( |
| 674 |
'display', |
| 675 |
this.model.get('labels_visibility') !== 'label' ? 'none' : 'inline' |
| 676 |
) |
| 677 |
.text((d: any) => d.name); |
| 678 |
|
| 679 |
const fill = this.model.get('fill'), |
| 680 |
area = fill === 'top' || fill === 'bottom' || fill === 'between'; |
| 681 |
|
| 682 |
this.draw_dots(); |
| 683 |
|
| 684 |
this.line = d3 |
| 685 |
.line() |
| 686 |
.curve(this.get_interpolation()) |
| 687 |
.defined((d: any) => { |
| 688 |
return d.y !== null && isFinite(yScale.scale(d.y)); |
| 689 |
}); |
| 690 |
|
| 691 |
this.area = d3 |
| 692 |
.area() |
| 693 |
.curve(this.get_interpolation()) |
| 694 |
.defined((d: any) => { |
| 695 |
return area && d.y !== null && isFinite(yScale.scale(d.y)); |
| 696 |
}); |
| 697 |
|
| 698 |
// Having a transition on exit is complicated. Please refer to |
| 699 |
// Scatter.js for detailed explanation. |
| 700 |
curvesSel.exit().remove(); |
| 701 |
this.update_line_xy(animate); |
| 702 |
this.update_style(); |
| 703 |
|
| 704 |
// alter the display only if a few of the curves are visible |
| 705 |
this.update_curves_subset(); |
| 706 |
} |
| 707 |
|
| 708 |
draw_dots(): void { |
| 709 |
if (this.model.get('marker')) { |
| 710 |
const dots = this.d3el |
| 711 |
.selectAll('.curve') |
| 712 |
.selectAll('.dot') |
| 713 |
.data((d: any, i) => { |
| 714 |
return d.values.map((e) => { |
| 715 |
return { x: e.x, y: e.y, sub_index: e.sub_index }; |
| 716 |
}); |
| 717 |
}); |
| 718 |
dots.enter().append('path').attr('class', 'dot'); |
| 719 |
dots.exit().remove(); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
update_dots_xy(animate): void { |
| 724 |
if (this.model.get('marker')) { |
| 725 |
const xScale = this.scales.x, |
| 726 |
yScale = this.scales.y; |
| 727 |
const animationDuration = |
| 728 |
animate === true ? this.parent.model.get('animation_duration') : 0; |
| 729 |
const dots = this.d3el.selectAll('.curve').selectAll('.dot'); |
| 730 |
|
| 731 |
dots |
| 732 |
.transition('update_dots_xy') |
| 733 |
.duration(animationDuration) |
| 734 |
.attr('transform', (d: any) => { |
| 735 |
return ( |
| 736 |
'translate(' + |
| 737 |
(xScale.scale(d.x) + xScale.offset) + |
| 738 |
',' + |
| 739 |
(yScale.scale(d.y) + yScale.offset) + |
| 740 |
')' |
| 741 |
); |
| 742 |
}) |
| 743 |
.attr( |
| 744 |
'd', |
| 745 |
this.dot |
| 746 |
.size(this.model.get('marker_size')) |
| 747 |
.type(this.model.get('marker')) |
| 748 |
); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
compute_view_padding(): void { |
| 753 |
//This function sets the padding for the view through the variables |
| 754 |
//xPadding and yPadding which are view specific paddings in pixel |
| 755 |
let xPadding; |
| 756 |
if (this.model.get('marker')) { |
| 757 |
const markerPadding = Math.sqrt(this.model.get('marker_size')) / 2 + 1.0; |
| 758 |
const linePadding = this.model.get('stroke_width') / 2.0; |
| 759 |
xPadding = Math.max(markerPadding, linePadding); |
| 760 |
} else { |
| 761 |
xPadding = this.model.get('stroke_width') / 2.0; |
| 762 |
} |
| 763 |
|
| 764 |
const yPadding = xPadding; |
| 765 |
if (xPadding !== this.xPadding || yPadding !== this.yPadding) { |
| 766 |
this.xPadding = xPadding; |
| 767 |
this.yPadding = yPadding; |
| 768 |
this.trigger('mark_padding_updated'); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
update_marker_style() { |
| 773 |
const that = this; |
| 774 |
const fillColor = this.model.get('fill_colors'); |
| 775 |
const opacities = this.model.get('opacities'); |
| 776 |
this.d3el.selectAll('.curve').each(function (d, i) { |
| 777 |
const curve = d3.select(this); |
| 778 |
curve |
| 779 |
.selectAll('.dot') |
| 780 |
.style('opacity', opacities[i]) |
| 781 |
.style('fill', that.get_mark_color(d, i) || fillColor[i]); |
| 782 |
}); |
| 783 |
} |
| 784 |
|
| 785 |
update_marker(model, marker): void { |
| 786 |
if (marker) { |
| 787 |
this.draw_dots(); |
| 788 |
this.update_dots_xy(false); |
| 789 |
this.update_marker_style(); |
| 790 |
if (this.legendEl) { |
| 791 |
this.legendEl.select('.dot').attr('d', this.dot.type(marker).size(25)); |
| 792 |
} |
| 793 |
} else { |
| 794 |
this.d3el.selectAll('.dot').remove(); |
| 795 |
if (this.legendEl) { |
| 796 |
this.legendEl.select('.dot').attr('d', this.dot.size(0)); |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
update_marker_size(model, markerSize): void { |
| 802 |
this.compute_view_padding(); |
| 803 |
this.d3el.selectAll('.dot').attr('d', this.dot.size(markerSize)); |
| 804 |
} |
| 805 |
|
| 806 |
clear_style(style_dict, indices?) {} |
| 807 |
|
| 808 |
set_default_style(indices) {} |
| 809 |
|
| 810 |
set_style_on_elements(style, indices) {} |
| 811 |
|
| 812 |
dot: any; |
| 813 |
legendEl: d3.Selection<any, any, any, any>; |
| 814 |
legendLine: d3.Line<[number, number]>; |
| 815 |
legendPathData: [number, number][]; |
| 816 |
selector: BaseSelector; |
| 817 |
selectorModel: SelectorModel; |
| 818 |
area: d3.Area<[number, number]>; |
| 819 |
line: d3.Line<[number, number]>; |
| 820 |
xPixels: number[]; |
| 821 |
yPixels: number[]; |
| 822 |
pixelCoords: number[]; |
| 823 |
|
| 824 |
// Overriding super class |
| 825 |
model: LinesModel; |
| 826 |
} |