Echarts Official documents:
http://echarts.baidu.com/doc/doc.html# Introduction
Echarts, abbreviated from Enterprise charts, a commercial-level data graph, a pure JavaScript chart library, can run smoothly on PCs and mobile devices, compatible with most current browsers (Ie6/7/8/9/10/11,chrome, Firefox,safari, etc.), the underlying reliance on lightweight Canvas class library Zrender provides intuitive, vivid, interactive, highly customizable data visualization charts. The innovative drag-and-drop weight calculation, Data View, domain roaming and other characteristics greatly enhance the user experience, giving users the ability to excavate and integrate data.
Supports line chart (area chart), histogram (bar chart), scatter chart (bubble chart), chart, pie chart (ring chart), radar (filled with radars), chord diagram, force-oriented layout diagram, map, dashboard, funnel map, event River diagram and other 12 types of charts, also provide title, details bubbles, legends, range, data region, 7 interactive components, such as timeline, toolbox, support multiple graphs, component linkage and mashup display.
The first: Data written dead in front of the
First put a picture of the effect: (HTML static page, mainly consider the icon to show the effect)
Download address for instance page:
http://download.csdn.net/detail/lmb55/9241793
The development of the Echarts page has two main steps:
1, set a storage icon div;
2, call the drawing method for drawing;
2.1 Initialize the DOM object (DIV) where the echarts chart will be stored;
MyChart = Echarts.init (document.getElementById (' main '));
2.2 Write Echarts chart required attributes (option);
var options = {...} ;
2.3 Injecting attributes into a chart
Mychart.setoption (options);
demo.jsp
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>ECharts</title>
<link type= "Text/css" rel= "stylesheet" href= "Bootstrap/css/bootstrap.css"/>
<link type= "Text/css" rel= "stylesheet" href= "Bootstrap/css/bootstrap-theme.min.css"/>
<script src= "Bootstrap/js/jquery-1.11.3.min.js" ></script>
<script src= "Bootstrap/js/bootstrap.min.js" ></script>
<!--prepare a DOM--> with size (width and height) for echarts
<div id= "main" style= "Height:500px;width:750px;float:left" ></div>
<script src= "Http://echarts.baidu.com/build/dist/echarts.js" ></script>
<script type= "Text/javascript" >
Path configuration (echarts the path where files need to be loaded are displayed correctly)
Require.config ({
Paths: {
Echarts: ' Echarts-2.2.7/build/dist '
}
}); Use
Require
[' Echarts ', ' echarts/chart/bar ', ' echarts/chart/line '//loading bar module with column graph, loading on demand
//function: Draw the graphic you want to display
function Drewecharts (EC) {
Initialize the Echarts chart in the appropriate div with the id attribute
MyChart = Ec.init (document.getElementById (' main '));
var option = {
Timeline: {//Timeline
Data: [ ' 10-9 ', ' 10-8 ', ' 10-7 ', ' 10-6 ', ' 10-5 ', ' 10-4 ', ' 10-3 ', ' 10-2 ', ' 10-1 '
],
HEIGHT:80,
X:50,
padding: [30, 10, 10, 10
],
autoplay:false,//settings are automatically rotated
playinterval:2000//Set automatic rotation time
},
options:[//interface Access total horizontal vertical axis setting
{//Icon properties
Calculable:true,
Title: {
Text: ' Total interface access ',
Subtext: ' Current weekly statistics view ',
x: ' Left '
},
Datazoom: {//Data area scaling (scroll bar)
Show:true,
Realtime:true,
start:30,//starts from 30%.
Y:20,
Databackgroundcolor: "Green",
end:60,//is closing at 60% data.
Witdh:5,
Height:15
},
ToolTip: {//hint
Trigger: ' Axis ',
Formatter: ""
},
Legend: {//Legend (can be set more depending on actual needs, multiple legends separated by commas)
Data:[' interface real-time concurrent volume ']
},
Toolbox: {//Auxiliary tool box, auxiliary function, such as adding marking line, box selecting zoom, etc.
x: ' Right ',
Y: ' Center ',
Orient: ' Vertical ',
Show:true,
Feature: {//definition of some features small features,
Mark: {Show:true},
DataView: {show:true, readonly:false},
Restore: {show:true},
Saveasimage: {show:true}
}
,//Set the X axis
Xaxis: [
{
Type: ' Category ',
Position: ' Bottom ',
Boundarygap:true,
Axisline: {//X axis setting
Show:false,//Set the display style of x axis lines
LineStyle: {
Color: ' Green ',
Type: ' Solid ',
Width:2
}
},
Axistick: {//x axis coordinate point mark
Show:true,
Length:5,//Set the display style of x axis coordinate points
LineStyle: {
Color: ' Red ',
Type: ' Solid ',
Width:1
}
},
Axislabel: {//x axis data style
Show:true,
Interval: The length of the ' auto ',//{number} scale, can be set to digital
rotate:0,
MARGIN:-20,
Splitnumber:18,
Formatter: ',//template formatter! ' {Value} unit '
/* TextStyle: {
Color: ' Blue ',
fontFamily: ' Sans-serif ',
Fontsize:15,
FontStyle: ' Italic ',
FontWeight: ' Bold '
}*/
},
Splitline: {
Show:false,
LineStyle: {
Color: ' #483d8b ',
Type: ' Dashed ',
Width:1
}
},/* Splitarea: {
Show:true,
areastyle:{
Color:[' Rgba (144,238,144,0.3) ', ' Rgba (135,200,250,0.3) ']
}
},*/
Data: [//x axis scale
' 0:00 ', ' 0:10 ', ' 0:20 ', ' 0:30 ', ' 0:40 ', ' 0:50 ', ' 1:00 ', ' 1:10 ', ' 1:20 ', ' 1:30 ', ' 1:40 ', ' 1:50 ', ' 2:00 ', ', ' 2:10 ', ' 2:20 ', ' 2:30 ', ' 2:40 ', ' 2:50 ', ' 3:00 ']
}
],//Set the y-axis
YAxis: [
{
Type: ' value ', ' name ': ' Total access to interface ',
Position: ' Left ',
Splitnumber:10,
BOUNDARYGAP: [0,0.1],
Axisline: {//Left Y axis
Show:true,/*linestyle: {
Color: ' Red ',
Type: ' Dashed ',
Width:2
}*/
},
Axistick: {//Left y-axis coordinates mark Point
Show:true,
Length:10,
LineStyle: {
Color: ' Green ',
Type: ' Solid ',
Width:2
}
},
Axislabel: {//y axis data style
Show:true,
interval:10,//' auto ',//{number}
rotate:0,//the selection angle of the data left to the left Y axis
Margin:18,
Formatter: ' {value} ',//Template formatter! ' {Value} unit '
},
},
],
Series: [//data series, a chart may contain multiple series, each series may contain multiple data
{
Name: ' Total interface access ',
Type: ' line ', the data that the//y axis needs to display
data:[2325, 2565, 5676,1356, 1622, 3266, 2007,2325, 2565, 5676,1356, 1622, 3266, 2007,2325, 2565, 5676,1356, 1622, 3266, 2 007,2325, 2565, 5676,1356, 1622],
Markpoint: {//Set special display point
Data: [
{type: ' Max ', Name: ' Max '},
{type: ' min ', Name: ' Min '}
]},
}
]
},//Interface success rate Vertical axis set
{
Calculable:true,
Title: {
Text: ' Interface success ',
Subtext: ' Current weekly statistics view ',
x: ' Left '
},
Datazoom: {//Data area scaling, often used to display large amounts of data, select the visual range
Show:true,
Realtime:true,
START:30,//Y:20,
Databackgroundcolor: "Green",
End:60,
Witdh:5,
Height:15
},
ToolTip: {//bubble tip box, often used to show more detailed data
Trigger: ' Axis ',
Formatter: ""
},
Legend: {
Data:[' interface success rate ']
Toolbox: {//Auxiliary tool box, auxiliary function, such as adding marking line, box selecting zoom, etc.
x: ' Right ',
Y: ' Center ',
Orient: ' Vertical ',
Show:true,
Feature: {
Mark: {Show:true},
DataView: {show:true},
Magictype: {show:true, type: [' line ', ' Bar ', ' stack ']},//for different graphic transformations
Restore: {show:true},
Saveasimage: {show:true}
}
},
Xaxis: [
{
Type: ' Category ',
Position: ' Bottom ',
Boundarygap:true,
Axisline: {//x Axis Axis Setup
Show:false,
LineStyle: {
Color: ' Green ',
Type: ' Solid ',
Width:2
}
},
Axistick: {//x axis coordinate point mark
Show:true,
Length:5,
LineStyle: {
Color: ' Red ',
Type: ' Solid ',
Width:2
}
},
Axislabel: {//x axis data style
Show:true,
Interval: ' Auto ',//{number}
rotate:0,
MARGIN:-20,
Splitnumber:23,
Formatter: ' {value} point ',//template formatter! ' {Value} unit '
/* TextStyle: {
Color: ' Blue ',
fontFamily: ' Sans-serif ',
Fontsize:15,
FontStyle: ' Italic ',
FontWeight: ' Bold '
}*/
},
Splitline: {
Show:false,
LineStyle: {
Color: ' #483d8b ',
Type: ' Dashed ',
Width:1
}
},/* Splitarea: {
Show:true,
areastyle:{
Color:[' Rgba (144,238,144,0.3) ', ' Rgba (135,200,250,0.3) ']
}
},*/
Data: [' 0:00 ', ' 0:10 ', ' 0:20 ', ' 0:30 ', ' 0:40 ', ' 0:50 ', ' 1:00 ', ' 1:10 ', ' 1:20 ', ' 1:30 ', ' 1:40 ', ' 1:5 0 ', ' 2:00 ', ' 2:10 ', ' 2:20 ', ' 2:30 ', ' 2:40 ', ' 2:50 ', ' 3:00 '
}
],
YAxis: [
{
Type: ' value ', ' Name ': ' Interface success ',
Position: ' Left ',
Splitnumber:10,
BOUNDARYGAP: [0,0.1],
Axisline: {//Left Y axis
Show:true,/*linestyle: {
Color: ' Red ',
Type: ' Dashed ',
Width:2
}*/
},
Axistick: {//Left y-axis coordinate mark
Show:true,
Length:10,
LineStyle: {
Color: ' Green ',
Type: ' Solid ',
Width:2
}
},
Axislabel: {
Show:true,
interval:10,//' auto ',//{number} defines the length of the horizontal coordinate scale
rotate:0,//the selection angle of the data left to the left Y axis
Margin:18,
Formatter: ' {value} ',//Template formatter! ' {Value} unit '
},
}
,//Available grid Properties control chart size
Grid: {
Width: ' 90% '
},
Series: [
{
Name: ' Interface Success ',
Type: ' line ',
data:[2325, 2565, 7676,1356, 1622, 3266, 2007],
Markpoint: {
Data: [
{type: ' Max ', Name: ' Max '},
{type: ' min ', Name: ' Min '}
]},
}
]
}]}; Loading data for a Echarts object
mychart.setoption (option);
}
); </script></body>
The second type: the former background data interactive implementation Echarts chart
Principle of implementation: Controller annotation based on SPRINGMVC. The foreground page obtains the data through the request controller method, gets the data successfully to set the corresponding position, the controller can obtain the data from the database or other third party system, this example uses the simple realization, directly will the data in the controller gives;
Instance resolution:
dynamicinteractiondemo.jsp
<! DOCTYPE html><meta charset= "Utf-8" >
<meta http-equiv= "x-ua-compatible" content= "Ie=edge" >
<meta name= "viewport" content= "Width=device-width, initial-scale=1.0" >
<meta name= "description" content= "Echarts" >
<meta name= "Author" content= "kener.linfeng@gmail.com" >
<title>echarts Front and Rear interactive demo</title>
<% Request.setattribute ("CTX", Request.getcontextpath ()); %>
<link rel= "shortcut icon" href= "${ctx}/config/echarts/echarts/doc/asset/ico/favicon.png" >
<link href= "${ctx}/config/echarts/echarts/doc/asset/css/font-awesome.min.css" rel= "stylesheet" >
<link href= "${ctx}/config/echarts/echarts/doc/asset/css/bootstrap.css" rel= "stylesheet" >
<link href= "${ctx}/config/echarts/echarts/doc/asset/css/carousel.css" rel= "stylesheet" >
<link href= "${ctx}/config/echarts/echarts/doc/asset/css/echartshome.css" rel= "stylesheet" >
<!--HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries-->
<!--[If Lt IE 9]>
<script src= "Https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js" ></script>
<script src= "Https://oss.maxcdn.com/respond/1.4.2/respond.min.js" ></script>
<! [endif]-->
<!---->
<script src= "${ctx}/config/echarts/echarts/src/echarts.js" ></script>
<script src= "${ctx}/config/echarts/echarts/doc/asset/js/codemirror.js" ></script>
<script src= "${ctx}/config/echarts/echarts/doc/asset/js/javascript.js" ></script>
<link href= "${ctx}/config/echarts/echarts/doc/asset/css/codemirror.css" rel= "stylesheet" >
<link href= "${ctx}/config/echarts/echarts/doc/asset/css/monokai.css" rel= "stylesheet" ><div id= "main" class= "main" ></div>//the div <!--echarts Single file to store the chart into-->
<script src= "${ctx}/config/echarts/echarts/build/dist/echarts-all.js" ></script>
<script type= "Text/javascript" language= "JavaScript" >
Call paint method to paint
Drawechart (Echarts); Create a way to draw a echarts chart
function Drawechart (echarts) {
var MyChart;
Alert (' Drawechart () ');
MyChart = Echarts.init (document.getElementById (' main '));
Mychart.showloading ({
Text: "The chart data is trying to load ..."
});
Define Chart Properties Options
var options = {
Title: {
Text: "Temperature changes in the coming week",
Subtext: "Pure Fiction",
Sublink: "Http://www.baidu.com"
},
ToolTip: {
Trigger: ' axis '
},
Legend: {
Data: ["Highest temperature"]
},
Toolbox: {
Show:true,
Feature: {
Mark: {
Show:true
},
DataView: {
Show:true,
Readonly:false
},
Magictype: {
Show:true,
Type: [' line ', ' Bar ']
},
Restore: {
Show:true
},
Saveasimage: {
Show:true
}
}
},
Calculable:true,
Xaxis: [{
Type: ' Category ',
Boundarygap:false,
Data: [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ']
} ],
YAxis: [{
Type: ' Value ',
Axislabel: {
Formatter: ' {value}°c '
},
Splitarea: {
Show:true
}
} ],
Grid: {
Width: ' 90% '
},
Series: [{
Name: ' Highest temperature ',
Type: ' line ',
Data: [11, 22, 33, 44, 55, 33, 44],//must be of type Integer, String calculates average error
Markpoint: {
Data: [{
Type: ' Max ',
Name: ' Max '
}, {
Type: ' min ',
Name: ' min value '
} ]
},
Markline: {
Data: [{
Type: ' Average ',
Name: ' Average '
} ]
}
} ]
};
Mychart.setoption (options); First inject the option into the MyChart
Mychart.hideloading ();
Alert (' ending '); Set delay
settimeout (function Getchartdata () {
Alert (' Getchartdata () ' + options); Get the Options object for a chart
var options = Mychart.getoption ();
Alert (options),
Get data through Ajax
$.ajax ({
URL: "${ctx}/restservice/echarts",//SPRINGMVC Controller request path
Type: "Post",
Async:true,//Synchronous execution
Data: {},
DataType: "JSON",//return data in the form of JSON
Success:function (Result) {
if (result) {
Alert (' requestsuccess '); The request successfully set the data to the appropriate location
Options.legend.data = Result.legend;
Options.xaxis[0].data = result.category;
Options.series[0].data = Result.series[0].data;
Mychart.hideloading ();
Mychart.setoption (options);
}
},
Error:function (Xmlhttprequest,errormsg,exceptionobject) {
Alert ("Requesterror");
Alert (' xmlhttprequest>>>>> ' + XMLHttpRequest + ' errormsg>>>>> ' + ERRORMSG + ') Exceptionobject>>>>> ' + exceptionobject);
Mychart.hideloading ();
}
});
}, 100); Getchartdata (MyChart);//aja background interaction
}; </script>
<!--Le JavaScript
==================================================-->
<!--Placed at the "End of" the document so the pages load faster-->
<script src= "${ctx}/config/echarts/echarts/doc/asset/js/jquery.min.js" ></script>
<script type= "Text/javascript" src= "${ctx}/config/echarts/echarts/doc/asset/js/echartshome.js" ></script >
<script src= "${ctx}/config/echarts/echarts/doc/asset/js/bootstrap.min.js" ></script>
<script src= "${ctx}/config/echarts/echarts/doc/asset/js/echartsexample.js" ></script></body>
Get the controller for && encapsulate form data:
Dynamicinteractioncontroller.java
Package Com.hollycrm.hollyuniproxy.controller;import Java.util.arraylist;import Java.util.arrays;import Java.util.list;import Javax.servlet.http.httpservletrequest;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestbody;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.responsebody;import Com.alibaba.fastjson.JSON; @Controller
public class Dynamicinteractioncontroller {
Private Log logger = Logfactory.getlog (Echartscontroller.class);
@RequestMapping (value = "/echarts", method=requestmethod.post,produces= "Text/plain;charset=utf-8")
Public @ResponseBody string Echartshandler (@RequestBody string json,httpservletrequest req) {
Logger.debug ("Enter Echartshandler method body >>>>>>>>>>>>>>>>>>> >>>>>> ");
List<string> legend = new Arraylist<string> (arrays.aslist (new string[]{"Highest Temperature"});//Data grouping
list<string> category = new Arraylist<string> (arrays.aslist (New String []{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"});/horizontal axis
list<series> Series = new arraylist<series> ()/Ordinate
Series.add (New series ("Highest temperature", "line", New Arraylist<integer> (Arrays.aslist (21,23,28,26,21,33,44)));
Echartdata data=new Echartdata (legend, category, series);
Logger.debug ("Echartshandler return string >>>>>>>>>>>>>>>" + Json.tojsonstring (data));
return json.tojsonstring (data);
}
}
The JavaBean used to encapsulate the form data:
Echartdata:
Package Com.hollycrm.hollyuniproxy.controller;import Java.util.arraylist;import Java.util.list;public class Echartdata {
Public list<string> legend = new arraylist<string> ()//Data grouping
Public list<string> category = new arraylist<string> ()/Horizontal axis
Public list<series> Series = new arraylist<series> ()/Ordinate
Public Echartdata (list<string> legendlist, list<string> categorylist, list<series> seriesList) {
Super ();
This.legend = legendlist;
This.category = categorylist;
This.series = serieslist;
}
}
JavaBean for encapsulating ordinate:
Series.java:
Package Com.hollycrm.hollyuniproxy.controller;import Java.util.list;public class Series {public String name;
public String type;
Public list<integer> data;//here to use an int cannot use a string otherwise the foreground display is not normal (especially when doing mathematical operations)
Public Series (string name, String type, list<integer> data) {
Super ();
THIS.name = name;
This.type = type;
This.data = data;
}
}
I have only given a graphical example of the above example, you need to use other graphics to change the individual properties. Finally, thanks to the Echarts team who provided such a powerful data graph library.
PS: There are two ways to control the size of a chart:
1, limit the size of the div to store the chart;
2, use the Grid property to set the chart size;