Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 修改仪表盘并添加demo #1392

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions packages/f2/src/components/gauge/gaugeView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { jsx } from '../../jsx';

export default (props) => {
const { center, startAngle, endAngle, r, percent, ticks } = props;
const { x, y } = center;
const diff = endAngle - startAngle;
return (
<group>
<arc
attrs={{
x,
y,
r,
startAngle,
endAngle,
lineWidth: '20px',
lineCap: 'round',
stroke: '#e7e7e7',
}}
/>
<arc
attrs={{
x,
y,
r,
startAngle,
endAngle: startAngle,
lineWidth: '40px',
lineCap: 'round',
stroke: '#0075ff',
}}
animation={{
appear: {
easing: 'linear',
duration: 500,
property: ['endAngle'],
start: {
endAngle: startAngle,
},
end: {
endAngle: startAngle + diff * percent,
},
},
}}
/>
{ticks.map((tick) => {
const { start, end } = tick;
return (
<line
attrs={{
x1: start.x,
y1: start.y,
x2: end.x,
y2: end.y,
lineWidth: '6px',
lineCap: 'round',
stroke: '#e7e7e7',
}}
/>
);
})}
</group>
);
};
90 changes: 4 additions & 86 deletions packages/f2/src/components/gauge/index.tsx
Original file line number Diff line number Diff line change
@@ -1,87 +1,5 @@
import { jsx } from '../../jsx';
import Component from '../../base/component';
import withGauge from './withGauge';
import GaugeView from './gaugeView';

const getPoint = (cener, angle, r) => {
const x = cener.x + Math.cos(angle) * r;
const y = cener.y + Math.sin(angle) * r;
return { x, y };
};

const getTicks = (start, end, tickCount) => {
const ticks = [];
const diff = end - start;
for (let i = 0; i <= tickCount; i++) {
ticks.push(start + (diff * i) / tickCount);
}
return ticks;
};

class Guage extends Component {
render() {
const { center, startAngle, endAngle, r, percent, tickCount } = this.props;

const diff = endAngle - startAngle;
const { x, y } = center;
const ticks = getTicks(startAngle, endAngle, tickCount);
return (
<group>
<arc
attrs={{
x,
y,
r,
startAngle,
endAngle,
lineWidth: '20px',
lineCap: 'round',
stroke: '#e7e7e7',
}}
/>
<arc
attrs={{
x,
y,
r,
startAngle,
endAngle: startAngle,
lineWidth: '40px',
lineCap: 'round',
stroke: '#0075ff',
}}
animation={{
appear: {
easing: 'linear',
duration: 500,
property: ['endAngle'],
start: {
endAngle: startAngle,
},
end: {
endAngle: startAngle + diff * percent,
},
},
}}
/>
{ticks.map((tick) => {
const start = getPoint(center, tick, 65);
const end = getPoint(center, tick, 75);
return (
<line
attrs={{
x1: start.x,
y1: start.y,
x2: end.x,
y2: end.y,
lineWidth: '6px',
lineCap: 'round',
stroke: '#e7e7e7',
}}
/>
);
})}
</group>
);
}
}

export default Guage;
export { withGauge, GaugeView };
export default withGauge(GaugeView);
52 changes: 52 additions & 0 deletions packages/f2/src/components/gauge/withGauge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { jsx } from '../../jsx';
import Component from '../../base/component';

const getPoint = (cener, angle, r) => {
const x = cener.x + Math.cos(angle) * r;
const y = cener.y + Math.sin(angle) * r;
return { x, y };
};

const getTicks = (
start: number,
end: number,
tickCount: number,
center,
r: number,
tickOffset: number,
tickLength: number
) => {
const ticks = [];
const diff = end - start;
for (let i = 0; i <= tickCount; i++) {
const tickValue = start + (diff * i) / tickCount;
const startPoint = getPoint(center, tickValue, r + tickOffset - tickLength);
const endPoint = getPoint(center, tickValue, r + tickOffset);
ticks.push({
tickValue,
start: startPoint,
end: endPoint,
});
}
return ticks;
};

export default (View) => {
return class Guage extends Component {
render() {
const { props, context } = this;
const { startAngle, endAngle, tickCount, center, r, tickOffset, tickLength } = props;

const ticks = getTicks(
startAngle,
endAngle,
tickCount,
center,
context.px2hd(r),
context.px2hd(tickOffset),
context.px2hd(tickLength)
);
return <View {...props} ticks={ticks} />;
}
};
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/f2/test/components/gauge/gauge.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe('Gauge', () => {
percent={0.5}
r="200px"
tickCount={6}
tickOffset="-40px"
tickLength="20px"
/>
</Canvas>
);
Expand Down
21 changes: 21 additions & 0 deletions packages/site/examples/other/area/demo/gauge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { jsx, Canvas, Gauge } from '@antv/f2';

const context = document.getElementById('container').getContext('2d');

const { props } = (
<Canvas context={context} pixelRatio={window.devicePixelRatio}>
<Gauge
center={{ x: 150, y: 150 }}
startAngle={Math.PI}
endAngle={Math.PI * 2}
percent={0.5}
r="200px"
tickCount={6}
tickOffset="-40px"
tickLength="20px"
/>
</Canvas>
);

const chart = new Canvas(props);
chart.render();
13 changes: 13 additions & 0 deletions packages/site/examples/other/area/demo/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"title": {
"zh": "中文分类",
"en": "Category"
},
"demos": [
{
"filename": "gauge.js",
"title": "仪表盘",
"screenshot": "https://gw.alipayobjects.com/zos/finxbff/compress-tinypng/85e35a0f-177f-4527-8960-f991d2077990.png"
}
]
}
4 changes: 4 additions & 0 deletions packages/site/examples/other/area/index.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: Basic Gauge
order: 0
---
4 changes: 4 additions & 0 deletions packages/site/examples/other/area/index.zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: 仪表盘
order: 0
---