RadarChart.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. const { ccclass, property, executeInEditMode, executionOrder } = cc._decorator;
  2. /**
  3. * 雷达图组件
  4. * @see RadarChart.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/components/RadarChart.ts
  5. */
  6. @ccclass
  7. @executeInEditMode
  8. @executionOrder(-10)
  9. export default class RadarChart extends cc.Component {
  10. @property({ type: cc.Node, tooltip: CC_DEV && '绘制节点(不指定则默认为当前节点)' })
  11. private target: cc.Node = null;
  12. @property private _axisLength: number = 200;
  13. @property({ tooltip: CC_DEV && '轴线长度' })
  14. public get axisLength() { return this._axisLength; }
  15. public set axisLength(value: number) { this._axisLength = value; this.draw(this.curDatas); }
  16. @property private _axes: number = 6;
  17. @property({ tooltip: CC_DEV && '轴线数量(至少 3 条)' })
  18. public get axes() { return this._axes; }
  19. public set axes(value: number) { this._axes = Math.floor(value >= 3 ? value : 3); this.draw(this.curDatas); }
  20. @property private _axisScales: number = 3;
  21. @property({ tooltip: CC_DEV && '轴线上的刻度数(至少 1 个)' })
  22. public get axisScales() { return this._axisScales; }
  23. public set axisScales(value: number) { this._axisScales = Math.floor(value >= 1 ? value : 1); this.draw(this.curDatas); }
  24. @property private _drawAxes: boolean = true;
  25. @property({ tooltip: CC_DEV && '是否绘制轴线' })
  26. public get drawAxes() { return this._drawAxes; }
  27. public set drawAxes(value: boolean) { this._drawAxes = value; this.draw(this.curDatas); }
  28. @property private _gridLineWidth: number = 4;
  29. @property({ tooltip: CC_DEV && '轴线和外网格线的宽度' })
  30. public get gridLineWidth() { return this._gridLineWidth; }
  31. public set gridLineWidth(value: number) { this._gridLineWidth = value; this.draw(this.curDatas); }
  32. @property private _innerGridLineWidth: number = 4;
  33. @property({ tooltip: CC_DEV && '内网格线宽度' })
  34. public get innerGridLineWidth() { return this._innerGridLineWidth; }
  35. public set innerGridLineWidth(value: number) { this._innerGridLineWidth = value; this.draw(this.curDatas); }
  36. @property private _gridLineColor: cc.Color = cc.Color.GRAY;
  37. @property({ tooltip: CC_DEV && '轴线和网格线的颜色' })
  38. public get gridLineColor() { return this._gridLineColor; }
  39. public set gridLineColor(value: cc.Color) { this._gridLineColor = value; this.draw(this.curDatas); }
  40. @property private _gridFillColor: cc.Color = cc.color(100, 100, 100, 100);
  41. @property({ tooltip: CC_DEV && '网格内部填充的颜色' })
  42. public get gridFillColor() { return this._gridFillColor; }
  43. public set gridFillColor(value: cc.Color) { this._gridFillColor = value; this.draw(this.curDatas); }
  44. @property private _dataValuesStrings: string[] = ['0.8,0.5,0.6,0.5,0.8,0.6', '0.5,0.9,0.5,0.8,0.5,0.9'];
  45. @property({ type: [cc.String], tooltip: CC_DEV && '数据数值(字符串形式,使用英文逗号分隔)' })
  46. public get dataValuesStrings() { return this._dataValuesStrings; }
  47. public set dataValuesStrings(value: string[]) { this._dataValuesStrings = value; this.drawWithProperties(); }
  48. @property private _dataLineWidths: number[] = [5, 5];
  49. @property({ type: [cc.Integer], tooltip: CC_DEV && '数据线宽度' })
  50. public get dataLineWidths() { return this._dataLineWidths; }
  51. public set dataLineWidths(value: number[]) { this._dataLineWidths = value; this.drawWithProperties(); }
  52. @property private _dataLineColors: cc.Color[] = [cc.Color.BLUE, cc.Color.RED];
  53. @property({ type: [cc.Color], tooltip: CC_DEV && '数据线颜色' })
  54. public get dataLineColors() { return this._dataLineColors; }
  55. public set dataLineColors(value: cc.Color[]) { this._dataLineColors = value; this.drawWithProperties(); }
  56. @property private _dataFillColors: cc.Color[] = [cc.color(120, 120, 180, 100), cc.color(180, 120, 120, 100)];
  57. @property({ type: [cc.Color], tooltip: CC_DEV && '数据填充颜色' })
  58. public get dataFillColors() { return this._dataFillColors; }
  59. public set dataFillColors(value: cc.Color[]) { this._dataFillColors = value; this.drawWithProperties(); }
  60. @property private _dataJoinColors: cc.Color[] = [];
  61. @property({ type: [cc.Color], tooltip: CC_DEV && '数据节点颜色' })
  62. public get dataJoinColors() { return this._dataJoinColors; }
  63. public set dataJoinColors(value: cc.Color[]) { this._dataJoinColors = value; this.drawWithProperties(); }
  64. @property private _drawDataJoin: boolean = true;
  65. @property({ tooltip: CC_DEV && '是否绘制数据节点' })
  66. public get drawDataJoin() { return this._drawDataJoin; }
  67. public set drawDataJoin(value: boolean) { this._drawDataJoin = value; this.draw(this.curDatas); }
  68. private graphics: cc.Graphics = null;
  69. private keepUpdating: boolean = false;
  70. private angles: number[] = null;
  71. private _curDatas: RadarChartData[] = [];
  72. public get curDatas() { return this._curDatas; }
  73. private toRes: () => void = null;
  74. protected onLoad() {
  75. this.init();
  76. this.drawWithProperties();
  77. }
  78. protected update() {
  79. if (!this.keepUpdating || this._curDatas.length === 0) return;
  80. this.draw(this._curDatas);
  81. }
  82. /**
  83. * 初始化
  84. */
  85. private init() {
  86. // 获取组件
  87. if (!this.target) this.target = this.node;
  88. this.graphics = this.target.getComponent(cc.Graphics) || this.target.addComponent(cc.Graphics);
  89. // 设置端点和拐角样式
  90. this.graphics.lineJoin = cc.Graphics.LineJoin.ROUND;
  91. this.graphics.lineCap = cc.Graphics.LineCap.ROUND;
  92. }
  93. /**
  94. * 使用当前属性绘制
  95. */
  96. private drawWithProperties() {
  97. // 获取属性面板配置
  98. let datas: RadarChartData[] = [];
  99. for (let i = 0; i < this.dataValuesStrings.length; i++) {
  100. datas.push({
  101. values: this.processValuesString(this.dataValuesStrings[i]),
  102. lineWidth: this._dataLineWidths[i] || defaultOptions.lineWidth,
  103. lineColor: this._dataLineColors[i] || defaultOptions.lineColor,
  104. fillColor: this._dataFillColors[i] || defaultOptions.fillColor,
  105. joinColor: this._dataJoinColors[i] || defaultOptions.joinColor
  106. });
  107. }
  108. // 绘制
  109. this.draw(datas);
  110. }
  111. /**
  112. * 将数值字符串转为数值数组
  113. * @param valuesString 数值字符串
  114. */
  115. private processValuesString(valuesString: string): number[] {
  116. const strings = valuesString.split(',');
  117. let values: number[] = [];
  118. for (let j = 0; j < strings.length; j++) {
  119. const value = parseFloat(strings[j]);
  120. values.push(isNaN(value) ? 0 : value);
  121. }
  122. return values;
  123. }
  124. /**
  125. * 画基本线框
  126. */
  127. private drawBase() {
  128. // 填充染料
  129. this.graphics.lineWidth = this._gridLineWidth;
  130. this.graphics.strokeColor = this._gridLineColor;
  131. this.graphics.fillColor = this._gridFillColor;
  132. // 计算轴线角度
  133. this.angles = [];
  134. const iAngle = 360 / this.axes; // 轴间夹角
  135. for (let i = 0; i < this.axes; i++) this.angles.push(iAngle * i);
  136. // 计算刻度坐标
  137. let scalesSet: cc.Vec2[][] = [];
  138. const iLength = this._axisLength / this._axisScales;
  139. for (let i = 0; i < this._axisScales; i++) {
  140. let scales = [];
  141. // 计算刻度在轴上的位置
  142. const length = this._axisLength - (iLength * i);
  143. for (let j = 0; j < this.angles.length; j++) {
  144. // 将角度转为弧度
  145. const radian = (Math.PI / 180) * this.angles[j];
  146. // 根据三角公式计算刻度相对于中心点(0, 0)的坐标
  147. scales.push(cc.v2(length * Math.cos(radian), length * Math.sin(radian)));
  148. }
  149. scalesSet.push(scales);
  150. }
  151. // 创建轴线
  152. if (this._drawAxes) {
  153. for (let i = 0; i < scalesSet[0].length; i++) {
  154. this.graphics.moveTo(0, 0);
  155. this.graphics.lineTo(scalesSet[0][i].x, scalesSet[0][i].y);
  156. }
  157. }
  158. // 创建外网格线
  159. this.graphics.moveTo(scalesSet[0][0].x, scalesSet[0][0].y);
  160. for (let i = 1; i < scalesSet[0].length; i++) {
  161. this.graphics.lineTo(scalesSet[0][i].x, scalesSet[0][i].y);
  162. }
  163. this.graphics.close(); // 闭合当前线条(外网格线)
  164. // 填充线条包围的空白区域
  165. this.graphics.fill();
  166. // 绘制已创建的线条(轴线和外网格线)
  167. this.graphics.stroke();
  168. // 画内网格线
  169. if (scalesSet.length > 1) {
  170. this.graphics.lineWidth = this._innerGridLineWidth;
  171. // 创建内网格线
  172. for (let i = 1; i < scalesSet.length; i++) {
  173. this.graphics.moveTo(scalesSet[i][0].x, scalesSet[i][0].y);
  174. for (let j = 1; j < scalesSet[i].length; j++) {
  175. this.graphics.lineTo(scalesSet[i][j].x, scalesSet[i][j].y);
  176. }
  177. this.graphics.close(); // 闭合当前线条(内网格线)
  178. }
  179. // 绘制已创建的线条(内网格线)
  180. this.graphics.stroke();
  181. }
  182. }
  183. /**
  184. * 绘制数据
  185. * @param data 数据
  186. */
  187. public draw(data: RadarChartData | RadarChartData[]) {
  188. // 擦除旧图像
  189. this.graphics.clear();
  190. // 画轴线和网格线
  191. this.drawBase();
  192. // 包装单条数据
  193. const datas = Array.isArray(data) ? data : [data];
  194. this._curDatas = datas;
  195. // 数值不足需补 0
  196. this.resizeCurDatasValues(0);
  197. // 开始绘制数据
  198. for (let i = 0; i < datas.length; i++) {
  199. // 装填染料
  200. this.graphics.strokeColor = datas[i].lineColor || defaultOptions.lineColor;
  201. this.graphics.fillColor = datas[i].fillColor || defaultOptions.fillColor;
  202. this.graphics.lineWidth = datas[i].lineWidth || defaultOptions.lineWidth;
  203. // 计算节点坐标
  204. let coords = [];
  205. for (let j = 0; j < this.axes; j++) {
  206. const length = (datas[i].values[j] > 1 ? 1 : datas[i].values[j]) * this.axisLength;
  207. const radian = (Math.PI / 180) * this.angles[j];
  208. coords.push(cc.v2(length * Math.cos(radian), length * Math.sin(radian)));
  209. }
  210. // 创建线条
  211. this.graphics.moveTo(coords[0].x, coords[0].y);
  212. for (let j = 1; j < coords.length; j++) {
  213. this.graphics.lineTo(coords[j].x, coords[j].y);
  214. }
  215. this.graphics.close(); // 闭合线条
  216. // 填充包围区域
  217. this.graphics.fill();
  218. // 绘制线条
  219. this.graphics.stroke();
  220. // 绘制数据节点
  221. if (this._drawDataJoin) {
  222. for (let j = 0; j < coords.length; j++) {
  223. // 大圆
  224. this.graphics.strokeColor = datas[i].lineColor || defaultOptions.lineColor;
  225. this.graphics.circle(coords[j].x, coords[j].y, 2);
  226. this.graphics.stroke();
  227. // 小圆
  228. this.graphics.strokeColor = datas[i].joinColor || defaultOptions.joinColor;
  229. this.graphics.circle(coords[j].x, coords[j].y, .65);
  230. this.graphics.stroke();
  231. }
  232. }
  233. }
  234. }
  235. /**
  236. * 缓动绘制
  237. * @param data 目标数据
  238. * @param duration 动画时长
  239. */
  240. public to(data: RadarChartData | RadarChartData[], duration: number): Promise<void> {
  241. return new Promise(res => {
  242. // 处理上一个 Promise
  243. this.unscheduleAllCallbacks();
  244. this.toRes && this.toRes();
  245. this.toRes = res;
  246. // 包装单条数据
  247. const datas = Array.isArray(data) ? data : [data];
  248. // 打开每帧更新
  249. this.keepUpdating = true;
  250. // 动起来!
  251. for (let i = 0; i < datas.length; i++) {
  252. if (!this._curDatas[i]) continue;
  253. // 数值动起来!
  254. for (let j = 0; j < this._curDatas[i].values.length; j++) {
  255. cc.tween(this._curDatas[i].values)
  256. .to(duration, { [j]: datas[i].values[j] > 1 ? 1 : datas[i].values[j] })
  257. .start();
  258. }
  259. // 样式动起来!
  260. cc.tween(this._curDatas[i])
  261. .to(duration, {
  262. lineWidth: datas[i].lineWidth || this._curDatas[i].lineWidth,
  263. lineColor: datas[i].lineColor || this._curDatas[i].lineColor,
  264. fillColor: datas[i].fillColor || this._curDatas[i].fillColor,
  265. joinColor: datas[i].joinColor || this._curDatas[i].joinColor
  266. })
  267. .start();
  268. }
  269. this.scheduleOnce(() => {
  270. // 关闭每帧更新
  271. this.keepUpdating = false;
  272. // resolve Promise
  273. this.toRes();
  274. this.toRes = null;
  275. }, duration);
  276. });
  277. }
  278. /**
  279. * 检查并调整数据中的数值数量
  280. * @param fill 填充数值
  281. */
  282. private resizeCurDatasValues(fill: number = 0) {
  283. for (let i = 0; i < this._curDatas.length; i++) {
  284. // 数值数量少于轴数时才进行调整
  285. if (this._curDatas[i].values.length < this._axes) {
  286. const diff = this._axes - this._curDatas[i].values.length;
  287. for (let j = 0; j < diff; j++) this._curDatas[i].values.push(fill);
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * 雷达图数据
  294. */
  295. export interface RadarChartData {
  296. /** 数值 */
  297. values: number[];
  298. /** 线的宽度 */
  299. lineWidth?: number;
  300. /** 线的颜色 */
  301. lineColor?: cc.Color;
  302. /** 填充的颜色 */
  303. fillColor?: cc.Color;
  304. /** 节点的颜色 */
  305. joinColor?: cc.Color;
  306. }
  307. /**
  308. * 不指定时使用的样式配置
  309. */
  310. const defaultOptions = {
  311. lineWidth: 5,
  312. lineColor: cc.Color.BLUE,
  313. fillColor: cc.color(120, 120, 180, 100),
  314. joinColor: cc.Color.WHITE,
  315. }