First, the introduction of the relevant JS file
Here we adopt the way of labeling to introduce file, we introduce two JS file, one is Esl.js file and a echarts.js.
<src= "js/esl.js" type= "Text/javascript"></ Script > < src= "js/echarts.js" type= "Text/javascript"></ script>
Second, the initialization of the Echarts object
The diagram can be instantiated after the require obtained Echarts interface in the file Esl.js, the Echarts interface has only one method Init, and a DOM node with a size (if no container size will be processed according to the size of 0, which cannot be seen if the chart is not visible) when executing init (width , height can be calculated can be, not necessarily visible) can be instantiated after the chart object, the chart library is implemented as multi-instance, the same page can be more than one init multiple charts. The chart instance is available in methods, and the initialization code after the introduction of Echarts is as follows:
//as a portalrequire ([' Echarts ', ' Echarts/chart/pie ' ], //Initialize the chart object within the callback function function(EC) {varMyChart = Ec.init (document.getElementById (' main '))); Mychart.setoption ({...}); });// -----------------------------//non-ingress or re-use, the chart has been loaded registerRequire (' Echarts '). Init (DOM). SetOption ({...});//If you need to use Echarts's chart instance again, it is recommended that you save the chart instance returned by init.varMyChart = require (' Echarts ')). Init (DOM); Mychart.setoption ({...});
If you are not accustomed to modularity you can also initialize this:
// to define a global chart object var Echarts;require ([function(EC) { // Save the object echarts = ec;}); // Yes, save the echarts after loading as a namespace use //-----------// Next we can do the graph init operation var mychart = echarts.init (DOM); Mychart.setoption ({...});
The Init method is described below:
| name |
Parameters |
Description |
| {echarts} init |
{dom} dom, {string | Object =} Theme |
Initializes the interface, returning the Echarts instance, where the DOM is the node where the chart is located, theme is an optional theme, the built-in theme (' Macarons ', ' infographic ') directly passes in the name, and the custom extension theme can be passed into the subject object. Such as:
var mycharts = echarts.init (document.getElementById (' main '), ' macarons '); |
Precautions:
1, before the instantiation, please ensure that the relevant JS file has been introduced and the path is correct;
2, in the execution of the chart object rendering method of the Init (DOM), remember that the DOM here is a page container element of the object, please ensure that the element object is present, otherwise executed the Init (DOM) method will be reported to have not defined errors;
3, DOM element must specify its size, about the size of the element I can be set through CSS:
<id= "main" style= "width:400px;height:300px;" ></ Div >
You can also set its Size property value using dynamic JavaScript code.
Echarts Study Summary (iii): Echarts Diagram Object Initialization (init) detailed and considerations