echarts 力导向分布图使用push给links赋值提示错误

_枫色 发布于 2016/05/11 20:48
阅读 376
收藏 0

由于需要多个动态的links数据,使用push方法将数据填充到数组中去,然而提示 Cannot read property 'push' of undefined,不知道为什么,明明上面的nodes使用了同样的方法都可以。

代码如下

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>EchartRadarTest</title> </head> <body> <div id="main" style="height: 700px;"></div> <script src="./js/dist/echarts.js"></script> <script type="text/javascript"> require.config({ paths: { echarts: './js/dist'  }
    }); require(
            [ 'echarts', 'echarts/chart/force', 'echarts/chart/chord',
            ], function (ec) { var myChart = ec.init(document.getElementById('main'),'shine'); var arr =[
                    [0,1,1,1],
                    [1,0,1,1],
                    [1,1,0,1],
                    [1,1,1,0]
                ]; var colorarr=[1,2,3,4]; var option = { title : { text: '人物关系:乔布斯', subtext: '数据来自人立方', x:'right', y:'bottom'  }, tooltip : { trigger: 'item', formatter: '{a} : {b}'  }, toolbox: { show : true, feature : { restore : {show: true}, magicType: {show: true, type: ['force', 'chord']}, saveAsImage : {show: true}
                        }
                    }, legend: { show:true, x: 'left', data:['1','2','3','4','5','6','7','8','9','10']
                    }, series : [
                        { type:'force', name : "", ribbonType: false, categories : [
                                {name: '1'},
                                {name: '2'},
                                {name:'3'},
                                {name:'4'},
                                {name:'5'},
                                {name:'6'},
                                {name:'7'},
                                {name:'8'},
                                {name:'9'},
                                {name:'10'},
                            ], itemStyle: { normal: { label: { show: true, textStyle: { color: '#333'  }
                                    }, nodeStyle : { brushType : 'both', borderColor : 'rgba(255,215,0,0.4)', borderWidth : 1  }, linkStyle: { type: 'curve'  }
                                }, emphasis: { label: { show: false  }, nodeStyle : {
                                    }, linkStyle : {}
                                }
                            }, useWorker: false, minRadius : 35, maxRadius : 55, gravity: 1.1, scaling: 1.1, roam: 'move', nodes:(function(){ var nodes = []; for(var i=0;i<colorarr.length;i++){ nodes.push(
                                                    {category:colorarr[i]-1,name:i+1}
                                            )
                                        } return nodes;
                            }
                            )(), // links : [ // {source : '1', target : '2'}, // {source : '2', target : '3'}, // {source : '3', target : '2'}, // {source : '2', target : '4'}, // ]   links : (function(){ var link = []; for(var j=0;j<arr.length/2;j++){ for(var k=0;k<arr.length;k++){ if(arr[j][k] == 1){ console.log(j+1,k+1) console.log("{source: %d ,target: %d}",j+1,k+1) link.push(
                                                    {source:j+1,target:k+1}
                                            )
                                        }
                                    }
                                } console.log(link) return link;
                            })()


                        }
                    ]

                }; myChart.setOption(option);
            }
    ); </script> </body> </html>


加载中
返回顶部
顶部