Skip to content

Commit

Permalink
Merge pull request #263 from antvis/gauge-demos
Browse files Browse the repository at this point in the history
拯救仪表盘大作战
  • Loading branch information
paleface001 committed Nov 19, 2019
2 parents aeb57ad + 28f9110 commit 90ac31b
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A charting library based on the Grammar of Graphics.
[![NPM downloads](http://img.shields.io/npm/dm/@antv/g2plot.svg)](http://npmjs.com/@antv/g2plot)
![最近提交](https://badgen.net/github/last-commit/antvis/g2plot)

G2plot is an interactive and responsive charting library based on [the grammar of graphics](https://github.com/antvis/g2), which enables users to generate high quality statistical charts through a few lines of code easily.
g2plot is an interactive and responsive charting library based on [the grammar of graphics](https://github.com/antvis/g2), which enables users to generate high quality statistical charts through a few lines of code easily.

Moreover, combining with AntV design principles, g2plot provides standard and elegant visual styles as well as better user experience.

Expand Down
2 changes: 2 additions & 0 deletions docs/manual/plots/gauge.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: Gauge 仪表盘
order: 12
---

<img src="https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*Kq3TQ4-I710AAAAAAAAAAABkARQnAQ" width="400">

## 图表故事

仪表盘 (Gauge) 是一种拟物化的图表,刻度表示度量,指针表示维度,指针角度表示数值。仪表盘图表就像汽车的速度表一样,有一个圆形的表盘及相应的刻度,有一个指针指向当前数值。目前很多的管理报表或报告上都是用这种图表,以直观的表现出某个指标的进度或实际情况。
Expand Down
33 changes: 32 additions & 1 deletion examples/gauge/basic/demo/basic.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import { Gauge } from '@antv/g2plot';
import insertCss from 'insert-css';

insertCss(`
.g2plot-gauge-label {
width: 100px;
height: 100px;
vertical-align: middle;
text-align: center;
line-height: 0.2;
}
.g2plot-gauge-label .value {
font-size: 20px;
color: #8c8c8c;
font-weight: 300;
}
.g2plot-gauge-label .title {
font-size: 30px;
color: #30bf78;
font-weight: bold;
}
`);

const gaugePlot = new Gauge(document.getElementById('container'), {
title: {
visible: true,
text: '仪表盘',
},
width: 400,
height: 400,
value: 64,
min: 0,
max: 100,
range: [20, 40, 60, 80],
gaugeStyle: {
colors: ['#f4664a', '#faad14', '#a0d911', '#30bf78'],
tickLabelSize: 20,
},
label: () => {
return '<div class="g2plot-gauge-label"><p class="title">优</p><p class="value">系统表现</p></div>';
},
});
gaugePlot.render();
57 changes: 57 additions & 0 deletions examples/gauge/basic/demo/half.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Gauge } from '@antv/g2plot';
import insertCss from 'insert-css';

insertCss(`
.g2plot-gauge-label {
width: 100px;
height: 100px;
vertical-align: middle;
text-align: center;
line-height: 0.2;
}
.g2plot-gauge-label .value {
font-size: 16px;
color: #8c8c8c;
font-weight: 300;
}
.g2plot-gauge-label .title {
font-size: 30px;
color: #000000;
font-weight: bold;
}
`);

const gaugePlot = new Gauge(document.getElementById('container'), {
title: {
visible: true,
text: '半圆仪表盘',
},
width: 400,
height: 400,
value: 64,
min: 0,
max: 100,
range: [0, 70],
startAngle: -1,
endAngle: 0,
format: (v) => {
if (v === 0 || v === 100) {
return v + '%';
}
return '';
},
label: () => {
return '<div class="g2plot-gauge-label"><p class="title">70%</p><p class="value">加载进度</p></div>';
},
gaugeStyle: {
colors: ['l(0) 0:#b0d0ff 1:#5f92f9'],
tickLabelSize: 16,
stripWidth: 20,
pointerColor: 'rgba(0,0,0,0)',
labelPos: ['50%', '80%'],
tickLabelPos: 'outer',
},
});
gaugePlot.render();
9 changes: 7 additions & 2 deletions examples/gauge/basic/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
{
"filename": "basic.js",
"title": "仪表盘",
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*ObzGQLmAn7AAAAAAAAAAAABkARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*Kq3TQ4-I710AAAAAAAAAAABkARQnAQ"
},
{
"filename": "style.js",
"title": "仪表盘-样式",
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*xMrmS6YOWK8AAAAAAAAAAABkARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*48_yRK1sI5UAAAAAAAAAAABkARQnAQ"
},
{
"filename": "half.js",
"title": "半圆仪表盘",
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*f7NtTIlLj5QAAAAAAAAAAABkARQnAQ"
}
]
}
36 changes: 32 additions & 4 deletions examples/gauge/basic/demo/style.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import { Gauge } from '@antv/g2plot';
import insertCss from 'insert-css';

insertCss(`
.g2plot-gauge-label {
width: 100px;
height: 100px;
vertical-align: middle;
text-align: center;
line-height: 0.2;
}
.g2plot-gauge-label .value {
font-size: 20px;
color: #8c8c8c;
font-weight: 300;
}
.g2plot-gauge-label .title {
font-size: 30px;
color: #faad14;
font-weight: bold;
}
`);

const gaugePlot = new Gauge(document.getElementById('container'), {
title: {
visible: true,
text: '仪表盘样式',
},
value: 64,
width: 400,
height: 400,
value: 40,
min: 0,
max: 100,
range: [20, 40, 60, 80],
label: () => {
return '<div class="g2plot-gauge-label"><p class="title">良</p><p class="value">系统表现</p></div>';
},
gaugeStyle: {
colors: ['#1890FF', '#2FC25B', '#FACC14', '#223273', '#8543E0', '#13C2C2', '#3436C7', '#F04864'],
colors: ['#f4664a', '#faad14', '#f0f0f0', '#f0f0f0'],
tickLabelSize: 20,
// stripWidth: 30,
// stripBackColor: '#ddd',
// tickInterval: 20,
Expand All @@ -19,7 +47,7 @@ const gaugePlot = new Gauge(document.getElementById('container'), {
// tickLabelColor: '#aaa',
// tickLineColor: '#aaa',
// subTickCount: 4,
pointerColor: '#1890FF',
// pointerColor: '#1890FF',
},
});
gaugePlot.render();
4 changes: 2 additions & 2 deletions examples/liquid/basic/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
{
"filename": "basic.js",
"title": "水波图",
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*BiuARomUQakAAAAAAAAAAABkARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*6GIlSr4Ltq4AAAAAAAAAAABkARQnAQ"
},
{
"filename": "percent.js",
"title": "水波图 - 百分比",
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*foSMQaHQ0b0AAAAAAAAAAABkARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*BSy3RaswYJwAAAAAAAAAAABkARQnAQ"
}
]
}
60 changes: 48 additions & 12 deletions src/plots/gauge/geometry/shape/pointer.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,63 @@
import { registerShape } from '@antv/g2';

const POINTER_RADIUS = 10;
const POINTER_LINE_WIDTH = 4;

registerShape('point', 'pointer', {
draw(cfg, group) {
// 获取第一个标记点
let point = cfg.points[0];
point = this.parsePoint({ ...point, y: 2 / 3 });
// 获取极坐标系下画布中心点
const point = cfg.points[0];
const center = this.parsePoint({
x: 0,
y: 0,
});
const points = [
[point.x, point.y],
[center.x + (point.y - center.y) / 16, center.y - (point.x - center.x) / 16],
[(13 * center.x - point.x) / 12, (13 * center.y - point.y) / 12],
[center.x - (point.y - center.y) / 16, center.y + (point.x - center.x) / 16],
const target = this.parsePoint({
x: point.x,
y: 0.5,
});
const dir_vec = {
x: center.x - target.x,
y: center.y - target.y,
};
//normalize
const length = Math.sqrt(dir_vec.x * dir_vec.x + dir_vec.y * dir_vec.y);
dir_vec.x *= 1 / length;
dir_vec.y *= 1 / length;
//rotate dir_vector by -90 and scale
const angle1 = -Math.PI / 2;
const x_1 = Math.cos(angle1) * dir_vec.x - Math.sin(angle1) * dir_vec.y;
const y_1 = Math.sin(angle1) * dir_vec.x + Math.cos(angle1) * dir_vec.y;
//rotate dir_vector by 90 and scale
const angle2 = Math.PI / 2;
const x_2 = Math.cos(angle2) * dir_vec.x - Math.sin(angle2) * dir_vec.y;
const y_2 = Math.sin(angle2) * dir_vec.x + Math.cos(angle2) * dir_vec.y;
// startx
const angle = Math.atan2(dir_vec.y, dir_vec.x);
const offset = POINTER_LINE_WIDTH / 2 - POINTER_RADIUS;
const startX = center.x + Math.cos(angle) * offset;
const startY = center.y + Math.sin(angle) * offset;
//polygon vertex
const path = [
['M', target.x + x_1 * 1, target.y + y_1 * 1],
['L', startX + x_1 * 3, startY + y_1 * 3],
['L', startX + x_2 * 3, startY + y_2 * 3],
['L', target.x + x_2 * 1, target.y + y_2 * 1],
['Z'],
];
// 绘制指针
return group.addShape('polygon', {
group.addShape('circle', {
attrs: {
x: center.x,
y: center.y,
r: POINTER_RADIUS,
lineWidth: POINTER_LINE_WIDTH,
stroke: cfg.color,
},
});
const tick = group.addShape('path', {
attrs: {
points,
path: path,
fill: cfg.color,
},
});
return tick;
},
});
Loading

0 comments on commit 90ac31b

Please sign in to comment.