1. highlight last edge. 2. padEnd() to left align capsule label

This commit is contained in:
Cindy Chang
2024-08-28 14:09:32 +08:00
parent f3b9f7cd41
commit 4b56130278
2 changed files with 18 additions and 56 deletions

View File

@@ -42,56 +42,10 @@ cytoscape.use(cola);
* @return {cytoscape.Core} cy * @return {cytoscape.Core} cy
*/ */
export default function cytoscapeMap(mapData, dataLayerType, dataLayerOption, curveStyle, rank, graphId) { export default function cytoscapeMap(mapData, dataLayerType, dataLayerOption, curveStyle, rank, graphId) {
// create color Gradient
// 設定每個 node, edges 的顏色與樣式 // 設定每個 node, edges 的顏色與樣式
let gradientArray = [];
let activityArray = [];
let edgeArray = [];
let nodeOption = [];
let edgeOption = [];
let nodes = mapData.nodes; let nodes = mapData.nodes;
let edges = mapData.edges; let edges = mapData.edges;
// 設定除了 start, end 的 node 顏色
// 找出 type activity's node
activityArray = nodes.filter(i => i.data.type === 'activity');
// 找出除了 start, end 以外所有的 node 的 option value
activityArray.map(node => nodeOption.push(node.data[dataLayerType][dataLayerOption]));
// 刪掉重複的元素,小到大排序(映對色階淺到深)
nodeOption = [...new Set(nodeOption)].sort((a, b) => a - b);
// 產生 node 色階
gradientArray = new Gradient()
.setColorGradient("#FFFFFF", "#FFFFFF")
.setMidpoint(nodeOption.length)
.getColors();
// 設定每個 node 的背景色
activityArray.forEach(node => {
// 透過 index 找對應位置
let gradientIndex = nodeOption.indexOf(node.data[dataLayerType][dataLayerOption]);
node.data.backgroundColor = gradientArray[gradientIndex];
})
// 設定除了 start, end 的 edges 粗細
// 找出除了 start, end 以外所有的 edge
edgeArray = edges.filter(i => i.data.source !== mapData.startId && i.data.target !== mapData.endId);
// 找出所有 edge 的 option value
edgeArray.map(edge => edgeOption.push(edge.data[dataLayerType][dataLayerOption]));
// 刪掉重複的元素,小到大排序(映對色階淺到深)
edgeOption = [...new Set(edgeOption)].sort((a, b) => a - b);
// 設定每個 edge 的粗細
edgeArray.forEach(edge => {
let edgeIndex = edgeOption.indexOf(edge.data[dataLayerType][dataLayerOption])
edge.data.lineWidth = (parseInt(edgeIndex) + 1) * 0.15
edge.data.style = 'solid'
})
// create Cytoscape // create Cytoscape
let cy = cytoscape({ let cy = cytoscape({
container: graphId, container: graphId,
@@ -121,10 +75,14 @@ export default function cytoscapeMap(mapData, dataLayerType, dataLayerOption, cu
// node.data(this.dataLayerType+"."+this.dataLayerOption) 為原先陣列 node.data.key.value // node.data(this.dataLayerType+"."+this.dataLayerOption) 為原先陣列 node.data.key.value
let optionValue = node.data(`${dataLayerType}.${dataLayerOption}`); let optionValue = node.data(`${dataLayerType}.${dataLayerOption}`);
let text = ''; let text = '';
const STRING_LIMIT = 8;
// 文字超過 10 字尾巴要加「...」style 要換兩行(\n 換行符號) if (node.data('label').length > STRING_LIMIT) {
// 使用 data() 是因為在 cytoscape 中從陣列轉為 function // 文字超過 10 字尾巴要加「...」style 要換兩行(\n 換行符號)
text = node.data('label').length > 10 ? `${node.data('label').substr(0, 10)}...\n\n` : `${node.data('label')}\n\n`; // 使用 data() 是因為在 cytoscape 中從陣列轉為 function
text = `${node.data('label').substr(0, STRING_LIMIT)}...\n\n`;
} else { // 補空白直到撐寬label的寬度這是為了統一所有label的寬度
text = `${node.data('label').padEnd(STRING_LIMIT, ' ')}\n\n`
}
// 在 element 中 activity 歸類在 default所以要先判斷 node 是否為 activity 才裝入文字。 // 在 element 中 activity 歸類在 default所以要先判斷 node 是否為 activity 才裝入文字。
// 可使用 parseInt(整數) parseFloat(浮點數) 將字串轉為數字 // 可使用 parseInt(整數) parseFloat(浮點數) 將字串轉為數字
@@ -179,7 +137,7 @@ export default function cytoscapeMap(mapData, dataLayerType, dataLayerOption, cu
'padding': function(node) { 'padding': function(node) {
return node.data('type') === 'activity' ? 0 : 0; return node.data('type') === 'activity' ? 0 : 0;
}, },
// 'text-justification': 'left', 'text-justification': 'left',
'text-halign': 'center', 'text-halign': 'center',
'text-valign': 'center', 'text-valign': 'center',
'height': 'data(height)', 'height': 'data(height)',
@@ -255,7 +213,7 @@ export default function cytoscapeMap(mapData, dataLayerType, dataLayerOption, cu
'overlay-opacity': 0.01, 'overlay-opacity': 0.01,
'overlay-padding': '5px', 'overlay-padding': '5px',
}, },
} },
], ],
}); });

View File

@@ -60,15 +60,19 @@ export default defineStore('useMapPathStore', {
curGraphNode = prevGraphNode.outgoers('node').filter(neighbor => curGraphNode = prevGraphNode.outgoers('node').filter(neighbor =>
neighbor.data('label') === curButton[listIndex][nodeIndex] neighbor.data('label') === curButton[listIndex][nodeIndex]
); );
if(!curGraphNode) {
// curGraphNode = prevGraphNode.outgoers('node').filter(node =>
// node.data('label').toLocaleLowerCase() === 'end');
}
curEdge = prevGraphNode.edgesWith(curGraphNode); curEdge = prevGraphNode.edgesWith(curGraphNode);
} }
} }
this.insightWithPath[INSIGHTS_FIELDS_AND_LABELS[i][0]][listIndex].nodes.push(curGraphNode); this.insightWithPath[INSIGHTS_FIELDS_AND_LABELS[i][0]][listIndex].nodes.push(curGraphNode);
this.insightWithPath[INSIGHTS_FIELDS_AND_LABELS[i][0]][listIndex].edges.push(curEdge); this.insightWithPath[INSIGHTS_FIELDS_AND_LABELS[i][0]][listIndex].edges.push(curEdge);
// 特殊狀況在for迴圈之外額外插入最後一條線段
if(nodeIndex === curButton[listIndex].length - 1){
const endNode = curGraphNode.outgoers('node').filter(neighbor =>
neighbor.data('label').toLocaleLowerCase() === 'end'
);
const lastEdge = curGraphNode.edgesWith(endNode);
this.insightWithPath[INSIGHTS_FIELDS_AND_LABELS[i][0]][listIndex].edges.push(lastEdge);
}
prevGraphNode = curGraphNode; prevGraphNode = curGraphNode;
} }
} }