This commit is contained in:
crusader 2018-09-13 16:56:21 +09:00
parent dc5ad26d73
commit 79885e9a8c

View File

@ -40,6 +40,7 @@ export class HomePageComponent implements OnInit, OnDestroy {
private nodes: Node[]; private nodes: Node[];
private links: Link[]; private links: Link[];
public simulation: d3.Simulation<Node, Link> | undefined; public simulation: d3.Simulation<Node, Link> | undefined;
private zoomBehavior: d3.ZoomBehavior<Element, {}>;
private discoveryContainerWidth: number; private discoveryContainerWidth: number;
private discoveryContainerHeight: number; private discoveryContainerHeight: number;
@ -265,11 +266,11 @@ export class HomePageComponent implements OnInit, OnDestroy {
const svg = d3.select(this.discoveryTargetRef.nativeElement); const svg = d3.select(this.discoveryTargetRef.nativeElement);
const _zoom = d3.zoom().on('zoom', () => { this.zoomBehavior = d3.zoom().on('zoom', () => {
const transform = d3.event.transform; const transform = d3.event.transform;
svg.select('g').attr('transform', 'translate(' + transform.x + ',' + transform.y + ') scale(' + transform.k + ')'); svg.select('g').attr('transform', 'translate(' + transform.x + ',' + transform.y + ') scale(' + transform.k + ')');
}); });
svg.call(_zoom); svg.call(this.zoomBehavior);
const __this = this; const __this = this;
this.simulation = d3 this.simulation = d3
@ -385,6 +386,31 @@ export class HomePageComponent implements OnInit, OnDestroy {
} }
} }
zoomToFit(paddingPercent, transitionDuration) {
const root = d3.select(this.discoveryTargetRef.nativeElement);
const bounds = root.node().getBBox();
const parent = root.node().parentElement;
const fullWidth = parent.clientWidth;
const fullHeight = parent.clientHeight;
const width = bounds.width;
const height = bounds.height;
const midX = bounds.x + width / 2;
const midY = bounds.y + height / 2;
if (width === 0 || height === 0) { return; } // nothing to fit
const scale = (paddingPercent || 0.75) / Math.max(width / fullWidth, height / fullHeight);
const translate = [fullWidth / 2 - scale * midX, fullHeight / 2 - scale * midY];
root
.transition()
.duration(transitionDuration || 0) // milliseconds
.call(this.zoomBehavior.transform, d3.zoomIdentity.translate(translate[0], translate[1]).scale(scale));
}
onTargetClick(node: Node) { onTargetClick(node: Node) {
console.log(node); console.log(node);
this.displaySidebar = true; this.displaySidebar = true;
@ -454,6 +480,7 @@ export class HomePageComponent implements OnInit, OnDestroy {
console.log('DiscoveryStop', stopDate); console.log('DiscoveryStop', stopDate);
this.discoveryConfigService.setStarted(false); this.discoveryConfigService.setStarted(false);
this.simulationRestart(true); this.simulationRestart(true);
this.zoomToFit(0.95, 500);
// this.simulation.stop(); // this.simulation.stop();
} }