Skip to content

Commit 7032a93

Browse files
committed
added zooming on canvas
1 parent 2749404 commit 7032a93

File tree

3 files changed

+108
-31
lines changed

3 files changed

+108
-31
lines changed

src/charts/topology-map.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="pf-topology-map">
2-
<canvas class="topology-graph"></canvas>
2+
<canvas id="topology-map" class="topology-graph"></canvas>
33
<div class="test">
44
<h1>Hello world</h1>
55
</div>

src/charts/topology-map/examples/topology-map-view.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
{
2121
id: 1,
2222
title: 'testNode',
23-
icon: 'fa'
23+
icon: '\uF044',
24+
usage: 50,
2425
},
2526
{
2627
id: 2,
2728
title: 'testNode2',
28-
fill: '#FF0000'
29-
},
29+
fill: '#FF0000',
30+
icon: '\uF047',
31+
usage: 75,
32+
},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},
3033
];
3134
$scope.edges = [{
3235
source: 0,

src/charts/topology-map/topologyMap.component.js

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
1616
var context;
1717
var nodes;
1818
var links;
19-
var icons;
20-
var maxNodeSize;
2119
var transform;
22-
var simulation;
2320
var options;
2421
var radius = ctrl.radius || 17;
25-
var graph;
2622

23+
ctrl.scale = 1;
2724
ctrl.canvas;
2825
ctrl.showLabels = false;
2926

3027
this.$onInit = function () {
3128
setUpCanvas();
3229
setUpForce();
33-
console.log(ctrl.force);
30+
setUpZoom();
31+
setUpClick();
3432
};
3533

3634
function setUpCanvas () {
@@ -51,6 +49,37 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
5149
ctrl.context.font = '17px FontAwesome';
5250
}
5351

52+
function setUpClick () {
53+
d3.select(ctrl.canvas).on('click', function () {
54+
console.log('click: ', d3.select(this));
55+
});
56+
}
57+
58+
function setUpZoom () {
59+
var zoom = d3.behavior.zoom()
60+
.translate([0, 0])
61+
.scale(1)
62+
.scaleExtent([1, 8]);
63+
var canvas = d3.select(ctrl.canvas);
64+
canvas
65+
.call(zoom.on("zoom", zoomed))
66+
.call(zoom.event);
67+
68+
function zoomed () {
69+
var t = zoom.translate();
70+
var s = zoom.scale();
71+
ctrl.scale = s;
72+
73+
ctrl.context.clearRect(0, 0, ctrl.canvasW, ctrl.canvasH);
74+
75+
ctrl.context.save();
76+
ctrl.context.translate(t[0], t[1]);
77+
ctrl.context.scale(s, s);
78+
ctrl.force.on('tick')();
79+
ctrl.context.restore();
80+
}
81+
}
82+
5483
function setUpForce () {
5584
if (! ctrl.force) {
5685
ctrl.force = d3.layout.force()
@@ -64,38 +93,83 @@ angular.module('patternfly.charts').component('pfTopologyMap', {
6493
.links(ctrl.edges)
6594
.on('tick', tick)
6695
.start();
67-
6896
function tick () {
6997
ctrl.context.clearRect(0, 0, ctrl.canvasW, ctrl.canvasH);
70-
ctrl.context.strokeStyle = "#ccc";
71-
ctrl.context.beginPath();
72-
ctrl.edges.forEach(function (d) {
73-
ctrl.context.moveTo(d.source.x, d.source.y);
74-
ctrl.context.lineTo(d.target.x, d.target.y);
75-
});
76-
ctrl.context.stroke();
77-
78-
// draw nodes
79-
ctrl.nodes.forEach(function (d) {
80-
drawNode(d);
81-
});
98+
drawEdges();
99+
drawNodes();
100+
if (ctrl.scale !== 1) {
101+
drawMiniMap();
102+
}
82103
}
83104
}
84105

85-
function drawNode (node) {
106+
function drawEdges () {
107+
ctrl.context.strokeStyle = "#ccc";
108+
ctrl.context.beginPath();
109+
ctrl.edges.forEach(function (d) {
110+
ctrl.context.lineWidth = 1;
111+
ctrl.context.moveTo(d.source.x, d.source.y);
112+
ctrl.context.lineTo(d.target.x, d.target.y);
113+
});
114+
ctrl.context.stroke();
115+
}
116+
117+
function normalizeNode (node) {
118+
node.size = node.size || 17;
119+
node.x = Math.max(node.size + 1, Math.min(ctrl.canvasW - node.size - 1, node.x));
120+
node.y = Math.max(node.size + 1, Math.min(ctrl.canvasH - node.size - 1, node.y));
121+
}
122+
123+
function drawNodes () {
86124
// draw circle
125+
ctrl.nodes.forEach(function (node) {
126+
normalizeNode(node);
127+
ctrl.context.beginPath();
128+
ctrl.context.fillStyle = node.fill || "steelblue";
129+
ctrl.context.moveTo(node.x, node.y);
130+
ctrl.context.arc(node.x, node.y, 17, 0, 2 * Math.PI);
131+
ctrl.context.fill();
132+
133+
if (node.usage) {
134+
ctrl.context.beginPath();
135+
ctrl.context.lineWidth = 5;
136+
ctrl.context.arc(node.x, node.y, node.size, 0, ((node.usage / 100) * 2) * Math.PI);
137+
ctrl.context.strokeStyle = '#003300';
138+
ctrl.context.stroke();
139+
}
140+
141+
//draw icon
142+
ctrl.context.beginPath();
143+
ctrl.context.fillStyle = 'white';
144+
ctrl.context.textAlign = 'center';
145+
ctrl.context.textBaseline = 'middle';
146+
ctrl.context.fillText(node.icon || '\uF049', node.x, node.y);
147+
ctrl.context.fill();
148+
});
149+
}
150+
151+
function drawMiniMap () {
152+
var mapX = 0.9 * ctrl.canvasW - 10;
153+
var mapY = 10;
154+
var mapW = 0.1 * ctrl.canvasW;
155+
var mapH = 0.1 * ctrl.canvasH;
87156
ctrl.context.beginPath();
88-
ctrl.context.fillStyle = "steelblue";
89-
ctrl.context.moveTo(node.x, node.y);
90-
ctrl.context.arc(node.x, node.y, 17, 0, 2 * Math.PI);
157+
ctrl.context.strokeStyle = 'rgba(0, 0, 0, 0.3)';
158+
ctrl.context.fillStyle = 'rgba(252, 252, 252, 0.3)';
159+
160+
ctrl.context.rect(mapX * ctrl.scale, mapY * ctrl.scale, mapW, mapH);
161+
ctrl.context.stroke();
91162
ctrl.context.fill();
92163

93-
//draw icon
94164
ctrl.context.beginPath();
95-
ctrl.context.fillStyle = 'white';
96-
ctrl.context.textAlign = 'center';
97-
ctrl.context.textBaseline = 'middle';
98-
ctrl.context.fillText('\uF047', node.x, node.y);
165+
ctrl.context.fillStyle = 'rgba(224, 224, 224, 0.3)';
166+
ctrl.context.rect(
167+
mapX / ctrl.scale,
168+
mapY / ctrl.scale,
169+
mapW / ctrl.scale,
170+
mapH / ctrl.scale
171+
);
172+
ctrl.context.stroke();
99173
ctrl.context.fill();
100174
}
101175
},

0 commit comments

Comments
 (0)