# nashorn-vega **Repository Path**: DLX4/nashorn-vega-test ## Basic Information - **Project Name**: nashorn-vega - **Description**: 在jdk8内置的nashorn搜索引擎上实现了图表的服务端渲染(SVG模式)。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-12-17 - **Last Updated**: 2022-01-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # nashorn-vega-test #### 介绍 在jdk8内置的nashorn搜索引擎上实现了图表的服务端渲染(SVG模式)。 #### 软件架构 图表渲染使用[vega](https://github.com/vega/vega)。 另外为了能在nashorn上成功运行SVG模式的图表渲染,还需要一些必要的polyfill: - [nashorn-polyfill](https://github.com/shendepu/nashorn-polyfill) 最新版本(5.21.0)的vega使用ES6写法可以编译成ES5,但是仍需要一些polyfill: - babel-polyfill - regenerator-runtime #### 运行示例 ``` String vegaSpec = "{\n" + " \"$schema\": \"https://vega.github.io/schema/vega/v5.json\",\n" + " \"description\": \"A basic line chart example.\",\n" + " \"width\": 500,\n" + " \"height\": 200,\n" + " \"padding\": 5,\n" + "\n" + " \"signals\": [\n" + " {\n" + " \"name\": \"interpolate\",\n" + " \"value\": \"linear\",\n" + " \"bind\": {\n" + " \"input\": \"select\",\n" + " \"options\": [\n" + " \"basis\",\n" + " \"cardinal\",\n" + " \"catmull-rom\",\n" + " \"linear\",\n" + " \"monotone\",\n" + " \"natural\",\n" + " \"step\",\n" + " \"step-after\",\n" + " \"step-before\"\n" + " ]\n" + " }\n" + " }\n" + " ],\n" + "\n" + " \"data\": [\n" + " {\n" + " \"name\": \"table\",\n" + " \"values\": [\n" + " {\"x\": \"中0\", \"y\": 28, \"c\":0}, {\"x\": \"中0\", \"y\": 20, \"c\":1},\n" + " {\"x\": \"中1\", \"y\": 43, \"c\":0}, {\"x\": \"中1\", \"y\": 35, \"c\":1},\n" + " {\"x\": \"中2\", \"y\": 81, \"c\":0}, {\"x\": \"中2\", \"y\": 10, \"c\":1},\n" + " {\"x\": \"中3\", \"y\": 19, \"c\":0}, {\"x\": \"中3\", \"y\": 15, \"c\":1},\n" + " {\"x\": \"中4\", \"y\": 52, \"c\":0}, {\"x\": \"中4\", \"y\": 48, \"c\":1},\n" + " {\"x\": \"中5\", \"y\": 24, \"c\":0}, {\"x\": \"中5\", \"y\": 28, \"c\":1},\n" + " {\"x\": \"中6\", \"y\": 87, \"c\":0}, {\"x\": \"中6\", \"y\": 66, \"c\":1},\n" + " {\"x\": \"中7\", \"y\": 17, \"c\":0}, {\"x\": \"中7\", \"y\": 27, \"c\":1},\n" + " {\"x\": \"中8\", \"y\": 68, \"c\":0}, {\"x\": \"中8\", \"y\": 16, \"c\":1},\n" + " {\"x\": \"中9\", \"y\": 49, \"c\":0}, {\"x\": \"中9\", \"y\": 25, \"c\":1}\n" + " ]\n" + " }\n" + " ],\n" + "\n" + " \"scales\": [\n" + " {\n" + " \"name\": \"x\",\n" + " \"type\": \"point\",\n" + " \"range\": \"width\",\n" + " \"domain\": {\"data\": \"table\", \"field\": \"x\"}\n" + " },\n" + " {\n" + " \"name\": \"y\",\n" + " \"type\": \"linear\",\n" + " \"range\": \"height\",\n" + " \"nice\": true,\n" + " \"zero\": true,\n" + " \"domain\": {\"data\": \"table\", \"field\": \"y\"}\n" + " },\n" + " {\n" + " \"name\": \"color\",\n" + " \"type\": \"ordinal\",\n" + " \"range\": \"category\",\n" + " \"domain\": {\"data\": \"table\", \"field\": \"c\"}\n" + " }\n" + " ],\n" + "\n" + " \"axes\": [\n" + " {\"orient\": \"bottom\", \"scale\": \"x\"},\n" + " {\"orient\": \"left\", \"scale\": \"y\"}\n" + " ],\n" + "\n" + " \"marks\": [\n" + " {\n" + " \"type\": \"group\",\n" + " \"from\": {\n" + " \"facet\": {\n" + " \"name\": \"series\",\n" + " \"data\": \"table\",\n" + " \"groupby\": \"c\"\n" + " }\n" + " },\n" + " \"marks\": [\n" + " {\n" + " \"type\": \"line\",\n" + " \"from\": {\"data\": \"series\"},\n" + " \"encode\": {\n" + " \"enter\": {\n" + " \"x\": {\"scale\": \"x\", \"field\": \"x\"},\n" + " \"y\": {\"scale\": \"y\", \"field\": \"y\"},\n" + " \"stroke\": {\"scale\": \"color\", \"field\": \"c\"},\n" + " \"strokeWidth\": {\"value\": 2}\n" + " },\n" + " \"update\": {\n" + " \"interpolate\": {\"signal\": \"interpolate\"},\n" + " \"strokeOpacity\": {\"value\": 1}\n" + " },\n" + " \"hover\": {\n" + " \"strokeOpacity\": {\"value\": 0.5}\n" + " }\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " ]\n" + " }"; System.out.println(renderVegaSVG(vegaSpec)); ``` 输出: ``` 中0中1中2中3中4中5中6中7中8中90102030405060708090 ``` #### 扩展资料 1. [各种JS引擎对比介绍](https://hllvm-group.iteye.com/group/topic/37596) 2. [bestofjs对vega polyfill的介绍](https://bestofjs.org/projects/vega) 3. [vega使用示例](https://vega.github.io/vega/examples/line-chart/) 4. [vega的一个issue提供了本项目的思路](https://github.com/vega/vega/issues/601)