cgDynamGraphDesign.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. var DEFAULT_,
  2. OPTION_,
  3. TYPE_='Pie2D',
  4. TITLE_ = 'Online 动态图表配置',
  5. DATA_,
  6. ANIMATION_ = true,
  7. SHADOW_ = false,
  8. f = true,
  9. BACKGROUND_COLOR = '#FEFEFE',
  10. COO_BACKGROUND_COLOR = '#FEFEFE',
  11. IMAGE_DATA = "";
  12. var color = ['#a5c2d5', '#cbab4f', '#76a871', '#c12c44', '#9f7961', '#6f83a5'];
  13. function doPie(){
  14. DEFAULT_ = {
  15. data : DATA_,
  16. sub_option : {
  17. label : {
  18. background_color : null,
  19. sign : false,
  20. padding : '0 4',
  21. border : {
  22. enable : false,
  23. color : '#666666'
  24. },
  25. fontsize : 12,
  26. fontweight : 600,
  27. color : '#982e35'
  28. }
  29. },
  30. showpercent : true,
  31. yHeight : 30,
  32. decimalsnum : 2
  33. };
  34. }
  35. function doColumn(){
  36. DEFAULT_ = {
  37. data : DATA_,
  38. text_space : TYPE_=='Column3D'?16:6,
  39. coordinate : {
  40. width : '86%',
  41. height : '80%',
  42. color_factor : 0.24,
  43. board_deep:10,//背面厚度
  44. pedestal_height:10,//底座高度
  45. left_board:false,//取消左侧面板
  46. axis : {
  47. color : '#c0d0e0',
  48. width : [0, 0, 1, 0]
  49. },
  50. scale : [{
  51. position : 'left',
  52. scale_enable : false,
  53. label : {
  54. fontsize : 11,
  55. color : '#4c4f48'
  56. }
  57. }]
  58. },
  59. label : {
  60. fontsize : 11,
  61. color : '#4c4f48'
  62. },
  63. zScale:0.5,
  64. bottom_scale:1.1,
  65. sub_option : {
  66. label : {
  67. fontsize : 11,
  68. fontweight : 600,
  69. color : '#4572a7'
  70. },
  71. border : {
  72. width : 2,
  73. color : '#ffffff'
  74. }
  75. }
  76. };
  77. }
  78. function doBar(){
  79. DEFAULT_ = {
  80. data : DATA_,
  81. coordinate : {
  82. width : '80%',
  83. height : '80%',
  84. axis : {
  85. color : '#c0d0e0',
  86. width:[1,1,1,4]
  87. },
  88. scale : [{
  89. position : 'bottom',
  90. scale_enable : false,
  91. label : {
  92. fontsize : 11,
  93. color : '#4c4f48'
  94. }
  95. }]
  96. },
  97. label : {
  98. fontsize : 11,
  99. color : '#4c4f48'
  100. },
  101. zScale:0.5,
  102. bottom_scale:1.1,
  103. sub_option : {
  104. label : {
  105. fontsize : 11,
  106. fontweight : 600,
  107. color : '#4572a7'
  108. },
  109. border : {
  110. width : 2,
  111. color : '#ffffff'
  112. }
  113. }
  114. };
  115. }
  116. function doLine(){
  117. var labels = [], data_l = [];
  118. for ( var i = 0; i < DATA_.length; i++) {
  119. labels.push(DATA_[i].name);
  120. data_l.push(DATA_[i].value);
  121. }
  122. if (data_l.length == 1) {
  123. alert("折线图至少需要2组数据才能画线哦!请定制数据或者选择其他图形.");
  124. return;
  125. }
  126. DEFAULT_ = {
  127. data_labels : labels,
  128. data : [{
  129. name : '',
  130. value : data_l,
  131. color : '#1f7e92',
  132. linewidth : 3
  133. }],
  134. //update-begin--author:zhangjiaqiang Date:20170213 for:修订综合报表当中折线图、面积图X轴信息错误
  135. labels:labels,
  136. //update-begin--author:zhangjiaqiang Date:20170213 for:修订综合报表当中折线图、面积图X轴信息错误
  137. sub_option : {
  138. label : {
  139. fontsize : 14,
  140. fontweight:600,
  141. color : '#4c4f48'
  142. },
  143. smooth : OPTION_ == '1',
  144. hollow_inside:false,
  145. point_size:16,
  146. hollow : true
  147. },
  148. coordinate : {
  149. width : '84%',
  150. height : '75%'
  151. }
  152. };
  153. }
  154. function doChart() {
  155. if (TYPE_ == 'Pie2D' || TYPE_ == 'Pie3D') {
  156. doPie();
  157. } else if (TYPE_ == 'Column2D' || TYPE_ == 'Column3D') {
  158. doColumn();
  159. } else if (TYPE_ == 'Bar2D') {
  160. doBar();
  161. } else if (TYPE_ == 'LineBasic2D' || TYPE_ == 'Area2D') {
  162. doLine();
  163. }
  164. if (TITLE_ != '') {
  165. DEFAULT_.title = TITLE_;
  166. }
  167. DEFAULT_.width = 800;
  168. DEFAULT_.height = 400;
  169. DEFAULT_.render = 'canvasDiv';
  170. DEFAULT_.animation = ANIMATION_;
  171. DEFAULT_.shadow = SHADOW_;
  172. DEFAULT_.shadow_blur = 3;
  173. DEFAULT_.background_color = BACKGROUND_COLOR;
  174. DEFAULT_.footnote = "Design by ichartjs";
  175. if (DEFAULT_.coordinate) {
  176. DEFAULT_.coordinate.background_color = COO_BACKGROUND_COLOR;
  177. DEFAULT_.coordinate.grid_color = iChart.dark(COO_BACKGROUND_COLOR, 0.3,0.1);
  178. }
  179. /**
  180. * 使导出图片按钮有效
  181. */
  182. DEFAULT_.listeners = {};
  183. DEFAULT_.listeners[ANIMATION_ ? 'afterAnimation' : 'draw'] = function(c) {
  184. download.disabled = false;
  185. IMAGE_DATA = this.target.canvas.toDataURL();
  186. }
  187. new iChart[TYPE_](DEFAULT_).draw();
  188. }
  189. function render() {
  190. if (!TYPE_)
  191. return;
  192. download.disabled = true;
  193. if (f) {
  194. doChart();
  195. f = false;
  196. } else {
  197. $canvas.fadeOut(300, function() {
  198. $(this).fadeIn(300);
  199. doChart();
  200. });
  201. }
  202. }
  203. var $form_tbody, $form_tr_temlate, $form_tr_head, $gallery_color_picker, $validateTips, download, download_a;
  204. $(function() {
  205. $validateTips = $("#validateTips");
  206. $form_tbody = $("#form_tbody");
  207. download = document.getElementById("download");
  208. download_a = document.getElementById("download_a");
  209. $gallery_color_picker = $("#gallery_color_picker");
  210. $form_tr_temlate = $('<tr><td><input type="text" class="form_text" /></td><td><input type="text" class="form_text" /></td><td class="td_color"><input type="text" class="form_text"/></td><td><a href="javascript:void(0)" onclick="removeRow(this);">移除</a></td></tr>');
  211. $form_tr_head = $("#form_tr_head");
  212. $("#dialog-download").dialog({
  213. autoOpen : false,
  214. height : 448,
  215. width : 848,
  216. modal : true
  217. });
  218. $("#dialog-form").dialog({
  219. autoOpen : false,
  220. height : 400,
  221. width : 450,
  222. modal : true,
  223. buttons : {
  224. "应用我的数据" : function() {
  225. var title = $("#form_title").val();
  226. if (title == "") {
  227. $validateTips.html("提示:标题项不能为空!");
  228. return;
  229. }
  230. TITLE_ = title;
  231. var $inputs = $form_tbody.children("tr").find("input"), data = [], CHECK = true;
  232. for ( var i = 0; i < $inputs.length; i += 3) {
  233. if ($inputs[i].value == "" || $inputs[i + 1].value == "" || $inputs[i + 2].value == "") {
  234. CHECK = false;
  235. break;
  236. }
  237. data.push({
  238. name : $inputs[i].value,
  239. value : parseFloat($inputs[i + 1].value) || 0,
  240. color : $inputs[i + 2].value
  241. });
  242. }
  243. if (!CHECK) {
  244. $validateTips.html("提示:数据项不能为空!");
  245. } else {
  246. $validateTips.html("所有文本均为必填项.");
  247. DATA_ = data;
  248. render();
  249. $(this).dialog("close");
  250. }
  251. },
  252. '取消' : function() {
  253. $(this).dialog("close");
  254. }
  255. },
  256. close : function() {
  257. }
  258. }).click(function() {
  259. $gallery_color_picker.fadeOut();
  260. });
  261. var img = document.getElementById("canvas_img");
  262. var a_ = document.getElementById("download_a");
  263. $("#download").click(function(e) {
  264. if (iChart.isChrome) {
  265. a_.href = IMAGE_DATA;
  266. } else {
  267. img.src = IMAGE_DATA;
  268. $("#dialog-download").dialog("open");
  269. e.stopPropagation();
  270. e.preventDefault();
  271. }
  272. });
  273. var w = $(document.body).width();
  274. var datatype, type;
  275. $canvas = $("#canvasDiv");
  276. document.getElementById("gallery_animate").checked = true;
  277. document.getElementById("gallery_shadow").checked = false;
  278. download.disabled = true;
  279. h = 450;
  280. w = w - 280;
  281. w = w > 800 ? w : 800;
  282. $("#gallery_right_container").width(w).height(h);
  283. $("#gallery_right_bg").width(w).height(h);
  284. $("#gallery_preview").height(h);
  285. $(".draggable").draggable({
  286. helper : function(event) {
  287. return $(this).clone();
  288. },
  289. cursor : "move",
  290. stack : '#canvasDiv',
  291. cursorAt : {
  292. top : 25,
  293. left : 25
  294. },
  295. opacity : 0.7,
  296. start : function(event, ui) {
  297. type = $(this).attr('type');
  298. datatype = $(this).attr('datatype');
  299. TITLE_ = $(this).attr('title');
  300. }
  301. });
  302. $(".gallery_draggable").click(function() {
  303. TYPE_ = $(this).attr('type');
  304. OPTION_ = $(this).attr('option');
  305. render();
  306. });
  307. $("#gallery_right_bg").droppable({
  308. accept : ".draggable",
  309. hoverClass : "gallery_hover",
  310. drop : function(event, ui) {
  311. if (type == "data") {
  312. if (datatype == "2") {
  313. DATA_ = data2;
  314. } else if (datatype == "3") {
  315. DATA_ = data3;
  316. } else {
  317. DATA_ = data1;
  318. }
  319. if (!TYPE_)
  320. return;
  321. } else {
  322. TYPE_ = type;
  323. }
  324. render();
  325. }
  326. });
  327. $("#gallery_animate").click(function() {
  328. ANIMATION_ = this.checked;
  329. render();
  330. });
  331. $("#gallery_shadow").click(function() {
  332. SHADOW_ = this.checked;
  333. render();
  334. });
  335. $(".gallery_color").hover(function() {
  336. $(this).addClass("gallery_color_hover");
  337. }, function() {
  338. $(this).removeClass("gallery_color_hover");
  339. }).click(function() {
  340. if ($(this).attr('type') == "0") {
  341. BACKGROUND_COLOR = $(this).css('background-color');
  342. $(this).parents(".gallery_bg").css("background-color", BACKGROUND_COLOR);
  343. } else {
  344. COO_BACKGROUND_COLOR = $(this).css('background-color');
  345. $(this).parents(".gallery_bg").css("background-color", COO_BACKGROUND_COLOR);
  346. }
  347. render();
  348. });
  349. $(".gallery_bg").hover(function() {
  350. $(this).children(".gallery_bg_list").fadeIn();
  351. }, function() {
  352. $(this).children(".gallery_bg_list").fadeOut();
  353. });
  354. $("#custom-data").click(function() {
  355. $("#dialog-form").dialog("open");
  356. });
  357. var $current_color;
  358. $(".td_color .form_text").click(function(e) {
  359. $current_color = $(this);
  360. $gallery_color_picker.css("top", 6).fadeIn();
  361. e.stopPropagation();
  362. });
  363. $form_tr_temlate.find('.td_color .form_text').click(function(e) {
  364. $current_color = $(this);
  365. var i = $(this).parents("tr").prevAll().length;
  366. $gallery_color_picker.css("top", 6 + 29 * i).fadeIn();
  367. e.stopPropagation();
  368. });
  369. $("#gallery_color_picker .color").hover(function() {
  370. $(this).addClass("gallery_color_hover");
  371. }, function() {
  372. $(this).removeClass("gallery_color_hover");
  373. }).click(function() {
  374. var color = $(this).attr('color');
  375. $current_color.val(color);
  376. $current_color.parent("td").css("background-color", color);
  377. });
  378. });
  379. function addRow() {
  380. $form_tbody.append($form_tr_temlate.clone(true));
  381. }
  382. function removeRow(a) {
  383. $(a).parents("tr").remove();
  384. }