@@ -3,10 +3,6 @@ const url = require("url");
33const util = require ( 'util' ) ;
44const { createChart } = require ( '@antv/g2-ssr' ) ;
55const 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
117http . 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
7659function 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}
0 commit comments