BA_phone.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. $(document).ready(function (e) {
  2. GetSerialChart(jsonPerformance, '性能对比');
  3. // MakeChart(jsonPerformance);
  4. });
  5. var jsonPerformance =
  6. (function(){
  7. var arr=[];
  8. $.ajax({
  9. type : "post",
  10. async : false, //同步执行
  11. url : "bar.do",
  12. data : {},
  13. dataType : "json", //返回数据形式为json
  14. success : function(result) {
  15. if (result) {
  16. for(var i=0;i<result.length;i++){
  17. console.log(result[i].name);
  18. arr.push(result[i].name);
  19. }
  20. }
  21. },
  22. error : function(errorMsg) {
  23. alert("不好意思,图表请求数据失败啦!");
  24. myChart.hideLoading();
  25. }
  26. });
  27. console.log(arr);
  28. return arr[0]['value'];
  29. })()
  30. var jsonSales = [{
  31. "name": "小米9",
  32. "value": "60"
  33. },
  34. {
  35. "name": "魅族16th",
  36. "value": 540
  37. },
  38. {
  39. "name": "一加7T",
  40. "value": "30"
  41. },
  42. {
  43. "name": "华为mate30Pro",
  44. "value": "40"
  45. },
  46. {
  47. "name": "iPhone11",
  48. "value": "30"
  49. },
  50. {
  51. "name": "vivo NEX3",
  52. "value": "50"
  53. },
  54. {
  55. "name": "oppo A57",
  56. "value": "5"
  57. },
  58. {
  59. "name": "小米5",
  60. "value": "80"
  61. }
  62. ]
  63. //
  64. //var jsonPopular = [{
  65. // "name": "小米9",
  66. // "value": "80"
  67. // },
  68. // {
  69. // "name": "魅族16th",
  70. // "value": "70"
  71. // },
  72. // {
  73. // "name": "一加7T",
  74. // "value": "30"
  75. // },
  76. // {
  77. // "name": "华为mate30Pro",
  78. // "value": "40"
  79. // },
  80. // {
  81. // "name": "iPhone11",
  82. // "value": "80"
  83. // },
  84. // {
  85. // "name": "vivo NEX3",
  86. // "value": "38"
  87. // },
  88. // {
  89. // "name": "oppo A57",
  90. // "value": "2"
  91. // },
  92. // {
  93. // "name": "小米5",
  94. // "value": "100"
  95. // }
  96. //]
  97. function GetSerialChart(json, title) {
  98. chart = new AmCharts.AmSerialChart();
  99. chart.dataProvider = json;
  100. //json数据的key
  101. chart.categoryField = "name";
  102. //不选择
  103. chart.rotate = false;
  104. //值越大柱状图面积越大
  105. chart.depth3D = 20;
  106. //柱子旋转角度角度
  107. chart.angle = 30;
  108. var mCtCategoryAxis = chart.categoryAxis;
  109. mCtCategoryAxis.axisColor = "#efefef";
  110. //背景颜色透明度
  111. mCtCategoryAxis.fillAlpha = 0.5;
  112. //背景边框线透明度
  113. mCtCategoryAxis.gridAlpha = 0;
  114. mCtCategoryAxis.fillColor = "#efefef";
  115. var valueAxis = new AmCharts.ValueAxis();
  116. //左边刻度线颜色
  117. valueAxis.axisColor = "#ccc";
  118. //标题
  119. valueAxis.title = title;
  120. //刻度线透明度
  121. valueAxis.gridAlpha = 0.2;
  122. chart.addValueAxis(valueAxis);
  123. var graph = new AmCharts.AmGraph();
  124. graph.title = "value";
  125. graph.valueField = "value";
  126. graph.type = "column";
  127. //鼠标移入提示信息
  128. graph.balloonText = "测试数据[[name]] [[value]]";
  129. //边框透明度
  130. graph.lineAlpha = 0.3;
  131. //填充颜色
  132. graph.fillColors = "#b9121b";
  133. graph.fillAlphas = 1;
  134. chart.addGraph(graph);
  135. // CURSOR
  136. var chartCursor = new AmCharts.ChartCursor();
  137. chartCursor.cursorAlpha = 0;
  138. chartCursor.zoomable = false;
  139. chartCursor.categoryBalloonEnabled = false;
  140. chart.addChartCursor(chartCursor);
  141. chart.creditsPosition = "top-right";
  142. //显示在Main div中
  143. chart.write("cylindrical");
  144. }
  145. //控制统计表(传说中的增删)
  146. controlTongJi();
  147. function controlTongJi() {
  148. /**
  149. * 查找目标进行删除
  150. * @param {String} name 名字
  151. * @param {Object} json 哪个json对象?
  152. * @param {String} title 标题
  153. */
  154. function findKill(json, name) {
  155. json.forEach(function (item, i, self) {
  156. if (item.name == name) {
  157. console.log('kill', i);
  158. json.splice(i, 1);
  159. GetSerialChart(json); //更新信息
  160. }
  161. });
  162. }
  163. $('#ctrl button:nth-of-type(3)').on('click', function () {
  164. console.log('删除成功');
  165. var value = $(this).prev().val();
  166. findKill(jsonPerformance, value);
  167. findKill(jsonSales, value);
  168. findKill(jsonPopular, value);
  169. });
  170. //增加
  171. $('#ctrl button:nth-of-type(1)').on('click', function () {
  172. console.log('增加成功');
  173. var phoneName = $(this).prev().val();
  174. var newPhonePerformance = {
  175. 'name': phoneName,
  176. 'value': null,
  177. }
  178. var newPhoneSales = {
  179. 'name': phoneName,
  180. 'value': null,
  181. }
  182. var newPhonePopular = {
  183. 'name': phoneName,
  184. 'value': null,
  185. }
  186. jsonPerformance.push(newPhonePerformance);
  187. jsonSales.push(newPhoneSales);
  188. jsonPopular.push(newPhonePopular);
  189. // console.log(newPhone);
  190. GetSerialChart(jsonPerformance);
  191. GetSerialChart(jsonSales);
  192. GetSerialChart(jsonPopular);
  193. });
  194. //设置 性能、人气、销量 那些在 compareWay 下面。。。。。。。。。。。
  195. }
  196. //比较方式
  197. compareWay();
  198. function compareWay() {
  199. //查找修改
  200. function search(jsons, name, value, title) {
  201. jsons.forEach(function (item, i, self) {
  202. if (item.name === name) {
  203. item.value = value;
  204. console.log('名字' + name, '性能' + value);
  205. }
  206. });
  207. GetSerialChart(jsons, title);
  208. }
  209. //设置性能、销量、人气
  210. $('#ctrl button:nth-of-type(2)').on('click', function () {
  211. console.log('设置成功');
  212. var phoneName = $('#ctrl input:nth-of-type(2)').val(),
  213. phoneValue = $('#ctrl input:nth-of-type(3)').val();
  214. console.log(phoneName);
  215. if ($(this).text() === '设置性能') {
  216. console.log('设置性能成功');
  217. search(jsonPerformance, phoneName, phoneValue, '性能比较');
  218. } else if ($(this).text() === '设置销量') {
  219. console.log('设置销量成功')
  220. search(jsonSales, phoneName, phoneValue, '销量比较');
  221. } else if ($(this).text() === '设置人气') {
  222. console.log('设置人气成功');
  223. search(jsonPopular, phoneName, phoneValue, '人气比较');
  224. }
  225. });
  226. //性能比较
  227. $('.performance').on('click', function () {
  228. console.log('click性能');
  229. $(this).css('background-color', 'rgb(199, 147, 229)');
  230. $(this).siblings().css('background-color', 'plum');
  231. GetSerialChart(jsonPerformance, '性能对比');
  232. $('#ctrl .setPerform').text('设置性能');
  233. });
  234. //销量比较
  235. $('.sales').on('click', function () {
  236. console.log('click销量');
  237. $(this).css('background-color', 'rgb(199, 147, 229)');
  238. $(this).siblings().css('background-color', 'plum');
  239. GetSerialChart(jsonSales, '销量对比');
  240. $('#ctrl .setPerform').text('设置销量');
  241. });
  242. //人气比较
  243. $('.popular').on('click', function () {
  244. console.log('click人气');
  245. $(this).css('background-color', 'rgb(199, 147, 229)');
  246. $(this).siblings().css('background-color', 'plum');
  247. GetSerialChart(jsonPopular, '人气对比');
  248. $('#ctrl .setPerform').text('设置人气');
  249. });
  250. }
  251. /**
  252. * 点击时候的状态
  253. */
  254. function thatStatus(value) {
  255. $('#ctrl .setPerform').text(value);
  256. }