treemap-obama.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <script src="esl.js"></script>
  5. <script src="config.js"></script>
  6. <meta name="viewport" content="width=device-width, initial-scale=1" />
  7. </head>
  8. <body>
  9. <style>
  10. html, body {
  11. width: 100%;
  12. height: 100%;
  13. }
  14. html, body, #main {
  15. margin: 0;
  16. padding: 0;
  17. }
  18. #main {
  19. margin-top: 90px;
  20. position: absolute;
  21. left: 0;
  22. right: 0;
  23. bottom: 0;
  24. top: 0;
  25. }
  26. .controller {
  27. font-size: 14px;
  28. position: fixed;
  29. top: 0;
  30. left: 0;
  31. right: 0;
  32. background: #eee;
  33. border-bottom: 1px solid #ccc;
  34. line-height: 20px;
  35. z-index: 100;
  36. }
  37. .controller label {
  38. margin-right: 10px;
  39. }
  40. .item-title {
  41. font-weight: bold;
  42. }
  43. .controller .mode-title {
  44. float: left;
  45. width: 60px;
  46. vertical-align: middle;
  47. padding-left: 10px;
  48. }
  49. .controller .mode-body {
  50. float: left;
  51. width: 700px;
  52. }
  53. .controller .query {
  54. margin-left: 800px;
  55. padding-top: 10px;
  56. }
  57. .controller .query #query-input {
  58. width: 250px;
  59. }
  60. .tooltip-title {
  61. color: yellow;
  62. font-size: 16px;
  63. margin-bottom: 5px;
  64. }
  65. </style>
  66. <div class="controller">
  67. <div class="mode-title">示例模式<br>(Mode)</div>
  68. <div class="mode-body">
  69. <input type="radio" id="area-meaning-0" name="area-meaning" onclick="areaMeasureChange(0);" checked="checked"/>
  70. <label for="area-meaning-0">面积代表“2012年预算额” (Area represents '2012 Amount)</label><br>
  71. <input type="radio" id="area-meaning-2" name="area-meaning" onclick="areaMeasureChange(2);"/>
  72. <label for="area-meaning-2">面积代表“2012年预算额”,明暗代表“2011年相比的增长率”<br>&nbsp;&nbsp;&nbsp;&nbsp; (Area represents '2012 Amount' and color-alpha represents 'Change from 2011')</label><br>
  73. <input type="radio" id="area-meaning-1" name="area-meaning" onclick="areaMeasureChange(1);"/>
  74. <label for="area-meaning-1">面积代表“2011年预算额” (Area represents '2011 Amount')</label>
  75. </div>
  76. <div class="query">
  77. <input id="query-input" type="text" placeholder="请输入节点名 (Please node name)" onkeypress="if (event.keyCode === 13) { query(); return false; }"/>
  78. <input type="button" class="query-btn" value="检索节点(query)" onclick="query();" />
  79. </div>
  80. </div>
  81. <div id="main"></div>
  82. <script src="data/obama_budget_proposal_2012.tree.js"></script>
  83. <script>
  84. var chart;
  85. var formatUtil;
  86. var SERIES_NAME = 'Obama’s 2012 Budget Proposal: How $3.7 Trillion is Spent';
  87. require([
  88. 'echarts',
  89. 'echarts/util/format',
  90. 'echarts/component/legend',
  91. 'echarts/component/tooltip',
  92. 'echarts/chart/treemap'
  93. ], init);
  94. function areaMeasureChange(mode) {
  95. chart.setOption({
  96. tooltip: {
  97. formatter: getTooltipFormatter(mode)
  98. },
  99. series: [{
  100. visualDimension: mode === 2 ? 2 : null,
  101. data: buildData(mode, window.obama_budget_2012),
  102. levels: getLevelOption(mode)
  103. }]
  104. });
  105. }
  106. function query() {
  107. var nodeName = document.getElementById('query-input').value;
  108. // 先找精确匹配的
  109. var nodeIdList = findNodeId(nodeName, window.obama_budget_2012);
  110. // 再找模糊匹配的
  111. if (!nodeIdList.length) {
  112. nodeIdList = findNodeId(nodeName, window.obama_budget_2012, true);
  113. }
  114. if (nodeIdList.length) {
  115. // FIXME
  116. // 接口?
  117. chart.dispatchAction({
  118. type: 'treemapZoomToNode',
  119. seriesName: SERIES_NAME,
  120. // 这个示例中简单处理,只聚焦到找到的第一个节点上。
  121. targetNodeId: nodeIdList[0]
  122. });
  123. }
  124. else {
  125. alert('没有找到节点 (No result)');
  126. }
  127. }
  128. function findNodeId(nodeName, originList, fuzzy) {
  129. var out = [];
  130. nodeName = nodeName.toLowerCase();
  131. for (var i = 0, len = originList.length; i < len; i++) {
  132. var node = originList[i];
  133. if (node.name
  134. && (
  135. fuzzy
  136. ? node.name.toLowerCase().indexOf(nodeName) === 0
  137. : node.name.toLowerCase() === nodeName
  138. )
  139. ) {
  140. out.push(node.id);
  141. }
  142. if (node.children) {
  143. out.push.apply(out, findNodeId(nodeName, node.children, fuzzy));
  144. }
  145. }
  146. return out;
  147. }
  148. function buildData(mode, originList) {
  149. var out = [];
  150. for (var i = 0; i < originList.length; i++) {
  151. var node = originList[i];
  152. var newNode = out[i] = cloneNodeInfo(node);
  153. var value = newNode.value;
  154. if (!newNode) {
  155. continue;
  156. }
  157. // Calculate amount per household.
  158. value[3] = value[0] / window.household_america_2012;
  159. // if mode === 0 and mode === 2 do nothing
  160. if (mode === 1) {
  161. // Set 'Change from 2010' to value[0].
  162. var tmp = value[1];
  163. value[1] = value[0];
  164. value[0] = tmp;
  165. }
  166. if (node.children) {
  167. newNode.children = buildData(mode, node.children);
  168. }
  169. }
  170. return out;
  171. }
  172. function cloneNodeInfo(node) {
  173. if (!node) {
  174. return;
  175. }
  176. var newNode = {};
  177. newNode.name = node.name;
  178. newNode.id = node.id;
  179. newNode.discretion = node.discretion;
  180. newNode.value = (node.value || []).slice();
  181. return newNode;
  182. }
  183. function getLevelOption(mode) {
  184. return [
  185. {
  186. color: mode === 2
  187. ? [
  188. '#5793f3', '#d14a61', '#fd9c35',
  189. '#675bba', '#fec42c', '#dd4444',
  190. '#d4df5a', '#cd4870'
  191. ]
  192. : null,
  193. colorMappingBy: 'id',
  194. itemStyle: {
  195. normal: {
  196. borderWidth: 3,
  197. gapWidth: 3
  198. }
  199. }
  200. },
  201. {
  202. colorAlpha: mode === 2
  203. ? [0.5, 1] : null,
  204. itemStyle: {
  205. normal: {
  206. gapWidth: 1
  207. }
  208. }
  209. }
  210. ];
  211. }
  212. function isValidNumber(num) {
  213. return num != null && isFinite(num);
  214. }
  215. function getTooltipFormatter(mode) {
  216. var amountIndex = mode === 1 ? 1 : 0;
  217. var amountIndex2011 = mode === 1 ? 0 : 1;
  218. return function (info) {
  219. var value = info.value;
  220. var amount = value[amountIndex];
  221. amount = isValidNumber(amount)
  222. ? formatUtil.addCommas(amount * 1000) + '$'
  223. : '-';
  224. var amount2011 = value[amountIndex2011];
  225. amount2011 = isValidNumber(amount2011)
  226. ? formatUtil.addCommas(amount2011 * 1000) + '$'
  227. : '-';
  228. var perHousehold = value[3];
  229. perHousehold = isValidNumber(perHousehold)
  230. ? formatUtil.addCommas((+perHousehold.toFixed(4)) * 1000) + '$'
  231. : '-';
  232. var change = value[2];
  233. change = isValidNumber(change)
  234. ? change.toFixed(2) + '%'
  235. : '-';
  236. return [
  237. '<div class="tooltip-title">' + formatUtil.encodeHTML(info.name) + '</div>',
  238. '2012 Amount: &nbsp;&nbsp;' + amount + '<br>',
  239. 'Per Household: &nbsp;&nbsp;' + perHousehold + '<br>',
  240. '2011 Amount: &nbsp;&nbsp;' + amount2011 + '<br>',
  241. 'Change From 2011: &nbsp;&nbsp;' + change
  242. ].join('');
  243. }
  244. }
  245. function init(echarts, format) {
  246. formatUtil = format;
  247. chart = echarts.init(document.getElementById('main'), null, {
  248. renderer: 'canvas'
  249. });
  250. chart.setOption({
  251. legend: {
  252. data: [SERIES_NAME]
  253. },
  254. tooltip: {
  255. formatter: getTooltipFormatter(0)
  256. },
  257. series: [
  258. {
  259. name: SERIES_NAME,
  260. type: 'treemap',
  261. label: {
  262. show: true,
  263. formatter: "{b}",
  264. normal: {
  265. textStyle: {
  266. ellipsis: true
  267. }
  268. }
  269. },
  270. itemStyle: {
  271. normal: {
  272. borderColor: 'black'
  273. }
  274. },
  275. levels: getLevelOption(0),
  276. data: buildData('2012 Amount', window.obama_budget_2012)
  277. }
  278. ]
  279. });
  280. }
  281. </script>
  282. </body>
  283. </html>