Skip to content

Commit 75161d2

Browse files
feat: bar column label position
1 parent 0eee5e5 commit 75161d2

File tree

5 files changed

+34
-41
lines changed

5 files changed

+34
-41
lines changed

g2-ssr/app.js

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ const url = require("url");
33
const util = require('util');
44
const { createChart } = require('@antv/g2-ssr');
55
const port = 3000;
6-
const { getPieOptions } = require('./charts/pie.js');
7-
const { getLineOptions } = require('./charts/line.js');
8-
const { getColumnOptions } = require('./charts/column.js');
9-
const { getBarOptions } = require('./charts/bar.js');
106

117
http.createServer((req, res) => {
128
res.statusCode = 200,
@@ -20,48 +16,35 @@ http.createServer((req, res) => {
2016
console.log(`Server listening on: http://localhost:${port}`);
2117
});
2218

23-
function getOptions(type, axis, data) {
24-
25-
const base_options = {
19+
// 创建 Chart 和配置
20+
async function GenerateCharts(obj) {
21+
const options = JSON.parse(obj.options || "{}") || {
2622
width: 640,
2723
height: 480,
28-
imageType: 'png',
29-
theme: {
30-
view: {
31-
viewFill: '#FFFFFF',
32-
},
33-
}
24+
imageType: 'png', // or 'jpeg'
25+
// 其他的配置透传 G2 Spec,可以参考 G2 的配置文档
26+
type: 'interval',
27+
data: [
28+
{ genre: ' UIUI看看', sold: 278 },
29+
{ genre: 'Strategy', sold: 115 },
30+
{ genre: 'Action', sold: 120 },
31+
{ genre: 'Shooter', sold: 350 },
32+
{ genre: 'Other', sold: 150 },
33+
],
34+
encode: {
35+
x: 'genre',
36+
y: 'sold',
37+
color: 'genre',
38+
},
3439
}
35-
36-
switch (type) {
37-
case 'bar':
38-
return getBarOptions(base_options, axis, data);
39-
case 'column':
40-
return getColumnOptions(base_options, axis, data);
41-
case 'line':
42-
return getLineOptions(base_options, axis, data);
43-
case 'pie':
44-
return getPieOptions(base_options, axis, data);
45-
}
46-
47-
return base_options
48-
}
49-
50-
51-
// 创建 Chart 和配置
52-
async function GenerateCharts(obj) {
53-
console.log(obj)
54-
const options = getOptions(obj.type, JSON.parse(obj.axis), JSON.parse(obj.data));
55-
console.log(options);
5640
const chart = await createChart(options);
5741

5842
// 导出
59-
chart.exportToFile(obj.path || 'chart');
43+
chart.exportToFile(obj.id || 'chart');
6044
// -> chart.png
6145

6246
chart.toBuffer();
6347
}
64-
6548
// -> get buffer
6649

6750

@@ -74,8 +57,8 @@ function toGet(req, res) {
7457

7558
//获取POST请求内容、cookie
7659
function toPost(req, res) {
77-
req.on('data', async function (chunk) {
78-
await GenerateCharts(JSON.parse(chunk))
60+
req.on('data', function (chunk) {
61+
GenerateCharts(JSON.parse(chunk))
7962
res.end('complete');
8063
});
8164
}

g2-ssr/charts/bar.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ function getBarOptions(baseOptions, axis, data) {
9393
}
9494
return `${value}${_data.isPercent ? '%' : ''}`
9595
},
96+
position: (data) => {
97+
if (data[y[0].value] < 0) {
98+
return 'bottom'
99+
}
100+
return 'top'
101+
},
96102
transform: [
97103
{type: 'contrastReverse'},
98104
{type: 'exceedAdjust'},

g2-ssr/charts/column.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ function getColumnOptions(baseOptions, axis, data) {
9292
}
9393
return `${value}${_data.isPercent ? '%' : ''}`
9494
},
95-
position: 'top',
95+
position: (data) => {
96+
if (data[y[0].value] < 0) {
97+
return 'bottom'
98+
}
99+
return 'top'
100+
},
96101
dy: -25,
97102
transform: [
98103
{type: 'contrastReverse'},

g2-ssr/charts/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {filter, endsWith, replace} = require("lodash-es");
1+
const {filter, endsWith, replace} = require("lodash");
22

33
function checkIsPercent(valueAxis, data) {
44
const result = {

g2-ssr/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"@antv/g2": "^5.3.3",
77
"@antv/g2-ssr": "^0.1.0",
88
"@rollup/plugin-json": "^6.1.0",
9-
"lodash-es": "^4.17.21",
109
"nan": "^2.22.2",
1110
"node-canvas": "^2.9.0",
1211
"nprogress": "^0.2.0"

0 commit comments

Comments
 (0)