xcharts.uts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. import { optionsInit, updateOptions } from './init.uts'
  2. import { TouchPosition, ChartScrollOption } from './Interface.uts'
  3. import { TuiChartConfig } from './config.uts'
  4. import { TuiChartsEvent } from './chartsEvent.uts'
  5. import { ChartOptionsTooltipOption, ChartOptions, ChartOptionsSeries, ChartOptionsTooltip, TextList, Offset, ChartOptionsXAxis, TuiChartsInterface } from './type.uts'
  6. import { drawCharts, getTouches, Animation, findCandleChartCurrentIndex, getMixToolTipData, findMapChartCurrentIndex, getCandleToolTipData, findWordChartCurrentIndex, findFunnelChartCurrentIndex, findRadarChartCurrentIndex, findRoseChartCurrentIndex, findPieChartCurrentIndex, findCurrentIndex, getSeriesDataItem, getToolTipData, calYAxisData, getXAxisPoints, calValidDistance, findLegendIndex, findBarChartCurrentIndex } from './util.uts'
  7. // ChartOptionsYAxis, ChartOptionsLegend, ChartOptionsExtra,
  8. export class TuiDrawCharts implements TuiChartsInterface {
  9. opts : ChartOptions
  10. context : CanvasRenderingContext2D
  11. uevent : TuiChartsEvent = new TuiChartsEvent()
  12. config : TuiChartConfig
  13. scrollOption = new ChartScrollOption()
  14. animationInstance ?: Animation = null
  15. offsetWidth : number
  16. offsetHeight : number
  17. constructor(option : UTSJSONObject) {
  18. this.context = option['context'] as CanvasRenderingContext2D
  19. const _opts = optionsInit(option)
  20. const conf = new TuiChartConfig()
  21. this.offsetWidth = this.context.canvas.offsetWidth
  22. this.offsetHeight = this.context.canvas.offsetHeight
  23. conf.fontSize = _opts.fontSize * _opts.pixelRatio;
  24. if (_opts.color != null) conf.color = _opts.color!
  25. if (_opts.type == 'pie') {
  26. if (_opts.dataLabel == false) {
  27. conf.pieChartLinePadding = 0
  28. } else {
  29. conf.pieChartLinePadding = _opts.extra.pie!.labelWidth * _opts.pixelRatio
  30. }
  31. }
  32. if (_opts.type == 'ring') {
  33. if (_opts.dataLabel == false) {
  34. conf.pieChartLinePadding = 0
  35. } else {
  36. conf.pieChartLinePadding = _opts.extra.ring!.labelWidth * _opts.pixelRatio
  37. }
  38. }
  39. if (_opts.type == 'rose') {
  40. if (_opts.dataLabel == false) {
  41. conf.pieChartLinePadding = 0
  42. } else {
  43. conf.pieChartLinePadding = _opts.extra.rose!.labelWidth * _opts.pixelRatio
  44. }
  45. }
  46. if (_opts.dataLabel == false) {
  47. conf.pieChartTextPadding = 0
  48. } else {
  49. conf.pieChartTextPadding = conf.pieChartTextPadding * _opts.pixelRatio
  50. }
  51. conf.rotate = _opts.rotate;
  52. this.config = conf;
  53. this.opts = _opts
  54. }
  55. setCoordinates(e : UTSJSONObject) {
  56. if (e.getNumber('height') != null) this.opts.height = e.getNumber('height')!
  57. if (e.getNumber('width') != null) this.opts.width = e.getNumber('width')!
  58. if (e.getNumber('yOffset') != null) this.opts.yOffset = e.getNumber('yOffset')!
  59. if (e.getNumber('xOffset') != null) this.opts.xOffset = e.getNumber('xOffset')!
  60. }
  61. draw() {
  62. drawCharts(this, this.opts.type, this.opts, this.config, this.context);
  63. }
  64. clearTooltip() {
  65. if (this.opts.tooltip != null) {
  66. this.opts.animation = false
  67. this.opts.tooltip = null
  68. this.draw()
  69. }
  70. }
  71. update(data : UTSJSONObject) {
  72. // let data = arguments.length > 0 && arguments[0] != undefined ? arguments[0] : {};
  73. // this.opts = assign({}, this.opts, data);
  74. // this.opts = UTSJSONObject.assign(this.opts, data);
  75. this.opts = updateOptions(this.opts, data)
  76. this.opts.updateData = true;
  77. this.opts.animation = true
  78. this.opts.tooltip = null
  79. let scrollPosition = data.get('scrollPosition') ?? 'current';
  80. switch (scrollPosition) {
  81. case 'current':
  82. this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  83. break;
  84. case 'left':
  85. this.opts._scrollDistance_ = 0;
  86. this.scrollOption.currentOffset = 0
  87. this.scrollOption.startTouchX = 0
  88. this.scrollOption.distance = 0
  89. this.scrollOption.lastMoveTime = 0
  90. break;
  91. case 'right':
  92. let _calYAxisData = calYAxisData(this.opts.series!, this.opts, this.config)
  93. let yAxisWidth = _calYAxisData.yAxisWidth!;
  94. this.config.yAxisWidth = yAxisWidth; //这里类型后续需要兼容才行
  95. let offsetLeft = 0;
  96. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories!, this.opts, this.config)
  97. let xAxisPoints = _getXAxisPoints0.xAxisPoints
  98. let startX = _getXAxisPoints0.startX!
  99. let endX = _getXAxisPoints0.endX!
  100. let eachSpacing = _getXAxisPoints0.eachSpacing!
  101. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  102. let screenWidth = endX - startX;
  103. offsetLeft = screenWidth - totalWidth;
  104. this.scrollOption.currentOffset = offsetLeft
  105. this.scrollOption.startTouchX = offsetLeft
  106. this.scrollOption.distance = 0
  107. this.scrollOption.lastMoveTime = 0
  108. this.opts._scrollDistance_ = offsetLeft;
  109. break;
  110. }
  111. drawCharts(this, this.opts.type, this.opts, this.config, this.context);
  112. }
  113. zoom(e : ChartOptionsXAxis | null) {
  114. let val : ChartOptionsXAxis = e ?? this.opts.xAxis
  115. if (this.opts.enableScroll != true) {
  116. console.log('[uCharts] 请启用滚动条后使用')
  117. return;
  118. }
  119. //当前屏幕中间点
  120. let centerPoint = Math.round(Math.abs(this.scrollOption.currentOffset) / this.opts.chartData.eachSpacing) + Math.round(this.opts.xAxis.itemCount / 2);
  121. this.opts.animation = false;
  122. this.opts.xAxis.itemCount = val.itemCount;
  123. //重新计算x轴偏移距离
  124. let _calYAxisData = calYAxisData(this.opts.series!, this.opts, this.config)
  125. let yAxisWidth = _calYAxisData.yAxisWidth!;
  126. this.config.yAxisWidth = yAxisWidth;
  127. let offsetLeft = 0;
  128. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories!, this.opts, this.config)
  129. let xAxisPoints = _getXAxisPoints0.xAxisPoints
  130. let startX = _getXAxisPoints0.startX!
  131. let endX = _getXAxisPoints0.endX!
  132. let eachSpacing = _getXAxisPoints0.eachSpacing!
  133. let centerLeft = eachSpacing * centerPoint;
  134. let screenWidth = endX - startX;
  135. let MaxLeft = screenWidth - eachSpacing * (xAxisPoints.length - 1);
  136. offsetLeft = screenWidth / 2 - centerLeft;
  137. if (offsetLeft > 0) {
  138. offsetLeft = 0;
  139. }
  140. if (offsetLeft < MaxLeft) {
  141. offsetLeft = MaxLeft;
  142. }
  143. this.scrollOption.currentOffset = offsetLeft
  144. this.scrollOption.startTouchX = 0
  145. this.scrollOption.distance = 0
  146. this.scrollOption.lastMoveTime = 0
  147. calValidDistance(this, offsetLeft, this.opts.chartData, this.opts);
  148. this.opts._scrollDistance_ = offsetLeft;
  149. drawCharts(this, this.opts.type, this.opts, this.config, this.context);
  150. }
  151. dobuleZoom(e : UniTouchEvent) {
  152. //问题有点多暂时不翻译
  153. // if (this.opts.enableScroll != true) {
  154. // console.log('[uCharts] 请启用滚动条后使用')
  155. // return;
  156. // }
  157. // const tcs : Touch[] = e.changedTouches;
  158. // if (tcs.length < 2) {
  159. // return;
  160. // }
  161. // // for (let i = 0; i < tcs.length; i++) {
  162. // // tcs[i].x = tcs[i].x ? tcs[i].x : tcs[i].clientX;
  163. // // tcs[i].y = tcs[i].y ? tcs[i].y : tcs[i].clientY;
  164. // // }
  165. // const ntcs = [getTouches(this.opts, e, 0), getTouches(this.opts, e, 1)];
  166. // const xlength = Math.abs(ntcs[0].x - ntcs[1].x);
  167. // // 记录初始的两指之间的数据
  168. // if (!this.scrollOption.moveCount) {
  169. // let cts0 = { changedTouches: [{ x: tcs[0].clientX, y: this.opts.area[0] / this.opts.pixelRatio + 2 }] };
  170. // let cts1 = { changedTouches: [{ x: tcs[1].clientX, y: this.opts.area[0] / this.opts.pixelRatio + 2 }] };
  171. // if (this.opts.rotate) {
  172. // cts0 = { changedTouches: [{ x: this.opts.height / this.opts.pixelRatio - this.opts.area[0] / this.opts.pixelRatio - 2, y: tcs[0].clientY }] };
  173. // cts1 = { changedTouches: [{ x: this.opts.height / this.opts.pixelRatio - this.opts.area[0] / this.opts.pixelRatio - 2, y: tcs[1].clientY }] };
  174. // }
  175. // const moveCurrent1 = this.getCurrentDataIndex(cts0).index;
  176. // const moveCurrent2 = this.getCurrentDataIndex(cts1).index;
  177. // const moveCount = Math.abs(moveCurrent1 - moveCurrent2);
  178. // this.scrollOption.moveCount = moveCount;
  179. // this.scrollOption.moveCurrent1 = Math.min(moveCurrent1, moveCurrent2);
  180. // this.scrollOption.moveCurrent2 = Math.max(moveCurrent1, moveCurrent2);
  181. // return;
  182. // }
  183. // let currentEachSpacing = xlength / this.scrollOption.moveCount;
  184. // let itemCount = (this.opts.width - this.opts.area[1] - this.opts.area[3]) / currentEachSpacing;
  185. // itemCount = itemCount <= 2 ? 2 : itemCount;
  186. // itemCount = itemCount >= this.opts.categories.length ? this.opts.categories.length : itemCount;
  187. // this.opts.animation = false;
  188. // this.opts.xAxis.itemCount = itemCount;
  189. // // 重新计算滚动条偏移距离
  190. // let offsetLeft = 0;
  191. // let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config),
  192. // xAxisPoints = _getXAxisPoints0.xAxisPoints,
  193. // startX = _getXAxisPoints0.startX,
  194. // endX = _getXAxisPoints0.endX,
  195. // eachSpacing = _getXAxisPoints0.eachSpacing;
  196. // let currentLeft = eachSpacing * this.scrollOption.moveCurrent1;
  197. // let screenWidth = endX - startX;
  198. // let MaxLeft = screenWidth - eachSpacing * (xAxisPoints.length - 1);
  199. // offsetLeft = -currentLeft + Math.min(ntcs[0].x, ntcs[1].x) - this.opts.area[3] - eachSpacing;
  200. // if (offsetLeft > 0) {
  201. // offsetLeft = 0;
  202. // }
  203. // if (offsetLeft < MaxLeft) {
  204. // offsetLeft = MaxLeft;
  205. // }
  206. // this.scrollOption.currentOffset = offsetLeft;
  207. // this.scrollOption.startTouchX = 0;
  208. // this.scrollOption.distance = 0;
  209. // calValidDistance(this, offsetLeft, this.opts.chartData, this.config, this.opts);
  210. // this.opts._scrollDistance_ = offsetLeft;
  211. // drawCharts(this, this.opts.type, this.opts, this.config, this.context);
  212. }
  213. // stopAnimation() {
  214. // this.animationInstance && this.animationInstance.stop();
  215. // }
  216. // addEventListener(type, listener) {
  217. // this.uevent.addEventListener(type, listener);
  218. // }
  219. // delEventListener(type) {
  220. // this.uevent.delEventListener(type);
  221. // }
  222. getLegendDataIndex(e : UniEvent) : number {
  223. let touches = getTouches(this.opts, e);
  224. touches.x = touches.x - this.opts.xOffset
  225. touches.y = touches.y - this.opts.yOffset
  226. return findLegendIndex(touches, this.opts.chartData.legendData);
  227. }
  228. touchLegend(e : UniEvent) : number {
  229. let index = this.getLegendDataIndex(e);
  230. if (index >= 0) {
  231. this.clearTooltip()
  232. if (this.opts.type == 'candle') {
  233. this.opts.seriesMA![index].show = !this.opts.seriesMA![index].show;
  234. } else {
  235. this.opts.series![index].show = !this.opts.series![index].show;
  236. }
  237. this.opts.animation = true
  238. this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  239. drawCharts(this, this.opts.type, this.opts, this.config, this.context);
  240. }
  241. return index
  242. }
  243. // translate(distance) {
  244. // this.scrollOption = {
  245. // currentOffset: distance,
  246. // startTouchX: distance,
  247. // distance: 0,
  248. // lastMoveTime: 0
  249. // };
  250. // let opts = assign({}, this.opts, {
  251. // _scrollDistance_: distance,
  252. // animation: false
  253. // });
  254. // drawCharts.call(this, this.opts.type, opts, this.config, this.context);
  255. // }
  256. scrollStart(e : UniEvent) {
  257. let touches = getTouches(this.opts, e);
  258. if (this.opts.enableScroll == true) {
  259. this.scrollOption.startTouchX = touches.x;
  260. }
  261. }
  262. scroll(e : UniEvent) {
  263. this.opts.showTooltip = false
  264. if (this.scrollOption.lastMoveTime == 0) {
  265. this.scrollOption.lastMoveTime = Date.now();
  266. }
  267. let Limit = this.opts.touchMoveLimit;
  268. let currMoveTime = Date.now();
  269. let duration = currMoveTime - this.scrollOption.lastMoveTime;
  270. if (duration < Math.floor(1000 / Limit)) return;
  271. if (this.scrollOption.startTouchX == 0) return;
  272. this.scrollOption.lastMoveTime = currMoveTime;
  273. if (this.opts.enableScroll == true) {
  274. let touches = getTouches(this.opts, e);
  275. let _distance = touches.x - this.scrollOption.startTouchX;
  276. let currentOffset = this.scrollOption.currentOffset;
  277. let validDistance = calValidDistance(this, currentOffset + _distance, this.opts.chartData, this.opts);
  278. _distance = validDistance - currentOffset;
  279. // 将计算出的滚动距离赋值给this.scrollOption.distance
  280. this.scrollOption.distance = _distance;
  281. this.opts._scrollDistance_ = currentOffset + _distance
  282. this.opts.animation = false
  283. drawCharts(this, this.opts.type, this.opts, this.config, this.context);
  284. // return currentOffset + _distance;
  285. }
  286. }
  287. scrollEnd(e : UniEvent) {
  288. if (this.opts.enableScroll == true) {
  289. let _scrollOption = this.scrollOption
  290. let currentOffset = _scrollOption.currentOffset
  291. let distance = _scrollOption.distance
  292. this.scrollOption.currentOffset = currentOffset + distance;
  293. this.scrollOption.distance = 0;
  294. this.scrollOption.moveCount = 0;
  295. }
  296. }
  297. showToolTip(...args : any[]) : number {
  298. let tipsIndex : number = -1
  299. const opts : ChartOptions = this.opts
  300. if (opts.extra.tooltip != null) {
  301. this.opts.showTooltip = true
  302. const e = args[0] as UniEvent
  303. const option : ChartOptionsTooltipOption = args.length > 1 && args[1] != null ? args[1] as ChartOptionsTooltipOption : new ChartOptionsTooltipOption();
  304. const _touches$ : TouchPosition = getTouches(this.opts, e);
  305. const currentOffset : number = this.scrollOption.currentOffset;
  306. opts._scrollDistance_ = currentOffset
  307. opts.animation = false
  308. if (this.opts.type == 'line' || this.opts.type == 'area' || this.opts.type == 'column' || this.opts.type == 'scatter' || this.opts.type == 'bubble') {
  309. let current : ChartOptionsTooltip = this.getCurrentDataIndex(e);
  310. let index : any | null = option.index == null ? current.index : option.index;
  311. if (index != null && (index as number) > -1) {
  312. tipsIndex = index as number
  313. let seriesData = getSeriesDataItem(this.opts.series!, index, current.group);
  314. if (seriesData.length != 0) {
  315. const _getToolTipData : ChartOptionsTooltipOption = getToolTipData(seriesData, this.opts, index, current.group, this.opts.categories!, option)
  316. const textList : TextList[] = _getToolTipData.textList!
  317. const offset : Offset = _getToolTipData.offset!
  318. offset.y = _touches$.y;
  319. opts.tooltip = new ChartOptionsTooltip()
  320. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  321. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  322. opts.tooltip!.option = option
  323. opts.tooltip!.index = index
  324. opts.tooltip!.group = current.group
  325. }
  326. } else {
  327. this.clearTooltip()
  328. }
  329. drawCharts(this, opts.type, opts, this.config, this.context);
  330. }
  331. if (this.opts.type == 'mount') {
  332. let index : any | null = option.index == null ? this.getCurrentDataIndex(e).index : option.index;
  333. if (index != null) {
  334. tipsIndex = index as number
  335. if (index as number > -1) {
  336. this.opts.animation = false
  337. let opts = this.opts
  338. let seriesData = opts._series_![index as number];
  339. const textlist : TextList = new TextList()
  340. if (option.formatter != null) {
  341. const fun = option.formatter as (seriesData : ChartOptionsSeries, index : number, opts : ChartOptions) => string
  342. textlist.text = fun(seriesData, index, opts)
  343. } else {
  344. textlist.text = seriesData.name + ': ' + seriesData.data![0].value
  345. }
  346. textlist.color = seriesData.color!
  347. textlist.legendShape = this.opts.extra.tooltip!.legendShape == 'auto' ? seriesData.legendShape! : this.opts.extra.tooltip!.legendShape
  348. const textList : TextList[] = [textlist]
  349. const offset = new Offset()
  350. offset.x = opts.chartData.calPoints![0][index].x
  351. offset.y = _touches$.y
  352. opts.tooltip = new ChartOptionsTooltip()
  353. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  354. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  355. opts.tooltip!.option = option
  356. opts.tooltip!.index = index
  357. }
  358. } else {
  359. this.clearTooltip()
  360. }
  361. drawCharts(this, opts.type, opts, this.config, this.context);
  362. }
  363. if (this.opts.type == 'bar') {
  364. let current = this.getCurrentDataIndex(e);
  365. let index : any | null = option.index == null ? current.index : option.index;
  366. if (index != null) {
  367. tipsIndex = index as number
  368. if (index as number > -1) {
  369. let seriesData = getSeriesDataItem(this.opts.series!, index, current.group);
  370. if (seriesData.length != 0) {
  371. let _getToolTipData = getToolTipData(seriesData, this.opts, index, current.group, this.opts.categories!, option)
  372. let textList = _getToolTipData.textList!
  373. let offset = _getToolTipData.offset!
  374. offset.x = _touches$.x;
  375. offset.y = offset.y + this.opts.yOffset
  376. opts.tooltip = new ChartOptionsTooltip()
  377. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  378. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  379. opts.tooltip!.option = option
  380. opts.tooltip!.index = index
  381. }
  382. }
  383. } else {
  384. this.clearTooltip()
  385. }
  386. drawCharts(this, opts.type, opts, this.config, this.context);
  387. }
  388. if (this.opts.type == 'mix') {
  389. let current = this.getCurrentDataIndex(e);
  390. let index : any | null = option.index == null ? current.index : option.index;
  391. if (index != null) {
  392. tipsIndex = index as number
  393. if ((index as number) > -1) {
  394. let currentOffset = this.scrollOption.currentOffset;
  395. let opts = this.opts
  396. opts._scrollDistance_ = currentOffset
  397. opts.animation = false
  398. let seriesData = getSeriesDataItem(this.opts.series!, index, current.group);
  399. if (seriesData.length != 0) {
  400. let _getMixToolTipData = getMixToolTipData(seriesData, this.opts, index, this.opts.categories!, option)
  401. let textList = _getMixToolTipData.textList!
  402. let offset = _getMixToolTipData.offset!
  403. offset.y = _touches$.y;
  404. opts.tooltip = new ChartOptionsTooltip()
  405. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  406. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  407. opts.tooltip!.option = option
  408. opts.tooltip!.index = index
  409. }
  410. }
  411. } else {
  412. this.clearTooltip()
  413. }
  414. drawCharts(this, opts.type, opts, this.config, this.context);
  415. }
  416. if (this.opts.type == 'candle') {
  417. let current = this.getCurrentDataIndex(e);
  418. let index : any | null = option.index == null ? current.index : option.index;
  419. if (index != null) {
  420. tipsIndex = index as number
  421. if (index as number > -1) {
  422. let currentOffset = this.scrollOption.currentOffset;
  423. let opts = this.opts
  424. opts._scrollDistance_ = currentOffset
  425. opts.animation = false
  426. let seriesData = getSeriesDataItem(this.opts.series!, index, current.group);
  427. if (seriesData.length != 0) {
  428. let _getToolTipData = getCandleToolTipData(this.opts.series![0].dataArr, seriesData, this.opts, index, this.opts.categories!, this.opts.extra.candle!, option)
  429. let textList = _getToolTipData.textList!
  430. let offset = _getToolTipData.offset!
  431. offset.y = _touches$.y;
  432. opts.tooltip = new ChartOptionsTooltip()
  433. opts.tooltip!.textList = option.textList ?? textList
  434. opts.tooltip!.offset = option.offset ?? offset
  435. opts.tooltip!.option = option
  436. opts.tooltip!.index = index
  437. }
  438. }
  439. } else {
  440. this.clearTooltip()
  441. }
  442. drawCharts(this, opts.type, opts, this.config, this.context);
  443. }
  444. if (this.opts.type == 'pie' || this.opts.type == 'ring' || this.opts.type == 'rose' || this.opts.type == 'funnel') {
  445. let index : any | null = option.index == null ? this.getCurrentDataIndex(e).index! : option.index;
  446. if (index != null) {
  447. tipsIndex = index as number
  448. if (index as number > -1) {
  449. let opts = this.opts
  450. opts.animation = false
  451. let seriesData : ChartOptionsSeries = opts._series_![index as number];
  452. const textlist : TextList = new TextList()
  453. if (option.formatter != null) {
  454. const fun = option.formatter as (seriesData : ChartOptionsSeries, index : any, opts : ChartOptions) => string
  455. textlist.text = fun(seriesData, index, opts)
  456. } else {
  457. textlist.text = seriesData.name + ': ' + seriesData.data![0].value
  458. }
  459. textlist.color = seriesData.color!
  460. textlist.legendShape = this.opts.extra.tooltip!.legendShape == 'auto' ? seriesData.legendShape! : this.opts.extra.tooltip!.legendShape
  461. const textList : TextList[] = [textlist]
  462. let offset = new Offset()
  463. offset.x = _touches$.x
  464. offset.y = _touches$.y
  465. opts.tooltip = new ChartOptionsTooltip()
  466. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  467. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  468. opts.tooltip!.option = option
  469. opts.tooltip!.index = index
  470. } else {
  471. this.clearTooltip()
  472. }
  473. drawCharts(this, opts.type, opts, this.config, this.context);
  474. }
  475. }
  476. if (this.opts.type == 'map') {
  477. let index : any | null = option.index == null ? this.getCurrentDataIndex(e).index : option.index;
  478. if (index != null) {
  479. tipsIndex = index as number
  480. if (index as number > -1) {
  481. let opts = this.opts
  482. opts.animation = false
  483. let seriesData = this.opts.series![index];
  484. seriesData.name = seriesData.properties.name
  485. const textList : TextList[] = []
  486. const text = new TextList()
  487. if (option.formatter != null) {
  488. const fun = option.formatter as (item : ChartOptionsSeries, index : number, opts : ChartOptions) => string
  489. text.text = fun(seriesData, index, this.opts)
  490. } else {
  491. text.text = seriesData.name!
  492. }
  493. text.color = seriesData.color!
  494. text.legendShape = this.opts.extra.tooltip!.legendShape == 'auto' ? seriesData.legendShape! : this.opts.extra.tooltip!.legendShape
  495. textList.push(text)
  496. const offset = new Offset()
  497. offset.x = _touches$.x - this.opts.xOffset
  498. offset.y = _touches$.y - this.opts.yOffset
  499. opts.tooltip = new ChartOptionsTooltip()
  500. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  501. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  502. opts.tooltip!.option = option
  503. opts.tooltip!.index = index
  504. } else {
  505. this.clearTooltip()
  506. }
  507. opts.updateData = false;
  508. drawCharts(this, opts.type, opts, this.config, this.context);
  509. }
  510. }
  511. if (this.opts.type == 'word') {
  512. let index : any | null = option.index == null ? this.getCurrentDataIndex(e).index : option.index;
  513. if (index != null) {
  514. tipsIndex = index as number
  515. if (index as number > -1) {
  516. let opts = this.opts
  517. opts.animation = false
  518. let seriesData = this.opts.series![index as number];
  519. const textlist : TextList = new TextList()
  520. if (option.formatter != null) {
  521. const fun = option.formatter as (seriesData : ChartOptionsSeries, index : any, opts : ChartOptions) => string
  522. textlist.text = fun(seriesData, index, opts)
  523. } else {
  524. textlist.text = seriesData.name!
  525. }
  526. textlist.color = seriesData.color!
  527. textlist.legendShape = this.opts.extra.tooltip!.legendShape == 'auto' ? seriesData.legendShape! : this.opts.extra.tooltip!.legendShape
  528. const textList : TextList[] = [textlist]
  529. let offset = new Offset()
  530. offset.x = _touches$.x
  531. offset.y = _touches$.y
  532. opts.tooltip = new ChartOptionsTooltip()
  533. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  534. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  535. opts.tooltip!.option = option
  536. opts.tooltip!.index = index
  537. } else {
  538. this.clearTooltip()
  539. }
  540. opts.updateData = false;
  541. drawCharts(this, opts.type, opts, this.config, this.context);
  542. }
  543. }
  544. if (this.opts.type == 'radar') {
  545. const current = this.getCurrentDataIndex(e)
  546. let index = option.index == null ? current.index : option.index;
  547. if (index != null) {
  548. tipsIndex = index as number
  549. if ((index as number) > -1) {
  550. let opts = this.opts;
  551. opts.animation = false
  552. let seriesData = getSeriesDataItem(this.opts.series!, index, current.group);
  553. if (seriesData.length != 0) {
  554. let textList : TextList[] = seriesData.map((item : ChartOptionsSeries) : TextList => {
  555. const list = new TextList()
  556. if (option.formatter != null) {
  557. const fun = option.formatter as (item : ChartOptionsSeries, categories : string, index : number, opts : ChartOptions) => string
  558. list.text = fun(item, this.opts.categories![index], index, this.opts)
  559. } else {
  560. list.text = item.name + ': ' + item.data![0].value!
  561. }
  562. list.color = item.color!
  563. list.legendShape = this.opts.extra.tooltip!.legendShape == 'auto' ? item.legendShape! : this.opts.extra.tooltip!.legendShape
  564. return list
  565. });
  566. let offset = new Offset()
  567. offset.x = _touches$.x
  568. offset.y = _touches$.y
  569. opts.tooltip = new ChartOptionsTooltip()
  570. opts.tooltip!.textList = option.textList != null ? option.textList! : textList
  571. opts.tooltip!.offset = option.offset != null ? option.offset! : offset
  572. opts.tooltip!.option = option
  573. opts.tooltip!.index = index
  574. }
  575. } else {
  576. this.clearTooltip()
  577. }
  578. drawCharts(this, opts.type, opts, this.config, this.context);
  579. }
  580. }
  581. }
  582. return tipsIndex
  583. }
  584. getCurrentDataIndex(e : UniEvent) : ChartOptionsTooltip {
  585. let tooltip : ChartOptionsTooltip = new ChartOptionsTooltip()
  586. let _touches$ : TouchPosition = getTouches(this.opts, e);
  587. _touches$.x = _touches$.x - this.opts.xOffset
  588. _touches$.y = _touches$.y - this.opts.yOffset
  589. if (this.opts.type == 'pie' || this.opts.type == 'ring') {
  590. return findPieChartCurrentIndex(_touches$, this.opts.chartData.pieData!, this.opts);
  591. } else if (this.opts.type == 'rose') {
  592. return findRoseChartCurrentIndex(_touches$, this.opts.chartData.pieData!, this.opts);
  593. } else if (this.opts.type == 'radar') {
  594. return findRadarChartCurrentIndex(_touches$, this.opts.chartData.radarData!, this.opts.categories!.length);
  595. } else if (this.opts.type == 'funnel') {
  596. _touches$.x = _touches$.x + this.opts.xOffset
  597. _touches$.y = _touches$.y + this.opts.yOffset
  598. return findFunnelChartCurrentIndex(_touches$, this.opts.chartData.funnelData!);
  599. } else if (this.opts.type == 'map') {
  600. return findMapChartCurrentIndex(_touches$, this.opts);
  601. } else if (this.opts.type == 'word') {
  602. return findWordChartCurrentIndex(_touches$, this.opts.chartData.wordCloudData!);
  603. } else if (this.opts.type == 'bar') {
  604. tooltip = findBarChartCurrentIndex(_touches$, this.opts.chartData.calPoints, this.opts, Math.abs(this.scrollOption.currentOffset));
  605. } else if (this.opts.type == 'candle') {
  606. tooltip = findCandleChartCurrentIndex(_touches$, this.opts.chartData.calMapPoints!, this.opts, Math.abs(this.scrollOption.currentOffset));
  607. } else {
  608. tooltip = findCurrentIndex(_touches$, this.opts.chartData.calPoints!, this.opts, Math.abs(this.scrollOption.currentOffset));
  609. }
  610. return tooltip
  611. }
  612. }