| 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 |
import { MarkModel } from './MarkModel'; |
| 18 |
import * as serialize from './serialize'; |
| 19 |
|
| 20 |
export interface NodeShapeAttrs { |
| 21 |
r?: number; |
| 22 |
width?: number; |
| 23 |
height?: number; |
| 24 |
rx?: number; |
| 25 |
ry?: number; |
| 26 |
} |
| 27 |
|
| 28 |
export interface NodeData extends d3.SimulationNodeDatum { |
| 29 |
label: string; |
| 30 |
label_display: 'center' | 'outside' | 'none'; |
| 31 |
shape: 'circle' | 'rect' | 'ellipse'; |
| 32 |
shape_attrs: NodeShapeAttrs; |
| 33 |
value?: number; |
| 34 |
xval: number; |
| 35 |
yval: number; |
| 36 |
color: number; |
| 37 |
} |
| 38 |
|
| 39 |
export interface LinkData { |
| 40 |
source: NodeData; |
| 41 |
target: NodeData; |
| 42 |
value: number; |
| 43 |
} |
| 44 |
|
| 45 |
export class GraphModel extends MarkModel { |
| 46 |
defaults() { |
| 47 |
return { |
| 48 |
...MarkModel.prototype.defaults(), |
| 49 |
_model_name: 'GraphModel', |
| 50 |
_view_name: 'Graph', |
| 51 |
node_data: [], |
| 52 |
link_matrix: [], |
| 53 |
link_data: [], |
| 54 |
charge: -600, |
| 55 |
static: false, |
| 56 |
link_distance: 100, |
| 57 |
link_type: 'arc', |
| 58 |
directed: true, |
| 59 |
highlight_links: true, |
| 60 |
colors: d3.scaleOrdinal(d3.schemeCategory10).range(), |
| 61 |
x: [], |
| 62 |
y: [], |
| 63 |
color: null, |
| 64 |
link_color: null, |
| 65 |
hovered_point: null, |
| 66 |
scales_metadata: { |
| 67 |
x: { orientation: 'horizontal', dimension: 'x' }, |
| 68 |
y: { orientation: 'vertical', dimension: 'y' }, |
| 69 |
color: { dimension: 'color' }, |
| 70 |
}, |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
initialize(attributes, options) { |
| 75 |
super.initialize(attributes, options); |
| 76 |
this.on_some_change( |
| 77 |
['x', 'y', 'color', 'link_color', 'node_data', 'link_data', 'link_color'], |
| 78 |
this.update_data, |
| 79 |
this |
| 80 |
); |
| 81 |
this.on_some_change(['preserve_domain'], this.update_domains, this); |
| 82 |
this.update_data(); |
| 83 |
} |
| 84 |
|
| 85 |
get static(): boolean { |
| 86 |
return this.get('static'); |
| 87 |
} |
| 88 |
|
| 89 |
get charge(): number { |
| 90 |
return this.get('static'); |
| 91 |
} |
| 92 |
|
| 93 |
get directed(): boolean { |
| 94 |
return this.get('directed'); |
| 95 |
} |
| 96 |
|
| 97 |
private get nodeData(): [NodeData | string] { |
| 98 |
return this.get('node_data'); |
| 99 |
} |
| 100 |
|
| 101 |
private getShapeAttrs( |
| 102 |
shape: 'circle' | 'rect' | 'ellipse', |
| 103 |
attrs: NodeShapeAttrs |
| 104 |
) { |
| 105 |
const newAttrs: NodeShapeAttrs = {}; |
| 106 |
switch (shape) { |
| 107 |
case 'circle': |
| 108 |
newAttrs.r = attrs.r || 15; |
| 109 |
break; |
| 110 |
case 'rect': |
| 111 |
newAttrs.width = attrs.width || 25; |
| 112 |
newAttrs.height = attrs.height || newAttrs.width * 0.8; |
| 113 |
newAttrs.rx = attrs.rx || 0; |
| 114 |
newAttrs.ry = attrs.ry || 0; |
| 115 |
break; |
| 116 |
case 'ellipse': |
| 117 |
newAttrs.rx = attrs.rx || 20; |
| 118 |
newAttrs.ry = attrs.ry || newAttrs.rx * 0.6; |
| 119 |
break; |
| 120 |
default: |
| 121 |
console.log('Invalid shape passed - ', shape); |
| 122 |
} |
| 123 |
return newAttrs; |
| 124 |
} |
| 125 |
|
| 126 |
private updateNodeData() { |
| 127 |
const x = this.get('x') || []; |
| 128 |
const y = this.get('y') || []; |
| 129 |
const color = this.get('color') || []; |
| 130 |
|
| 131 |
this.mark_data = this.nodeData.map((d, i) => { |
| 132 |
const data: Partial<NodeData> = typeof d === 'string' ? { label: d } : d; |
| 133 |
|
| 134 |
data.label = data.label || 'N' + i; |
| 135 |
data.label_display = data.label_display || 'center'; |
| 136 |
data.shape = data.shape || 'circle'; |
| 137 |
data.shape_attrs = this.getShapeAttrs(data.shape, data.shape_attrs || {}); |
| 138 |
data.value = data.value || null; |
| 139 |
|
| 140 |
if (x.length > i) { |
| 141 |
data.xval = x[i]; |
| 142 |
} |
| 143 |
if (y.length > i) { |
| 144 |
data.yval = y[i]; |
| 145 |
} |
| 146 |
if (color.length > i) { |
| 147 |
data.color = color[i]; |
| 148 |
} |
| 149 |
|
| 150 |
return data as NodeData; |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
private updateLinkData() { |
| 155 |
const link_color_scale = this.get('scales').link_color; |
| 156 |
this.link_data = this.get('link_data') || []; |
| 157 |
let link_matrix = this.get('link_matrix'); |
| 158 |
const link_color = this.get('link_color'); |
| 159 |
|
| 160 |
if (link_color_scale !== undefined && link_color.length > 0) { |
| 161 |
link_matrix = link_color; |
| 162 |
} |
| 163 |
|
| 164 |
// Coerce link matrix into format understandable by d3 force layout |
| 165 |
if (this.link_data.length === 0 && link_matrix.length > 0) { |
| 166 |
link_matrix.forEach((d: number[], i: number) => { |
| 167 |
d.forEach((e, j) => { |
| 168 |
if (e !== null && i != j) { |
| 169 |
this.link_data.push({ |
| 170 |
source: this.mark_data[i], |
| 171 |
target: this.mark_data[j], |
| 172 |
value: e, |
| 173 |
}); |
| 174 |
} |
| 175 |
}); |
| 176 |
}); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
update_data() { |
| 181 |
this.dirty = true; |
| 182 |
this.updateNodeData(); |
| 183 |
this.updateLinkData(); |
| 184 |
this.update_domains(); |
| 185 |
this.dirty = false; |
| 186 |
this.trigger('data_updated'); |
| 187 |
} |
| 188 |
|
| 189 |
update_domains() { |
| 190 |
const scales = this.get('scales'); |
| 191 |
|
| 192 |
if (scales.x) { |
| 193 |
if (!this.get('preserve_domain').x && this.mark_data) { |
| 194 |
scales.x.compute_and_set_domain( |
| 195 |
this.mark_data.map((elem) => { |
| 196 |
return elem.xval; |
| 197 |
}), |
| 198 |
this.model_id + '_x' |
| 199 |
); |
| 200 |
} else { |
| 201 |
scales.x.del_domain([], this.model_id + '_x'); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (scales.y) { |
| 206 |
if (!this.get('preserve_domain').y && this.mark_data) { |
| 207 |
scales.y.compute_and_set_domain( |
| 208 |
this.mark_data.map((elem) => { |
| 209 |
return elem.yval; |
| 210 |
}), |
| 211 |
this.model_id + '_y' |
| 212 |
); |
| 213 |
} else { |
| 214 |
scales.y.del_domain([], this.model_id + '_y'); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if (scales.color) { |
| 219 |
if (!this.get('preserve_domain').color && this.mark_data) { |
| 220 |
scales.color.compute_and_set_domain( |
| 221 |
this.mark_data.map((elem) => { |
| 222 |
return elem.color; |
| 223 |
}), |
| 224 |
this.model_id + '_color' |
| 225 |
); |
| 226 |
} else { |
| 227 |
scales.color.del_domain([], this.model_id + '_color'); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if (scales.link_color) { |
| 232 |
if (!this.get('preserve_domain').link_color && this.link_data) { |
| 233 |
scales.link_color.compute_and_set_domain( |
| 234 |
this.link_data.map((elem) => { |
| 235 |
return elem.value; |
| 236 |
}), |
| 237 |
this.model_id + '_link_color' |
| 238 |
); |
| 239 |
} else { |
| 240 |
scales.link_color.del_domain([], this.model_id + '_link_color'); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
static serializers = { |
| 246 |
...MarkModel.serializers, |
| 247 |
x: serialize.array_or_json_serializer, |
| 248 |
y: serialize.array_or_json_serializer, |
| 249 |
color: serialize.array_or_json_serializer, |
| 250 |
link_color: serialize.array_or_json_serializer, |
| 251 |
link_matrix: serialize.array_or_json_serializer, |
| 252 |
}; |
| 253 |
|
| 254 |
mark_data: NodeData[]; |
| 255 |
link_data: LinkData[]; |
| 256 |
} |