Commit d4e5f89d by chenwq

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

1 parent df7d19d2
...@@ -10,154 +10,156 @@ const WEEKLY_AVG_START = 5000; ...@@ -10,154 +10,156 @@ 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]
let demand = [...colSums]
// Cap supply to create loss // Cap supply to create loss
let caps = supply.map(s => Math.floor(s * (0.80 + Math.random() * 0.15))); // 5-20% loss let caps = supply.map(s => Math.floor(s * (0.8 + Math.random() * 0.15))) // 5-20% loss
let totalCapped = caps.reduce((a,b) => a+b, 0); let totalCapped = caps.reduce((a, b) => a + b, 0)
let totalDemand = demand.reduce((a,b) => a+b, 0); let totalDemand = demand.reduce((a, b) => a + b, 0)
if (totalCapped < totalDemand) { if (totalCapped < totalDemand) {
let ratio = totalDemand / totalCapped; let ratio = totalDemand / totalCapped
caps = caps.map(c => Math.ceil(c * ratio * 1.01)); caps = caps.map(c => Math.ceil(c * ratio * 1.01))
} }
let cells = []; let cells = []
for(let i=0; i<rowSums.length; i++) { for (let i = 0; i < rowSums.length; i++) {
for(let j=0; j<colSums.length; j++) { for (let j = 0; j < colSums.length; j++) {
cells.push([i, j]); cells.push([i, j])
} }
} }
cells = shuffle(cells); cells = shuffle(cells)
let effectiveSupply = [...caps]; let effectiveSupply = [...caps]
for(let [i, j] of cells) { for (let [i, j] of cells) {
if(effectiveSupply[i] <= 0 || demand[j] <= 0) continue; if (effectiveSupply[i] <= 0 || demand[j] <= 0) continue
let maxPossible = Math.min(effectiveSupply[i], demand[j]); let maxPossible = Math.min(effectiveSupply[i], demand[j])
let amount = maxPossible; let amount = maxPossible
if (Math.random() > 0.2) { if (Math.random() > 0.2) {
let ratio = 0.3 + Math.random() * 0.5; let ratio = 0.3 + Math.random() * 0.5
amount = Math.floor(maxPossible * ratio); amount = Math.floor(maxPossible * ratio)
if (amount < 50) amount = maxPossible; if (amount < 50) amount = maxPossible
} }
matrix[i][j] += amount; matrix[i][j] += amount
effectiveSupply[i] -= amount; effectiveSupply[i] -= amount
demand[j] -= amount; demand[j] -= amount
} }
for(let i=0; i<rowSums.length; i++) { for (let i = 0; i < rowSums.length; i++) {
for(let j=0; j<colSums.length; j++) { for (let j = 0; j < colSums.length; j++) {
if (demand[j] > 0 && effectiveSupply[i] > 0) { if (demand[j] > 0 && effectiveSupply[i] > 0) {
let maxPossible = Math.min(effectiveSupply[i], demand[j]); let maxPossible = Math.min(effectiveSupply[i], demand[j])
matrix[i][j] += maxPossible; matrix[i][j] += maxPossible
effectiveSupply[i] -= maxPossible; effectiveSupply[i] -= maxPossible
demand[j] -= 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
...@@ -166,36 +168,36 @@ function generateGraphData(startValue, minValue) { ...@@ -166,36 +168,36 @@ function generateGraphData(startValue, minValue) {
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 // Loss
let loss = currentLayerNodes[i].value - sentTotal; let loss = currentLayerNodes[i].value - sentTotal
if (loss > 0) { if (loss > 0) {
let lossNodeName = `${currentLayerNodes[i].name}__Loss`; let lossNodeName = `${currentLayerNodes[i].name}__Loss`
// Place loss node at same depth as next layer // Place loss node at same depth as next layer
let lossDepth = (d===1 ? 0 : (d-1)*DEPTH_SCALE) + 1; let lossDepth = nextDepth
nodes.push({ nodes.push({
name: lossNodeName, name: lossNodeName,
...@@ -204,71 +206,69 @@ function generateGraphData(startValue, minValue) { ...@@ -204,71 +206,69 @@ function generateGraphData(startValue, minValue) {
itemStyle: { opacity: 0 }, itemStyle: { opacity: 0 },
label: { show: false }, label: { show: false },
isLoss: true isLoss: true
}); })
links.push({ links.push({
source: currentLayerNodes[i].name, source: currentLayerNodes[i].name,
target: lossNodeName, target: lossNodeName,
value: loss, value: loss
lineStyle: { color: '#ffbaba', opacity: 0.6, curveness: 0 } })
});
} }
} }
currentLayerNodes = nextLayerNodes; currentLayerNodes = nextLayerNodes
currentLayerTotal = nextLayerTotal; currentLayerTotal = nextLayerTotal
} }
// End Node // End Node
let lastDepth = 5 * DEPTH_SCALE; let lastDepth = 5 * DEPTH_SCALE
let endDepth = 6 * DEPTH_SCALE; let endDepth = 6 * DEPTH_SCALE
let endValue = Math.floor(currentLayerTotal * 0.9); let endValue = Math.floor(currentLayerTotal * 0.9)
let endNode = {name: "$关闭App", depth: endDepth, itemStyle: {color: COLOR_MAP['$关闭App']}, value: endValue}; let endNode = { name: '$关闭App', depth: endDepth, itemStyle: { color: COLOR_MAP['$关闭App'] }, value: endValue }
nodes.push(endNode); nodes.push(endNode)
currentLayerNodes.forEach(node => { currentLayerNodes.forEach(node => {
let val = Math.floor(node.value * 0.9); let val = Math.floor(node.value * 0.9)
links.push({ links.push({
source: node.name, source: node.name,
target: "$关闭App", target: '$关闭App',
value: val value: val
}); })
let loss = node.value - val; let loss = node.value - val
if (loss > 0) { if (loss > 0) {
let lossNodeName = `${node.name}__Loss`; let lossNodeName = `${node.name}__Loss`
nodes.push({ nodes.push({
name: lossNodeName, name: lossNodeName,
depth: lastDepth + 1, depth: endDepth,
value: loss, value: loss,
itemStyle: { opacity: 0 }, itemStyle: { opacity: 0 },
label: { show: false }, label: { show: false },
isLoss: true isLoss: true
}); })
links.push({ links.push({
source: node.name, source: node.name,
target: lossNodeName, target: lossNodeName,
value: loss, value: loss
lineStyle: { color: '#ffbaba', opacity: 0.6, curveness: 0 } })
});
} }
}); })
// Sorting // Sorting
nodes.sort((a, b) => { nodes.sort((a, b) => {
if (a.depth !== b.depth) return a.depth - b.depth; if (a.depth !== b.depth) return a.depth - b.depth
const getRank = (n) => { const getRank = n => {
if (n.isLoss) return 2; if (n.isLoss) return 2
if (n.name.includes('更多')) return 1; if (n.name.includes('更多')) return 1
return 0; return 0
}; }
const rankA = getRank(a); const rankA = getRank(a)
const rankB = getRank(b); const rankB = getRank(b)
if (rankA !== rankB) return rankA - rankB; if (rankA !== rankB) return rankA - rankB
return b.value - a.value; return b.value - a.value
}); })
return { nodes, links }; 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!