标记ParentNode - Protovis Treemap

时间:2011-10-13 20:33:20

标签: javascript treemap protovis

我正在尝试标记树形图的父节点,但到目前为止只能成功访问树形图的各个叶子。现在它确实打印了父节点的名称,但是我希望它以父节点为中心,而不是我现在拥有的单个叶子。

 json = {"sectors":{"Beer":{"Coors Light":77,"The Coors Light Brewing Company":61,"Coors Banquet":31,"Miller Lite":30},"Media":{"Disney Movies":91,"Disney":90,"CBSSports.com":87,"FOX Broadcasting":85,"ABC News":85,"Netflix":74,"Fox News":70,"CBS Home Entertainment":70,"FOX Sports":68,"ESPN":68,"DISH Network":68,"ABC 20/20":64,"Walt Disney Animation Studios":62,"ABC Family":58,"XFINITY":56,"Disney Pixar":49,"SportsCenter":35,"DirecTV":28,"Disneyland":25,"CBS":17,"ABC Family\u2019s 25 Days of Christmas":16,"Walt Disney World":15,"CBS News":12,"Fox Searchlight Pictures":11,"20th Century Fox":5},"Delivery":{"USPS Stamps":96,"UPS":86,"FedEx":75,"U.S. Postal Service":46,"I am FedEx":20},"Finance":{"Chase Freedom":99,"Capital One":98,"Chase Community Giving":86,"Wells Fargo":72,"J.P. Morgan Corporate Challenge: Boston":70,"J.P. Morgan Community":69,"Citi":68,"Chase Blueprint":63,"E*TRADE Financial":50,"Building Opportunity from Bank of America":31,"Small Business Saturday":21,"J.P. Morgan Corporate Challenge":15,"Fidelity Investments":1},"Automotive":{"Cadillac":98,"Lincoln":93,"Toyota 4Runner":93,"Ford Focus":87,"Chevrolet":87,"Toyota Prius":81,"Honda Insight":81,"Ford Electric Vehicles":77,"Dodge":75,"VW Passat":74,"Honda CR-Z":73,"Toyota USA":72,"BMW USA":72,"Mercedes-Benz USA":70,"VW Beetle":70,"Ford Fiesta":69,"VW Eos":68,"Chevrolet Volt":68,"Toyota Sienna":66,"Team Chevy":63,"Dodge Viper":60,"Ford Motor Company":58,"Acura":56,"Dodge Challenger":53,"Infiniti":51,"Ford Explorer":51,"Nissan":51,"Mercedes-Benz":49,"Ford Taurus":43,"BMW":43,"VW Jetta":42,"Jeep":38,"Toyota Yaris":37,"GMC":36,"Ford Trucks":36,"Ford Mustang":35,"Mazda":34,"VW USA":33,"Lexus":32,"Honda Civic":30,"Subaru of America, Inc.":26,"Ford Fusion + Hybrid":22,"Kia Motors America":18,"Audi R8":16,"Ford Ranger":14,"Honda":14,"Chrysler":13,"VW CC":10,"VW Routan":9,"VW GTI":5,"Ram":4,"Buick":0},"Food":{"Dr Pepper":95,"Campbell's Kitchen":66,"Kraft Foods - Recipes and Tips":66,"Dannon Light & Fit":54,"Pepsi Max":53,"Diet Pepsi":45,"Cheerios":42,"Diet Coke":32,"Coca-Cola Zero":23,"Pepsi":21,"PepsiCo":21,"Yoplait":20,"Coca-Cola":11,"Yoplait Smoothie":9,"Gatorade":0,"Pillsbury Baking":0,"Pillsbury":0},"Other":{"Pella Windows and Doors":80,"Bowflex":69,"BP America":68,"Liberty Travel":67,"Sinceramente Hallmark":51,"Thank You, Mom by P&G":43,"AARP":42,"your GREETING CARD COMPETITION brought to you by Hallmark Cards":32,"Procter & Gamble":31,"Pro Plan":29,"Hallmark":23,"P&G Everyday Solutions":17,"Bridgestone Tires":16,"Purina ONE":16,"Southwest Airlines":4,"Purina Cat Chow":4,"H&R Block ":3},"Personal Care":{"Maybelline New York":58,"CoverGirl":53,"Axe Angels club":53,"Revlon":21,"AXE ":0},"Insurance":{"Travelers Insurance":87,"State Farm Insurance":82,"Careers at Allstate":79,"Mayhem":78,"Farmers Insurance":71,"Allstate Insurance":64,"GEICO Motorcycle":62,"GEICO RV":50,"GEICO Careers":48,"Progressive ":42,"Liberty Mutual":41,"Nationwide Insurance":41,"Esurance":26,"GEICO":22,"GEICO Caveman":21,"GEICO Kash":20,"The GEICO Gecko":16,"State Farm Careers":15,"Allstate Motorcycle":13,"State Farm Nation":5},"Apparel":{"Nike Sportswear":80,"Nike 6.0":47,"Nike Basketball":42,"Nike Football":38,"Nike":34,"Nike Golf":32,"Nike Running":26,"Nike Women":7,"Nike Baseball":6},"Cleaning":{"Swiffer":99,"Clorox":96,"Lysol":96,"Glade":79,"CloroxConnects":61,"Tide":55,"Febreze":38,"Clorox Clean-Up":5},"Telecom":{"Verizon FiOS":80,"Vonage":80,"T-Mobile USA":69,"Sprint":66,"Verizon Wireless Careers":37,"Verizon Wireless":5},"Education":{"University of Phoenix":4},"Restaurant":{"Taco Bell":90,"Burger King":88,"Wendy's":80,"Dunkin' Donuts":76,"Chili's Grill & Bar":70,"McDonald's":57,"Subway":46,"Pizza Hut":46,"Starbucks":27},"Weight Loss":{"Nutrisystem":34,"Weight Watchers":13},"Drug":{"Pfizer":58},"Retail":{"The Home Depot":90,"Best Buy":86,"Lowe's Home Improvement":81,"Best Buy Music":67,"Amazon.com":66,"Target Baby":62,"Walmart":60,"Best Buy Mobile":54,"Target Style":41,"Best Buy Movies":31,"Macy's":29,"Target":24,"The Scooter Store":19,"Safeway":5},"Electronics":{"BlackBerry":99,"Dell":97,"GE Appliances":96,"Microsoft Outlook":95,"IBM":94,"GE":92,"Microsoft OneNote":91,"Microsoft":90,"BlackBerry for Sprint":86,"Samsung Digital Imaging":85,"Time Warner":84,"Microsoft Tag":84,"Samsung Televisions":79,"Microsoft Visual Studio":72,"NBC Olympics":71,"Samsung Mobile USA":71,"Sony Electronics":66,"Samsung USA":65,"Samsung Service":63,"LG USA Mobile":61,"Sony PlayStation":60,"LG Electronics USA":58,"Microsoft Publisher":57,"NBC":57,"Microsoft Office":56,"Samsung Galaxy Tab":56,"Microsoft PowerPoint":54,"Time Warner Cable":50,"Samsung Notebook":48,"Dell Spot":47,"Microsoft Web Platform":46,"Canon":45,"Samsung Apps":44,"Dell Go Green":36,"Samsung Galaxy S":32,"Microsoft Access":32,"Microsoft Visio":26,"Microsoft Excel":25,"BlackBerry for AT&T":24,"IBM Research":23,"LG-Ericsson USA":20,"Sony":20,"Microsoft Word":17,"Microsoft Learning":14,"Microsoft Windows Server":11,"Microsoft Lync":11,"Dell Deals":7,"HP":2,"Dell Outlet":0}},"ids":{"Coors Light":300005,"The Coors Light Brewing Company":300007,"Coors Banquet":300006,"Miller Lite":280745,"Disney Movies":299960,"Disney":299957,"CBSSports.com":299977,"FOX Broadcasting":299966,"ABC News":299971,"Netflix":272975,"Fox News":299963,"CBS Home Entertainment":299976,"FOX Sports":299965,"ESPN":299968,"DISH Network":299955,"ABC 20/20":299973,"Walt Disney Animation Studios":299962,"ABC Family":299970,"XFINITY":299956,"Disney Pixar":299959,"SportsCenter":299969,"DirecTV":299954,"Disneyland":299958,"CBS":299974,"ABC Family\u2019s 25 Days of Christmas":299972,"Walt Disney World":299961,"CBS News":299975,"Fox Searchlight Pictures":299964,"20th Century Fox":299967,"USPS Stamps":300012,"UPS":300010,"FedEx":300008,"U.S. Postal Service":300011,"I am FedEx":300009,"Chase Freedom":296680,"Capital One":296679,"Chase Community Giving":296253,"Wells Fargo":296249,"J.P. Morgan Corporate Challenge: Boston":296678,"J.P. Morgan Community":296258,"Citi":296248,"Chase Blueprint":296257,"E*TRADE Financial":268920,"Building Opportunity from Bank of America":296254,"Small Business Saturday":299930,"J.P. Morgan Corporate Challenge":296677,"Fidelity Investments":296103,"Cadillac":235989,"Lincoln":235985,"Toyota 4Runner":263153,"Ford Focus":235416,"Chevrolet":235410,"Toyota Prius":263165,"Honda Insight":235343,"Ford Electric Vehicles":279420,"Dodge":235891,"VW Passat":235942,"Honda CR-Z":269590,"Toyota USA":235417,"BMW USA":234256,"Mercedes-Benz USA":269302,"VW Beetle":235911,"Ford Fiesta":235415,"VW Eos":235941,"Chevrolet Volt":235409,"Toyota Sienna":263154,"Team Chevy":300043,"Dodge Viper":252252,"Ford Motor Company":235411,"Acura":234261,"Dodge Challenger":252251,"Infiniti":234266,"Ford Explorer":279418,"Nissan":234267,"Mercedes-Benz":234258,"Ford Taurus":279419,"BMW":234251,"VW Jetta":235946,"Jeep":235892,"Toyota Yaris":235421,"GMC":235987,"Ford Trucks":235414,"Ford Mustang":235412,"Mazda":299866,"VW USA":235947,"Lexus":235267,"Honda Civic":235341,"Subaru of America, Inc.":235344,"Ford Fusion + Hybrid":263026,"Kia Motors America":235949,"Audi R8":235922,"Ford Ranger":279421,"Honda":235340,"Chrysler":235988,"VW CC":235945,"VW Routan":235944,"VW GTI":235943,"Ram":252250,"Buick":299865,"Dr Pepper":289234,"Campbell's Kitchen":293375,"Kraft Foods - Recipes and Tips":236532,"Dannon Light & Fit":255741,"Pepsi Max":280650,"Diet Pepsi":289233,"Cheerios":293370,"Diet Coke":289235,"Coca-Cola Zero":289238,"Pepsi":273002,"PepsiCo":280738,"Yoplait":293945,"Coca-Cola":289229,"Yoplait Smoothie":293946,"Gatorade":243071,"Pillsbury Baking":293372,"Pillsbury":293368,"Pella Windows and Doors":300037,"Bowflex":300032,"BP America":300030,"Liberty Travel":300036,"Sinceramente Hallmark":285603,"Thank You, Mom by P&G":300040,"AARP":300029,"your GREETING CARD COMPETITION brought to you by Hallmark Cards":285547,"Procter & Gamble":300038,"Pro Plan":300035,"Hallmark":285219,"P&G Everyday Solutions":300039,"Bridgestone Tires":300041,"Purina ONE":300033,"Southwest Airlines":300028,"Purina Cat Chow":300034,"H&R Block ":300031,"Maybelline New York":256088,"CoverGirl":256087,"Axe Angels club":242965,"Revlon":256093,"AXE ":242957,"Travelers Insurance":241056,"State Farm Insurance":237669,"Careers at Allstate":296694,"Mayhem":296695,"Farmers Insurance":270396,"Allstate Insurance":237667,"GEICO Motorcycle":290103,"GEICO RV":290099,"GEICO Careers":244667,"Progressive ":240858,"Liberty Mutual":240862,"Nationwide Insurance":237668,"Esurance":240861,"GEICO":237671,"GEICO Caveman":290100,"GEICO Kash":290102,"The GEICO Gecko":237672,"State Farm Careers":244668,"Allstate Motorcycle":296693,"State Farm Nation":277522,"Nike Sportswear":287251,"Nike 6.0":287249,"Nike Basketball":287254,"Nike Football":287252,"Nike":287248,"Nike Golf":259273,"Nike Running":287250,"Nike Women":260392,"Nike Baseball":287253,"Swiffer":300003,"Clorox":299998,"Lysol":300004,"Glade":300001,"CloroxConnects":300000,"Tide":299997,"Febreze":300002,"Clorox Clean-Up":299999,"Verizon FiOS":237830,"Vonage":293315,"T-Mobile USA":237832,"Sprint":237835,"Verizon Wireless Careers":237831,"Verizon Wireless":237829,"University of Phoenix":277367,"Taco Bell":293332,"Burger King":293330,"Wendy's":293943,"Dunkin' Donuts":279338,"Chili's Grill & Bar":299890,"McDonald's":293331,"Subway":273659,"Pizza Hut":293400,"Starbucks":277858,"Nutrisystem":270685,"Weight Watchers":263087,"Pfizer":257351,"The Home Depot":290087,"Best Buy":290089,"Lowe's Home Improvement":290088,"Best Buy Music":290090,"Amazon.com":293412,"Target Baby":290082,"Walmart":290085,"Best Buy Mobile":290091,"Target Style":290083,"Best Buy Movies":290093,"Macy's":290096,"Target":290081,"The Scooter Store":300054,"Safeway":296038,"BlackBerry":237821,"Dell":236264,"GE Appliances":288918,"Microsoft Outlook":277495,"IBM":292839,"GE":299950,"Microsoft OneNote":277493,"Microsoft":234136,"BlackBerry for Sprint":237884,"Samsung Digital Imaging":263317,"Time Warner":266908,"Microsoft Tag":277494,"Samsung Televisions":288921,"Microsoft Visual Studio":234168,"NBC Olympics":266564,"Samsung Mobile USA":294087,"Sony Electronics":263313,"Samsung USA":263316,"Samsung Service":288922,"LG USA Mobile":289167,"Sony PlayStation":236075,"LG Electronics USA":288915,"Microsoft Publisher":277505,"NBC":266563,"Microsoft Office":234169,"Samsung Galaxy Tab":294086,"Microsoft PowerPoint":277497,"Time Warner Cable":266909,"Samsung Notebook":289021,"Dell Spot":236265,"Microsoft Web Platform":277504,"Canon":299953,"Samsung Apps":289022,"Dell Go Green":236267,"Samsung Galaxy S":294883,"Microsoft Access":277500,"Microsoft Visio":277498,"Microsoft Excel":277506,"BlackBerry for AT&T":237824,"IBM Research":292838,"LG-Ericsson USA":288916,"Sony":263312,"Microsoft Word":277496,"Microsoft Learning":277503,"Microsoft Windows Server":277501,"Microsoft Lync":277502,"Dell Deals":236268,"HP":292840,"Dell Outlet":236270},"eranks":[{"sector":"Beer","score":0,"pages":[{"brand":"Coors Light","id":300005,"score":77},{"brand":"The Coors Light Brewing Company","id":300007,"score":61},{"brand":"Coors Banquet","id":300006,"score":31},{"brand":"Miller Lite","id":280745,"score":30}]},{"sector":"Media","score":0,"pages":[{"brand":"Disney Movies","id":299960,"score":91},{"brand":"Disney","id":299957,"score":90},{"brand":"CBSSports.com","id":299977,"score":87},{"brand":"FOX Broadcasting","id":299966,"score":85},{"brand":"ABC News","id":299971,"score":85},{"brand":"Netflix","id":272975,"score":74},{"brand":"Fox News","id":299963,"score":70},{"brand":"CBS Home Entertainment","id":299976,"score":70},{"brand":"FOX Sports","id":299965,"score":68},{"brand":"ESPN","id":299968,"score":68},{"brand":"DISH Network","id":299955,"score":68},{"brand":"ABC 20/20","id":299973,"score":64},{"brand":"Walt Disney Animation Studios","id":299962,"score":62},{"brand":"ABC Family","id":299970,"score":58},{"brand":"XFINITY","id":299956,"score":56},{"brand":"Disney Pixar","id":299959,"score":49},{"brand":"SportsCenter","id":299969,"score":35},{"brand":"DirecTV","id":299954,"score":28},{"brand":"Disneyland","id":299958,"score":25},{"brand":"CBS","id":299974,"score":17},{"brand":"ABC Family\u2019s 25 Days of Christmas","id":299972,"score":16},{"brand":"Walt Disney World","id":299961,"score":15},{"brand":"CBS News","id":299975,"score":12},{"brand":"Fox Searchlight Pictures","id":299964,"score":11},{"brand":"20th Century Fox","id":299967,"score":5}]},{"sector":"Delivery","score":0,"pages":[{"brand":"USPS Stamps","id":300012,"score":96},{"brand":"UPS","id":300010,"score":86},{"brand":"FedEx","id":300008,"score":75},{"brand":"U.S. Postal Service","id":300011,"score":46},{"brand":"I am FedEx","id":300009,"score":20}]},{"sector":"Finance","score":0,"pages":[{"brand":"Chase Freedom","id":296680,"score":99},{"brand":"Capital One","id":296679,"score":98},{"brand":"Chase Community Giving","id":296253,"score":86},{"brand":"Wells Fargo","id":296249,"score":72},{"brand":"J.P. Morgan Corporate Challenge: Boston","id":296678,"score":70},{"brand":"J.P. Morgan Community","id":296258,"score":69},{"brand":"Citi","id":296248,"score":68},{"brand":"Chase Blueprint","id":296257,"score":63},{"brand":"E*TRADE Financial","id":268920,"score":50},{"brand":"Building Opportunity from Bank of America","id":296254,"score":31},{"brand":"Small Business Saturday","id":299930,"score":21},{"brand":"J.P. Morgan Corporate Challenge","id":296677,"score":15},{"brand":"Fidelity Investments","id":296103,"score":1}]},{"sector":"Automotive","score":0,"pages":[{"brand":"Cadillac","id":235989,"score":98},{"brand":"Lincoln","id":235985,"score":93},{"brand":"Toyota 4Runner","id":263153,"score":93},{"brand":"Ford Focus","id":235416,"score":87},{"brand":"Chevrolet","id":235410,"score":87},{"brand":"Toyota Prius","id":263165,"score":81},{"brand":"Honda Insight","id":235343,"score":81},{"brand":"Ford Electric Vehicles","id":279420,"score":77},{"brand":"Dodge","id":235891,"score":75},{"brand":"VW Passat","id":235942,"score":74},{"brand":"Honda CR-Z","id":269590,"score":73},{"brand":"Toyota USA","id":235417,"score":72},{"brand":"BMW USA","id":234256,"score":72},{"brand":"Mercedes-Benz USA","id":269302,"score":70},{"brand":"VW Beetle","id":235911,"score":70},{"brand":"Ford Fiesta","id":235415,"score":69},{"brand":"VW Eos","id":235941,"score":68},{"brand":"Chevrolet Volt","id":235409,"score":68},{"brand":"Toyota Sienna","id":263154,"score":66},{"brand":"Team Chevy","id":300043,"score":63},{"brand":"Dodge Viper","id":252252,"score":60},{"brand":"Ford Motor Company","id":235411,"score":58},{"brand":"Acura","id":234261,"score":56},{"brand":"Dodge Challenger","id":252251,"score":53},{"brand":"Infiniti","id":234266,"score":51},{"brand":"Ford Explorer","id":279418,"score":51},{"brand":"Nissan","id":234267,"score":51},{"brand":"Mercedes-Benz","id":234258,"score":49},{"brand":"Ford Taurus","id":279419,"score":43},{"brand":"BMW","id":234251,"score":43},{"brand":"VW Jetta","id":235946,"score":42},{"brand":"Jeep","id":235892,"score":38},{"brand":"Toyota Yaris","id":235421,"score":37},{"brand":"GMC","id":235987,"score":36},{"brand":"Ford Trucks","id":235414,"score":36},{"brand":"Ford Mustang","id":235412,"score":35},{"brand":"Mazda","id":299866,"score":34},{"brand":"VW USA","id":235947,"score":33},{"brand":"Lexus","id":235267,"score":32},{"brand":"Honda Civic","id":235341,"score":30},{"brand":"Subaru of America, Inc.","id":235344,"score":26},{"brand":"Ford Fusion + Hybrid","id":263026,"score":22},{"brand":"Kia Motors America","id":235949,"score":18},{"brand":"Audi R8","id":235922,"score":16},{"brand":"Ford Ranger","id":279421,"score":14},{"brand":"Honda","id":235340,"score":14},{"brand":"Chrysler","id":235988,"score":13},{"brand":"VW CC","id":235945,"score":10},{"brand":"VW Routan","id":235944,"score":9},{"brand":"VW GTI","id":235943,"score":5},{"brand":"Ram","id":252250,"score":4},{"brand":"Buick","id":299865,"score":0}]},{"sector":"Food","score":0,"pages":[{"brand":"Dr Pepper","id":289234,"score":95},{"brand":"Campbell's Kitchen","id":293375,"score":66},{"brand":"Kraft Foods - Recipes and Tips","id":236532,"score":66},{"brand":"Dannon Light & Fit","id":255741,"score":54},{"brand":"Pepsi Max","id":280650,"score":53},{"brand":"Diet Pepsi","id":289233,"score":45},{"brand":"Cheerios","id":293370,"score":42},{"brand":"Diet Coke","id":289235,"score":32},{"brand":"Coca-Cola Zero","id":289238,"score":23},{"brand":"Pepsi","id":273002,"score":21},{"brand":"PepsiCo","id":280738,"score":21},{"brand":"Yoplait","id":293945,"score":20},{"brand":"Coca-Cola","id":289229,"score":11},{"brand":"Yoplait Smoothie","id":293946,"score":9},{"brand":"Gatorade","id":243071,"score":0},{"brand":"Pillsbury Baking","id":293372,"score":0},{"brand":"Pillsbury","id":293368,"score":0}]},{"sector":"Other","score":0,"pages":[{"brand":"Pella Windows and Doors","id":300037,"score":80},{"brand":"Bowflex","id":300032,"score":69},{"brand":"BP America","id":300030,"score":68},{"brand":"Liberty Travel","id":300036,"score":67},{"brand":"Sinceramente Hallmark","id":285603,"score":51},{"brand":"Thank You, Mom by P&G","id":300040,"score":43},{"brand":"AARP","id":300029,"score":42},{"brand":"your GREETING CARD COMPETITION brought to you by Hallmark Cards","id":285547,"score":32},{"brand":"Procter & Gamble","id":300038,"score":31},{"brand":"Pro Plan","id":300035,"score":29},{"brand":"Hallmark","id":285219,"score":23},{"brand":"P&G Everyday Solutions","id":300039,"score":17},{"brand":"Bridgestone Tires","id":300041,"score":16},{"brand":"Purina ONE","id":300033,"score":16},{"brand":"Southwest Airlines","id":300028,"score":4},{"brand":"Purina Cat Chow","id":300034,"score":4},{"brand":"H&R Block ","id":300031,"score":3}]},{"sector":"Personal Care","score":0,"pages":[{"brand":"Maybelline New York","id":256088,"score":58},{"brand":"CoverGirl","id":256087,"score":53},{"brand":"Axe Angels club","id":242965,"score":53},{"brand":"Revlon","id":256093,"score":21},{"brand":"AXE ","id":242957,"score":0}]},{"sector":"Insurance","score":0,"pages":[{"brand":"Travelers Insurance","id":241056,"score":87},{"brand":"State Farm Insurance","id":237669,"score":82},{"brand":"Careers at Allstate","id":296694,"score":79},{"brand":"Mayhem","id":296695,"score":78},{"brand":"Farmers Insurance","id":270396,"score":71},{"brand":"Allstate Insurance","id":237667,"score":64},{"brand":"GEICO Motorcycle","id":290103,"score":62},{"brand":"GEICO RV","id":290099,"score":50},{"brand":"GEICO Careers","id":244667,"score":48},{"brand":"Progressive ","id":240858,"score":42},{"brand":"Liberty Mutual","id":240862,"score":41},{"brand":"Nationwide Insurance","id":237668,"score":41},{"brand":"Esurance","id":240861,"score":26},{"brand":"GEICO","id":237671,"score":22},{"brand":"GEICO Caveman","id":290100,"score":21},{"brand":"GEICO Kash","id":290102,"score":20},{"brand":"The GEICO Gecko","id":237672,"score":16},{"brand":"State Farm Careers","id":244668,"score":15},{"brand":"Allstate Motorcycle","id":296693,"score":13},{"brand":"State Farm Nation","id":277522,"score":5}]},{"sector":"Apparel","score":0,"pages":[{"brand":"Nike Sportswear","id":287251,"score":80},{"brand":"Nike 6.0","id":287249,"score":47},{"brand":"Nike Basketball","id":287254,"score":42},{"brand":"Nike Football","id":287252,"score":38},{"brand":"Nike","id":287248,"score":34},{"brand":"Nike Golf","id":259273,"score":32},{"brand":"Nike Running","id":287250,"score":26},{"brand":"Nike Women","id":260392,"score":7},{"brand":"Nike Baseball","id":287253,"score":6}]},{"sector":"Cleaning","score":0,"pages":[{"brand":"Swiffer","id":300003,"score":99},{"brand":"Clorox","id":299998,"score":96},{"brand":"Lysol","id":300004,"score":96},{"brand":"Glade","id":300001,"score":79},{"brand":"CloroxConnects","id":300000,"score":61},{"brand":"Tide","id":299997,"score":55},{"brand":"Febreze","id":300002,"score":38},{"brand":"Clorox Clean-Up","id":299999,"score":5}]},{"sector":"Telecom","score":0,"pages":[{"brand":"Verizon FiOS","id":237830,"score":80},{"brand":"Vonage","id":293315,"score":80},{"brand":"T-Mobile USA","id":237832,"score":69},{"brand":"Sprint","id":237835,"score":66},{"brand":"Verizon Wireless Careers","id":237831,"score":37},{"brand":"Verizon Wireless","id":237829,"score":5}]},{"sector":"Education","score":0,"pages":[{"brand":"University of Phoenix","id":277367,"score":4}]},{"sector":"Restaurant","score":0,"pages":[{"brand":"Taco Bell","id":293332,"score":90},{"brand":"Burger King","id":293330,"score":88},{"brand":"Wendy's","id":293943,"score":80},{"brand":"Dunkin' Donuts","id":279338,"score":76},{"brand":"Chili's Grill & Bar","id":299890,"score":70},{"brand":"McDonald's","id":293331,"score":57},{"brand":"Subway","id":273659,"score":46},{"brand":"Pizza Hut","id":293400,"score":46},{"brand":"Starbucks","id":277858,"score":27}]},{"sector":"Weight Loss","score":0,"pages":[{"brand":"Nutrisystem","id":270685,"score":34},{"brand":"Weight Watchers","id":263087,"score":13}]},{"sector":"Drug","score":0,"pages":[{"brand":"Pfizer","id":257351,"score":58}]},{"sector":"Retail","score":0,"pages":[{"brand":"The Home Depot","id":290087,"score":90},{"brand":"Best Buy","id":290089,"score":86},{"brand":"Lowe's Home Improvement","id":290088,"score":81},{"brand":"Best Buy Music","id":290090,"score":67},{"brand":"Amazon.com","id":293412,"score":66},{"brand":"Target Baby","id":290082,"score":62},{"brand":"Walmart","id":290085,"score":60},{"brand":"Best Buy Mobile","id":290091,"score":54},{"brand":"Target Style","id":290083,"score":41},{"brand":"Best Buy Movies","id":290093,"score":31},{"brand":"Macy's","id":290096,"score":29},{"brand":"Target","id":290081,"score":24},{"brand":"The Scooter Store","id":300054,"score":19},{"brand":"Safeway","id":296038,"score":5}]},{"sector":"Electronics","score":0,"pages":[{"brand":"BlackBerry","id":237821,"score":99},{"brand":"Dell","id":236264,"score":97},{"brand":"GE Appliances","id":288918,"score":96},{"brand":"Microsoft Outlook","id":277495,"score":95},{"brand":"IBM","id":292839,"score":94},{"brand":"GE","id":299950,"score":92},{"brand":"Microsoft OneNote","id":277493,"score":91},{"brand":"Microsoft","id":234136,"score":90},{"brand":"BlackBerry for Sprint","id":237884,"score":86},{"brand":"Samsung Digital Imaging","id":263317,"score":85},{"brand":"Time Warner","id":266908,"score":84},{"brand":"Microsoft Tag","id":277494,"score":84},{"brand":"Samsung Televisions","id":288921,"score":79},{"brand":"Microsoft Visual Studio","id":234168,"score":72},{"brand":"NBC Olympics","id":266564,"score":71},{"brand":"Samsung Mobile USA","id":294087,"score":71},{"brand":"Sony Electronics","id":263313,"score":66},{"brand":"Samsung USA","id":263316,"score":65},{"brand":"Samsung Service","id":288922,"score":63},{"brand":"LG USA Mobile","id":289167,"score":61},{"brand":"Sony PlayStation","id":236075,"score":60},{"brand":"LG Electronics USA","id":288915,"score":58},{"brand":"Microsoft Publisher","id":277505,"score":57},{"brand":"NBC","id":266563,"score":57},{"brand":"Microsoft Office","id":234169,"score":56},{"brand":"Samsung Galaxy Tab","id":294086,"score":56},{"brand":"Microsoft PowerPoint","id":277497,"score":54},{"brand":"Time Warner Cable","id":266909,"score":50},{"brand":"Samsung Notebook","id":289021,"score":48},{"brand":"Dell Spot","id":236265,"score":47},{"brand":"Microsoft Web Platform","id":277504,"score":46},{"brand":"Canon","id":299953,"score":45},{"brand":"Samsung Apps","id":289022,"score":44},{"brand":"Dell Go Green","id":236267,"score":36},{"brand":"Samsung Galaxy S","id":294883,"score":32},{"brand":"Microsoft Access","id":277500,"score":32},{"brand":"Microsoft Visio","id":277498,"score":26},{"brand":"Microsoft Excel","id":277506,"score":25},{"brand":"BlackBerry for AT&T","id":237824,"score":24},{"brand":"IBM Research","id":292838,"score":23},{"brand":"LG-Ericsson USA","id":288916,"score":20},{"brand":"Sony","id":263312,"score":20},{"brand":"Microsoft Word","id":277496,"score":17},{"brand":"Microsoft Learning","id":277503,"score":14},{"brand":"Microsoft Windows Server","id":277501,"score":11},{"brand":"Microsoft Lync","id":277502,"score":11},{"brand":"Dell Deals","id":236268,"score":7},{"brand":"HP","id":292840,"score":2},{"brand":"Dell Outlet","id":236270,"score":0}]}]};

