@zhsz/cool-design-dv
Version:
68 lines (67 loc) • 1.68 kB
JavaScript
import { ref, computed, watch } from "vue";
import themeFn from "../packages/chart/utils/theme.mjs";
function useChart($emit, props, page) {
const width = ref(0);
const height = ref(0);
const chartData = ref();
const loading = ref(true);
const theme = computed(() => {
const config = (page == null ? void 0 : page.settings) || {};
return Object.freeze(themeFn(config.value));
});
function resize($el) {
var _a, _b;
const rect = $el == null ? void 0 : $el.getBoundingClientRect();
if (!rect) {
return;
}
width.value = rect.width / (((_a = page == null ? void 0 : page.widthScale) == null ? void 0 : _a.value) ?? 1);
height.value = rect.height / (((_b = page == null ? void 0 : page.heightScale) == null ? void 0 : _b.value) ?? 1);
$emit("resize", [width.value, height.value]);
}
function load() {
if (props.loader) {
const loader = props.loader;
loading.value = true;
loader().then((data) => {
chartData.value = Object.freeze(data || {});
}).finally(() => {
loading.value = false;
});
} else {
if (props.columns && props.rows) {
chartData.value = Object.freeze({
columns: props.columns,
rows: props.rows,
type: props.type || "map"
});
}
loading.value = false;
}
}
watch(
() => props.loader,
() => {
load();
},
{
immediate: true
}
);
watch(
() => props.rows,
() => {
load();
}
);
watch(
() => props.columns,
() => {
load();
}
);
return { resize, width, height, loading, theme, chartData };
}
export {
useChart
};