Skip to content

Commit

Permalink
feat: 补充legend获取逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Sep 3, 2021
1 parent 0b61e1e commit 3606ef7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 54 deletions.
7 changes: 7 additions & 0 deletions packages/f2-next/src/chart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ class Chart extends Container implements IChart, ThemeMixin, CoordMixin, ScaleMi
return geometrys[0].getSnapRecords(point);
}

getLegendItems(point) {
const geometrys = this.getGeometrys();
if (!geometrys.length) return;
// @ts-ignore
return geometrys[0].getLegendItems(point);
}

getXScales() {
const geometrys = this.getGeometrys();
return geometrys.map(component => {
Expand Down
38 changes: 36 additions & 2 deletions packages/f2-next/src/components/geometry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const GROUP_ATTRS = ["color", "size", "shape"];
class Geometry extends Component implements AttrMixin {

isGeometry = true;
isInit = false;
chart: Chart;
data: any;
attrs: any = {};
Expand Down Expand Up @@ -71,10 +72,18 @@ class Geometry extends Component implements AttrMixin {
});
}

mount() {
_init() {
if (this.isInit) {
return;
}
this._createAttrs();
this._adjustScales();
this._processData();
this.isInit = true;
}

mount() {
this._init();
}

_createAttrs() {
Expand Down Expand Up @@ -422,13 +431,38 @@ class Geometry extends Component implements AttrMixin {
const originValue = record[FIELD_ORIGIN][xfield];
if (xScale.type === 'timeCat' && toTimeStamp(originValue) === value) {
rst.push(record);
} else if (originValue === value){
} else if (originValue === value) {
rst.push(record);
}
}
}
return rst;
}

getLegendItems() {
// TODO 挪到chart里去
if (!this.isInit) {
this._init();
}
const colorAttr = this.getAttr('color');
if (!colorAttr) return null;
const { scale } = colorAttr;
if (!scale.isCategory) return null;
const { chart } = this;
const { theme } = chart;
const ticks = scale.getTicks();
colorAttr.setRange(theme.colors);
const items = ticks.map(tick => {
const { text, tickValue } = tick;
const color = colorAttr.mapping(tickValue) || theme.colors[0];
return {
color,
name: text, // for display
tickValue,
};
});
return items;
}
}

applyMixins(Geometry, [ AttrMixin ]);
Expand Down
35 changes: 21 additions & 14 deletions packages/f2-next/src/components/legend/legendView.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { jsx } from '../../jsx';

export default (props) => {
const { items } = props;
return null;
const { items, layout } = props;
const { width } = layout;
return (
<group style={{
left: 50,
top: 50,
width: 100,
flexDirection: 'row'
width,
flexDirection: 'row',
flexWrap: 'wrap',
paddingBottom: '20px'
}}>
{
items.map(item => {
const { color, name, value } = item;
return (
<group style={{ flexDirection: 'row', flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<group style={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
padding: [0, 0, '10px', '10px']
}}>
<circle
style={{
width: '20px',
height: '20px',
marginRight: '6px'
}}
attrs={{
fill: color,
Expand All @@ -28,19 +35,19 @@ export default (props) => {
<text
attrs={{
fill: 'black',
text: item.name,
text: name,
}}
/>
{
value ?
<text
attrs={{
fill: 'black',
text: `: ${value}`,
}}
/>
attrs={{
fill: 'black',
text: `: ${value}`,
}}
/>
:
null
null
}
</group>
)
Expand Down
47 changes: 12 additions & 35 deletions packages/f2-next/src/components/legend/withLegend.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,27 @@
import { jsx } from '../../jsx';
import { jsx, render, renderJSXElement } from '../../jsx';
import Component from '../../base/component';
import Layout from '../../base/layout';

export default View => {
return class Legend extends Component {

chart: any;
layout: Layout;

getGeometry() {
const { chart } = this;

const geometrys = chart.getGeometrys();
// 默认处理第一个图形
return geometrys[0];
}

mount() {
// const { layout } = this;
// this.layout = new Layout({
// left: layout.left,
// top: layout.top,
// width: 0,
// height: 0,
// });
// this.getItems();
const { chart, container } = this;
const { coord } = chart;
const shape = render(renderJSXElement(this.render()), container);
const bbox = shape.getBBox();
shape.remove();
const { top, height } = coord;
coord.update({
top: top + bbox.height,
height: height - bbox.height,
});
}

getItems() {
const geometry = this.getGeometry();
const colorOption = geometry.getAttrOption('color');
if (!colorOption) return null;
const { field } = colorOption;
const { chart } = this;
const scale = chart.getScale(field);
if (!scale.isCategory) return null;

return [{
color: 'red',
name: '1',
value: '11'
}, {
color: 'red',
name: '1',
value: '11'
}]
return chart.getLegendItems();
}

render() {
Expand Down
5 changes: 3 additions & 2 deletions packages/f2-next/src/components/tooltip/tooltipView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ export default (props) => {
return (
<group
style={{
flexDirection: 'row',
width,
}}
>
<group
style={{
display: 'flex',
flexDirection: 'row',
// paddingLeft: '10px',
flexWrap: 'wrap',
flex: 1,
}}
attrs={{
Expand All @@ -27,10 +26,12 @@ export default (props) => {
records.map(record => {
return (
<group style={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
// justifyContent: 'center',
padding: [ '6px', '10px', '6px', '10px' ],
// width: '100px'
}}>
<circle
attrs={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ describe("Chart", () => {
{/* <Axis field="genre" position="top"/> */}
<Axis field="sold" />
{/* <Axis field="sold" position="right" /> */}
<Legend />
<Interval
x="genre"
y="sold"
color="genre"
// adjust="stack"
/>
<Legend />
<Tooltip />
</Chart>
</Canvas>
Expand Down

0 comments on commit 3606ef7

Please sign in to comment.