ECharts Configuration Syntax
In this chapter we will introduce some configuration for generating charts with ECharts.
Step 1: Create an HTML Page
Create an HTML page and import echarts.min.js:
Step 2: Prepare a DOM container with height and width for ECharts
In the example, the div with id main is used to contain the chart drawn by ECharts:
Step 3: Set the configuration information
The ECharts library uses the json format for configuration.
echarts.init(document.getElementById('main')).setOption(option);
Here option means using json data format configuration to draw the chart. The steps are:
Title
Configure a title for the chart:
title: {
text: '第一个 ECharts 实例'
}
Tooltip
Configure the tooltip:
tooltip: {},
Legend Component
The legend component shows the marker (symbol), color, and name of different series. You can click the legend to control which series are not displayed.
legend: {
data: [{
name: '系列1',
// Force the shape to be a circle.
icon: 'circle',
// Set text to red
textStyle: {
color: 'red'
}
}]
}
X Axis
Configure the items to display on the X axis:
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
}
Y Axis
Configure the items to display on the Y axis.
yAxis: {}
Series List
Each series determines its chart type through type:
series: [{
name: '销量', // 系列名称
type: 'bar', // 系列图表类型
data: [5, 20, 36, 10, 10, 20] // 系列中的数据内容
}]
Each series determines its chart type through type:
- type: 'bar': bar chart
- type: 'line': line/area chart
- type: 'pie': pie chart
- type: 'scatter': scatter (bubble) chart
- type: 'effectScatter': scatter (bubble) chart with ripple effect animation
- type: 'radar': radar chart
- type: 'tree': tree chart
- type: 'treemap': treemap
- type: 'sunburst': sunburst chart
- type: 'boxplot': box plot
- type: 'candlestick': candlestick chart
- type: 'heatmap': heatmap
- type: 'map': map
- type: 'parallel': series of parallel coordinates
- type: 'lines': line chart
- type: 'graph': relationship graph
- type: 'sankey': sankey diagram
- type: 'funnel': funnel chart
- type: 'gauge': gauge
- type: 'pictorialBar': pictorial bar chart
- type: 'themeRiver': theme river
- type: 'custom': custom series
Example
The following is a complete example:
Example
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>First ECharts Example
title
<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js" script head>
<body>
<div id="main" style=">width: 600px;height:400px;
"
div
<script type="text/javascript">// Initialize the echarts instance based on the prepared DOM var myChart = echarts.init(document.getElementById('main')); // Specify the chart's configuration and data var option = { title: { text: 'First ECharts Example' }, tooltip: {}, legend: { data:['Sales'] }, xAxis: { data: ["Shirt","Sweater","Chiffon Shirt","Pants","High Heels","Socks"] }, yAxis: {}, series: [{ name: 'Sales', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }; // Display the chart using the just-specified configuration and data. myChart.setOption(option);
script
body
html
Try it »
Click the "Try it" button to view the online example
For more configuration item references:https://echarts.apache.org/zh/option.html