每天坐地铁,经常看地铁图,有一天突然想到,地铁图不也是一种拓扑结果吗?TWaver到底能与地铁图擦出怎样的火花呢?
 
我们就以上面这幅地铁图为模版来进行制作。
{
	"stations":{
		"l01s01":{ },
		…………
	}
	"lines":{
		"l01":{……},
		…………
	}
	"sundrys":{
		"railwaystationshanghai":{……},
		…………
	}
}
"l01s01":{
	"id":"l01s01",
	"name":"莘庄",
	"loc":{"x":419,"y":1330},
	"label":"bottomright.bottomright",
},
…………
"l01":{
	"id":"l01",
	"name":"1号线",
	"color":"#e52035",
	"stations":{
		"l01s01":"l01s01",
		"l01s02":"l01s02",
		……
	}
},
……
"airporthongqiao":{
	"sign":"airport",
	"station":"l02s20",
	"name":"虹桥国际机场",
	"offset":{"x":0, "y":-1}
},
……
function loadJSON(path,callback){
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function(){
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
               dataJson = JSON.parse(xhr.responseText);
               callback && callback();
           }
       }
   };
   xhr.open("GET", path, true);
   xhr.send();
}
因为读取文件是一个异步的过程,所以要程序的展开都要放在文件读取函数的内部。
function init(){
    loadJSON("shanghaiMetro.json", function(){
        initNetwork(dataJson);
        initNode(dataJson);
    });
}
for(staId in json.stations){
    var station = json.stations[staId];
    staNode = new twaver.Node({
        id: staId,
        name: station.name,
        image:‘station‘,
    });
    staNode.s(‘label.color‘,‘rgba(99,99,99,1)‘);
    staNode.s(‘label.font‘,‘12px 微软雅黑‘);
    staNode.s(‘label.position‘,station.label);
    staNode.setClient(‘location‘,station.loc);
    box.add(staNode);
}
 
for(lineId in json.lines) {
    ……
    for(staSn in line.stations) {
        ……
        var link = new twaver.Link(linkId,prevSta,staNode);
        link.s(‘link.color‘, line.color);
        link.s(‘link.width‘, linkWidth);
        link.setToolTip(line.name);
        box.add(link);
    }
}


 

 
var setTrunType = function(json){
    box.forEach(function (ele) {
        var id = ele.getId();
        if(ele instanceof twaver.Link){
            var link = ele;
            var f = link.getFromNode().getCenterLocation();
            var t = link.getToNode().getCenterLocation();
            if(needAddPoint(f, t)){
                var so=0, os=0;
                if(link.getClient(‘prevLink‘)){
                    so += byPrevPoint(f,t,link).so;
                    os += byPrevPoint(f,t,link).os;
                }
                if(link.getClient(‘nextLink‘)){
                    os += byNextPoint(f,t,link).os;
                    so += byNextPoint(f,t,link).so;
                }
                p = os>so ? obliqueStraight(f, t) : straightOblique(f, t);
                link.setClient(‘point‘, p);
                link.setClient(‘truntype‘, os>so?‘os‘:‘so‘);
            }
        }
    });
}

var createTurnSta = function(line, staSn){
    staTurn = new twaver.Node(staSn);
    staTurn.setImage();
    staTurn.setClient(‘lineColor‘,line.color);
    staTurn.setClient(‘lines‘,[line.id]);
    var loc = line.stations[staSn];
    staTurn.setClient(‘location‘,loc);
    box.add(staTurn);
    return staTurn;
}

var createFollowSta = function(json, line, staNode, staId){
    staFollow = new twaver.Follower(staId);
    staFollow.setImage();
    staFollow.setClient(‘lineColor‘,line.color);
    staFollow.setClient(‘lines‘,[line.id]);
    staFollow.setHost(staNode);
    var az = azimuth[staId.substr(6,2)];
    var loc0 = json.stations[staId.substr(0,6)].loc;
    var loc = {x:loc0.x+az.x, y:loc0.y+az.y};
    staFollow.setClient(‘location‘,loc);
    box.add(staFollow);
    return staFollow;
}

