ze-react-component-library
Version:
ZeroETP React Component Library
145 lines (122 loc) • 4.64 kB
JavaScript
import { getNameProperty } from "zeroetp-api-sdk";
import { formatWithProperty, formatResultName, getExportFileName } from "../../util";
export var tooltipFormatter = function tooltipFormatter(_a) {
var params = _a.params,
properties = _a.properties,
defaultName = _a.defaultName;
var res = "";
if (!params) {
return res;
}
var data = params instanceof Array ? params : [params];
data.slice(0, 1).forEach(function (d) {
var _a;
var name = d.name || defaultName;
if ((_a = d === null || d === void 0 ? void 0 : d.data) === null || _a === void 0 ? void 0 : _a.labelName) {
name = ("" + d.data.labelName).replace("\n", "<br />");
}
var itemTip = name ? "" + name : "";
if (itemTip.length > 0) {
itemTip += "<br />";
}
properties.forEach(function (property, index) {
var _a, _b, _c;
var v = (_a = d === null || d === void 0 ? void 0 : d.data) === null || _a === void 0 ? void 0 : _a[property.name];
var displayV = formatWithProperty(property, v); // 内置字段处理
if (property.name.startsWith('@@')) {
if (property.name === '@@value') {
itemTip = ((_b = data[index] || d) === null || _b === void 0 ? void 0 : _b.marker) + "\n " + name + (property.unit ? "(" + property.unit + ")" : "") + " <span style=\"float:right;margin-left:20px;font-size:14px;color:#666;font-weight:900\">" + displayV + "</span><br />";
}
} else {
itemTip = "" + itemTip + ((_c = data[index] || d) === null || _c === void 0 ? void 0 : _c.marker) + property.name + "\n " + (property.unit ? "(" + property.unit + ")" : "") + " <span style=\"float:right;margin-left:20px;font-size:14px;color:#666;font-weight:900\">" + displayV + "</span><br />";
}
});
res = "" + res + itemTip;
});
return res;
};
export var getChartDataset = function getChartDataset(apiResult, useNameAsDimension) {
var dataset = {
source: [],
dimensions: []
};
if ((apiResult === null || apiResult === void 0 ? void 0 : apiResult.result) && (apiResult === null || apiResult === void 0 ? void 0 : apiResult.columnProperties)) {
// dataset source
dataset.source = formatResultName(apiResult); // dataset dimensions
dataset.dimensions = apiResult.columnProperties.map(function (property) {
if (useNameAsDimension && property.type === "object" && property.schema) {
var nameProp = getNameProperty(property.schema);
if (nameProp) {
return "" + property.name;
}
}
return property.name;
});
}
return dataset;
}; // 这里填上所有图表默认的option
export default (function (apiResult, isMobile, useNameAsDimension, // 主要给地图用,地图的话,dimension要设置为geo entity的name
theme, name) {
var _a, _b, _c, _d;
if (isMobile === void 0) {
isMobile = false;
}
if (useNameAsDimension === void 0) {
useNameAsDimension = false;
}
if (theme === void 0) {
theme = "light";
}
var textColor = theme === "dark" ? "#fff" : "#000";
var option = {
legend: {
show: ((_a = apiResult.columnProperties) === null || _a === void 0 ? void 0 : _a.filter(function (f) {
return f.primal_type === "number";
}).length) > 1,
type: "scroll",
padding: [0, 40],
textStyle: {
color: textColor
}
},
toolbox: {
show: !isMobile,
feature: {
saveAsImage: {
title: "下载图片",
name: getExportFileName(name, "")
}
}
},
tooltip: {
confine: true,
trigger: "axis",
extraCssText: "white-space: unset;"
},
barMaxWidth: 48,
grid: {
left: "1%",
right: "12%",
bottom: 0,
containLabel: true
},
animation: false,
textStyle: {
color: textColor
}
}; // dataset
option.dataset = getChartDataset(apiResult, useNameAsDimension);
if ((apiResult === null || apiResult === void 0 ? void 0 : apiResult.result) && (apiResult === null || apiResult === void 0 ? void 0 : apiResult.columnProperties)) {
// values
var measurementProps_1 = apiResult.columnProperties.slice((_d = (_c = (_b = apiResult.logicform) === null || _b === void 0 ? void 0 : _b.groupby) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0); // tooltip formatter
if (!Array.isArray(option.tooltip)) {
option.tooltip.formatter = function (params) {
return tooltipFormatter({
params: params,
properties: measurementProps_1
});
};
}
}
return option;
});