Commit d4e5f89d by chenwq

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

1 parent df7d19d2
......@@ -10,265 +10,265 @@ const WEEKLY_AVG_START = 5000;
const WEEKLY_MIN_VALUE = 200;
const ITEM_COUNT = 7;
const DEPTH_SCALE = 16;
const DEPTH_SCALE = 1
// Color Palette
const COLOR_MAP = {
'$启动APP': '#409EFF',
'$关闭App': '#909399',
'App激活': '#67C23A',
'App浏览页面': '#E6A23C',
'App元素点击': '#F56C6C',
'App推送点击': '#9C27B0',
'web浏览页面': '#00BCD4',
'小程序首次启动': '#3F51B5',
'小程序浏览页面': '#2196F3',
'微信端内活跃': '#009688',
'搜索': '#FF9800',
'签到': '#795548',
'所有支付': '#607D8B',
'更多': '#DCDFE6'
};
const EVENT_POOL = Object.keys(COLOR_MAP).filter(k => !k.startsWith('$') && k !== '更多');
$启动APP: '#409EFF',
$关闭App: '#909399',
App激活: '#67C23A',
App浏览页面: '#E6A23C',
App元素点击: '#F56C6C',
App推送点击: '#9C27B0',
web浏览页面: '#00BCD4',
小程序首次启动: '#3F51B5',
小程序浏览页面: '#2196F3',
微信端内活跃: '#009688',
搜索: '#FF9800',
签到: '#795548',
所有支付: '#607D8B',
更多: '#DCDFE6'
}
const EVENT_POOL = Object.keys(COLOR_MAP).filter(k => !k.startsWith('$') && k !== '更多')
// --- Helper Functions ---
function shuffle(array) {
const newArr = [...array];
for (let i = newArr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[newArr[i], newArr[j]] = [newArr[j], newArr[i]];
}
return newArr;
const newArr = [...array]
for (let i = newArr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[newArr[i], newArr[j]] = [newArr[j], newArr[i]]
}
return newArr
}
function generateDist(total, count, minVal) {
let remaining = total - (count * minVal);
if (remaining < 0) return Array(count).fill(Math.floor(total/count));
let cuts = [];
for(let i=0; i<count-1; i++) cuts.push(Math.random());
cuts.sort((a,b) => a-b);
let parts = [];
let prev = 0;
for(let cut of cuts) {
parts.push(cut - prev);
prev = cut;
}
parts.push(1 - prev);
parts = parts.map(p => Math.floor(p * remaining) + minVal);
let sum = parts.reduce((a,b) => a+b, 0);
parts[0] += (total - sum);
return parts;
let remaining = total - count * minVal
if (remaining < 0) return Array(count).fill(Math.floor(total / count))
let cuts = []
for (let i = 0; i < count - 1; i++) cuts.push(Math.random())
cuts.sort((a, b) => a - b)
let parts = []
let prev = 0
for (let cut of cuts) {
parts.push(cut - prev)
prev = cut
}
parts.push(1 - prev)
parts = parts.map(p => Math.floor(p * remaining) + minVal)
let sum = parts.reduce((a, b) => a + b, 0)
parts[0] += total - sum
return parts
}
function generateMatrixWithLoss(rowSums, colSums) {
let matrix = Array(rowSums.length).fill(0).map(() => Array(colSums.length).fill(0));
let supply = [...rowSums];
let demand = [...colSums];
// Cap supply to create loss
let caps = supply.map(s => Math.floor(s * (0.80 + Math.random() * 0.15))); // 5-20% loss
let totalCapped = caps.reduce((a,b) => a+b, 0);
let totalDemand = demand.reduce((a,b) => a+b, 0);
if (totalCapped < totalDemand) {
let ratio = totalDemand / totalCapped;
caps = caps.map(c => Math.ceil(c * ratio * 1.01));
let matrix = Array(rowSums.length)
.fill(0)
.map(() => Array(colSums.length).fill(0))
let supply = [...rowSums]
let demand = [...colSums]
// Cap supply to create 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 totalDemand = demand.reduce((a, b) => a + b, 0)
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 = [];
for(let i=0; i<rowSums.length; i++) {
for(let j=0; j<colSums.length; j++) {
cells.push([i, j]);
}
}
cells = shuffle(cells);
let effectiveSupply = [...caps];
for(let [i, j] of cells) {
if(effectiveSupply[i] <= 0 || demand[j] <= 0) continue;
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;
}
cells = shuffle(cells)
let effectiveSupply = [...caps]
for (let [i, j] of cells) {
if (effectiveSupply[i] <= 0 || demand[j] <= 0) continue
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
}
for(let i=0; i<rowSums.length; i++) {
for(let j=0; j<colSums.length; j++) {
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;
}
}
matrix[i][j] += amount
effectiveSupply[i] -= amount
demand[j] -= amount
}
for (let i = 0; i < rowSums.length; i++) {
for (let j = 0; j < colSums.length; j++) {
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 ---
function generateGraphData(startValue, minValue) {
let nodes = [];
let links = [];
const minLayerTotal = ITEM_COUNT * minValue;
// Start Node
nodes.push({name: "$启动APP", depth: 0, itemStyle: {color: COLOR_MAP['$启动APP']}, value: startValue});
let currentLayerNodes = [{name: "$启动APP", value: startValue}];
let currentLayerTotal = startValue;
// Layers 1 to 5
for(let d=1; d<=5; d++) {
let currentDepth = (d-1) * DEPTH_SCALE;
if (d===1) currentDepth = 0;
let nextDepth = d * DEPTH_SCALE;
// Calculate Next Layer Total
let retention = 0.85;
let nextLayerTotal = Math.floor(currentLayerTotal * retention);
if (nextLayerTotal < minLayerTotal) nextLayerTotal = minLayerTotal;
// Generate Real Nodes
let parts = generateDist(nextLayerTotal, ITEM_COUNT, minValue);
parts.sort((a,b) => b-a); // Descending
let eventSubset = shuffle([...EVENT_POOL]).slice(0, 6);
let nextLayerNodes = [];
// Events
for(let i=0; i<6; i++) {
let eventName = eventSubset[i];
nextLayerNodes.push({
name: `${eventName} (${d})`,
depth: nextDepth,
value: parts[i+1],
itemStyle: { color: COLOR_MAP[eventName] || '#000' }
});
}
let nodes = []
let links = []
const minLayerTotal = ITEM_COUNT * minValue
// Start Node
nodes.push({ name: '$启动APP', depth: 0, itemStyle: { color: COLOR_MAP['$启动APP'] }, value: startValue })
let currentLayerNodes = [{ name: '$启动APP', value: startValue }]
let currentLayerTotal = startValue
// Layers 1 to 5
for (let d = 1; d <= 5; d++) {
let currentDepth = (d - 1) * DEPTH_SCALE
if (d === 1) currentDepth = 0
let nextDepth = d * DEPTH_SCALE
// Calculate Next Layer Total
let retention = 0.85
let nextLayerTotal = Math.floor(currentLayerTotal * retention)
if (nextLayerTotal < minLayerTotal) nextLayerTotal = minLayerTotal
// Generate Real Nodes
let parts = generateDist(nextLayerTotal, ITEM_COUNT, minValue)
parts.sort((a, b) => b - a) // Descending
let eventSubset = shuffle([...EVENT_POOL]).slice(0, 6)
let nextLayerNodes = []
// Events
for (let i = 0; i < 6; i++) {
let eventName = eventSubset[i]
nextLayerNodes.push({
name: `${eventName} (${d})`,
depth: nextDepth,
value: parts[i + 1],
itemStyle: { color: COLOR_MAP[eventName] || '#000' }
})
}
// More
nextLayerNodes.push({
name: `更多 (${d})`,
depth: nextDepth,
value: parts[0],
itemStyle: { color: COLOR_MAP['更多'] }
});
nextLayerNodes.forEach(n => nodes.push(n));
// Links
let sourceValues = currentLayerNodes.map(n => n.value);
let targetValues = nextLayerNodes.map(n => n.value);
let linkMatrix = generateMatrixWithLoss(sourceValues, targetValues);
for(let i=0; i<sourceValues.length; i++) {
let sentTotal = 0;
for(let j=0; j<targetValues.length; j++) {
let val = linkMatrix[i][j];
if(val > 0) {
links.push({
source: currentLayerNodes[i].name,
target: nextLayerNodes[j].name,
value: 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 }
});
}
// More
nextLayerNodes.push({
name: `更多 (${d})`,
depth: nextDepth,
value: parts[0],
itemStyle: { color: COLOR_MAP['更多'] }
})
nextLayerNodes.forEach(n => nodes.push(n))
// Links
let sourceValues = currentLayerNodes.map(n => n.value)
let targetValues = nextLayerNodes.map(n => n.value)
let linkMatrix = generateMatrixWithLoss(sourceValues, targetValues)
for (let i = 0; i < sourceValues.length; i++) {
let sentTotal = 0
for (let j = 0; j < targetValues.length; j++) {
let val = linkMatrix[i][j]
if (val > 0) {
links.push({
source: currentLayerNodes[i].name,
target: nextLayerNodes[j].name,
value: val
})
sentTotal += val
}
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
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 = nextLayerNodes
currentLayerTotal = nextLayerTotal
}
// End Node
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 => {
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 };
return { nodes, links }
}
// --- 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!