@amcharts/amcharts5
Version:
amCharts 5
151 lines • 4.79 kB
JavaScript
import { Series } from "../../core/render/Series";
import { MapPolygon } from "./MapPolygon";
import { Template } from "../../core/util/Template";
import { ListTemplate } from "../../core/util/List";
/**
* Holds instances of nodes for a [[MapSankeySeries]].
*
* @see {@link https://www.amcharts.com/docs/v5/charts/map-chart/map-sankey-series/} for more info
* @since 5.17.0
* @important
*/
export class MapSankeyNodes extends Series {
constructor() {
super(...arguments);
/**
* Flag indicating user has explicitly set node data.
* Prevents auto-clearing of nodes when link data changes.
* @ignore
*/
this._userDataSet = false;
/**
* A [[ListTemplate]] of all node polygons.
*
* `mapPolygons.template` can be used to configure node appearance.
*
* @default new ListTemplate<MapPolygon>
*/
this.mapPolygons = this.addDisposer(new ListTemplate(Template.new({}), () => MapPolygon._new(this._root, {}, [this.mapPolygons.template])));
}
/**
* @ignore
*/
_applyThemes(force = false) {
const colors = this.get("colors");
if (colors) {
colors.reset();
}
return super._applyThemes(force);
}
/**
* @ignore
*/
_afterNew() {
this.fields.push("name", "fill", "longitude", "latitude");
this._setRawDefault("idField", "id");
this._setRawDefault("nameField", "name");
this._setRawDefault("fillField", "fill");
this._setRawDefault("longitudeField", "longitude");
this._setRawDefault("latitudeField", "latitude");
super._afterNew();
}
/**
* @ignore
*/
_onDataClear() {
const colors = this.get("colors");
if (colors) {
colors.reset();
}
this._userDataSet = true;
}
/**
* Processes a newly added node data item, assigning a fill color
* and creating its map polygon.
*
* @param dataItem Data item to process
*/
processDataItem(dataItem) {
super.processDataItem(dataItem);
// Auto-assign fill from ColorSet if not provided
if (dataItem.get("fill") == null) {
const colors = this.get("colors");
if (colors) {
dataItem.setRaw("fill", colors.next());
}
}
// Create MapPolygon visual
const mapPolygon = this.makeMapPolygon(dataItem);
dataItem.setRaw("mapPolygon", mapPolygon);
}
/**
* @ignore
*/
_updateNodeColor(dataItem) {
const mapPolygon = dataItem.get("mapPolygon");
if (mapPolygon) {
mapPolygon.set("fill", dataItem.get("fill"));
}
}
/**
* Creates a MapPolygon visual for a node data item.
* @ignore
*/
makeMapPolygon(dataItem) {
const mapPolygon = this.mapPolygons.make();
this.mapPolygons.push(mapPolygon);
mapPolygon._setDataItem(dataItem);
// Set series to parent MapSankeySeries for projection access
if (this.flow) {
mapPolygon.series = this.flow;
this.flow.children.push(mapPolygon);
}
// Apply fill from data item and listen for changes
const fill = dataItem.get("fill");
if (fill) {
mapPolygon._setSoft("fill", fill);
}
dataItem.on("fill", () => {
this._updateNodeColor(dataItem);
});
return mapPolygon;
}
/**
* Add an incoming link to a node data item.
* @ignore
*/
addIncomingLink(dataItem, link) {
let incoming = dataItem.get("incomingLinks");
if (!incoming) {
incoming = [];
dataItem.setRaw("incomingLinks", incoming);
}
incoming.push(link);
}
/**
* Add an outgoing link to a node data item.
* @ignore
*/
addOutgoingLink(dataItem, link) {
let outgoing = dataItem.get("outgoingLinks");
if (!outgoing) {
outgoing = [];
dataItem.setRaw("outgoingLinks", outgoing);
}
outgoing.push(link);
}
/**
* @ignore
*/
disposeDataItem(dataItem) {
super.disposeDataItem(dataItem);
const mapPolygon = dataItem.get("mapPolygon");
if (mapPolygon) {
this.mapPolygons.removeValue(mapPolygon);
mapPolygon.dispose();
}
}
}
MapSankeyNodes.className = "MapSankeyNodes";
MapSankeyNodes.classNames = Series.classNames.concat([MapSankeyNodes.className]);
//# sourceMappingURL=MapSankeyNodes.js.map