Skip to content

Commit

Permalink
chore(graphin):fix styles
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Jan 12, 2021
1 parent 223b077 commit 336dc85
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 24 deletions.
14 changes: 11 additions & 3 deletions packages/graphin-components/src/Legend/Node.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphinContext } from '@antv/graphin/dist';
import React from 'react';
import React, { useEffect } from 'react';
import './index.less';

export interface LegendOption {
Expand All @@ -13,7 +13,6 @@ export interface LegendOption {
checked?: boolean;
}
export interface LegendProps {
options: LegendOption[];
style?: React.CSSProperties;
onChange?: (checked: LegendOption, newOptions: LegendOption[], props: any) => any; // eslint-disable-line
}
Expand All @@ -28,13 +27,21 @@ const LegendNode: React.FunctionComponent<LegendProps> = (props) => {
const { onChange = () => {}, style } = props;

const { options: defaultOptions, dataMap } = legend.node;

const [state, setState] = React.useState({
options: defaultOptions,
});

/** 更新state依赖 */
useEffect(() => {
setState({
options: defaultOptions,
});
}, [defaultOptions]);

const { options } = state;

console.log('options', options);

const handleClick = (option: LegendOption) => {
const checkedValue = { ...option, checked: !option.checked };
const result = options.map((c) => {
Expand All @@ -58,6 +65,7 @@ const LegendNode: React.FunctionComponent<LegendProps> = (props) => {
/** 给用户的回调函数 */
onChange(checkedValue, result, props);
};
console.log('%c legend Content', 'color:red');

return (
<ul className="graphin-components-legend-content" style={style}>
Expand Down
8 changes: 5 additions & 3 deletions packages/graphin-components/src/Legend/demos/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ data.nodes.forEach((node, index) => {
node.data.type = isCompany ? 'company' : 'person';
node.type = 'graphin-circle';
node.style = {
fill: isCompany ? 'green' : 'yellow',
size: [30],
keyshape: {
fill: isCompany ? 'green' : 'yellow',
size: [30],
},
icon: {
type: 'font',
fontFamily: 'graphin',
Expand All @@ -26,7 +28,7 @@ data.nodes.forEach((node, index) => {
const Demo = () => {
return (
<Graphin data={data}>
<Legend bindType="node" sortKey="data.type" colorKey="style.fill">
<Legend bindType="node" sortKey="data.type" colorKey="style.keyshape.fill">
<Legend.Node />
</Legend>
</Graphin>
Expand Down
34 changes: 34 additions & 0 deletions packages/graphin-components/src/Legend/demos/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable react/destructuring-assignment */
import * as React from 'react';
import Graphin, { Utils } from '@antv/graphin';
import iconsLoader from '@antv/graphin-icons';
import { Legend } from '@antv/graphin-components';
import { useState, useEffect } from 'react';

const Demo = (props) => {
const propsOptions = props.options;
const [state, setState] = React.useState({
options: propsOptions,
});
useEffect(() => {
setState({
options: props.options,
});
}, [props.options]);

console.log('props', propsOptions, 'state', state.options);
return <div></div>;
};

export default () => {
const [render, setRedener] = useState(0);
useEffect(() => {
setRedener(1);
}, []);
console.log('root', render);
return (
<div>
<Demo options={render} />
</div>
);
};
23 changes: 19 additions & 4 deletions packages/graphin-components/src/Legend/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import {
GraphinContext,
Graph,
Expand All @@ -19,9 +19,13 @@ export interface LegendProps {
sortKey: string;
/**
* @description 颜色映射的Key值
* @default "style.fill"
* @default "style.stroke"
*/
colorKey?: string;
/**
* @description 样式
*/
style?: React.CSSProperties;
}
export interface OptionType {
/** 颜色 */
Expand Down Expand Up @@ -54,6 +58,7 @@ const calculate = ({
colorKey: string;
}) => {
const data = graph.save();

const treeData = data as TreeGraphData;
const graphData = data as GraphData;
const nodeMapByMapKey: Map<string | number, NodeConfig[]> = new Map();
Expand Down Expand Up @@ -151,10 +156,15 @@ export interface LegendContextType extends GraphinContextType {
};
};
}
const defaultStyle: React.CSSProperties = {
position: 'absolute',
top: '0px',
right: '0px',
};
const Legend: React.FunctionComponent<LegendProps> & { Node: typeof Node } = (props) => {
const graphin = React.useContext<GraphinContextType>(GraphinContext);
const { graph } = graphin;
const { bindType, sortKey, children, colorKey = 'style.fill' } = props;
const { bindType, sortKey, children, colorKey = 'style.stroke', style } = props;
const { dataMap, options } = calculate({ bindType, sortKey, graph, colorKey });

graphin.legend = {
Expand All @@ -168,7 +178,12 @@ const Legend: React.FunctionComponent<LegendProps> & { Node: typeof Node } = (pr
options,
},
};
return <div className="graphin-components-legend">{children}</div>;
console.log('%c legend Container', 'color:red');
return (
<div className="graphin-components-legend" style={{ ...defaultStyle, ...style }}>
{children}
</div>
);
};

Legend.Node = Node;
Expand Down
21 changes: 16 additions & 5 deletions packages/graphin/docs/geamaker/demos/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import Graphin, { Behaviors } from '@antv/graphin';
import { ContextMenu, FishEye, MiniMap } from '@antv/graphin-components';
import { ContextMenu, FishEye, MiniMap, Legend } from '@antv/graphin-components';
import G6 from '@antv/g6';
import { colorSets, clusterColorMap } from './color';
import hexToRgba from '../../../src/utils/hexToRgba';
Expand Down Expand Up @@ -44,14 +44,21 @@ const transClusterData = (data, sourceData) => {
...node,
type: 'graphin-circle',
style: {
fill: hexToRgba(primaryColor, 0.1), // '#fff',
strokeWidth: 1.2,
stroke: primaryColor,
size: [nodeSize, nodeSize],
keyshape: {
fill: hexToRgba(primaryColor, 0.1), // '#fff',
strokeWidth: 1.2,
stroke: primaryColor,
size: [nodeSize, nodeSize],
},
label: {
value: `cluster-${node.id}(${node.nodes.length})`,
fill: hexToRgba('#000', '0.85'),
},
halo: {
fill: hexToRgba(primaryColor, 0.1), // '#fff',
strokeWidth: 1.2,
stroke: primaryColor,
},
icon: {
fontFamily: 'graphin',
type: 'font',
Expand Down Expand Up @@ -162,6 +169,7 @@ const App = () => {
// if (!data) {
// return null;
// }
console.log('GEAMAKER RENDER', state.data);
return (
<div>
<Graphin data={data} layout={layout} height={900}>
Expand All @@ -178,6 +186,9 @@ const App = () => {
</Menu>
</ContextMenu>
<FishEye options={{ showLabel: false }} visible={visible} handleEscListener={handleClose} />
<Legend bindType="node" sortKey="id" colorKey="style.keyshape.stroke">
<Legend.Node />
</Legend>
</Graphin>
</div>
);
Expand Down
16 changes: 7 additions & 9 deletions packages/graphin/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ export const TREE_LAYOUTS = ['dendrogram', 'compactBox', 'mindmap', 'indented'];

export const G6_DEFAULT_NODE = {
type: 'graphin-circle',
size: [30, 30],
fill: '#9EC9FF',
stroke: '#5B8FF9',
lineWidth: 3,
labelCfg: {
position: 'bottom',
},
icon: {
show: true,
style: {
keyshape: {
size: [30, 30],
fill: 'red',
stroke: 'red',
lineWidth: 3,
},
},
};

Expand Down

0 comments on commit 336dc85

Please sign in to comment.