parallel-nutrients.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <script src="lib/esl.js"></script>
  6. <script src="lib/config.js"></script>
  7. <script src="lib/jquery.min.js"></script>
  8. <meta name="viewport" content="width=device-width, initial-scale=1" />
  9. <link rel="stylesheet" href="lib/reset.css">
  10. </head>
  11. <body>
  12. <style>
  13. body {
  14. background: #333;
  15. }
  16. #main {
  17. height: 1000px;
  18. }
  19. </style>
  20. <div id="main"></div>
  21. <script>
  22. var echarts;
  23. var colorTool;
  24. var chart;
  25. var myChart;
  26. var groupCategories = [];
  27. var groupColors = [];
  28. require([
  29. 'echarts'
  30. // 'zrender/tool/color',
  31. // 'echarts/chart/parallel',
  32. // 'echarts/component/legend',
  33. // 'echarts/component/tooltip',
  34. // 'echarts/component/toolbox',
  35. // 'echarts/component/visualMap'
  36. ], function (ec) {
  37. $.getJSON('./data/nutrients.json', function (data) {
  38. echarts = ec;
  39. colorTool = echarts.color;
  40. normalizeData(data);
  41. chart = myChart = echarts.init(document.getElementById('main'));
  42. console.time('render');
  43. chart.setOption(getOption(data));
  44. console.timeEnd('render');
  45. chart.on('axisAreaSelected', function (event) {
  46. // var indices = chart.getModel().getSeries()[0].getRawIndicesByActiveState('active');
  47. // console.log('北京: ', indices);
  48. });
  49. });
  50. var indices = {
  51. name: 0,
  52. group: 1,
  53. id: 16
  54. };
  55. var schema = [
  56. {name: 'name', index: 0},
  57. {name: 'group', index: 1},
  58. {name: 'protein', index: 2},
  59. {name: 'calcium', index: 3},
  60. {name: 'sodium', index: 4},
  61. {name: 'fiber', index: 5},
  62. {name: 'vitaminc', index: 6},
  63. {name: 'potassium', index: 7},
  64. {name: 'carbohydrate', index: 8},
  65. {name: 'sugars', index: 9},
  66. {name: 'fat', index: 10},
  67. {name: 'water', index: 11},
  68. {name: 'calories', index: 12},
  69. {name: 'saturated', index: 13},
  70. {name: 'monounsat', index: 14},
  71. {name: 'polyunsat', index: 15},
  72. {name: 'id', index: 16}
  73. ];
  74. function normalizeData(originData) {
  75. var groupMap = {};
  76. originData.forEach(row => {
  77. var groupName = row[indices.group];
  78. if (!groupMap.hasOwnProperty(groupName)) {
  79. groupMap[groupName] = 1;
  80. }
  81. });
  82. originData.forEach(row => {
  83. row.forEach((item, index) => {
  84. if (index !== indices.name
  85. && index !== indices.group
  86. && index !== indices.id
  87. ) {
  88. // Convert null to zero, as all of them under unit "g".
  89. row[index] = parseFloat(item) || 0;
  90. }
  91. });
  92. });
  93. for (var groupName in groupMap) {
  94. if (groupMap.hasOwnProperty(groupName)) {
  95. groupCategories.push(groupName);
  96. }
  97. }
  98. var hStep = Math.round(300 / (groupCategories.length - 1));
  99. for (var i = 0; i < groupCategories.length; i++) {
  100. groupColors.push(colorTool.modifyHSL('#5A94DF', hStep * i));
  101. }
  102. }
  103. function getOption(data) {
  104. var lineStyle = {
  105. normal: {
  106. width: 0.5,
  107. opacity: 0.05
  108. // shadowBlur: 10,
  109. // shadowOffsetX: 0,
  110. // shadowOffsetY: 0,
  111. // shadowColor: 'rgba(0, 0, 0, 0.5)'
  112. }
  113. };
  114. return {
  115. backgroundColor: '#333',
  116. tooltip: {
  117. padding: 10,
  118. backgroundColor: '#222',
  119. borderColor: '#777',
  120. borderWidth: 1,
  121. formatter: function (obj) {
  122. var value = obj[0].value;
  123. return '<div style="border-bottom: 1px solid rgba(255,255,255,.3); font-size: 18px;padding-bottom: 7px;margin-bottom: 7px">'
  124. + schema[1].name + ':' + value[1] + '<br>'
  125. + schema[2].name + ':' + value[2] + '<br>'
  126. + schema[3].name + ':' + value[3] + '<br>'
  127. + schema[4].name + ':' + value[4] + '<br>'
  128. + schema[5].name + ':' + value[5] + '<br>'
  129. + schema[6].name + ':' + value[6] + '<br>';
  130. }
  131. },
  132. title: [
  133. {
  134. text: 'Groups',
  135. top: 0,
  136. left: 0,
  137. textStyle: {
  138. color: '#fff'
  139. }
  140. }
  141. ],
  142. visualMap: {
  143. show: true,
  144. type: 'piecewise',
  145. categories: groupCategories,
  146. dimension: indices.group,
  147. inRange: {
  148. color: groupColors //['#d94e5d','#eac736','#50a3ba']
  149. },
  150. outOfRange: {
  151. color: ['#ccc'] //['#d94e5d','#eac736','#50a3ba']
  152. },
  153. top: 20,
  154. textStyle: {
  155. color: '#fff'
  156. },
  157. realtime: false
  158. },
  159. parallelAxis: [
  160. {dim: 16, name: schema[16].name, scale: true, nameLocation: 'end'},
  161. {dim: 2, name: schema[2].name, nameLocation: 'end'},
  162. {dim: 4, name: schema[4].name, nameLocation: 'end'},
  163. {dim: 3, name: schema[3].name, nameLocation: 'end'},
  164. {dim: 5, name: schema[5].name, nameLocation: 'end'},
  165. {dim: 6, name: schema[6].name, nameLocation: 'end'},
  166. {dim: 7, name: schema[7].name, nameLocation: 'end'},
  167. {dim: 8, name: schema[8].name, nameLocation: 'end'},
  168. {dim: 9, name: schema[9].name, nameLocation: 'end'},
  169. {dim: 10, name: schema[10].name, nameLocation: 'end'},
  170. {dim: 11, name: schema[11].name, nameLocation: 'end'},
  171. {dim: 12, name: schema[12].name, nameLocation: 'end'},
  172. {dim: 13, name: schema[13].name, nameLocation: 'end'},
  173. {dim: 14, name: schema[14].name, nameLocation: 'end'},
  174. {dim: 15, name: schema[15].name, nameLocation: 'end'}
  175. ],
  176. parallel: {
  177. left: 280,
  178. top: 20,
  179. // top: 150,
  180. // height: 300,
  181. width: 400,
  182. layout: 'vertical',
  183. parallelAxisDefault: {
  184. type: 'value',
  185. name: 'nutrients',
  186. nameLocation: 'end',
  187. nameGap: 20,
  188. nameTextStyle: {
  189. color: '#fff',
  190. fontSize: 14
  191. },
  192. axisLine: {
  193. lineStyle: {
  194. color: '#aaa'
  195. }
  196. },
  197. axisTick: {
  198. lineStyle: {
  199. color: '#777'
  200. }
  201. },
  202. splitLine: {
  203. show: false
  204. },
  205. axisLabel: {
  206. textStyle: {
  207. color: '#fff'
  208. }
  209. },
  210. realtime: false
  211. }
  212. },
  213. animation: false,
  214. series: [
  215. {
  216. name: 'nutrients',
  217. type: 'parallel',
  218. lineStyle: lineStyle,
  219. inactiveOpacity: 0,
  220. activeOpacity: 0.01,
  221. progressive: 500,
  222. smooth: true,
  223. data: data
  224. }
  225. ]
  226. };
  227. }
  228. });
  229. </script>
  230. </body>
  231. </html>