| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /**
- * 数据辅助函数模块
- * 用于处理和计算图表相关数据
- */
- // @ts-nocheck - 为了与原始 u-charts 代码保持兼容性,跳过类型检查
- import type { SeriesItem } from '../data-processing/index';
- import { measureText } from '../utils/text';
- /**
- * 获取系列数据项
- * @param series 数据系列
- * @param index 索引或索引数组
- * @param group 分组数组
- * @returns 数据项数组
- */
- export function getSeriesDataItem(
- series: SeriesItem[],
- index: number | number[],
- group: number[]
- ): Array<{
- color?: string;
- type?: string;
- style?: string;
- pointShape?: string;
- disableLegend?: boolean;
- legendShape?: string;
- name?: string;
- show?: boolean;
- data: any;
- }> {
- const data: any[] = [];
- let newSeries: SeriesItem[] = [];
- const indexIsArr = Array.isArray(index);
- if (indexIsArr) {
- const tempSeries = filterSeries(series);
- for (let i = 0; i < group.length; i++) {
- newSeries.push(tempSeries[group[i]]);
- }
- } else {
- newSeries = series;
- }
- for (let i = 0; i < newSeries.length; i++) {
- const item = newSeries[i];
- let tmpindex = -1;
- if (indexIsArr) {
- tmpindex = (index as number[])[i];
- } else {
- tmpindex = index as number;
- }
- if (
- item.data[tmpindex] !== null &&
- typeof item.data[tmpindex] !== 'undefined' &&
- item.show
- ) {
- const seriesItem: any = {};
- seriesItem.color = item.color;
- seriesItem.type = item.type;
- seriesItem.style = item.style;
- seriesItem.pointShape = item.pointShape;
- seriesItem.disableLegend = item.disableLegend;
- seriesItem.legendShape = item.legendShape;
- seriesItem.name = item.name;
- seriesItem.show = item.show;
- seriesItem.data = item.formatter
- ? item.formatter(item.data[tmpindex], tmpindex, item)
- : item.data[tmpindex];
- data.push(seriesItem);
- }
- }
- return data;
- }
- /**
- * 过滤系列数据
- * @param series 数据系列
- * @returns 过滤后的系列数据
- */
- export function filterSeries(series: SeriesItem[]): SeriesItem[] {
- const tempSeries: SeriesItem[] = [];
- for (let i = 0; i < series.length; i++) {
- if (series[i].show == true) {
- tempSeries.push(series[i]);
- }
- }
- return tempSeries;
- }
- /**
- * 分割点数据
- * @param points 点数据数组
- * @param eachSeries 系列配置
- * @returns 分割后的点数组
- */
- export function splitPoints(
- points: any[],
- eachSeries: { connectNulls?: boolean }
- ): any[][] {
- const newPoints: any[][] = [];
- const items: any[] = [];
- points.forEach(function (item) {
- if (eachSeries.connectNulls) {
- if (item !== null) {
- items.push(item);
- }
- } else {
- if (item !== null) {
- items.push(item);
- } else {
- if (items.length) {
- newPoints.push(items);
- }
- items.length = 0;
- }
- }
- });
- if (items.length) {
- newPoints.push(items);
- }
- return newPoints;
- }
- /**
- * 获取文本列表最大长度
- * @param list 文本列表
- * @param fontSize 字体大小
- * @param context Canvas上下文
- * @returns 最大长度
- */
- export function getMaxTextListLength(
- list: string[],
- fontSize: number,
- context: any
- ): number {
- const lengthList = list.map(function (item) {
- return measureText(item, fontSize, context);
- });
- return Math.max.apply(null, lengthList);
- }
- /**
- * 获取雷达图坐标系列
- * @param length 数据长度
- * @returns 坐标角度数组
- */
- export function getRadarCoordinateSeries(length: number): number[] {
- const eachAngle = (2 * Math.PI) / length;
- const CoordinateSeries: number[] = [];
- for (let i = 0; i < length; i++) {
- CoordinateSeries.push(eachAngle * i);
- }
- return CoordinateSeries.map(function (item) {
- return -1 * item + Math.PI / 2;
- });
- }
- /**
- * 获取K线图提示框数据
- * @param series K线数据系列
- * @param seriesData 系列数据
- * @param opts 图表配置
- * @param index 当前索引
- * @param categories 分类数据
- * @param extra 额外配置
- * @param option 提示框选项
- * @returns 提示框数据对象
- */
- export function getCandleToolTipData(
- series: any[],
- seriesData: any[],
- opts: any,
- index: number,
- categories: any[],
- extra: any,
- option: any = {}
- ): { textList: any[]; offset: { x: number; y: number } } {
- const calPoints = opts.chartData.calPoints;
- const upColor = extra.color.upFill;
- const downColor = extra.color.downFill;
- //颜色顺序为开盘,收盘,最低,最高
- const color = [upColor, upColor, downColor, upColor];
- const textList: any[] = [];
- seriesData.map(function (item) {
- if (index == 0) {
- if (item.data[1] - item.data[0] < 0) {
- color[1] = downColor;
- } else {
- color[1] = upColor;
- }
- } else {
- if (item.data[0] < series[index - 1][1]) {
- color[0] = downColor;
- }
- if (item.data[1] < item.data[0]) {
- color[1] = downColor;
- }
- if (item.data[2] > series[index - 1][1]) {
- color[2] = upColor;
- }
- if (item.data[3] < series[index - 1][1]) {
- color[3] = downColor;
- }
- }
- const text1 = {
- text: '开盘:' + item.data[0],
- color: color[0],
- legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
- };
- const text2 = {
- text: '收盘:' + item.data[1],
- color: color[1],
- legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
- };
- const text3 = {
- text: '最低:' + item.data[2],
- color: color[2],
- legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
- };
- const text4 = {
- text: '最高:' + item.data[3],
- color: color[3],
- legendShape: opts.extra.tooltip.legendShape == 'auto' ? item.legendShape : opts.extra.tooltip.legendShape
- };
- textList.push(text1, text2, text3, text4);
- });
- const validCalPoints: any[] = [];
- const offset = {
- x: 0,
- y: 0
- };
- for (let i = 0; i < calPoints.length; i++) {
- const points = calPoints[i];
- if (typeof points[index] !== 'undefined' && points[index] !== null) {
- validCalPoints.push(points[index]);
- }
- }
- offset.x = Math.round(validCalPoints[0][0].x);
- return {
- textList: textList,
- offset: offset
- };
- }
|