var azimuth = {
    bb: {x: 0, y: linkWidth*zoom/2},
    tt: {x: 0, y: -linkWidth*zoom/2},
    rr: {x: linkWidth*zoom/2, y: 0},
    ll: {x: -linkWidth/2, y: 0},
    br: {x: linkWidth*zoom*0.7/2, y: linkWidth*zoom*0.7/2},
    bl: {x: -linkWidth*zoom*0.7/2, y: linkWidth*zoom*0.7/2},
    tr: {x: linkWidth*zoom*0.7/2, y: -linkWidth*zoom*0.7/2},
    tl: {x: -linkWidth*zoom*0.7/2, y: -linkWidth*zoom*0.7/2},
    BB: {x: 0, y: linkWidth*zoom},
    TT: {x: 0, y: -linkWidth*zoom},
    RR: {x: linkWidth*zoom, y: 0},
    LL: {x: -linkWidth, y: 0},
    BR: {x: linkWidth*zoom*0.7, y: linkWidth*zoom*0.7},
    BL: {x: -linkWidth*zoom*0.7, y: linkWidth*zoom*0.7},
    TR: {x: linkWidth*zoom*0.7, y: -linkWidth*zoom*0.7},
    TL: {x: -linkWidth*zoom*0.7, y: -linkWidth*zoom*0.7}
};
twaver.Util.registerImage(‘station‘,{
    w: linkWidth*1.6,
    h: linkWidth*1.6,
    v: function (data, view) {
        var result = [];
        if(data.getClient(‘focus‘)){
            result.push({
                shape: ‘circle‘,
                r: linkWidth*0.7,
                lineColor:  data.getClient(‘lineColor‘),
                lineWidth: linkWidth*0.2,
                fill: ‘white‘,
            });
            result.push({
                shape: ‘circle‘,
                r: linkWidth*0.2,
                fill:  data.getClient(‘lineColor‘),
            });
        }else{
            result.push({
                shape: ‘circle‘,
                r: linkWidth*0.6,
                lineColor: data.getClient(‘lineColor‘),
                lineWidth: linkWidth*0.2,
                fill: ‘white‘,
            });
        }
        return result;
    }
});

twaver.Util.registerImage(‘rotateArrow‘, {
    w: 124,
    h: 124,
    v: [{
        shape: ‘vector‘,
        name: ‘doubleArrow‘,
        rotate: 360,
        animate: [{
            attr: ‘rotate‘,
            to: 0,
            dur: 2000,
            reverse: false,
            repeat: Number.POSITIVE_INFINITY
        }]
    }]
});
 
  

network.setZoomManager(new twaver.vector.MixedZoomManager(network));
network.setMinZoom(0.2);
network.setMaxZoom(3);
network.setZoomVisibilityThresholds({
    label : 0.6,
});



network.addInteractionListener(function(e){
   if(mapDiv){
        mapDiv.style.display = ‘none‘;
        mapDiv = null;
        dbclickSta = null;
    }
    if(e.kind == ‘doubleClickElement‘ && e.element && e.element.getClassName() == ‘twaver.Node‘ && e.element.getId().length == 6){
        dbclickSta = e.element;
        if(dbclickSta.getClient(‘coord‘)){
            coord = dbclickSta.getClient(‘coord‘);
            mapDiv = createMap(coord, e.event);
        }else{
            dbclickSta.setClient(‘dbclick‘, true);
            var lineName = json.lines[dbclickSta.getId().substr(0,3)].name;
            var stationName = dbclickSta.getName();
            var addr = "上海市地铁" + lineName + stationName;
            var geocoder = new qq.maps.Geocoder();
            geocoder.getLocation(addr);
            geocoder.setComplete(function(result) {
                coord =  result.detail.location;
                mapDiv = createMap(coord, e.event);
                dbclickSta.setClient(‘dbclick‘, false);
            });
            geocoder.setError(function() {
                var coord = {"lat":31.188,"lng":121.425};
                mapDiv = createMap(coord, e.event);
            });
        }
    }
});

原文:http://www.cnblogs.com/xiaor2/p/5940348.html