var tree = json.sectors;
var ids = json.ids;
arr=[];
sRank={};
function active(d, v) {
            d.active = v; 
            if (d.parentNode) {
                active(d.parentNode, v);
                }
}

// used for alpha
sRank={};
for(var i in json.eranks)
{
    var sector = json.eranks[i].sector;
    sRank[sector]=[];
    for(var p in json.eranks[i].pages){
        sRank[sector].push(json.eranks[i].pages[p].brand)
    }
}
        var nodes = pv.dom(tree).root("tree").nodes();
        color = pv.Colors.category20().by(function (d) { return d.parentNode.nodeName });


        var vis = new pv.Panel()
                 .width(660)
                 .height(900);

        var treemap = vis.add(pv.Layout.Treemap)
                 .nodes(nodes);



        treemap.leaf.add(pv.Panel)
                 .fillStyle(function (d) { return color(d).alpha(1-sRank[d.parentNode.nodeName].indexOf(d.nodeName)/sRank[d.parentNode.nodeName].length) })
                 .strokeStyle("#fff")
                 .lineWidth(1)
                 .antialias(false)
                 .cursor("pointer")                  
                 .title(function(d) { return d.nodeName})
                 .event("click", function (d) {alert(d.parentNode.nodeName); });


       treemap.leaf.add(pv.Label)
            .font("20px sans-serif")
            .textBaseline("bottom")
            .text(function(d) { middle=Math.round(sRank[d.parentNode.nodeName].length/2)-1; 
            if(sRank[d.parentNode.nodeName].indexOf(d.nodeName) == middle) 
                {return d.parentNode.nodeName}}
            );

        vis.render();

1 个答案:

答案 0 :(得分:1)

这里最好的选择是使用treemap.label,它会自动为每个节点居中。默认情况下,treemap.label的{​​{1}}属性设置如下:

.visible

仅显示叶节点的标签。但是,您可以在添加树形图后自己设置.visible(function(n) { return !n.firstChild; }) 属性,并将其更改为仅显示父节点(您必须确保它们也有父节点,或者您需要额外的,不需要的根节点的标签):

.visible

在您的示例中,您可以将其链接到您的标签,因此完整的代码段如下所示:

treemap.label
    .visible(function(d) { return d.firstChild && d.parentNode });

在这里工作jsFiddle:http://jsfiddle.net/nrabinowitz/T3fnT/1/