forked from perspective-dev/perspective
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
85 lines (71 loc) · 2.71 KB
/
template.js
File metadata and controls
85 lines (71 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
import * as d3 from "d3";
import style from "../../less/chart.less";
import perspectiveStyle from "../../less/perspective-view.less";
import template from "../../html/d3fc-chart.html";
import {areArraysEqualSimple} from "../utils/utils";
import {bindTemplate} from "@jpmorganchase/perspective-viewer/cjs/js/utils";
const styleWithD3FC = `${style}${getD3FCStyles()}`;
@bindTemplate(template, styleWithD3FC) // eslint-disable-next-line no-unused-vars
class D3FCChartElement extends HTMLElement {
connectedCallback() {
this._container = this.shadowRoot.querySelector(".chart");
this._chart = null;
this._settings = null;
// Add the additional styles needed for the perspective-viewer host
var style = document.createElement("style");
style.setAttribute("scope", "perspective-viewer");
style.textContent = perspectiveStyle;
this.shadowRoot.host.getRootNode().appendChild(style);
}
render(chart, settings) {
this.remove();
this._chart = chart;
this._settings = this._configureSettings(this._settings, settings);
this.draw();
}
draw() {
if (this._settings.data) {
this._settings.size = this._container.getBoundingClientRect();
this._chart(d3.select(this._container).attr("class", `chart ${this._chart.plugin.type}`), this._settings);
}
}
resize() {
this.draw();
}
remove() {
this._container.innerHTML = "";
}
delete() {
this.remove();
}
getContainer() {
return this._container;
}
_configureSettings(oldSettings, newSettings) {
if (oldSettings) {
const oldValues = [oldSettings.crossValues, oldSettings.mainValues, oldSettings.splitValues];
const newValues = [newSettings.crossValues, newSettings.mainValues, newSettings.splitValues];
if (areArraysEqualSimple(oldValues, newValues)) return {...oldSettings, data: newSettings.data};
}
this.remove();
return newSettings;
}
}
function getD3FCStyles() {
const headerStyles = document.querySelector("head").querySelectorAll("style");
const d3fcStyles = [];
headerStyles.forEach(s => {
if (s.innerText.indexOf("d3fc-") !== -1) {
d3fcStyles.push(s.innerText);
}
});
return d3fcStyles.join("");
}