echart freemarker 模板_SpringBoot集成Freemarker模板生成Echarts圖片(三)

緊接著前面兩篇,本篇采用Freemarker模板來生成Echarts圖片。

一. 項目結構

二. Freemarker模板

(1)柱狀圖

{

"title":?{

"text":?"${title}",

"textStyle":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"tooltip":?{

"formatter":?"{a}
{b}?:?{c}",

"show":?true

},

"legend":?{

"textStyle":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder"

},

"data":?["${legend}"]

},

"xAxis":?[{

"type":?"category",

"axisLabel":?{

"show":?true,

"textStyle":?{

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"axisLine":{

"lineStyle":{

"color":?"#315070",

"width":4

}

},

"data":?${category}

}],

"yAxis":?[{

"type":?"value",

"axisLabel":?{

"show":?true,

"textStyle":?{

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"axisLine":{

"lineStyle":{

"color":"#315070",

"width":4

}

}

}],

"series":?[{

"name":?"${legend}",

"data":?${values},

"type":?"bar",

"label":?{

"normal":?{

"color":?"green",

"fontSize":?15,

"fontWeight":?"bolder",

"show":?true,

"position":?"top"

}

}

}]

}

(2)漸變色柱狀圖

{

"title":?{

"text":?"${title}",

"textStyle":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"toolbox":?{

"feature":?{},

"show":?true

},

"tooltip":?{

"formatter":?"{a}
{b}?:?{c}",

"show":?true

},

"legend":?{

"textStyle":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder"

},

"data":?["${legend}"]

},

"xAxis":?[{

"type":?"category",

"axisLabel":?{

"show":?true,

"textStyle":?{

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"axisLine":{

"lineStyle":{

"color":'#315070',

"width":4

}

},

"data":?${category}

}],

"yAxis":?[{

"type":?"value",

"axisLabel":?{

"show":?true,

"textStyle":?{

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"axisLine":{

"show":true,

"lineStyle":{

"color":'#315070',

"width":4

}

},

"axisTick":?{

"show":?true

}

}],

"series":?[{

"name":?"${legend}",

"type":?"bar",

"label":?{

"normal":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder",

"show":?true,

"position":?"top"

}

},

"data":${values},

"itemStyle":?"__itemStyle"

}]

}

(3)折線圖

{

"title":?{

"text":?"${title}",

"subtext":?"",

"textStyle":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder"

},

"x":?"left"

},

"tooltip":?{

"trigger":?"axis"

},

"legend":?{

"textStyle":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder"

},

"data":?["${legend}"]

},

"xAxis":?[{

"type":?"category",

"axisLabel":?{

"show":?true,

"textStyle":?{

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"axisLine":{

"lineStyle":{

"color":"#315070",

"width":4

}

},

"boundaryGap":?false,

"data":?${category}

}],

"yAxis":?[{

"type":?"value",

"axisLabel":?{

"show":?true,

"textStyle":?{

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"axisLine":{

"lineStyle":{

"color":"#315070",

"width":4

}

}

}],

"series":?[{

"smooth":?true,

"dataFilter":?"nearest",

"lineStyle":?{

"color":?"red",

"width":?4

},

"legendHoverLink":?true,

"name":?"${legend}",

"type":?"line",

"stack":?"總量",

"symbolSize":?"15",

"showAllSymbol":?true,

"label":?{

"normal":?{

"color":?"green",

"show":?true,

"position":?"right"

}

},

"data":?${values}

}]

}

(4)餅圖

{

"calculable":?true,

"legend":?{

"data":?${types},

"orient":?"vertical",

"x":?"left",

"textStyle":?{

"color":?"red",

"fontSize":?15,

"fontWeight":?"bolder"

}

},

"series":?[{

"center":?["50%",?"60%"],

"data":?${datas},

"name":?"${title}",

"radius":?"65%",

"type":?"pie",

"avoidLabelOverlap":?true,

"label":?{

"normal":?{

"show":?true,

"position":?"top",

"textStyle":?{

"color":"red",

"fontSize":?"15",

"fontWeight":?"bold"

}

},

"emphasis":?{

"show":?true,

"textStyle":?{

"fontSize":?"20",

"fontWeight":?"bold"

}

}

},

"labelLine":?{

"normal":?{

"show":?true

}

}

}],

"title":?{

"subtext":?"",

"text":?"${title}",

"x":?"center",

"textStyle":?{

"color":?"green",

"fontSize":?20,

"fontWeight":?"bolder"

}

},

"toolbox":?{

"feature":?{

"mark":?{

"lineStyle":?{

"color":?"#1e90ff",

"type":?"dashed",

"width":?2

},

"show":?true

},

"dataView":?{

"lang":?["數據視圖",?"關閉",?"刷新"],

"readOnly":?false,

"show":?true,

"title":?"數據視圖"

},

"magicType":?{

"show":?true,

"title":?{

"bar":?"柱形圖切換",

"stack":?"堆積",

"tiled":?"平鋪",

"line":?"折線圖切換"

},

"type":?["pie",?"funnel"]

},

"restore":?{

"show":?true,

"title":?"還原"

},

"saveAsImage":?{

"lang":?["點擊保存"],

"show":?true,

"title":?"保存為圖片",

"type":?"png"

}

},

"show":?true

},

"tooltip":?{

"formatter":?"{a}?
{b}?:?{c}?({d}%)",

"trigger":?"item"

}

}

三.插件js(

(1)echarts-util-one.js

phantom.outputEncoding?=?"gbk";//?為防止輸出中文時出現亂碼,可設置輸出編碼格式,寫在最頂部

var?params?=?require('system');//?獲取系統參數

var?server?=?require('webserver').create();?//?服務端

var?port?=?params.args[3];//?端口,與啟動命令有關,不一定是3

var?listen?=?server.listen(port,?function(request,?response)?{//?監聽端口

var?args?=?serverGetArgs(request);//?得到網絡請求參數

args.response?=?response;

methodDis(args);

});

var?jslib?=?{

jquery?:?phantom.libraryPath?+?'/lib/jquery-3.2.1.min.js',

echarts?:?phantom.libraryPath?+?'/lib/echarts.min.js',

china?:?phantom.libraryPath?+?'/lib/china.js',

};

/**

*?請求分發

*

*?@author?liansh

*?@data?2019年9月19日?下午11:32:59

*?@param?args

*/

function?methodDis(args)?{

if?(args.reqMethod?==?"table")?{

table(args);

}?else?if?(args.reqMethod?==?"echarts")?{

echarts(args);

}

if?(args.exit?==?"true")?{

writeResponse(args.response,?{

error_no?:?0

});

phantom.exit();

}

}

function?table(args)?{

var?page?=?require('webpage').create();//?打開頁面

//?設置分辨率

page.viewportSize?=?{

width?:?1000,

height?:?1200

};

//?打開頁面

page.open(args.url?||?'http://127.0.0.1:8080/hello',?function(status)?{

if?(status?==?"fail")?{

writeResponse(args.response,?{

error_no?:?-1

});

return;

}

page.injectJs(jslib.jquery);

var?tableheight?=?page.evaluate(function()?{

return?$('body').height()?+?20;

});

//?定義剪切范圍

page.clipRect?=?{

top?:?0,

left?:?0,

width?:?1000,

height?:?tableheight

};

//?var?base64?=?'data:image/png;base64,'?+?page.renderBase64('png');

page.render(args.file);//?將整個page保存為文件,可以是png,jpg,?gif,pdf

page.close();

writeResponse(args.response,?{

error_no?:?0

});

});

page.onError?=?function(msg,?trace)?{

writeResponse(args.response,?{

error_no?:?-1,

error_info?:?trace

});

};

}

function?echarts(args)?{

var?page?=?require('webpage').create();?//?客戶端

page.open("about:blank",?function(status)?{//?空白頁

/**

*?報錯{"file":"undefined","line":3,"function":""},{"file":"","line":18,"function":""}

*?"file":"undefined"?為所引用的jslib文件路徑不對,需要重新檢查路徑

*/

page.injectJs(jslib.jquery);

page.injectJs(jslib.echarts);

page.injectJs(jslib.china);

var?pageBody?=?page.evaluate(function(args)?{

//?動態加載js,獲取options數據??實現漸變效果

var?itemStyle?=?'{"normal":{"color"?:?new?echarts.graphic.LinearGradient(0,?0,?0,?1,?[?'

+?'{offset?:?0,????color?:?"#83bff6"},?{offset?:?0.5,color?:?"#188df0"},?{offset?:?1,color?:?"#188df0"}?])}}';

//?動態加載js,獲取options數據

$('

+?JSON.stringify(args.opt).replace('\"__itemStyle"',?itemStyle)).appendTo(document.head);

//?取消動畫,否則生成圖片過快,會出現無數據

if?(options?!==?undefined)?{

options.animation?=?false;

}

//?body背景設置為白色

$(document.body).css('backgroundColor',?'white');

//?echarts容器

var?container?=?$("

").attr('id',?'container').css({

width?:?args.width,

height?:?args.height

}).appendTo(document.body);

var?eChart?=?echarts.init(container[0]);

eChart.setOption(options);

},?args);

//?定義剪切范圍

page.clipRect?=?{

top?:?0,

left?:?0,

width?:?args.width?-?100,

height?:?args.height?+?10

};

//?var?base64?=?'data:image/png;base64,'?+?page.renderBase64('png');

//?writeResponse(args.response,?{//?返回給http請求

//?error_no?:?0,

//?base64?:?base64

//?});

page.render(args.file);//?將整個page保存為文件,可以是png,jpg,?gif,pdf

page.close();

writeResponse(args.response,?{

error_no?:?0

});

});

page.onError?=?function(msg,?trace)?{

writeResponse(args.response,?{

error_no?:?-1,

error_info?:?trace

});

};

}

function?writeResponse(response,?msg)?{

response.write(JSON.stringify(msg?||?{

error_no?:?0

}));

response.close();

}

/**

*?獲取請求參數

*

*?@author?liansh

*?@data?2019年9月19日?下午11:27:16

*?@param?request

*?@returns

*/

function?serverGetArgs(request)?{

var?args?=?{};

if?('GET'?===?request.method)?{

var?index?=?request.url.indexOf('?');

if?(index?!==?-1)?{

pairs?=?request.url.substr(index?+?1).split("&");

for?(var?i?=?0;?i?<?pairs.length;?i++)?{

var?pos?=?pairs[i].indexOf('=');

if?(pos?===?-1)

continue;

var?key?=?pairs[i].substring(0,?pos);

var?value?=?pairs[i].substring(pos?+?1);

//?中文解碼,必須寫兩層

value?=?decodeURIComponent(decodeURIComponent(value));

args[key]?=?value;

}

}

}?else?if?('POST'?===?request.method)?{

args?=?JSON.parse(request.post);

}

args.width?=?args.width?||?1000;

args.height?=?args.height?||?400;

return?args;

};

另外涉及到的js(jquery-3.2.1.min.js? ? echarts.min.js? ?china.js) 請到https://gitee.com/saintlee/echartsconvert/tree/master?這里下載。

說明:echarts-util-one.js是重新改造的,之前是利用echartsconvert這個插件來生成圖表的,但是echartsconvert這個插件不能生成餅圖,所以就改造了echarts-util-one.js

三.環境配置說明

可以結合前面兩篇看,首先安裝phantomjs-2.1.1-windows,然后在其bin目錄下,放入echarts-util-one.js以及相關lib包下的js(jquery-3.2.1.min.js? ? echarts.min.js? ?china.js),然后啟動服務即可,命令如下:

D:\>cd?D:\softpack\echarts\phantomjs-2.1.1-windows\bin

D:\softpack\echarts\phantomjs-2.1.1-windows\bin>phantomjs?D:\softpack\echarts\phantomjs-2.1.1-windows\bin\echarts-util-one.js?-s?-p?6668

四.測試效果

(1)柱狀圖{

"barParamList":?{

"barName":?[

"A罩杯",?"B罩杯",?"C罩杯",?"D罩杯",?"E罩杯",?"F罩杯","G罩杯"

],

"barValue":?[

43364,?13899,?12000,?2181,?21798,?1796,?1300

],

"legendName":?"胸罩圖例"

},

"horizontal":?true,

"title":?"胸罩使用人數"

}

(2)折線圖{

"horizontal":?true,

"lineParam":?{

"legendName":?"胸罩圖例",

"xdatas":?[

[

43364,?13899,?12000,?2181,?21798,?1796,?1300

]

],

"ydatas":?[

"A罩杯",?"B罩杯",?"C罩杯",?"D罩杯",?"E罩杯",?"F罩杯","G罩杯"

]

},

"title":?"胸罩使用人數"

}

(3)餅圖{

"datas":?[

43364,?13899,?12000,?2181,?21798,?1796,?1300

],

"title":?"胸罩圖例",

"types":?[

"A罩杯",?"B罩杯",?"C罩杯",?"D罩杯",?"E罩杯",?"F罩杯","G罩杯"

]

}

效果展示:

AAffA0nNPuCLAAAAAElFTkSuQmCC

漸變色柱狀圖

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

分享就到這里,代碼后續會開源出來,如果幫助到你了,請點個贊,感謝你的支持!如果你需要源代碼,可以聯系我!

在此,為你推薦幾門優秀的課程:

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/530598.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/530598.shtml
英文地址,請注明出處:http://en.pswp.cn/news/530598.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ios 系統提示框_ios13終于能屏蔽系統更新了!附詳細教程

很多人會遇到這樣的煩惱&#xff0c;ios頻繁更新&#xff0c;自己用慣了當前系統版本&#xff0c;系統總提示新的更新請求&#xff0c;不小心點到之后&#xff0c;系統就會自動更新安裝&#xff0c;有很多小伙伴更新后&#xff0c;手機變卡頓&#xff0c;耗電量加大&#xff0c…

1114: 逆序

1114: 逆序 時間限制: 1 Sec 內存限制: 128 MB 提交: 9639 解決: 5690 [提交] [狀態] [討論版] [命題人:admin] 題目描述 輸入n&#xff08;1<n<10&#xff09;和n個整數&#xff0c;逆序輸出這n個整數。 輸入 輸入n&#xff08;1<n<10&#xff09;&#xff0c;然…

判斷是否有小數_一線老師筆記:數學判斷題拿高分的技巧

01什么是判斷題&#xff1f;判斷是關于對象和它屬性有所肯定或者否定的思維形式。在研究數學中&#xff0c;經常要對現實世界的空間形式和數量關系&#xff0c;作出肯定或者否定的回答&#xff0c;因而要大量使用判斷&#xff0c;并把一些正確的判斷作為進一步研究問題的依據。…

1115: 數組最小值

1115: 數組最小值 時間限制: 1 Sec 內存限制: 128 MB 提交: 11045 解決: 4919 [提交] [狀態] [討論版] [命題人:admin] 題目描述 數組a有n個元素&#xff0c;請輸出n個元素的最小值及其下標。若最小值有多個&#xff0c;請輸出下標最小的一個。注意&#xff0c;有效下標從0開始…

1116: 刪除元素

1116: 刪除元素 時間限制: 1 Sec 內存限制: 128 MB 提交: 9496 解決: 4106 [提交] [狀態] [討論版] [命題人:admin] 題目描述 輸入一個遞增有序的整型數組A有n個元素&#xff0c;刪除下標為i的元素&#xff0c;使其仍保持連續有序。注意&#xff0c;有效下標從0開始。 定義如下…

mysql 導入 sqlite_Mysql 數據導入SQlite

SQlite 有一個很強大的管理工具名字叫做Sqlite Developer (官方網站)。在Sqlite Developer中有關于數據導入的強大功能。可以將MSSQL Oracl Aeecss等等數據庫導入到Mysql中。但是&#xff0c;默認在Win7環境中沒有Mysql的倒入方式。你需要一個叫做“Mysql ODBC”的win客戶端工具…

第一次寫的dfs

在一個給定形狀的棋盤&#xff08;形狀可能是不規則的&#xff09;上面擺放棋子&#xff0c;棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列&#xff0c;請編程求解對于給定形狀和大小的棋盤&#xff0c;擺放k個棋子的所有可行的擺放方案C。 Input 輸…

centos 安裝mysql 5.7.9_CentOS 6.6下RPM方式安裝MySQL 5.7.9

說明&#xff1a;從MySQL5.7.4起&#xff0c;以RPM包的方式安裝后的MySQL的部署&#xff0c;默認是安全的&#xff0c;并且有這些特點&#xff1a;1. 在大多數情況下&#xff0c;你只需要安裝MySQL-server和MySQL-client安裝包就可以安裝上一個標準功能的MySQL。對于一個標準安…

1097: 計算平均成績(函數專題)

1097: 計算平均成績&#xff08;函數專題&#xff09; 時間限制: 1 Sec 內存限制: 128 MB 提交: 7214 解決: 4094 [提交] [狀態] [討論版] [命題人:admin] 題目描述 輸入某位同學各門課的成績&#xff0c;輸出平均成績。輸入的成績均為五級制成績&#xff0c;五級制成績轉換為百…

mongodb 導入 mysql_將mongodb 數據指定字段導出,然后指定字段導入mysql 實例 及相關問題解決...

需求&#xff1a;將mongodb 數據指定字段導出&#xff0c;然后再指定字段導入mysql 表中直接上圖吧&#xff0c;最后 會將遇到幾個問題及解決方案貼出&#xff0c;以便遇到類似問題可以迅速解決(期間所用命令具體用法不在本文詳解之內)將mongodb 數據指定字段導出&#xff0c;…

mysql外部排序_深入淺出MySQL優先隊列(你一定會踩到的order by limit 問題)

0.先拋問題假設字段category無索引且有重復值&#xff0c;order by category 和 limit 組合使用的結果會和預期不符。問題復現&#xff1a;表結構(就是兩個字段)CREATE TABLE ratings (id int(11) NOT NULL AUTO_INCREMENT,category int(11) DEFAULT NULL,PRIMARY KEY (id)) EN…

int signed in mysql_【轉】mysql 中int類型字段unsigned和signed的區別

轉自https://www.cnblogs.com/wangzhongqiu/p/6424827.html用法&#xff1a;mysql> CREATE TABLE t ( a INT UNSIGNED, b INT UNSIGNED )探索一&#xff1a;正負數問題拿tinyint字段來舉例&#xff0c;unsigned后&#xff0c;字段的取值范圍是0-255&#xff0c;而signed的范…

navicat fo mysql 教程_Navicat For MySQL的簡單使用教程

1.前提是必須先安裝好MySQL數據庫(Mac下安裝MySQL數據庫見前一篇)2.安裝Navicat3.點擊navicate左上角&#xff1a;連接->MySQL->先測鏈接下&#xff0c;如果提示連接成功&#xff0c;就可以填寫連接名&#xff0c;點擊連接即可。雙擊剛創建的連接下面會有四個數據庫用naV…

mysql linux32_Linux 配置 mysql 5.7.32 實操記錄

下載環節官網自行獲取檢查環境環節1. 檢測自帶mysql#rpm -qa | grep mysql2.刪除 “1” 找到的 所有#rpm -e --nodeps 名稱3.查詢所有mysql文件夾#whereis mysql#find / -name mysql刪除所有結果 (rm -rf 文件路徑)安裝環節1. 切換路徑#cd /usr/local2.拷貝mysql安裝包到此目錄…

mysql8.0 tar安裝_CentOS7安裝MySQL8.0 tar包

CentOS7安裝MySQL8.0 tar包一、卸載1. 查看有沒有安裝mysqlrpm -qa | grep mysql刪除#rpm -e --nodeps mysql-libs-5.1.71-1.el6.i686 或# for i in $(rpm -qa|grep mysql);do rpm -e $i --nodeps;done2. 使用 find / -name mysql 命令查找原有mysql的相關配置文件&#xff0c;…

mysql官網 ab_MySQLAB同步

MySQL 支持單向、異步復制,復制過程中一個服務器充當主服務器,而一個或多個其它服務器充當從服務器。主服務器將更新寫入二進制日1 . 介紹MySQL 支持單向、異步復制,復制過程中一個服務器充當主服務器,而一個或多個其它服務器充當從服務器。主服務器將更新寫入二進制日志文件,并…

jtree和mysql_java 已經獲取某個mysql數據庫的所有表名 創建JTree

展開全部那只e68a843231313335323631343130323136353331333335303530能創建一層的JTree &#xff1f;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.swing.JFram…

mvc json 亂碼_你了解JSON嗎?——Jackson、FastJson在SpringMVC中的簡單使用

原文參考分享自CSDN&#xff1a;你了解JSON嗎&#xff1f;--Jackson、FastJson在SpringMVC中的簡單使用_歡迎來到 Baret~H 的博客-CSDN博客1. 什么是 JSONJSON&#xff08;JavaScript Object Notation, JS 對象標記&#xff09;是一種輕量級的數據交換格式采用完全獨立于編程語…

format 函數包含_Python成為專業人士筆記-高級對象Format格式化

“專業人士筆記”系列目錄&#xff1a;創帆云&#xff1a;Python成為專業人士筆記--強烈建議收藏&#xff01;每日持續更新&#xff01;?zhuanlan.zhihu.com在存儲和轉換數據輸出供查看時&#xff0c;字符串格式可能變得非常重要。Python提供了本文概述的各種字符串格式化方法…

python 預測算法_Python 與金融數據使用機器學習算法預測交易策略

記得 關注、分享、點在看呀&#xff5e; 這樣您就能持續收到優質的推送啦這一期&#xff0c;我們將使用上一期處理好的數據特征和標簽訓練機器&#xff0c;然后預測交易策略。我們將分別使用 K近鄰算法和集成學習兩種方法分別完成模型的訓練和預測。FinTech HistoryPython 與金…