Skip to content

Commit 7dda942

Browse files
committed
feat: add matrix component
1 parent 1892938 commit 7dda942

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

src/components.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
MarkAreaComponentOption,
88
MarkLineComponentOption,
99
MarkPointComponentOption,
10+
MatrixComponentOption,
1011
PolarComponentOption,
1112
TitleComponentOption,
1213
ToolboxComponentOption,
@@ -21,6 +22,7 @@ import {
2122
MarkAreaComponent,
2223
MarkLineComponent,
2324
MarkPointComponent,
25+
MatrixComponent,
2426
PolarComponent,
2527
TitleComponent,
2628
ToolboxComponent,
@@ -62,6 +64,8 @@ export const MarkLine = /*#__PURE__*/ defineComponent<MarkLineComponentOption>(M
6264

6365
export const MarkPoint = /*#__PURE__*/ defineComponent<MarkPointComponentOption>(MarkPointComponent);
6466

67+
export const Matrix = /*#__PURE__*/ defineComponent<MatrixComponentOption>(MatrixComponent);
68+
6569
export const Polar = /*#__PURE__*/ defineComponent<PolarComponentOption>(PolarComponent);
6670

6771
export const Title = /*#__PURE__*/ defineComponent<TitleComponentOption>(TitleComponent);

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export {
77
MarkArea,
88
MarkLine,
99
MarkPoint,
10+
Matrix,
1011
Polar,
1112
Title,
1213
Toolbox,

stories/line.stories.tsx

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MarkArea,
1111
MarkLine,
1212
MarkPoint,
13+
Matrix,
1314
PieChart,
1415
Polar,
1516
Title,
@@ -2243,3 +2244,153 @@ export function DatasetLink() {
22432244
</LineChart>
22442245
);
22452246
}
2247+
2248+
export function MatrixSparkline() {
2249+
const _matrixDimensionData = {
2250+
x: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
2251+
y: [
2252+
{ value: '8:00\n~\n10:00' },
2253+
{ value: '10:00\n~\n12:00' },
2254+
{ value: '12:00\n~\n14:00', size: 55 },
2255+
{ value: '14:00\n~\n16:00' },
2256+
{ value: '16:00\n~\n18:00' },
2257+
{ value: '18:00\n~\n20:00' },
2258+
],
2259+
};
2260+
const _yBreakTimeIndex = 2; // '12:00 - 14:00',
2261+
const _seriesFakeDataLength = 365;
2262+
2263+
function makeId(xidx: number, yidx: number) {
2264+
return `${xidx}|${yidx}`;
2265+
}
2266+
function eachMatrixCell(cb: (xval: string, yval: string, xidx: number, yidx: number) => void) {
2267+
_matrixDimensionData.y.forEach((yvalItem, yidx) => {
2268+
const yval = yvalItem.value;
2269+
if (yidx === _yBreakTimeIndex) {
2270+
return;
2271+
}
2272+
_matrixDimensionData.x.forEach((xval, xidx) => {
2273+
cb(xval, yval, xidx, yidx);
2274+
});
2275+
});
2276+
}
2277+
function generateFakeSeriesData(dayCount: number, xidx: number, _yidx: number) {
2278+
const dayStart = new Date('2025-05-05T00:00:00.000Z'); // Monday
2279+
dayStart.setDate(xidx + 5);
2280+
const timeStart = dayStart.getTime();
2281+
const sevenDay = 7 * 1000 * 3600 * 24;
2282+
const cellData = [];
2283+
let lastVal = +(Math.random() * 300).toFixed(0);
2284+
let turnCount = null;
2285+
let sign = -1;
2286+
for (let idx = 0; idx < dayCount; idx++) {
2287+
if (turnCount == null || idx >= turnCount) {
2288+
turnCount = idx + Math.round((dayCount / 4) * ((Math.random() - 0.5) * 0.1));
2289+
sign = -sign;
2290+
}
2291+
const deltaMag = 50;
2292+
const delta = +(Math.random() * deltaMag - deltaMag / 2 + (sign * deltaMag) / 3).toFixed(0);
2293+
const val = Math.max(0, (lastVal += delta));
2294+
const xTime = timeStart + idx * sevenDay;
2295+
const dataXVal = echarts.time.format(xTime, '{yyyy}-{MM}-{dd}', true);
2296+
cellData.push([dataXVal, val]);
2297+
}
2298+
return cellData;
2299+
}
2300+
2301+
const grid: GridOption[] = [];
2302+
const xAxis: XAXisOption[] = [];
2303+
const yAxis: YAXisOption[] = [];
2304+
const series: LineSeriesOption[] = [];
2305+
2306+
eachMatrixCell((xval, yval, xidx, yidx) => {
2307+
const id = makeId(xidx, yidx);
2308+
grid.push({
2309+
id: id,
2310+
coordinateSystem: 'matrix',
2311+
coord: [xval, yval],
2312+
top: 10,
2313+
bottom: 10,
2314+
left: 'center',
2315+
width: '90%',
2316+
containLabel: true,
2317+
});
2318+
xAxis.push({
2319+
type: 'category',
2320+
id: id,
2321+
gridId: id,
2322+
axisTick: { show: false },
2323+
axisLabel: { show: false },
2324+
axisLine: { show: false },
2325+
splitLine: { show: false },
2326+
});
2327+
yAxis.push({
2328+
id: id,
2329+
gridId: id,
2330+
interval: Number.MAX_SAFE_INTEGER,
2331+
scale: true,
2332+
axisLabel: {
2333+
showMaxLabel: true,
2334+
fontSize: 9,
2335+
},
2336+
axisLine: { show: false },
2337+
axisTick: { show: false },
2338+
});
2339+
series.push({
2340+
xAxisId: id,
2341+
yAxisId: id,
2342+
type: 'line',
2343+
symbol: 'none',
2344+
lineStyle: { width: 1 },
2345+
data: generateFakeSeriesData(_seriesFakeDataLength, xidx, yidx),
2346+
});
2347+
});
2348+
2349+
return (
2350+
<LineChart style={{ width: 480, height: 512 }} grid={grid} xAxis={xAxis} yAxis={yAxis} series={series}>
2351+
<Legend legend={{}} />
2352+
<Tooltip tooltip={{ trigger: 'axis' }} />
2353+
<DataZoom
2354+
dataZoom={[
2355+
// @ts-expect-error xAxisIndex: 'all'
2356+
{ type: 'slider', xAxisIndex: 'all', left: '10%', right: '10%', bottom: 30, height: 30, throttle: 120 },
2357+
// @ts-expect-error xAxisIndex: 'all'
2358+
{ type: 'inside', xAxisIndex: 'all', throttle: 120 },
2359+
]}
2360+
/>
2361+
<Matrix
2362+
matrix={{
2363+
x: {
2364+
data: _matrixDimensionData.x,
2365+
levelSize: 40,
2366+
label: { fontSize: 16, color: '#555' },
2367+
},
2368+
y: {
2369+
data: _matrixDimensionData.y,
2370+
levelSize: 70,
2371+
label: { fontSize: 14, color: '#777' },
2372+
},
2373+
corner: {
2374+
data: [{ coord: [-1, -1], value: 'Time' }],
2375+
label: { fontSize: 16, color: '#777' },
2376+
},
2377+
body: {
2378+
data: [
2379+
{
2380+
coord: [null, _yBreakTimeIndex],
2381+
coordClamp: true,
2382+
mergeCells: true,
2383+
value: 'Break',
2384+
label: { color: '#999', fontSize: 16 },
2385+
},
2386+
],
2387+
},
2388+
top: 30,
2389+
bottom: 80,
2390+
width: '90%',
2391+
left: 'center',
2392+
}}
2393+
/>
2394+
</LineChart>
2395+
);
2396+
}

0 commit comments

Comments
 (0)