You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.9 KiB
JavaScript

3 months ago
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.doUpdate = doUpdate;
exports.Updater = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var Updater = function Updater(config, series) {
(0, _classCallCheck2["default"])(this, Updater);
var chart = config.chart,
key = config.key,
getGraphConfig = config.getGraphConfig;
if (typeof getGraphConfig !== 'function') {
console.warn('Updater need function getGraphConfig!');
return;
}
if (!chart[key]) this.graphs = chart[key] = [];
Object.assign(this, config);
this.update(series);
};
exports.Updater = Updater;
Updater.prototype.update = function (series) {
var _this = this;
var graphs = this.graphs,
beforeUpdate = this.beforeUpdate;
delRedundanceGraph(this, series);
if (!series.length) return;
var beforeUpdateType = (0, _typeof2["default"])(beforeUpdate);
series.forEach(function (seriesItem, i) {
if (beforeUpdateType === 'function') beforeUpdate(graphs, seriesItem, i, _this);
var cache = graphs[i];
if (cache) {
changeGraphs(cache, seriesItem, i, _this);
} else {
addGraphs(graphs, seriesItem, i, _this);
}
});
};
function delRedundanceGraph(updater, series) {
var graphs = updater.graphs,
render = updater.chart.render;
var cacheGraphNum = graphs.length;
var needGraphNum = series.length;
if (cacheGraphNum > needGraphNum) {
var needDelGraphs = graphs.splice(needGraphNum);
needDelGraphs.forEach(function (item) {
return item.forEach(function (g) {
return render.delGraph(g);
});
});
}
}
function changeGraphs(cache, seriesItem, i, updater) {
var getGraphConfig = updater.getGraphConfig,
render = updater.chart.render,
beforeChange = updater.beforeChange;
var configs = getGraphConfig(seriesItem, updater);
balanceGraphsNum(cache, configs, render);
cache.forEach(function (graph, j) {
var config = configs[j];
if (typeof beforeChange === 'function') beforeChange(graph, config);
updateGraphConfigByKey(graph, config);
});
}
function balanceGraphsNum(graphs, graphConfig, render) {
var cacheGraphNum = graphs.length;
var needGraphNum = graphConfig.length;
if (needGraphNum > cacheGraphNum) {
var lastCacheGraph = graphs.slice(-1)[0];
var needAddGraphNum = needGraphNum - cacheGraphNum;
var needAddGraphs = new Array(needAddGraphNum).fill(0).map(function (foo) {
return render.clone(lastCacheGraph);
});
graphs.push.apply(graphs, (0, _toConsumableArray2["default"])(needAddGraphs));
} else if (needGraphNum < cacheGraphNum) {
var needDelCache = graphs.splice(needGraphNum);
needDelCache.forEach(function (g) {
return render.delGraph(g);
});
}
}
function addGraphs(graphs, seriesItem, i, updater) {
var getGraphConfig = updater.getGraphConfig,
getStartGraphConfig = updater.getStartGraphConfig,
chart = updater.chart;
var render = chart.render;
var startConfigs = null;
if (typeof getStartGraphConfig === 'function') startConfigs = getStartGraphConfig(seriesItem, updater);
var configs = getGraphConfig(seriesItem, updater);
if (!configs.length) return;
if (startConfigs) {
graphs[i] = startConfigs.map(function (config) {
return render.add(config);
});
graphs[i].forEach(function (graph, i) {
var config = configs[i];
updateGraphConfigByKey(graph, config);
});
} else {
graphs[i] = configs.map(function (config) {
return render.add(config);
});
}
var afterAddGraph = updater.afterAddGraph;
if (typeof afterAddGraph === 'function') afterAddGraph(graphs[i]);
}
function updateGraphConfigByKey(graph, config) {
var keys = Object.keys(config);
keys.forEach(function (key) {
if (key === 'shape' || key === 'style') {
graph.animation(key, config[key], true);
} else {
graph[key] = config[key];
}
});
}
function doUpdate() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
chart = _ref.chart,
series = _ref.series,
key = _ref.key,
getGraphConfig = _ref.getGraphConfig,
getStartGraphConfig = _ref.getStartGraphConfig,
beforeChange = _ref.beforeChange,
beforeUpdate = _ref.beforeUpdate,
afterAddGraph = _ref.afterAddGraph;
if (chart[key]) {
chart[key].update(series);
} else {
chart[key] = new Updater({
chart: chart,
key: key,
getGraphConfig: getGraphConfig,
getStartGraphConfig: getStartGraphConfig,
beforeChange: beforeChange,
beforeUpdate: beforeUpdate,
afterAddGraph: afterAddGraph
}, series);
}
}