3、使用ECharts控件

这个示例使用了 ECharts 的饼图,并使用了 Vue.js 的生命周期钩子函数 mounted 来在组件挂载后初始化图表。在 data 中,chartData 存储了图表的数据,chartColors 存储了图表的颜色配置。在 methods 中,initECharts 方法用于初始化 ECharts 实例和配置项。

<template>
  <div>
    <!-- ECharts 图表容器 -->
    <div class="chart-container" ref="chart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  data() {
    return {
      // 模拟的图表数据
      chartData: {
        xAxisData: ['A', 'B', 'C'],
        seriesData: [5, 3, 5],
      },
      // 图表颜色配置
      chartColors: ['#3398DB', '#FF6666', '#3CB371', '#FFD700', '#8B4513'],
    };
  },
  mounted() {
    // 使用固定数据配置 ECharts 实例
    this.initECharts();
  },
  methods: {
    initECharts() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom);

      // ECharts 配置项
      const option = {
        title: {
          text: '示例数据',
          left: 'center',
          top: 20,
          textStyle: {
            fontSize: 16,
            fontWeight: 'bold',
          },
        },
        series: [{
          name: '示例数据',
          type: 'pie',
          radius: '55%',
          center: ['50%', '50%'],
          data: this.chartData.xAxisData.map((name, index) => ({
            name,
            value: this.chartData.seriesData[index],
            itemStyle: {
              color: this.chartColors[index % this.chartColors.length],
            },
          })),
          label: {
            show: true,
            formatter: '{b} : {c} ({d}%)',
          },
        }],
      };

      // 使用配置项设置图表
      myChart.setOption(option);
    },
  },
};
</script>

<style scoped>
.chart-container {
  height: 300px;
  /* 设置图表容器的高度 */
}
</style>
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 引入ECharts框架 在HTML页面中引入ECharts框架,可以通过CDN或者下载ECharts源码后引入。 例如,可以在head标签中引入以下代码: ```html <head> <meta charset="UTF-8"> <title>使用ECharts控件</title> <!-- 引入ECharts框架 --> <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script> </head> ``` 2. 创建容器 在HTML页面中创建一个div容器,用来展示ECharts控件。 例如,可以在body标签中创建一个id为chart的div容器: ```html <body> <div id="chart" style="width: 600px;height:400px;"></div> </body> ``` 3. 初始化ECharts控件JavaScript代码中初始化ECharts控件,并配置相关参数。 例如,可以在script标签中添加以下代码: ```javascript <script> // 获取div容器 var chartDom = document.getElementById('chart'); // 初始化ECharts控件 var myChart = echarts.init(chartDom); // 配置参数 var option = { title: { text: 'ECharts 入门示例' }, tooltip: {}, legend: { data:['销量'] }, xAxis: { data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }; // 使用配置项显示ECharts控件 myChart.setOption(option); </script> ``` 以上代码实现了一个简单的柱状图,并将其显示在id为chart的div容器中。 其中,通过echarts.init()方法初始化ECharts控件,并使用setOption()方法显示图表。在配置参数中,可以设置ECharts控件的各种参数,例如标题、提示框、图例、坐标轴、数据系列等等。具体参数可以参考ECharts官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值