130d9b0e631f02b249c2eadb95c299ecfe4e1d5b.svn-base 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * echarts日期运算格式化相关
  3. *
  4. * @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
  5. * @author Kener (@Kener-林峰, kener.linfeng@gmail.com)
  6. *
  7. */
  8. define(function() {
  9. var _timeGap = [
  10. {formatter: 'hh : mm : ss', value: 1000}, // 1s
  11. {formatter: 'hh : mm : ss', value: 1000 * 5}, // 5s
  12. {formatter: 'hh : mm : ss', value: 1000 * 10}, // 10s
  13. {formatter: 'hh : mm : ss', value: 1000 * 15}, // 15s
  14. {formatter: 'hh : mm : ss', value: 1000 * 30}, // 30s
  15. {formatter: 'hh : mm\nMM - dd', value: 60000}, // 1m
  16. {formatter: 'hh : mm\nMM - dd', value: 60000 * 5}, // 5m
  17. {formatter: 'hh : mm\nMM - dd', value: 60000 * 10}, // 10m
  18. {formatter: 'hh : mm\nMM - dd', value: 60000 * 15}, // 15m
  19. {formatter: 'hh : mm\nMM - dd', value: 60000 * 30}, // 30m
  20. {formatter: 'hh : mm\nMM - dd', value: 3600000}, // 1h
  21. {formatter: 'hh : mm\nMM - dd', value: 3600000 * 2}, // 2h
  22. {formatter: 'hh : mm\nMM - dd', value: 3600000 * 6}, // 6h
  23. {formatter: 'hh : mm\nMM - dd', value: 3600000 * 12}, // 12h
  24. {formatter: 'MM - dd\nyyyy', value: 3600000 * 24}, // 1d
  25. {formatter: 'week', value: 3600000 * 24 * 7}, // 7d
  26. {formatter: 'month', value: 3600000 * 24 * 31}, // 1M
  27. {formatter: 'quarter', value: 3600000 * 24 * 380 / 4}, // 3M
  28. {formatter: 'half-year', value: 3600000 * 24 * 380 / 2},// 6M
  29. {formatter: 'year', value: 3600000 * 24 * 380} // 1Y
  30. ];
  31. /**
  32. * 获取最佳formatter
  33. * @params {number} min 最小值
  34. * @params {number} max 最大值
  35. * @params {=number} splitNumber 分隔段数
  36. */
  37. function getAutoFormatter(min, max, splitNumber) {
  38. splitNumber = splitNumber > 1 ? splitNumber : 2;
  39. // 最优解
  40. var curValue;
  41. var totalGap;
  42. // 目标
  43. var formatter;
  44. var gapValue;
  45. for (var i = 0, l = _timeGap.length; i < l; i++) {
  46. curValue = _timeGap[i].value;
  47. totalGap = Math.ceil(max / curValue) * curValue
  48. - Math.floor(min / curValue) * curValue;
  49. if (Math.round(totalGap / curValue) <= splitNumber * 1.2) {
  50. formatter = _timeGap[i].formatter;
  51. gapValue = _timeGap[i].value;
  52. // console.log(formatter, gapValue,i);
  53. break;
  54. }
  55. }
  56. if (formatter == null) {
  57. formatter = 'year';
  58. curValue = 3600000 * 24 * 367;
  59. totalGap = Math.ceil(max / curValue) * curValue
  60. - Math.floor(min / curValue) * curValue;
  61. gapValue = Math.round(totalGap / (splitNumber - 1) / curValue) * curValue;
  62. }
  63. return {
  64. formatter: formatter,
  65. gapValue: gapValue
  66. };
  67. }
  68. /**
  69. * 一位数字补0
  70. */
  71. function s2d (v) {
  72. return v < 10 ? ('0' + v) : v;
  73. }
  74. /**
  75. * 百分比计算
  76. */
  77. function format(formatter, value) {
  78. if (formatter == 'week'
  79. || formatter == 'month'
  80. || formatter == 'quarter'
  81. || formatter == 'half-year'
  82. || formatter == 'year'
  83. ) {
  84. formatter = 'MM - dd\nyyyy';
  85. }
  86. var date = getNewDate(value);
  87. var y = date.getFullYear();
  88. var M = date.getMonth() + 1;
  89. var d = date.getDate();
  90. var h = date.getHours();
  91. var m = date.getMinutes();
  92. var s = date.getSeconds();
  93. formatter = formatter.replace('MM', s2d(M));
  94. formatter = formatter.toLowerCase();
  95. formatter = formatter.replace('yyyy', y);
  96. formatter = formatter.replace('yy', y % 100);
  97. formatter = formatter.replace('dd', s2d(d));
  98. formatter = formatter.replace('d', d);
  99. formatter = formatter.replace('hh', s2d(h));
  100. formatter = formatter.replace('h', h);
  101. formatter = formatter.replace('mm', s2d(m));
  102. formatter = formatter.replace('m', m);
  103. formatter = formatter.replace('ss', s2d(s));
  104. formatter = formatter.replace('s', s);
  105. return formatter;
  106. }
  107. function nextMonday(value) {
  108. value = getNewDate(value);
  109. value.setDate(value.getDate() + 8 - value.getDay());
  110. return value;
  111. }
  112. function nextNthPerNmonth(value, nth, nmon) {
  113. value = getNewDate(value);
  114. value.setMonth(Math.ceil((value.getMonth() + 1) / nmon) * nmon);
  115. value.setDate(nth);
  116. return value;
  117. }
  118. function nextNthOnMonth(value, nth) {
  119. return nextNthPerNmonth(value, nth, 1);
  120. }
  121. function nextNthOnQuarterYear(value, nth) {
  122. return nextNthPerNmonth(value, nth, 3);
  123. }
  124. function nextNthOnHalfYear(value, nth) {
  125. return nextNthPerNmonth(value, nth, 6);
  126. }
  127. function nextNthOnYear(value, nth) {
  128. return nextNthPerNmonth(value, nth, 12);
  129. }
  130. function getNewDate(value) {
  131. return value instanceof Date
  132. ? value
  133. : new Date(typeof value == 'string' ? value.replace(/-/g, '/') : value);
  134. }
  135. return {
  136. getAutoFormatter: getAutoFormatter,
  137. getNewDate: getNewDate,
  138. format: format,
  139. nextMonday: nextMonday,
  140. nextNthPerNmonth: nextNthPerNmonth,
  141. nextNthOnMonth: nextNthOnMonth,
  142. nextNthOnQuarterYear: nextNthOnQuarterYear,
  143. nextNthOnHalfYear: nextNthOnHalfYear,
  144. nextNthOnYear : nextNthOnYear
  145. };
  146. });