Debian Codegraph @

bqplot/js/src/Tooltip.ts

raw
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 { DOMWidgetView } from '@jupyter-widgets/base';
17 import * as d3 from 'd3';
18 // var d3 =Object.assign({}, require("d3-selection"), require("d3-format"), require("d3-time-format"));
19 import * as utils from './utils';
20 import * as _ from 'underscore';
21
22 export class Tooltip extends DOMWidgetView {
23 initialize() {
24 this.d3el = d3.select(this.el);
25 super.initialize.apply(this, arguments);
26 }
27
28 render() {
29 this.parent = this.options.parent;
30 this.update_formats();
31 this.create_listeners();
32 this.create_table();
33 }
34
35 create_listeners() {
36 this.listenTo(this.parent, 'update_tooltip', this.update_tooltip);
37 this.model.on_some_change(
38 ['fields', 'show_labels', 'labels'],
39 this.create_table,
40 this
41 );
42 this.listenTo(this.model, 'change:formats', this.update_formats);
43 }
44
45 update_formats() {
46 const fields = this.model.get('fields');
47 const formats = this.model.get('formats');
48 this.tooltip_formats = fields.map((field, index) => {
49 const fmt = formats[index];
50 if (fmt === undefined || fmt === '') {
51 return function (d) {
52 return d;
53 };
54 } else {
55 if (utils.is_valid_time_format(fmt)) {
56 return d3.utcFormat(fmt);
57 } else {
58 return d3.format(fmt);
59 }
60 }
61 });
62 }
63
64 update_tooltip(data) {
65 //data is a dictionary passed by the parent along with the update_
66 //tooltip event. Responsibility of the mark to pass the data
67 const that = this;
68 this.d3el
69 .select('table')
70 .selectAll('tr')
71 .select('.datavalue')
72 .text((datum: any, index) => that.tooltip_formats[index](data[datum]));
73 }
74
75 create_table() {
76 const fields = this.model.get('fields');
77 const labels = _.clone(this.model.get('labels'));
78 for (let ind = labels.length; ind < fields.length; ind++) {
79 labels[ind] = fields[ind] + ':';
80 }
81
82 this.d3el.select('table').remove();
83 const tooltip_table = this.d3el
84 .append('table')
85 .selectAll('tr')
86 .data(fields);
87
88 tooltip_table.exit().remove();
89 const table_rows = tooltip_table
90 .enter()
91 .append('tr')
92 .attr('class', 'datarow');
93 if (this.model.get('show_labels')) {
94 table_rows
95 .append('td')
96 .text((datum, index) => labels[index])
97 .attr('class', 'tooltiptext datafield');
98 }
99 table_rows.append('td').attr('class', 'tooltiptext datavalue');
100 this.update_formats();
101 }
102
103 d3el: d3.Selection<HTMLElement, any, any, any>;
104 parent: DOMWidgetView;
105 tooltip_formats: ((x: any) => string)[];
106 }