data-helpers.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * 数据辅助函数模块
  3. * 用于处理和计算图表相关数据
  4. */
  5. // @ts-nocheck - 为了与原始 u-charts 代码保持兼容性,跳过类型检查
  6. import type { SeriesItem } from '../data-processing/index';
  7. import { measureText } from '../utils/text';
  8. /**
  9. * 获取系列数据项
  10. * @param series 数据系列
  11. * @param index 索引或索引数组
  12. * @param group 分组数组
  13. * @returns 数据项数组
  14. */
  15. export function getSeriesDataItem(
  16. series: SeriesItem[],
  17. index: number | number[],
  18. group: number[]
  19. ): Array<{
  20. color?: string;
  21. type?: string;
  22. style?: string;
  23. pointShape?: string;
  24. disableLegend?: boolean;
  25. legendShape?: string;
  26. name?: string;
  27. show?: boolean;
  28. data: any;
  29. }> {
  30. const data: any[] = [];
  31. let newSeries: SeriesItem[] = [];
  32. const indexIsArr = Array.isArray(index);
  33. if (indexIsArr) {
  34. const tempSeries = filterSeries(series);
  35. for (let i = 0; i < group.length; i++) {
  36. newSeries.push(tempSeries[group[i]]);
  37. }
  38. } else {
  39. newSeries = series;
  40. }
  41. for (let i = 0; i < newSeries.length; i++) {
  42. const item = newSeries[i];
  43. let tmpindex = -1;
  44. if (indexIsArr) {
  45. tmpindex = (index as number[])[i];
  46. } else {
  47. tmpindex = index as number;
  48. }
  49. if (
  50. item.data[tmpindex] !== null &&
  51. typeof item.data[tmpindex] !== 'undefined' &&
  52. item.show
  53. ) {
  54. const seriesItem: any = {};
  55. seriesItem.color = item.color;
  56. seriesItem.type = item.type;
  57. seriesItem.style = item.style;
  58. seriesItem.pointShape = item.pointShape;
  59. seriesItem.disableLegend = item.disableLegend;
  60. seriesItem.legendShape = item.legendShape;
  61. seriesItem.name = item.name;
  62. seriesItem.show = item.show;
  63. seriesItem.data = item.formatter
  64. ? item.formatter(item.data[tmpindex], tmpindex, item)
  65. : item.data[tmpindex];
  66. data.push(seriesItem);
  67. }
  68. }
  69. return data;
  70. }
  71. /**
  72. * 过滤系列数据
  73. * @param series 数据系列
  74. * @returns 过滤后的系列数据
  75. */
  76. export function filterSeries(series: SeriesItem[]): SeriesItem[] {
  77. const tempSeries: SeriesItem[] = [];
  78. for (let i = 0; i < series.length; i++) {
  79. if (series[i].show == true) {
  80. tempSeries.push(series[i]);
  81. }
  82. }
  83. return tempSeries;
  84. }
  85. /**
  86. * 分割点数据
  87. * @param points 点数据数组
  88. * @param eachSeries 系列配置
  89. * @returns 分割后的点数组
  90. */
  91. export function splitPoints(
  92. points: any[],
  93. eachSeries: { connectNulls?: boolean }
  94. ): any[][] {
  95. const newPoints: any[][] = [];
  96. const items: any[] = [];
  97. points.forEach(function (item) {
  98. if (eachSeries.connectNulls) {
  99. if (item !== null) {
  100. items.push(item);
  101. }
  102. } else {
  103. if (item !== null) {
  104. items.push(item);
  105. } else {
  106. if (items.length) {
  107. newPoints.push(items);
  108. }
  109. items.length = 0;
  110. }
  111. }
  112. });
  113. if (items.length) {
  114. newPoints.push(items);
  115. }
  116. return newPoints;
  117. }
  118. /**
  119. * 获取文本列表最大长度
  120. * @param list 文本列表
  121. * @param fontSize 字体大小
  122. * @param context Canvas上下文
  123. * @returns 最大长度
  124. */
  125. export function getMaxTextListLength(
  126. list: string[],
  127. fontSize: number,
  128. context: any
  129. ): number {
  130. const lengthList = list.map(function (item) {
  131. return measureText(item, fontSize, context);
  132. });
  133. return Math.max.apply(null, lengthList);
  134. }
  135. /**
  136. * 获取雷达图坐标系列
  137. * @param length 数据长度
  138. * @returns 坐标角度数组
  139. */
  140. export function getRadarCoordinateSeries(length: number): number[] {
  141. const eachAngle = (2 * Math.PI) / length;
  142. const CoordinateSeries: number[] = [];
  143. for (let i = 0; i < length; i++) {
  144. CoordinateSeries.push(eachAngle * i);
  145. }
  146. return CoordinateSeries.map(function (item) {
  147. return -1 * item + Math.PI / 2;
  148. });
  149. }
  150. /**
  151. * 获取K线图提示框数据
  152. * @param series K线数据系列
  153. * @param seriesData 系列数据
  154. * @param opts 图表配置
  155. * @param index 当前索引
  156. * @param categories 分类数据
  157. * @param extra 额外配置
  158. * @param option 提示框选项
  159. * @returns 提示框数据对象
  160. */
  161. export function getCandleToolTipData(
  162. series: any[],
  163. seriesData: any[],
  164. opts: any,
  165. index: number,
  166. categories: any[],
  167. extra: any,
  168. option: any = {}
  169. ): { textList: any[]; offset: { x: number; y: number } } {
  170. const calPoints = opts.chartData.calPoints;
  171. const upColor = extra.color.upFill;
  172. const downColor = extra.color.downFill;
  173. //颜色顺序为开盘,收盘,最低,最高
  174. const color = [upColor, upColor, downColor, upColor];
  175. const textList: any[] = [];
  176. seriesData.map(function (item) {
  177. if (index == 0) {
  178. if (item.data[1] - item.data[0] < 0) {
  179. color[1] = downColor;
  180. } else {
  181. color[1] = upColor;
  182. }
  183. } else {
  184. if (item.data[0] < series[index - 1][1]) {
  185. color[0] = downColor;
  186. }
  187. if (item.data[1] < item.data[0]) {
  188. color[1] = downColor;
  189. }
  190. if (item.data[2] > series[index - 1][1]) {
  191. color[2] = upColor;
  192. }
  193. if (item.data[3] < series[index - 1][1]) {
  194. color[3] = downColor;
  195. }
  196. }
  197. const text1 = {
  198. text: '开盘:' + item.data[0],
  199. color: color[0],
  200. legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
  201. };
  202. const text2 = {
  203. text: '收盘:' + item.data[1],
  204. color: color[1],
  205. legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
  206. };
  207. const text3 = {
  208. text: '最低:' + item.data[2],
  209. color: color[2],
  210. legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
  211. };
  212. const text4 = {
  213. text: '最高:' + item.data[3],
  214. color: color[3],
  215. legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
  216. };
  217. textList.push(text1, text2, text3, text4);
  218. });
  219. const validCalPoints: any[] = [];
  220. const offset = {
  221. x: 0,
  222. y: 0
  223. };
  224. for (let i = 0; i < calPoints.length; i++) {
  225. const points = calPoints[i];
  226. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  227. validCalPoints.push(points[index]);
  228. }
  229. }
  230. offset.x = Math.round(validCalPoints[0][0].x);
  231. return {
  232. textList: textList,
  233. offset: offset
  234. };
  235. }