-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.ts
107 lines (106 loc) · 3.29 KB
/
series.ts
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
import Chart from "../chart";
import { ChartType, echarts } from "../types";
import { color } from "../constants";
import * as utils from "../utils";
import { DataFrame } from "../dataframe";
export function series<T extends ChartType>(
chart: Chart<T>,
datasets: echarts.DataSet[]
): echarts.Series[] {
const series: echarts.Series[] = [];
const colors: string[] = [];
datasets.slice(1).forEach((dataset) => {
if (!dataset.id) throw new Error("Dataset for series must include ID");
let [mix, t, dix, name] = dataset.id.split("::");
const metric = chart.getMetric(
(m) =>
m.index === Number(mix) && (m.chartType ?? chart.getChartType()) === t
);
if (!metric) throw new Error("Metric not found");
const colorId = `${mix}-${metric.color}`;
const c = colors.includes(colorId)
? utils.array.unboundedReadItem(color.COLOR_PALETTE, Number(dix) - 1)
: metric.color;
colors.push(colorId);
t = t === "calendar" ? "heatmap" : t;
const item: echarts.Series = {
type: t,
color: c,
datasetIndex: Number(dix),
name: name,
};
if (chart.getStyleOrientation() === "horizontal") {
item.xAxisIndex = 0;
item.encode = { x: dataset.dimensions[1], y: dataset.dimensions[0] };
} else if (chart.getChartType() === "pie") {
item.encode = {
itemName: dataset.dimensions[0],
value: dataset.dimensions[1],
};
item.itemStyle = {
borderColor: color.ZINC_900,
borderRadius: 10,
borderWidth: 2,
};
item.label = { color: color.ZINC_500, show: true };
item.yAxisIndex = 0;
item.textStyle = { color: color.ZINC_500 };
item.radius = ["40%", "70%"];
} else if (chart.getChartType() === "scatter") {
const metrics = chart.getMetrics();
item.symbolSize = 15;
item.encode = {
x: dataset.dimensions[metrics[0].index],
y: dataset.dimensions[metrics[1].index],
tooltip: [
dataset.dimensions[chart.getGroupByDimension()?.index ?? 0],
dataset.dimensions[metrics[0].index],
dataset.dimensions[metrics[1].index],
],
};
} else if (chart.getChartType() === "calendar") {
const df = DataFrame.fromDataSet(dataset);
Array.from(
new Set(
df
.col(0)
.map((v) => new Date(String(v)))
.map((d) => d.getUTCFullYear())
.sort()
)
).forEach((_, i) => {
series.push({
type: "heatmap",
coordinateSystem: "calendar",
name: name,
datasetIndex: 1,
calendarIndex: i,
});
});
return;
} else if (chart.getChartType() === "heatmap") {
delete item.color;
item.datasetIndex = 1;
item.encode = {
x: dataset.dimensions[0],
y: dataset.dimensions[1],
value: dataset.dimensions[2],
};
item.name = dataset.dimensions[2];
} else {
item.yAxisIndex = 0;
item.encode = { x: dataset.dimensions[0], y: dataset.dimensions[1] };
}
if (
chart.getStyleBarStyle() === "stacked" ||
chart.getStyleLineStyle() === "area"
) {
item.stack = "total";
if (chart.getChartType() === "line") {
item.areaStyle = {};
}
}
series.push(item);
});
return series;
}