In the previous article, we introduced the use of histogram in Echarts, from the demo we can see whether it is a histogram or a pie or a line chart, are imported JS, prepare the option parameter. For ease of use our next step is to encapsulate.
We separate the graphic type and the graphic data from the graphical display, encapsulating the independent JS file, as long as the graphic type parameters and graphical data parameters can be displayed corresponding graphics, so that there will not be too much redundant code. Next we say encapsulate JS file.
Is the histogram package histogram of the JS pie chart Packaging pie chart js, or all the graphics encapsulated into a JS it. If we were to encapsulate it individually, we would draw out the changes in the option content, if we encapsulate a JS, different kinds of graphical option format, the encapsulated JS is equivalent to a separate package of the collection. The specific package also needs to be based on the actual needs of the project to facilitate the use of the purpose.
Individually encapsulated JS Introduction: A histogram as an example.
1, Initialize JS
Jsonarray graphic Data Array ReportName graphical display title
function Initechartsbar (JSON, ReportName) {
require.config ({
paths: {
echarts: ' Js/echarts ',//Here is the path to the Echarts file you quoted
' Echarts/chart/bar ': ' js/echarts ',// It is important to note that in addition to the Mapchart use of the configuration file for Echarts-map, the other graphics reference configuration files are Echarts, which is also the general graph with the difference between the map
' echarts/chart/line ': ' js/ Echarts ',
' echarts/chart/pie ': ' Js/echarts '
}
});
Require ([' echarts ',//here defines the class required in the project
' Echarts/chart/bar ',], function (EC) {
var mychart = Ec.init ( document.getElementById (' main '));//Where the chart is initialized, there is a place in the page to display the chart, his ID is main
option = Getoptionbyarray (JSON, reportdesign);//Get option graphic
mychart.setoption (option);//display Graphics
});
}
2, get the option data
function Getoptionbyarray (JSON, ReportName) {
var option = {
title: {
text:reportname,
x: ' Center '
},
tooltip: {
trigger: ' Item ',
formatter: "{A} <br/>{b}: {C} ({d}%)"
},
legend: {
Orient: ' Vertical ',
x: ' Left ',
data:json.getText
},
Toolbox: {
show:true,
feature: {
mark: {show:true},
DataView: {show:true, readonly:false},
restore: {show:true},
saveasimage: {show:true}
}
,
calculable:true,
series: [
{
name:json.getText,
type: ' Pie ',
radius: ' 55% ',
Center: [' 50% ', ' 60% '],
data:json.getValue
}
]
};
return option;
}
3, in the interface reference JS
An interface that only shows a single graphic, just a reference to the file head tag. As follows:
If you display a variety of graphics in the interface, you need to dynamically import the required encapsulated JS file after determining which graphics are selected, as follows:
$.getscript (): Remote Call JS, in the program runtime according to the program given by the JS path dynamically to the path of the JS file loaded into the program, in order to use the program.
Manually write an HTML file, test the remote Call JS method, under the Firefox and Google Browser can not be passed under IE browser. So if the test does not pass, try changing the browser or create a Web project test in the Eclipse tool.