Spaces:
Running
Running
File size: 13,815 Bytes
4bb817b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* global Uint32Array, Float64Array, Float32Array */
import SeriesModel from '../../model/Series';
import SeriesData from '../../data/SeriesData';
import { concatArray, mergeAll, map, isNumber } from 'zrender/src/core/util';
import CoordinateSystem from '../../core/CoordinateSystem';
import {
SeriesOption,
SeriesOnCartesianOptionMixin,
SeriesOnGeoOptionMixin,
SeriesOnPolarOptionMixin,
SeriesOnCalendarOptionMixin,
SeriesLargeOptionMixin,
LineStyleOption,
OptionDataValue,
StatesOptionMixin,
SeriesLineLabelOption,
DimensionDefinitionLoose,
DefaultStatesMixinEmphasis,
ZRColor,
CallbackDataParams
} from '../../util/types';
import GlobalModel from '../../model/Global';
import type { LineDrawModelOption } from '../helper/LineDraw';
import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup';
const Uint32Arr = typeof Uint32Array === 'undefined' ? Array : Uint32Array;
const Float64Arr = typeof Float64Array === 'undefined' ? Array : Float64Array;
function compatEc2(seriesOpt: LinesSeriesOption) {
const data = seriesOpt.data;
if (data && data[0] && (data as LegacyDataItemOption[][])[0][0] && (data as LegacyDataItemOption[][])[0][0].coord) {
if (__DEV__) {
console.warn('Lines data configuration has been changed to'
+ ' { coords:[[1,2],[2,3]] }');
}
seriesOpt.data = map(data as LegacyDataItemOption[][], function (itemOpt) {
const coords = [
itemOpt[0].coord, itemOpt[1].coord
];
const target: LinesDataItemOption = {
coords: coords
};
if (itemOpt[0].name) {
target.fromName = itemOpt[0].name;
}
if (itemOpt[1].name) {
target.toName = itemOpt[1].name;
}
return mergeAll([target, itemOpt[0], itemOpt[1]]);
});
}
}
type LinesCoords = number[][];
type LinesValue = OptionDataValue | OptionDataValue[];
interface LinesLineStyleOption<TClr> extends LineStyleOption<TClr> {
curveness?: number
}
// @deprecated
interface LegacyDataItemOption {
coord: number[]
name: string
}
interface LinesStatesMixin {
emphasis?: DefaultStatesMixinEmphasis
}
export interface LinesStateOption<TCbParams = never> {
lineStyle?: LinesLineStyleOption<(TCbParams extends never ? never : (params: TCbParams) => ZRColor) | ZRColor>
label?: SeriesLineLabelOption
}
export interface LinesDataItemOption extends LinesStateOption,
StatesOptionMixin<LinesStateOption, LinesStatesMixin> {
name?: string
fromName?: string
toName?: string
symbol?: string[] | string
symbolSize?: number[] | number
coords?: LinesCoords
value?: LinesValue
effect?: LineDrawModelOption['effect']
}
export interface LinesSeriesOption
extends SeriesOption<LinesStateOption, LinesStatesMixin>, LinesStateOption<CallbackDataParams>,
SeriesOnCartesianOptionMixin, SeriesOnGeoOptionMixin, SeriesOnPolarOptionMixin,
SeriesOnCalendarOptionMixin, SeriesLargeOptionMixin {
type?: 'lines'
coordinateSystem?: string
symbol?: string[] | string
symbolSize?: number[] | number
effect?: LineDrawModelOption['effect']
/**
* If lines are polyline
* polyline not support curveness, label, animation
*/
polyline?: boolean
/**
* If clip the overflow.
* Available when coordinateSystem is cartesian or polar.
*/
clip?: boolean
data?: LinesDataItemOption[]
// Stored as a flat array. In format
// Points Count(2) | x | y | x | y | Points Count(3) | x | y | x | y | x | y |
| ArrayLike<number>
dimensions?: DimensionDefinitionLoose | DimensionDefinitionLoose[]
}
class LinesSeriesModel extends SeriesModel<LinesSeriesOption> {
static readonly type = 'series.lines';
readonly type = LinesSeriesModel.type;
static readonly dependencies = ['grid', 'polar', 'geo', 'calendar'];
visualStyleAccessPath = 'lineStyle';
visualDrawType = 'stroke' as const;
private _flatCoords: ArrayLike<number>;
private _flatCoordsOffset: ArrayLike<number>;
init(option: LinesSeriesOption) {
// The input data may be null/undefined.
option.data = option.data || [];
// Not using preprocessor because mergeOption may not have series.type
compatEc2(option);
const result = this._processFlatCoordsArray(option.data);
this._flatCoords = result.flatCoords;
this._flatCoordsOffset = result.flatCoordsOffset;
if (result.flatCoords) {
option.data = new Float32Array(result.count);
}
super.init.apply(this, arguments as any);
}
mergeOption(option: LinesSeriesOption) {
compatEc2(option);
if (option.data) {
// Only update when have option data to merge.
const result = this._processFlatCoordsArray(option.data);
this._flatCoords = result.flatCoords;
this._flatCoordsOffset = result.flatCoordsOffset;
if (result.flatCoords) {
option.data = new Float32Array(result.count);
}
}
super.mergeOption.apply(this, arguments as any);
}
appendData(params: Pick<LinesSeriesOption, 'data'>) {
const result = this._processFlatCoordsArray(params.data);
if (result.flatCoords) {
if (!this._flatCoords) {
this._flatCoords = result.flatCoords;
this._flatCoordsOffset = result.flatCoordsOffset;
}
else {
this._flatCoords = concatArray(this._flatCoords, result.flatCoords);
this._flatCoordsOffset = concatArray(this._flatCoordsOffset, result.flatCoordsOffset);
}
params.data = new Float32Array(result.count);
}
this.getRawData().appendData(params.data);
}
_getCoordsFromItemModel(idx: number) {
const itemModel = this.getData().getItemModel<LinesDataItemOption>(idx);
const coords = (itemModel.option instanceof Array)
? itemModel.option : itemModel.getShallow('coords');
if (__DEV__) {
if (!(coords instanceof Array && coords.length > 0 && coords[0] instanceof Array)) {
throw new Error(
'Invalid coords ' + JSON.stringify(coords) + '. Lines must have 2d coords array in data item.'
);
}
}
return coords;
}
getLineCoordsCount(idx: number) {
if (this._flatCoordsOffset) {
return this._flatCoordsOffset[idx * 2 + 1];
}
else {
return this._getCoordsFromItemModel(idx).length;
}
}
getLineCoords(idx: number, out: number[][]) {
if (this._flatCoordsOffset) {
const offset = this._flatCoordsOffset[idx * 2];
const len = this._flatCoordsOffset[idx * 2 + 1];
for (let i = 0; i < len; i++) {
out[i] = out[i] || [];
out[i][0] = this._flatCoords[offset + i * 2];
out[i][1] = this._flatCoords[offset + i * 2 + 1];
}
return len;
}
else {
const coords = this._getCoordsFromItemModel(idx);
for (let i = 0; i < coords.length; i++) {
out[i] = out[i] || [];
out[i][0] = coords[i][0];
out[i][1] = coords[i][1];
}
return coords.length;
}
}
_processFlatCoordsArray(data: LinesSeriesOption['data']) {
let startOffset = 0;
if (this._flatCoords) {
startOffset = this._flatCoords.length;
}
// Stored as a typed array. In format
// Points Count(2) | x | y | x | y | Points Count(3) | x | y | x | y | x | y |
if (isNumber(data[0])) {
const len = data.length;
// Store offset and len of each segment
const coordsOffsetAndLenStorage = new Uint32Arr(len) as Uint32Array;
const coordsStorage = new Float64Arr(len) as Float64Array;
let coordsCursor = 0;
let offsetCursor = 0;
let dataCount = 0;
for (let i = 0; i < len;) {
dataCount++;
const count = data[i++] as number;
// Offset
coordsOffsetAndLenStorage[offsetCursor++] = coordsCursor + startOffset;
// Len
coordsOffsetAndLenStorage[offsetCursor++] = count;
for (let k = 0; k < count; k++) {
const x = data[i++] as number;
const y = data[i++] as number;
coordsStorage[coordsCursor++] = x;
coordsStorage[coordsCursor++] = y;
if (i > len) {
if (__DEV__) {
throw new Error('Invalid data format.');
}
}
}
}
return {
flatCoordsOffset: new Uint32Array(coordsOffsetAndLenStorage.buffer, 0, offsetCursor),
flatCoords: coordsStorage,
count: dataCount
};
}
return {
flatCoordsOffset: null,
flatCoords: null,
count: data.length
};
}
getInitialData(option: LinesSeriesOption, ecModel: GlobalModel) {
if (__DEV__) {
const CoordSys = CoordinateSystem.get(option.coordinateSystem);
if (!CoordSys) {
throw new Error('Unknown coordinate system ' + option.coordinateSystem);
}
}
const lineData = new SeriesData(['value'], this);
lineData.hasItemOption = false;
lineData.initData(option.data, [], function (dataItem, dimName, dataIndex, dimIndex) {
// dataItem is simply coords
if (dataItem instanceof Array) {
return NaN;
}
else {
lineData.hasItemOption = true;
const value = dataItem.value;
if (value != null) {
return value instanceof Array ? value[dimIndex] : value;
}
}
});
return lineData;
}
formatTooltip(
dataIndex: number,
multipleSeries: boolean,
dataType: string
) {
const data = this.getData();
const itemModel = data.getItemModel<LinesDataItemOption>(dataIndex);
const name = itemModel.get('name');
if (name) {
return name;
}
const fromName = itemModel.get('fromName');
const toName = itemModel.get('toName');
const nameArr = [];
fromName != null && nameArr.push(fromName);
toName != null && nameArr.push(toName);
return createTooltipMarkup('nameValue', {
name: nameArr.join(' > ')
});
}
preventIncremental() {
return !!this.get(['effect', 'show']);
}
getProgressive() {
const progressive = this.option.progressive;
if (progressive == null) {
return this.option.large ? 1e4 : this.get('progressive');
}
return progressive;
}
getProgressiveThreshold() {
const progressiveThreshold = this.option.progressiveThreshold;
if (progressiveThreshold == null) {
return this.option.large ? 2e4 : this.get('progressiveThreshold');
}
return progressiveThreshold;
}
getZLevelKey() {
const effectModel = this.getModel('effect');
const trailLength = effectModel.get('trailLength');
return this.getData().count() > this.getProgressiveThreshold()
// Each progressive series has individual key.
? this.id
: (effectModel.get('show') && trailLength > 0 ? trailLength + '' : '');
}
static defaultOption: LinesSeriesOption = {
coordinateSystem: 'geo',
// zlevel: 0,
z: 2,
legendHoverLink: true,
// Cartesian coordinate system
xAxisIndex: 0,
yAxisIndex: 0,
symbol: ['none', 'none'],
symbolSize: [10, 10],
// Geo coordinate system
geoIndex: 0,
effect: {
show: false,
period: 4,
constantSpeed: 0,
symbol: 'circle',
symbolSize: 3,
loop: true,
trailLength: 0.2
},
large: false,
// Available when large is true
largeThreshold: 2000,
polyline: false,
clip: true,
label: {
show: false,
position: 'end'
// distance: 5,
// formatter: 标签文本格式器,同Tooltip.formatter,不支持异步回调
},
lineStyle: {
opacity: 0.5
}
};
}
export default LinesSeriesModel;
|