Разработка плагина таблицы и перенос его из проекта в проект в ApacheSuperset.
В продолжение темы о разработке плагина для ApacheSuperset, возникла идея создать чарт, ориентированный под решения узкой задачи для тестирования работы кастомных визуализаций и проверки реализуемых возможностей. В этой статье будет описан процесс разработки таблицы с возможностью градиентной заливки ячеек в выбранной строке заданными цветами.
npm i @ant-design/icons
npm i antd
npm i @ant-design/icons –force
import { Table, Tag } from 'antd';
import type { ColumnsType } from 'antd/es/table';
const dataSource = [
{
key: '1',
name: 'Mike',
age: 32,
address: '10 Downing Street',
},
{
key: '2',
name: 'John',
age: 42,
address: '10 Downing Street',
},
];
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
},
{
title: 'Address',
dataIndex: 'address',
key: 'address',
},
];
return (
<Styles
ref={rootElem}
boldText={props.boldText}
headerFontSize={props.headerFontSize}
height={height}
width={width}
>
<Table dataSource={dataSource} columns={columns} />
</Styles>
);
}
npm i <название библиотеки>
npm install --save-dev process
SELECT '2022' as name, 14000 as Jan, 14 as Feb, 5 as Mar, 10 as Apr, 35 as May, 16 as Jun, 15 as Jul, 65 as Aug, 2 as Sep, 2 as Oct, 3 as Nov, 6 as Dec
union all
SELECT '2023' as name, 13000 as Jan, 15 as Feb, 10 as Mar, 7 as Apr, 33 as May, 18 as Jun, 19 as Jul, 55 as Aug, 1 as Sep, 4 as Oct, 3 as Nov, 3 as Dec
union all
SELECT 'Прирост' as name, 6 as Jan, 5 as Feb, 4 as Mar, 3 as Apr, 2 as May, 1 as Jun, 0 as Jul, -2 as Aug, -4 as Sep, -6 as Oct, -8 as Nov, -10 as Dec
const { data, height, width} = props;
const data22: DataType[] = data;
return (
<Styles
ref={rootElem}
boldText={props.boldText}
headerFontSize={props.headerFontSize}
height={height}
width={width}
>
<Table dataSource={data22} size="small"/>
</Styles>
);
controlSetRows: [
[
{
name: 'col_name',
config: {
type: 'TextControl',
default: 'met',
renderTrigger: true,
label: t('Column for customize'),
},
},
],
[
{
name: 'row_name',
config: {
type: 'TextControl',
default: 'Прирост',
renderTrigger: true,
label: t('Row for customize'),
},
},
],
[
{
name: 'color1',
config: {
type: 'TextControl',
default: '#F79689',
renderTrigger: true,
label: t('Color < '),
},
},
],
[
{
name: 'color2',
config: {
type: 'TextControl',
default: '#C0F789',
renderTrigger: true,
label: t('Color >= '),
},
},
],
[
{
name: 'relative_number',
config: {
type: 'TextControl',
default: '0',
renderTrigger: true,
label: t('Relative Number'),
},
},
],
const { width, height, formData, queriesData } = chartProps;
const { boldText, headerFontSize, colName, rowName, color1, color2, relativeNumber } = formData;
const data = queriesData[0].data as TimeseriesDataRecord[];
console.log('formData via TransformProps.ts', formData);
return {
width,
height,
data,
boldText,
headerFontSize,
colName,
rowName,
color1,
color2,
relativeNumber,
};
}
export interface SuperTableStylesProps {
height: number;
width: number;
headerFontSize: keyof typeof supersetTheme.typography.sizes;
boldText: boolean;
colName: string;
rowName: string;
color1: string;
color2: string;
relativeNumber: number;
}
import React, { useEffect, createRef } from 'react';
import { styled } from '@superset-ui/core';
import { SuperTableProps, SuperTableStylesProps } from './types';
import { Table, Tag } from 'antd';
import type { ColumnsType } from 'antd/es/table';
const Styles = styled.div<SuperTableStylesProps>`
height: ${({ height }) => height}px;
width: ${({ width }) => width}px;
colName: ${({ colName }) => colName};
color1: ${({ color1 }) => color1};
color2: ${({ color2 }) => color2};
rowName: ${({ rowName }) => rowName};
padding: 2px;
`;
var numberFormat = new Intl.NumberFormat('ru-RU');
function hexToRGB(hex = 'FF0000', alpha = 0.1) {
var r = parseInt(hex.slice(1, 3), 16),
g = parseInt(hex.slice(3, 5), 16),
b = parseInt(hex.slice(5, 7), 16);
return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
}
function getColorRGB(
hex='FF0000',
val=1,
arr=[1,2,3],
isMax=1,
relativeNumber=0) {
var min = isMax == 1 ? Math.min(...arr) : relativeNumber,
max = isMax == 1 ? relativeNumber : Math.max(...arr),
alpha = isMax == 1 ? (max - val)/(max - min) : (val - min)/(max - min) ; //
console.log('Plugin', min, max,val);
return hexToRGB(hex, alpha);
}
export default function SuperTable(props: SuperTableProps) {
const { data, height, width} = props;
const rootElem = createRef<HTMLDivElement>();
useEffect(() => {
const root = rootElem.current as HTMLElement;
console.log('Plugin element', root);
});
interface DataType {
[data: string]: any;
}
const columns: ColumnsType<DataType> = [];
//console.log('Plugin color', props.color2);
for (let colval in Object.keys(data[0])) {
let coltitle = Object.keys(data[0])[colval];
if (coltitle != props.colName) {
columns.push({
title: coltitle,
dataIndex: coltitle,
key: coltitle,
render: (met, key) => {
let c = 'white';
if (props.rowName.split(', ').includes(key[props.colName]) )
{
const result = Object.values(data.reduce((res, obj) => obj.name == props.rowName ? obj : res, {}));
var arr_val: number[] = [];
for (var i = 1; i < result.length; i++) {
arr_val.push(result[i]);
}
c = (met >= props.relativeNumber) ?
getColorRGB( props.color2, met, arr_val, 0, props.relativeNumber) :
getColorRGB( props.color1, met, arr_val, 1, props.relativeNumber) ; //props.color2
}
return (
<Tag style={{display: 'block', background: c, border: 'none'}} key={met} >
{numberFormat.format(met)}
</Tag>
);
}
})
}
else {
columns.push({
title: coltitle,
dataIndex: coltitle,
key: coltitle,
})
}
}
const data22: DataType[] = data;
return (
<Styles
ref={rootElem}
boldText={props.boldText}
headerFontSize={props.headerFontSize}
height={height}
width={width}
>
<Table columns={columns} dataSource={data22} size="small"/>
</Styles>
);
}
npm login
npm init
npm publish
npm i @superset-cat/super-table
import { SuperTable } from 'super-table';
new SuperTable().configure({ key: 'super-table' }),
sudo docker build -f Dockerfile --force-rm -t apache/superset:${TAG:-latest-<Номер образа>} /home/juls/tst/superset
sudo docker-compose -f docker-compose-non-dev.yml up –d