|
201 | 201 | });
|
202 | 202 | }
|
203 | 203 |
|
204 |
| - function calculateLabelWidths() { |
| 204 | + function calculateLabelHeightsAndWidths() { |
205 | 205 | nodeElements.forEach(node => {
|
206 | 206 | if (node.data.label && node.data.label.length > 0) {
|
207 |
| - node.data.width = txtCtx.measureText(node.data.label).width * 1.75; // Don't know why, but the width of node has to be about 75% bigger than the width of the label text. |
| 207 | + let labelText = node.data.label; |
| 208 | + let metrics = txtCtx.measureText(labelText); |
| 209 | + if (labelText.indexOf('\n') > -1){ |
| 210 | + let longestStringLength = 0; |
| 211 | + const lines = node.data.label.split('\n'); |
| 212 | + lines.forEach(s => { |
| 213 | + if (s.length > longestStringLength) { |
| 214 | + longestStringLength = s.length; |
| 215 | + labelText = s; |
| 216 | + } |
| 217 | + }); |
| 218 | + metrics = txtCtx.measureText(labelText); |
| 219 | + node.data.height = (metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent) * (lines.length + 2); |
| 220 | + } |
| 221 | + node.data.width = metrics.width * 1.75; // Don't know why, but the width of node has to be about 75% bigger than the width of the label text. |
208 | 222 | }
|
209 | 223 | });
|
210 | 224 | }
|
211 | 225 |
|
212 | 226 | function setNetworkLayout() {
|
213 |
| - calculateLabelWidths(); |
| 227 | + calculateLabelHeightsAndWidths(); |
214 | 228 | cy = cytoscape({
|
215 | 229 | container: cyContainerDiv,
|
216 | 230 |
|
217 | 231 | style: [{
|
218 | 232 | selector: 'node',
|
219 | 233 | style: {
|
220 | 234 | 'width': 'data(width)',
|
| 235 | + 'height': 'data(height)', |
221 | 236 | 'label': 'data(label)',
|
222 | 237 | 'text-valign': 'data(labelvalign)',
|
223 | 238 | 'text-halign': 'center',
|
| 239 | + 'text-wrap': 'wrap', |
224 | 240 | 'font-family': 'data(fontFamily)',
|
225 | 241 | 'font-size': 'data(fontSize)',
|
226 | 242 | 'font-weight': 'data(fontWeight)',
|
|
0 commit comments