5 分钟上手 ECharts
获取 ECharts
你可以通过以下几种方式获取 ECharts。
从官网下载界面选择你需要的版本下载,根据开发者功能和体积上的需求,我们提供了不同打包的下载,如果你在体积上没有要求,可以直接下载完整版本。开发环境建议下载源代码版本,包含了常见的错误提示和警告。
在 ECharts 的 GitHub 上下载最新的
release
版本,解压出来的文件夹里的dist
目录里可以找到最新版本的 echarts 库。通过 npm 获取 echarts,
npm install echarts --save
,详见“在 webpack 中使用 echarts”
引入 ECharts
ECharts 3 开始不再强制使用 AMD 的方式按需引入,代码里也不再内置 AMD 加载器。因此引入方式简单了很多,只需要像普通的 JavaScript 库一样用 script 标签引入。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 引入 ECharts 文件 -->
<script src="echarts.min.js"></script>
</head>
</html>
绘制一个简单的图表
在绘图前我们需要为 ECharts 准备一个具备高宽的 DOM 容器。
<body>
<!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
<div id="main" style="width: 600px;height:400px;"></div>
</body>
然后就可以通过 echarts.init 方法初始化一个 echarts 实例并通过 setOption 方法生成一个简单的柱状图,下面是完整代码。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts</title> <!-- 引入 echarts.js --> <script src="echarts.min.js"></script> </head> <body> <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main'));
</span><span class="com">// 指定图表的配置项和数据</span><span class="pln"> </span><span class="kwd">var</span><span class="pln"> option </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> title</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> text</span><span class="pun">:</span><span class="pln"> </span><span class="str">'ECharts 入门示例'</span><span class="pln"> </span><span class="pun">},</span><span class="pln"> tooltip</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{},</span><span class="pln"> legend</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> data</span><span class="pun">:[</span><span class="str">'销量'</span><span class="pun">]</span><span class="pln"> </span><span class="pun">},</span><span class="pln"> xAxis</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> data</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="str">"衬衫"</span><span class="pun">,</span><span class="str">"羊毛衫"</span><span class="pun">,</span><span class="str">"雪纺衫"</span><span class="pun">,</span><span class="str">"裤子"</span><span class="pun">,</span><span class="str">"高跟鞋"</span><span class="pun">,</span><span class="str">"袜子"</span><span class="pun">]</span><span class="pln"> </span><span class="pun">},</span><span class="pln"> yAxis</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{},</span><span class="pln"> series</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[{</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'销量'</span><span class="pun">,</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> </span><span class="str">'bar'</span><span class="pun">,</span><span class="pln"> data</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">20</span><span class="pun">,</span><span class="pln"> </span><span class="lit">36</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span class="lit">20</span><span class="pun">]</span><span class="pln"> </span><span class="pun">}]</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> </span><span class="com">// 使用刚指定的配置项和数据显示图表。</span><span class="pln"> myChart</span><span class="pun">.</span><span class="pln">setOption</span><span class="pun">(</span><span class="pln">option</span><span class="pun">);</span><span class="pln"> </span><span class="tag"></script></span><span class="pln">
</body>
</html>
这样你的第一个图表就诞生了!
你也可以直接进入 ECharts Gallery 中查看编辑示例