温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用echarts画柱状图

发布时间:2026-10-09 08:30:02 来源:亿速云 阅读:89 作者:小樊 栏目:编程语言

下面用 ECharts 画一个最基础的柱状图示例,从环境准备到完整代码,一步步说明。


一、引入 ECharts

方式 1:CDN(最简单)

<script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>

方式 2:npm(项目中使用)

npm install echarts
import * as echarts from 'echarts';

二、准备 HTML 容器

<div id="chart" style="width: 600px; height: 400px;"></div>

三、初始化 ECharts 并画柱状图

<script>
  // 初始化
  const chart = echarts.init(document.getElementById('chart'));

  // 配置项
  const option = {
    title: {
      text: '月度销售额'
    },
    tooltip: {},
    xAxis: {
      type: 'category',
      data: ['1月', '2月', '3月', '4月', '5月']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '销售额',
        type: 'bar',
        data: [120, 200, 150, 80, 170]
      }
    ]
  };

  // 渲染
  chart.setOption(option);
</script>

四、常见柱状图变体

1️⃣ 多系列柱状图

series: [
  {
    name: '产品A',
    type: 'bar',
    data: [120, 200, 150, 80, 170]
  },
  {
    name: '产品B',
    type: 'bar',
    data: [90, 140, 110, 60, 130]
  }
]

2️⃣ 横向柱状图

xAxis: { type: 'value' },
yAxis: { type: 'category', data: ['A', 'B', 'C'] },
series: [{ type: 'bar', data: [10, 20, 30] }]

3️⃣ 堆叠柱状图

series: [
  { name: '线上', type: 'bar', stack: 'total', data: [120, 200] },
  { name: '线下', type: 'bar', stack: 'total', data: [80, 100] }
]

五、响应式(窗口变化)

window.addEventListener('resize', () => {
  chart.resize();
});

六、完整最小示例

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>ECharts 柱状图</title>
  <script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
</head>
<body>
  <div id="chart" style="width: 600px; height: 400px;"></div>
  <script>
    const chart = echarts.init(document.getElementById('chart'));
    chart.setOption({
      xAxis: { type: 'category', data: ['A', 'B', 'C'] },
      yAxis: { type: 'value' },
      series: [{ type: 'bar', data: [10, 20, 30] }]
    });
  </script>
</body>
</html>

如果你有 具体数据 / 业务场景(如销售、成绩、财务),我可以帮你定制更合适的柱状图配置。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手