yikai.shao Backend Dev Engineer

echarts使用实例

2017-08-10

方式一:单文件引入

<!DOCTYPE html>
<html>
<head>
    <title>echarts使用</title>
</head>
<body>

<div id="main" style="width:300px;height:250px;"></div>
<script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>
<script type="text/javascript">

    var myChart = echarts.init(document.getElementById('main'));

    var option = {
        tooltip: {
            show: true
        },
        legend: {
            data: ['销量']
        },
        xAxis: [
            {
                type: 'category',
                data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                "name": "销量",
                "type": "bar",
                "data": [5, 20, 40, 10, 10, 20]
            }
        ]
    };

    myChart.setOption(option);

</script>

</body>
</html>

方式二:require方式

理解AMD模式,可参考 Javascript模块化编程(二):AMD规范

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>ECharts - 使用</title>
</head>
<body>

    <div id="main" style="height:400px"></div>

    <script src="http://echarts.baidu.com/build/dist/echarts.js"></script>
    <script type="text/javascript">

        require.config({
            paths: {
                echarts: 'http://echarts.baidu.com/build/dist'
            }
        });

        require(
            [
                'echarts',
                'echarts/chart/bar' // 使用柱状图就加载bar模块,按需加载
            ],
            function (ec) {
                var myChart = ec.init(document.getElementById('main'));

                var option = {
                    tooltip: {
                        show: true
                    },
                    legend: {
                        data:['销量']
                    },
                    xAxis : [
                        {
                            type : 'category',
                            data : ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
                        }
                    ],
                    yAxis : [
                        {
                            type : 'value'
                        }
                    ],
                    series : [
                        {
                            "name":"销量",
                            "type":"bar",
                            "data":[5, 20, 40, 10, 10, 20]
                        }
                    ]
                };

                myChart.setOption(option);
            }
        );
    </script>
</body>
</html>

下一篇 GIT用法:tag

概要