博客列表 >vue3 echart代码示例

vue3 echart代码示例

搁浅
搁浅原创
2023年03月21日 17:51:36475浏览
官方安装npm install echarts —save
https://echarts.apache.org/
  1. <template>
  2. <div>
  3. <div class="chart" id="main" ></div>
  4. </div>
  5. </template>
  6. <script setup>
  7. import * as echarts from "echarts";
  8. import { onMounted } from 'vue';
  9. onMounted(()=>{
  10. initEcharts()
  11. })
  12. function initEcharts() {
  13. // 基本柱状图
  14. const option = {
  15. xAxis: {
  16. data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  17. },
  18. yAxis: {},
  19. series: [
  20. {
  21. type: "bar", //形状为柱状图
  22. data: [23, 24, 18, 25, 27, 28, 25]
  23. }
  24. ]
  25. };
  26. const myChart = echarts.init(document.getElementById("main"));
  27. myChart.setOption(option);
  28. //随着屏幕大小调节图表
  29. window.addEventListener("resize", () => {
  30. myChart.resize();
  31. });
  32. }
  33. </script>
  34. <style scoped>
  35. .chart {
  36. height: 100vh;
  37. }
  38. </style>
npm install echarts vue-echarts//安装插件
https://github.com/ecomfe/vue-echarts/blob/HEAD/README.zh-Hans.md
vue-echarts属于二次封装
  1. <template>
  2. <v-chart class="chart" :option="option" autoresize />
  3. </template>
  4. <script setup>
  5. import { use } from 'echarts/core';// 挂载组件方法
  6. import { CanvasRenderer } from 'echarts/renderers';
  7. import { PieChart } from 'echarts/charts';// 引入图类型
  8. import {
  9. TitleComponent,
  10. TooltipComponent,
  11. LegendComponent,
  12. } from 'echarts/components';// 相关组件
  13. // InjectionKey 引入
  14. // THEME_KEY 主题关键字
  15. // UPDATE_OPTIONS_KEY 刷新画布关键字
  16. import VChart, { THEME_KEY,UPDATE_OPTIONS_KEY } from 'vue-echarts';
  17. import { ref, provide } from 'vue';
  18. // 挂载
  19. use([
  20. CanvasRenderer,
  21. PieChart,
  22. TitleComponent,
  23. TooltipComponent,
  24. LegendComponent,
  25. ]);
  26. provide(THEME_KEY, 'dark');// 主题颜色
  27. const option = ref({
  28. title: {
  29. text: 'Traffic Sources',
  30. left: 'center',
  31. },
  32. tooltip: {
  33. trigger: 'item',
  34. formatter: '{a} <br/>{b} : {c} ({d}%)',
  35. },
  36. legend: {
  37. orient: 'vertical',
  38. left: 'left',
  39. data: ['Direct', 'Email', 'Ad Networks', 'Video Ads', 'Search Engines'],
  40. },
  41. series: [
  42. {
  43. name: 'Traffic Sources',
  44. type: 'pie',
  45. radius: '55%',
  46. center: ['50%', '60%'],
  47. data: [
  48. { value: 335, name: 'Direct' },
  49. { value: 310, name: 'Email' },
  50. { value: 234, name: 'Ad Networks' },
  51. { value: 135, name: 'Video Ads' },
  52. { value: 1548, name: 'Search Engines' },
  53. ],
  54. emphasis: {
  55. itemStyle: {
  56. shadowBlur: 10,
  57. shadowOffsetX: 0,
  58. shadowColor: 'rgba(0, 0, 0, 0.5)',
  59. },
  60. },
  61. },
  62. ],
  63. });
  64. </script>
  65. <style scoped>
  66. .chart {
  67. height: 100vh;
  68. }
  69. </style>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议