Commit d4e5f89d by chenwq

style: 格式化代码缩进和代码风格

1 parent df7d19d2
...@@ -10,265 +10,265 @@ const WEEKLY_AVG_START = 5000; ...@@ -10,265 +10,265 @@ const WEEKLY_AVG_START = 5000;
const WEEKLY_MIN_VALUE = 200; const WEEKLY_MIN_VALUE = 200;
const ITEM_COUNT = 7; const ITEM_COUNT = 7;
const DEPTH_SCALE = 16; const DEPTH_SCALE = 1
// Color Palette // Color Palette
const COLOR_MAP = { const COLOR_MAP = {
'$启动APP': '#409EFF', $启动APP: '#409EFF',
'$关闭App': '#909399', $关闭App: '#909399',
'App激活': '#67C23A', App激活: '#67C23A',
'App浏览页面': '#E6A23C', App浏览页面: '#E6A23C',
'App元素点击': '#F56C6C', App元素点击: '#F56C6C',
'App推送点击': '#9C27B0', App推送点击: '#9C27B0',
'web浏览页面': '#00BCD4', web浏览页面: '#00BCD4',
'小程序首次启动': '#3F51B5', 小程序首次启动: '#3F51B5',
'小程序浏览页面': '#2196F3', 小程序浏览页面: '#2196F3',
'微信端内活跃': '#009688', 微信端内活跃: '#009688',
'搜索': '#FF9800', 搜索: '#FF9800',
'签到': '#795548', 签到: '#795548',
'所有支付': '#607D8B', 所有支付: '#607D8B',
'更多': '#DCDFE6' 更多: '#DCDFE6'
}; }
const EVENT_POOL = Object.keys(COLOR_MAP).filter(k => !k.startsWith('$') && k !== '更多'); const EVENT_POOL = Object.keys(COLOR_MAP).filter(k => !k.startsWith('$') && k !== '更多')
// --- Helper Functions --- // --- Helper Functions ---
function shuffle(array) { function shuffle(array) {
const newArr = [...array]; const newArr = [...array]
for (let i = newArr.length - 1; i > 0; i--) { for (let i = newArr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1)); const j = Math.floor(Math.random() * (i + 1))
[newArr[i], newArr[j]] = [newArr[j], newArr[i]]; ;[newArr[i], newArr[j]] = [newArr[j], newArr[i]]
} }
return newArr; return newArr
} }
function generateDist(total, count, minVal) { function generateDist(total, count, minVal) {
let remaining = total - (count * minVal); let remaining = total - count * minVal
if (remaining < 0) return Array(count).fill(Math.floor(total/count)); if (remaining < 0) return Array(count).fill(Math.floor(total / count))
let cuts = []; let cuts = []
for(let i=0; i<count-1; i++) cuts.push(Math.random()); for (let i = 0; i < count - 1; i++) cuts.push(Math.random())
cuts.sort((a,b) => a-b); cuts.sort((a, b) => a - b)
let parts = []; let parts = []
let prev = 0; let prev = 0
for(let cut of cuts) { for (let cut of cuts) {
parts.push(cut - prev); parts.push(cut - prev)
prev = cut; prev = cut
} }
parts.push(1 - prev); parts.push(1 - prev)
parts = parts.map(p => Math.floor(p * remaining) + minVal); parts = parts.map(p => Math.floor(p * remaining) + minVal)
let sum = parts.reduce((a,b) => a+b, 0); let sum = parts.reduce((a, b) => a + b, 0)
parts[0] += (total - sum); parts[0] += total - sum
return parts; return parts
} }
function generateMatrixWithLoss(rowSums, colSums) { function generateMatrixWithLoss(rowSums, colSums) {
let matrix = Array(rowSums.length).fill(0).map(() => Array(colSums.length).fill(0)); let matrix = Array(rowSums.length)
let supply = [...rowSums]; .fill(0)
let demand = [...colSums]; .map(() => Array(colSums.length).fill(0))
let supply = [...rowSums]
// Cap supply to create loss let demand = [...colSums]
let caps = supply.map(s => Math.floor(s * (0.80 + Math.random() * 0.15))); // 5-20% loss
// Cap supply to create loss
let totalCapped = caps.reduce((a,b) => a+b, 0); let caps = supply.map(s => Math.floor(s * (0.8 + Math.random() * 0.15))) // 5-20% loss
let totalDemand = demand.reduce((a,b) => a+b, 0);
let totalCapped = caps.reduce((a, b) => a + b, 0)
if (totalCapped < totalDemand) { let totalDemand = demand.reduce((a, b) => a + b, 0)
let ratio = totalDemand / totalCapped;
caps = caps.map(c => Math.ceil(c * ratio * 1.01)); if (totalCapped < totalDemand) {
let ratio = totalDemand / totalCapped
caps = caps.map(c => Math.ceil(c * ratio * 1.01))
}
let cells = []
for (let i = 0; i < rowSums.length; i++) {
for (let j = 0; j < colSums.length; j++) {
cells.push([i, j])
} }
}
let cells = []; cells = shuffle(cells)
for(let i=0; i<rowSums.length; i++) {
for(let j=0; j<colSums.length; j++) { let effectiveSupply = [...caps]
cells.push([i, j]);
} for (let [i, j] of cells) {
} if (effectiveSupply[i] <= 0 || demand[j] <= 0) continue
cells = shuffle(cells); let maxPossible = Math.min(effectiveSupply[i], demand[j])
let amount = maxPossible
let effectiveSupply = [...caps]; if (Math.random() > 0.2) {
let ratio = 0.3 + Math.random() * 0.5
for(let [i, j] of cells) { amount = Math.floor(maxPossible * ratio)
if(effectiveSupply[i] <= 0 || demand[j] <= 0) continue; if (amount < 50) amount = maxPossible
let maxPossible = Math.min(effectiveSupply[i], demand[j]);
let amount = maxPossible;
if (Math.random() > 0.2) {
let ratio = 0.3 + Math.random() * 0.5;
amount = Math.floor(maxPossible * ratio);
if (amount < 50) amount = maxPossible;
}
matrix[i][j] += amount;
effectiveSupply[i] -= amount;
demand[j] -= amount;
} }
matrix[i][j] += amount
for(let i=0; i<rowSums.length; i++) { effectiveSupply[i] -= amount
for(let j=0; j<colSums.length; j++) { demand[j] -= amount
if (demand[j] > 0 && effectiveSupply[i] > 0) { }
let maxPossible = Math.min(effectiveSupply[i], demand[j]);
matrix[i][j] += maxPossible; for (let i = 0; i < rowSums.length; i++) {
effectiveSupply[i] -= maxPossible; for (let j = 0; j < colSums.length; j++) {
demand[j] -= maxPossible; if (demand[j] > 0 && effectiveSupply[i] > 0) {
} let maxPossible = Math.min(effectiveSupply[i], demand[j])
} matrix[i][j] += maxPossible
effectiveSupply[i] -= maxPossible
demand[j] -= maxPossible
}
} }
return matrix; }
return matrix
} }
// --- Generator --- // --- Generator ---
function generateGraphData(startValue, minValue) { function generateGraphData(startValue, minValue) {
let nodes = []; let nodes = []
let links = []; let links = []
const minLayerTotal = ITEM_COUNT * minValue; const minLayerTotal = ITEM_COUNT * minValue
// Start Node // Start Node
nodes.push({name: "$启动APP", depth: 0, itemStyle: {color: COLOR_MAP['$启动APP']}, value: startValue}); nodes.push({ name: '$启动APP', depth: 0, itemStyle: { color: COLOR_MAP['$启动APP'] }, value: startValue })
let currentLayerNodes = [{name: "$启动APP", value: startValue}]; let currentLayerNodes = [{ name: '$启动APP', value: startValue }]
let currentLayerTotal = startValue; let currentLayerTotal = startValue
// Layers 1 to 5 // Layers 1 to 5
for(let d=1; d<=5; d++) { for (let d = 1; d <= 5; d++) {
let currentDepth = (d-1) * DEPTH_SCALE; let currentDepth = (d - 1) * DEPTH_SCALE
if (d===1) currentDepth = 0; if (d === 1) currentDepth = 0
let nextDepth = d * DEPTH_SCALE; let nextDepth = d * DEPTH_SCALE
// Calculate Next Layer Total // Calculate Next Layer Total
let retention = 0.85; let retention = 0.85
let nextLayerTotal = Math.floor(currentLayerTotal * retention); let nextLayerTotal = Math.floor(currentLayerTotal * retention)
if (nextLayerTotal < minLayerTotal) nextLayerTotal = minLayerTotal; if (nextLayerTotal < minLayerTotal) nextLayerTotal = minLayerTotal
// Generate Real Nodes // Generate Real Nodes
let parts = generateDist(nextLayerTotal, ITEM_COUNT, minValue); let parts = generateDist(nextLayerTotal, ITEM_COUNT, minValue)
parts.sort((a,b) => b-a); // Descending parts.sort((a, b) => b - a) // Descending
let eventSubset = shuffle([...EVENT_POOL]).slice(0, 6); let eventSubset = shuffle([...EVENT_POOL]).slice(0, 6)
let nextLayerNodes = []; let nextLayerNodes = []
// Events // Events
for(let i=0; i<6; i++) { for (let i = 0; i < 6; i++) {
let eventName = eventSubset[i]; let eventName = eventSubset[i]
nextLayerNodes.push({ nextLayerNodes.push({
name: `${eventName} (${d})`, name: `${eventName} (${d})`,
depth: nextDepth, depth: nextDepth,
value: parts[i+1], value: parts[i + 1],
itemStyle: { color: COLOR_MAP[eventName] || '#000' } itemStyle: { color: COLOR_MAP[eventName] || '#000' }
}); })
} }
// More // More
nextLayerNodes.push({ nextLayerNodes.push({
name: `更多 (${d})`, name: `更多 (${d})`,
depth: nextDepth, depth: nextDepth,
value: parts[0], value: parts[0],
itemStyle: { color: COLOR_MAP['更多'] } itemStyle: { color: COLOR_MAP['更多'] }
}); })
nextLayerNodes.forEach(n => nodes.push(n)); nextLayerNodes.forEach(n => nodes.push(n))
// Links // Links
let sourceValues = currentLayerNodes.map(n => n.value); let sourceValues = currentLayerNodes.map(n => n.value)
let targetValues = nextLayerNodes.map(n => n.value); let targetValues = nextLayerNodes.map(n => n.value)
let linkMatrix = generateMatrixWithLoss(sourceValues, targetValues); let linkMatrix = generateMatrixWithLoss(sourceValues, targetValues)
for(let i=0; i<sourceValues.length; i++) { for (let i = 0; i < sourceValues.length; i++) {
let sentTotal = 0; let sentTotal = 0
for(let j=0; j<targetValues.length; j++) { for (let j = 0; j < targetValues.length; j++) {
let val = linkMatrix[i][j]; let val = linkMatrix[i][j]
if(val > 0) { if (val > 0) {
links.push({ links.push({
source: currentLayerNodes[i].name, source: currentLayerNodes[i].name,
target: nextLayerNodes[j].name, target: nextLayerNodes[j].name,
value: val value: val
}); })
sentTotal += val; sentTotal += val
}
}
// Loss
let loss = currentLayerNodes[i].value - sentTotal;
if (loss > 0) {
let lossNodeName = `${currentLayerNodes[i].name}__Loss`;
// Place loss node at same depth as next layer
let lossDepth = (d===1 ? 0 : (d-1)*DEPTH_SCALE) + 1;
nodes.push({
name: lossNodeName,
depth: lossDepth,
value: loss,
itemStyle: { opacity: 0 },
label: { show: false },
isLoss: true
});
links.push({
source: currentLayerNodes[i].name,
target: lossNodeName,
value: loss,
lineStyle: { color: '#ffbaba', opacity: 0.6, curveness: 0 }
});
}
} }
}
currentLayerNodes = nextLayerNodes;
currentLayerTotal = nextLayerTotal; // Loss
let loss = currentLayerNodes[i].value - sentTotal
if (loss > 0) {
let lossNodeName = `${currentLayerNodes[i].name}__Loss`
// Place loss node at same depth as next layer
let lossDepth = nextDepth
nodes.push({
name: lossNodeName,
depth: lossDepth,
value: loss,
itemStyle: { opacity: 0 },
label: { show: false },
isLoss: true
})
links.push({
source: currentLayerNodes[i].name,
target: lossNodeName,
value: loss
})
}
} }
// End Node currentLayerNodes = nextLayerNodes
let lastDepth = 5 * DEPTH_SCALE; currentLayerTotal = nextLayerTotal
let endDepth = 6 * DEPTH_SCALE; }
let endValue = Math.floor(currentLayerTotal * 0.9);
let endNode = {name: "$关闭App", depth: endDepth, itemStyle: {color: COLOR_MAP['$关闭App']}, value: endValue}; // End Node
nodes.push(endNode); let lastDepth = 5 * DEPTH_SCALE
let endDepth = 6 * DEPTH_SCALE
let endValue = Math.floor(currentLayerTotal * 0.9)
let endNode = { name: '$关闭App', depth: endDepth, itemStyle: { color: COLOR_MAP['$关闭App'] }, value: endValue }
nodes.push(endNode)
currentLayerNodes.forEach(node => {
let val = Math.floor(node.value * 0.9)
links.push({
source: node.name,
target: '$关闭App',
value: val
})
let loss = node.value - val
if (loss > 0) {
let lossNodeName = `${node.name}__Loss`
nodes.push({
name: lossNodeName,
depth: endDepth,
value: loss,
itemStyle: { opacity: 0 },
label: { show: false },
isLoss: true
})
links.push({
source: node.name,
target: lossNodeName,
value: loss
})
}
})
// Sorting
nodes.sort((a, b) => {
if (a.depth !== b.depth) return a.depth - b.depth
const getRank = n => {
if (n.isLoss) return 2
if (n.name.includes('更多')) return 1
return 0
}
const rankA = getRank(a)
const rankB = getRank(b)
if (rankA !== rankB) return rankA - rankB
return b.value - a.value
})
currentLayerNodes.forEach(node => { return { nodes, links }
let val = Math.floor(node.value * 0.9);
links.push({
source: node.name,
target: "$关闭App",
value: val
});
let loss = node.value - val;
if (loss > 0) {
let lossNodeName = `${node.name}__Loss`;
nodes.push({
name: lossNodeName,
depth: lastDepth + 1,
value: loss,
itemStyle: { opacity: 0 },
label: { show: false },
isLoss: true
});
links.push({
source: node.name,
target: lossNodeName,
value: loss,
lineStyle: { color: '#ffbaba', opacity: 0.6, curveness: 0 }
});
}
});
// Sorting
nodes.sort((a, b) => {
if (a.depth !== b.depth) return a.depth - b.depth;
const getRank = (n) => {
if (n.isLoss) return 2;
if (n.name.includes('更多')) return 1;
return 0;
};
const rankA = getRank(a);
const rankB = getRank(b);
if (rankA !== rankB) return rankA - rankB;
return b.value - a.value;
});
return { nodes, links };
} }
// --- Main Execution --- // --- Main Execution ---
......
This diff could not be displayed because it is too large.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!