美文网首页
ECharts配置.md

ECharts配置.md

作者: 时修七年 | 来源:发表于2019-02-15 17:21 被阅读10次

ECharts,是百度开源的可视化库,它使用 JavaScript和canvas 实现,具有简单易用的特点。

echarts的使用分为5步

//1.引入echarts
<script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.js"></script>
// 2.echarts需要一个承载点,因此我们需要设置一个容器
<div id="echarts"></div>
// 3.定义配置项
let option = {}
// 4.初始化echarts
let Chart = echarts.init(document.getElementById("#echarts"))
// 5.设置option
Chart.setOption(option)

echart.png

柱状图及常用配置

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.js"></script>
</head>
<body>
<div id="echarts" style="width: 850px;height: 600px;margin: 30px auto; padding: 40px;>

</div>

<script type="text/javascript">
    let option = {
        // 设置标题
        title: {
            text: "北京市人口分布图",//主标题
            subtext: "2019年度",//副标题
            textStyle: { 
                color: "#00cc66"
            },
            subtextStyle: {
                color: "#9ea7b4"
            }
        },
        // 设置图例
        legend: {
            data: ['男','女','平均年龄'],
            orient: 'vertical',
            top: "middle",
            right: "right"
        },
        // 鼠标hover到柱子上展示数值
        tooltip: {},
        // x轴设置
        xAxis: [
            {
                type: "category",
                data: ['一月份','二月份','三月份','四月份',"五月份"],
            }
        ],
        // y轴设置
        yAxis: [
            {
                type: "value",
                name: '人口'
            },
        ],
        // series与图列相对应
        series: [
            {
                name: '男', 
                type: 'bar',
                // 配置柱状图颜色
                itemStyle: {
                    color: "#ff9900",
                },
                barWidth: "20%",
                data: [786,23,816,945,1002],
                // 条形图上是否显示值
                label: {
            normal: {
            show: true,
            position: 'top'
            }
          },
            },
            {
                name: '女',
                type: 'bar',
                barWidth: "20%",
                barGap: "50%",//不同条目之间的距离,比如该图的男女,用在最后一个
                // barCategoryGap: 20,//不同类别之间的距离,与barWidth只能存在一个,用在最后一个
                data: [1000,40,906,1034,1302],
                label: {
            normal: {
            show: true,
            position: 'top' // inside,top
            }
          },
            },
        ],
        // 效果等同ps参考线
        // axisPointer: {
        //  show: true
        // }


    }

    let Echart = echarts.init(document.getElementById("echarts"))

    Echart.setOption(option)
</script>
</body>
</html>

在一个图表中同时使用多种

在legend的data中,增加图例说明,在y轴新建标尺,同时在series 中增加配置项

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.js"></script>
</head>
<body>
<div id="echarts" style="width: 850px;height: 600px;margin: 30px auto; padding: 40px;border: 1px solid red">

</div>

<script type="text/javascript">
    let option = {
        // 设置标题
        title: {
            text: "北京市人口分布图",//主标题
            subtext: "2019年度",//副标题
            textStyle: { 
                color: "#00cc66"
            },
            subtextStyle: {
                color: "#9ea7b4"
            }
        },
        // 设置图例
        legend: {
            data: ['男','女','平均年龄'],
            orient: 'vertical',
            top: "middle",
            right: "right"
        },
        // 鼠标hover到柱子上展示数值
        tooltip: {},
        // x轴设置
        xAxis: [
            {
                type: "category",
                data: ['一月份','二月份','三月份','四月份',"五月份"],
            }
        ],
        // y轴设置
        yAxis: [
            {
                type: "value",
                name: '人口'
            },
            {
                type: 'value',
                name: '年龄',
                axisLabel: {
          formatter: '{value} 岁'
        },
    
            }
        ],
        // series与图列相对应
        series: [
            {
                name: '男', 
                type: 'bar',
                // 配置柱状图颜色
                itemStyle: {
                    color: "#ff9900",
                },
                barWidth: "20%",
                data: [786,23,816,945,1002],
                // 条形图上是否显示值
                label: {
            normal: {
            show: true,
            position: 'top'
            }
          },
            },
            {
                name: '女',
                type: 'bar',
                barWidth: "20%",
                barGap: "50%",//不同条目之间的距离,比如该图的男女,用在最后一个
                // barCategoryGap: 20,//不同类别之间的距离,与barWidth只能存在一个,用在最后一个
                data: [1000,40,906,1034,1302],
                label: {
            normal: {
            show: true,
            position: 'top' // inside,top
            }
          },
            },
            // 折线图
            {
                name: '平均年龄',
                type: 'line',
                yAxisIndex: 1,
                smooth: true,
                data: [
                    {name: '女',value:33},
                    {name: '女',value: 28},
                    {name: '女',value: 36},
                    {name: '女',value: 34},
                    {name: '女',value: 32}
                ]
            },
            {
                name: '平均年龄',
                type: 'line',
                yAxisIndex: 1,
                smooth: true,
                itemStyle: {
                    color: "#ff9900",
                },
                data: [
                    {name: '男',value:33},
                    {name: '男',value: 23},
                    {name: '男',value: 26},
                    {name: '男',value: 35},
                    {name: '男',value: 42}
                ]
            }
        ],
        // 效果等同ps参考线
        // axisPointer: {
        //  show: true
        // }


    }

    let Echart = echarts.init(document.getElementById("echarts"))

    Echart.setOption(option)
</script>
</body>
</html>

ECharts配置.md

相关文章

  • ECharts配置.md

    ECharts,是百度开源的可视化库,它使用 JavaScript和canvas 实现,具有简单易用的特点。 ec...

  • Echarts 饼状图 基础配置和高度自适应 formatter

    1.Echarts 饼状图 基础配置 Echarts 基础配置推荐 官网 http://echarts.baidu...

  • vue项目中配置ECharts

    目录 介绍 5 分钟上手 echarts echarts 基础配置 在 vue 项目中使用 echarts (一)...

  • 网站地址

    echarts option 配置项echarts 文档 在线转pdfOGIS开发中心

  • RN中使用图表

    使用native-echarts 具体的配置项可看echarts的官网Echarts网站 npm installn...

  • Echarts给Y轴、tooltip添加单位

    Echarts中给Y轴加单位 Echarts中给tooltip加单位 折线图相关配置 配置如下: 柱状图相关配置 ...

  • 2018-08-17

    echarts标题旋转 引入echarts-better 标题旋转配置 title: { rotate: -90 },

  • 2018-08-17

    echarts图例旋转 引入echarts-better 图例旋转配置如下 legend: { rotation:...

  • echarts图例收集

    echarts 属性配置 : https://www.w3cschool.cn/echarts_tutorial/...

  • 2018-08-17

    echarts图例展示数据 引入echarts-better 配置如下 需要data有数据 series : [ ...

网友评论

      本文标题:ECharts配置.md

      本文链接:https://www.haomeiwen.com/subject/wiemeqtx.html