metamaps--metamaps/frontend/src/Metamaps/Organize.js

119 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-09-22 15:51:33 +00:00
import _ from 'lodash'
import $jit from '../patched/JIT'
import Visualize from './Visualize'
import JIT from './JIT'
2016-04-15 00:28:28 +00:00
const Organize = {
2016-04-15 00:28:28 +00:00
arrange: function (layout, centerNode) {
// first option for layout to implement is 'grid', will do an evenly spaced grid with its center at the 0,0 origin
if (layout == 'grid') {
var numNodes = _.size(Visualize.mGraph.graph.nodes); // this will always be an integer, the # of nodes on your graph visualization
2016-04-15 00:28:28 +00:00
var numColumns = Math.floor(Math.sqrt(numNodes)) // the number of columns to make an even grid
var GRIDSPACE = 400
var row = 0
var column = 0
Visualize.mGraph.graph.eachNode(function (n) {
2016-04-15 00:28:28 +00:00
if (column == numColumns) {
column = 0
row += 1
}
var newPos = new $jit.Complex()
newPos.x = column * GRIDSPACE
newPos.y = row * GRIDSPACE
n.setPos(newPos, 'end')
column += 1
})
Visualize.mGraph.animate(JIT.ForceDirected.animateSavedLayout)
2016-04-15 00:28:28 +00:00
} else if (layout == 'grid_full') {
// this will always be an integer, the # of nodes on your graph visualization
var numNodes = _.size(Visualize.mGraph.graph.nodes)
2016-04-15 00:28:28 +00:00
// var numColumns = Math.floor(Math.sqrt(numNodes)) // the number of columns to make an even grid
// var GRIDSPACE = 400
var height = Visualize.mGraph.canvas.getSize(0).height
var width = Visualize.mGraph.canvas.getSize(0).width
2016-04-15 00:28:28 +00:00
var totalArea = height * width
var cellArea = totalArea / numNodes
var ratio = height / width
var cellWidth = sqrt(cellArea / ratio)
var cellHeight = cellArea / cellWidth
var row = floor(height / cellHeight)
var column = floor(width / cellWidth)
var totalCells = row * column
if (totalCells) {
Visualize.mGraph.graph.eachNode(function (n) {
2016-04-15 00:28:28 +00:00
if (column == numColumns) {
column = 0
row += 1
}
var newPos = new $jit.Complex()
newPos.x = column * GRIDSPACE
newPos.y = row * GRIDSPACE
n.setPos(newPos, 'end')
column += 1
})
}
Visualize.mGraph.animate(JIT.ForceDirected.animateSavedLayout)
2016-04-15 00:28:28 +00:00
} else if (layout == 'radial') {
var centerX = centerNode.getPos().x
var centerY = centerNode.getPos().y
centerNode.setPos(centerNode.getPos(), 'end')
console.log(centerNode.adjacencies)
var lineLength = 200
var usedNodes = {}
usedNodes[centerNode.id] = centerNode
var radial = function (node, level, degree) {
if (level == 1) {
var numLinksTemp = _.size(node.adjacencies)
var angleTemp = 2 * Math.PI / numLinksTemp
} else {
angleTemp = 2 * Math.PI / 20
}
node.eachAdjacency(function (a) {
var isSecondLevelNode = (centerNode.adjacencies[a.nodeTo.id] != undefined && level > 1)
if (usedNodes[a.nodeTo.id] == undefined && !isSecondLevelNode) {
var newPos = new $jit.Complex()
newPos.x = level * lineLength * Math.sin(degree) + centerX
newPos.y = level * lineLength * Math.cos(degree) + centerY
a.nodeTo.setPos(newPos, 'end')
usedNodes[a.nodeTo.id] = a.nodeTo
radial(a.nodeTo, level + 1, degree)
degree += angleTemp
}
})
}
radial(centerNode, 1, 0)
Visualize.mGraph.animate(JIT.ForceDirected.animateSavedLayout)
2016-04-15 00:28:28 +00:00
} else if (layout == 'center_viewport') {
var lowX = 0,
lowY = 0,
highX = 0,
highY = 0
var oldOriginX = Visualize.mGraph.canvas.translateOffsetX
var oldOriginY = Visualize.mGraph.canvas.translateOffsetY
2016-04-15 00:28:28 +00:00
Visualize.mGraph.graph.eachNode(function (n) {
2016-04-15 00:28:28 +00:00
if (n.id === 1) {
lowX = n.getPos().x
lowY = n.getPos().y
highX = n.getPos().x
highY = n.getPos().y
}
if (n.getPos().x < lowX) lowX = n.getPos().x
if (n.getPos().y < lowY) lowY = n.getPos().y
if (n.getPos().x > highX) highX = n.getPos().x
if (n.getPos().y > highY) highY = n.getPos().y
})
console.log(lowX, lowY, highX, highY)
var newOriginX = (lowX + highX) / 2
var newOriginY = (lowY + highY) / 2
} else window.alert('please call function with a valid layout dammit!')
2016-04-15 00:28:28 +00:00
}
}
export default Organize