echarts案例

本文展示了如何使用ECharts库创建一个折线图,并在图中显示最大值、最小值和平均值。通过示例代码详细解释了配置项和数据的设置,包括X轴、Y轴、提示栏、图例、网格和工具箱等元素的定制。此外,还实现了点击图表时显示对应数值的功能。
摘要由CSDN通过智能技术生成

最近在写图表,研究了一下echarts,写个小案例里面有各种配置的备注,折线图显示最大、最小值、平均值的功能。话不多说上代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>echarts练习</title>
  <script src="./4.0.3/echarts.common.js"></script>
  <script src="./jquery-2.2.3.min.js"></script>
</head>
<body>
  <div class="publicity"></div>
  <div class="money">15</div>
  <div id="main" style="width:300px;height:250px;"></div>
</body>
<style>
  .money {
    height: 30px;
    width:100px;
    margin-left: 50px;
    margin-bottom: 20px;
    text-align: center;
  }
</style>
<script type="text/javascript">
  var publicity = ['科大讯飞', '企业价值观', '扭转乾坤', '牛年大吉', '聆听胡']
  var pubIndex = Math.floor(Math.random() * publicity.length)
  $('.publicity').text(publicity[pubIndex])
  var money = document.querySelector('.money')
  // 基于准备好的dom,初始化echarts实例
  var myChart = echarts.init(document.getElementById('main'));
  // 指定图表的配置项和数据
  var option = {
    title: {
      textStyle: {
        fontSize: 10,
        lineHeight: 20
      },
      backgroundColor: '#ccc',
      borderColor: 'rgb(255,0,255)',
      borderRadius: 5,
      text: 'ECharts 入门示例'
    },
    tooltip: { // 提示栏配置
      trigger: 'axis',
      backgroundColor: '#abcdef'
    },
    legend: {  // 标题配置
      data: ['销量'],
    },
    grid:{
      show:true,
      backgroundColor:'rgba(128, 128, 128, 0.5)'
    },
    toolbox: {  // 转换的工具
      show: true,
      feature: {
        magicType: { type: ['line', 'bar'] },
      }
    },
    xAxis: {  // X轴上面的配置
      type: 'category',
      boundaryGap: false,
      data: ["1月", "2月", "3月", "4月", "5月", "6月", '7月', '8月', '9月', '10月', '11月', '12月'],
      // 显示全部的X轴数据
      axisLabel: { interval: 0 },
      axisLine: {
        symbol: ['none', 'arrow'],
      },
    },
    yAxis: {  // Y轴上面的配置
      type: 'value',
      splitLine: {
        show: true,
        lineStyle: {
          color: ['blue'],
          width: 1,
          type: 'solid'
        }
      }
    },
    series: [{
      name: '销量',
      type: 'line',
      smooth: true,
      data: [5, 20, 36, 10, 10, 20, 30, 29, 37, 19, 11, 29],
      markPoint: {  // 图表上显示的值
        data: [
          { type: 'max', name: '最大值' },
          { type: 'min', name: '最小值' }
        ]
      },
      markLine: {
        data: [
          { type: 'average', name: '平均值' }
        ]
      },
    }],
  };

  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
  myChart.on('click', function (params) {
    console.log(params.data, money)
    money.innerHTML = params.data
  });
</script>
</html>

效果图
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值