ChartDynamic.vue 3.79 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue';
import * as echarts from 'echarts';
type EChartsOption = echarts.EChartsOption;
type ECharts = echarts.ECharts;

const colors = ['#9EB23B', '#EF9F9F'];
const dynamicRef = ref<any>(null);
const state = reactive({
  app: {} as any,
  categories: null as any,
  categories2: null as any,
  data: [] as number[],
  data2: [] as number[],
});

onMounted(() => {
  getCategories();
  getCategories2();
  getData();
  getData2();
  getChart();
});

var dynamicChart: ECharts;
function getChart() {
  if (dynamicChart != null && dynamicChart != undefined) {
    dynamicChart.dispose();
  }
  dynamicChart = echarts.init(dynamicRef.value);
  let option: EChartsOption;

  option = {
    color: colors,
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
      },
    },
    legend: {},
    dataZoom: {
      show: false,
      start: 0,
      end: 100,
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: true,
        data: state.categories,
      },
      {
        type: 'category',
        boundaryGap: true,
        data: state.categories2,
      },
    ],
    yAxis: [
      {
        type: 'value',
        scale: true,
        name: '折线图',
        max: 30,
        min: 0,
        boundaryGap: [0.2, 0.2],
        axisLine: {
          show: true,
          lineStyle: {
            color: colors[0],
          },
        },
      },
      {
        type: 'value',
        scale: true,
        name: '柱状图',
        max: 1200,
        min: 0,
        boundaryGap: [0.2, 0.2],
        axisLine: {
          show: true,
          lineStyle: {
            color: colors[1],
          },
        },
      },
    ],
    series: [
      {
        name: '折线图',
        type: 'line',
        data: state.data2,
        itemStyle: {
          color: colors[0],
        },
      },
      {
        name: '柱状图',
        type: 'bar',
        // 使用的 x 轴的 index,在单个图表实例中存在多个 x 轴的时候有用。
        xAxisIndex: 1,
        yAxisIndex: 1,
        data: state.data,
        barWidth: 35,
        itemStyle: {
          borderRadius: 10,
          color: colors[1],
        },
      },
    ],
  };

  state.app.count = 11;

  setInterval(function () {
    let axisData = new Date().toLocaleTimeString().replace(/^\D*/, '');

    state.data.shift();
    state.data.push(Math.round(Math.random() * 1000));
    state.data2.shift();
    state.data2.push(+(Math.random() * 10 + 5).toFixed(1));

    state.categories.shift();
    state.categories.push(axisData);
    state.categories2.shift();
    state.categories2.push(state.app.count++);

    dynamicChart.setOption<EChartsOption>({
      xAxis: [
        {
          data: state.categories,
        },
        {
          data: state.categories2,
        },
      ],
      series: [
        {
          data: state.data2,
        },
        {
          data: state.data,
        },
      ],
    });
  }, 2100);

  option && dynamicChart.setOption(option);
}

function getCategories() {
  let now = new Date();
  let res = <any>[];
  let len = 10;
  while (len--) {
    res.unshift(now.toLocaleTimeString().replace(/^\D*/, ''));
    now = new Date(+now - 2000);
  }
  state.categories = res;
}

function getCategories2() {
  let res = [];
  let len = 10;
  while (len--) {
    res.push(10 - len - 1);
  }
  state.categories2 = res;
}

function getData() {
  let res = [];
  let len = 10;
  while (len--) {
    res.push(Math.round(Math.random() * 1000));
  }
  state.data = res;
}

function getData2() {
  let res = [];
  let len = 0;
  while (len < 10) {
    res.push(+(Math.random() * 10 + 5).toFixed(1));
    len++;
  }
  state.data2 = res;
}
</script>
<template>
  <div class="fit" ref="dynamicRef"></div>
</template>

<style lang="scss" scoped></style>