• echarts案例之日历


    一、此案例基于Vue3+ts,效果展示:

    二、单个属性的值:

    1、visualMap.pieces 根据值自定义每个小块的颜色

          pieces: [

            {

              min: 0, // 最小值

              max: 20, // 最大值

              label: '未统计', 

              color: 'rgba(27, 61, 71,1)',

            },

            {

              min: 20,

              max: 50,

              label: '优',

              color: '#22a9a5',

            },

            {

              min: 50,

              max: 70,

              label: '中',

              color: '#c6bb2a',

            },

            {

              min: 70,

              // max: 200,

              label: '差',

              color: '#a03e3e',

            },

          ],

    2、yearLabel、monthLabel、dayLabel 年月日

    例如:

    yearLabel: { show: false }, // 不显示年(月同理)

        dayLabel: {

              firstDay: 1, // 设置第一天是几号

              nameMap: 'cn',

              color: '#407781',

            },

    3、range 范围(数组)

    数组里面是月份,格式如下:

     range: ['2023-01'],

    4、splitLine.lineStyle.color

    将日历外层边框颜色设为透明色,并且将边框的颜色和背景保持一致,就能实现格与格的间隔

    splitLine: {

              lineStyle: {

                color: 'transparent', // 设置日历外层边框颜色

              },

            },

     itemStyle: {

              borderColor: '#0e2029', 

              borderWidth: 4,

            },

    三、完整代码如下:

    1. <script setup lang="ts" name="airWallQualityStatistics-calendar">
    2. import { ref, onMounted } from 'vue';
    3. import * as echarts from 'echarts';
    4. import { formatDate } from '/@/utils/formatTime';
    5. const barRef = ref();
    6. let myChart: any = null; // echarts实例对象
    7. onMounted(() => {
    8. initCharts();
    9. });
    10. // 变化尺寸
    11. const onResize = (resize: any) => {
    12. console.log('resize: ', resize);
    13. myChart && myChart.resize();
    14. };
    15. window.addEventListener('resize', onResize);
    16. const initCharts = () => {
    17. // 获取当前月(格式'2023-01')
    18. let time = formatDate(new Date(), 'YYYY-mm');
    19. // 对应值的数组
    20. let maxArr = [];
    21. //获取当前年
    22. const year = new Date(time).getFullYear();
    23. //获取当前月
    24. let month = new Date(time).getMonth() + 1;
    25. //新建日期数组(日期+值)
    26. const datas = [];
    27. //获取当前年月日期最大天数
    28. const dataLength = new Date(year, month).toJSON().substring(0, 10).split('-')[2];
    29. for (let i = 0; i < Number(dataLength); i++) {
    30. let months = month >= 10 ? month : '0' + month;
    31. let day = i + 1 >= 10 ? i + 1 : '0' + (i + 1);
    32. datas.push({ date: year + '-' + months + '-' + day, total: Math.floor(Math.random() * 100) }); //存入数组
    33. }
    34. let chartData: any = [];
    35. datas.forEach((item) => {
    36. maxArr.push(item.total);
    37. chartData.push({
    38. value: [item.date, item.total],
    39. });
    40. });
    41. let thisMonth = time; //当前月份
    42. let option = {
    43. tooltip: {
    44. backgroundColor: 'rgba(69, 173, 175, 0.6)',
    45. borderWidth: 0,
    46. textStyle: {
    47. color: '#fff',
    48. fontSize: 14,
    49. },
    50. formatter: function (params: any) {
    51. return '空气质量: ' + params.value[1];
    52. },
    53. },
    54. visualMap: {
    55. min: 0,
    56. max: 100,
    57. show: false,
    58. pieces: [
    59. {
    60. min: 0,
    61. max: 20,
    62. label: '未统计',
    63. color: 'rgba(27, 61, 71,1)',
    64. },
    65. {
    66. min: 20,
    67. max: 50,
    68. label: '优',
    69. color: '#22a9a5',
    70. },
    71. {
    72. min: 50,
    73. max: 70,
    74. label: '中',
    75. color: '#c6bb2a',
    76. },
    77. {
    78. min: 70,
    79. // max: 200,
    80. label: '差',
    81. color: '#a03e3e',
    82. },
    83. ],
    84. },
    85. calendar: [
    86. {
    87. left: 'center',
    88. bottom: '10',
    89. cellSize: [35, 25],
    90. yearLabel: { show: false }, // 是否显示月份
    91. orient: 'vertical',
    92. dayLabel: {
    93. firstDay: 1,
    94. nameMap: 'cn',
    95. color: '#407781',
    96. },
    97. monthLabel: {
    98. show: false,
    99. },
    100. range: [thisMonth],
    101. itemStyle: {
    102. borderColor: '#0e2029',
    103. borderWidth: 4,
    104. },
    105. splitLine: {
    106. lineStyle: {
    107. color: 'transparent', // 设置日历外层边框颜色
    108. },
    109. },
    110. },
    111. ],
    112. series: [
    113. {
    114. type: 'scatter',
    115. coordinateSystem: 'calendar',
    116. label: {
    117. show: true,
    118. formatter: function (params: any) {
    119. const d = echarts.number.parseDate(params.value[0]);
    120. return d.getDate();
    121. },
    122. // color: '#52fffc',
    123. color: '#fff',
    124. },
    125. data: chartData,
    126. silent: true,
    127. },
    128. {
    129. // name: '空气质量',
    130. type: 'heatmap',
    131. coordinateSystem: 'calendar',
    132. data: chartData,
    133. },
    134. ],
    135. };
    136. myChart = echarts.init(barRef.value as HTMLDivElement);
    137. myChart.setOption(option, true);
    138. };
    139. script>
    140. <style lang="scss" scoped>
    141. .container {
    142. width: 100%;
    143. height: 100%;
    144. }
    145. style>
  • 相关阅读:
    Himall商城- web私有方法
    力扣(139.198)补8.2
    Amazon Bedrock | 大语言模型CLAUDE 2体验
    Revit土建模块【图转轴网】功能快速生成轴网
    CTF之信息收集
    nvm下载安装+使用教程(管理nodejs版本,实现按需加载版本)
    关于对象数组的一些方法总结
    什么是tomcat?tomcat是干什么用的?下面带你们认识tomcat!通俗易懂!
    [LeetCode319周赛] 环图,最大公因数,中心扩展+DP
    探索arkui(2)--- 布局(列表)--- 1(列表数据的展示)
  • 原文地址:https://blog.csdn.net/weixin_48082900/article/details/134030511