Echars and a Java class library (http://git.oschina.net/free/ECharts) on Echars are used to develop charting capabilities in the project. The main purpose of this class library is to facilitate the construction of data structures in Java that may be used in echarts, that is, we can construct the good one option object (Java object) directly on the backend, and convert it to a JSON string and return it to the front end. However, in the option configuration item in Echarts, it is usually necessary to configure the parameters of some function types, and the option object constructed on the backend can only be used to stitch strings in the way of passing function type parameters, so stitching is not only cumbersome, It is also prone to errors when converting a background-constructed option object to a JSON string by using some of the JSON toolkits provided by Java. So I made a simple encapsulation of echars, the following code:
1 /**2 * Created by Liubaozhe on 2016/7/20.3 */4 functionEcharsfun () {5 This. cfg={6Echartsinitdom:NULL,//initialize DOM node, jquery object Type (required)7Ajaxurl:NULL,//Ajax Submission Path (required)8ajaxparam:{},//Ajax parameters (required)9Ajaxsuccess:NULL,//Ajax Request Success callback functionTenOption:NULL,//Option Object Parameter OneIsresize:true //whether to change the chart size according to the window size A } - } -Echarsfun.prototype=$.extend ({},{ theDoajax:function(){ - varthat= This; - $.ajax ({ - Url:that.cfg.ajaxUrl, +Type: "POST", - Data:that.cfg.ajaxParam, +DataType: "JSON", AErrorfunction(XMLHttpRequest, Textstatus, Errorthrown) { at That.error (XMLHttpRequest, Textstatus, errorthrown); - }, -Successfunction(data) { - that._success (data); - }, -Beforesend:function() {//Pre-Request callback function in that.beforesend (); - }, toCompletefunction(XMLHttpRequest, Textstatus) {//Request Completion callback function + That.complete (XMLHttpRequest, textstatus); - } the }); * }, $Beforesend:function(){Panax Notoginseng varthat= This; - that._mychart.clear (); the that._mychart.showloading ({ +Text: ' Trying to read data ... '//Loading speech surgery A }) the }, +Completefunction(){ - varthat= This; $ that._mychart.hideloading (); $ }, -_success:function(data) { - varthat= This; the varoption={}; - if(Data &&data.option) {Wuyioption =$.parsejson (data.option); the } - //If the option object is not empty, the merge parameter Wu if(!$.isemptyobject (option)) { -$.extend (true, option, that.cfg.option);//merge Option About } $ //Set the Ajax callback function -That.cfg.ajaxSuccess &&that.cfg.ajaxSuccess (data); - //SET Option -That._mychart.setoption (option,true); A }, +Errorfunction(Error) { theConsole.error ("Chart request data failed!")); - }, $Initecharts:function() { the varthat = This; theThat._mychart = Echarts.init (that.cfg.echartsinitdom[0]); the }, theBindevent:function(){//Binding UI Events - varthat= This; in if(that.cfg.isResize) { theWindow.onresize =that._mychart.resize; the } About }, theInit:function(CFG) { the varthat= This; thethat._mychart=NULL; + $.extend (that.cfg,cfg); - that.initecharts (); the That.doajax ();Bayi that.bindevent (); the } the});
How to use:
var echars = new Echarsfun (); Echars.init ({ echartsinitdom: $ (' #issue-total-bar '), ajaxUrl:createIssueTotalPie.do,//ajax commit path (required) Ajaxparam: $ (' from '). SerializeObject (), isresize:true//Whether to change the chart size according to the window size});
If you need to configure additional function parameters, as configured on the front-end echarts option parameter, the front-end configuration has high parameter precedence and overrides the generated parameters:
var echars = new Echarsfun (); Echars.init ({echartsinitdom: $ (' #issue-total-bar '), ajaxUrl:createIssueTotalPie.do,//ajax commit path (required) Ajaxparam: $ (' from '). SerializeObject (), option: {series: [{ ItemStyle: {normal: {label: {F Ormatter:function (params) {return params.name + ': ' + params.value + ' \ n ratio: ' + param S.percent + '% '; }}}, emphasis: {Lab El: {formatter:function (params) {return Params.perce NT + '% '; } } } } } ] }, Isresize:true//Whether according to the window largeSmall change in chart size});
End: Welcome to point out my mistakes and shortcomings in this article, I thanked ☆⌒ (*^-゜) v
About Echarts Java class library a jquery plugin