|
@@ -1,265 +0,0 @@
|
|
|
-<template>
|
|
|
- <div class="chart">
|
|
|
- <div v-if="!showListBtn" class="textGroup van-hairline--surround">
|
|
|
- <span
|
|
|
- :class="{ text: true, act: type === item.type }"
|
|
|
- v-for="(item, i) in btnG"
|
|
|
- :key="i"
|
|
|
- @click="() => clickBtn(item)"
|
|
|
- >{{ item.name }}</span
|
|
|
- >
|
|
|
- </div>
|
|
|
- <canvas
|
|
|
- ref="chart"
|
|
|
- :width="canvasRegion.width"
|
|
|
- :height="canvasRegion.height"
|
|
|
- ></canvas>
|
|
|
- </div>
|
|
|
-</template>
|
|
|
-
|
|
|
-<script>
|
|
|
-// import F2 from "@antv/f2";
|
|
|
-const F2 = require("@antv/f2/lib/core");
|
|
|
-require("@antv/f2/lib/geom/line");
|
|
|
-require("@antv/f2/lib/geom/point");
|
|
|
-require("@antv/f2/lib/geom/area");
|
|
|
-const GroupAnimation = require("@antv/f2/lib/animation/detail");
|
|
|
-F2.Chart.plugins.register(GroupAnimation); // 这里进行全局注册,也可以给 chart 的实例注册
|
|
|
-const Tooltip = require("@antv/f2/lib/plugin/tooltip");
|
|
|
-F2.Chart.plugins.register(Tooltip);
|
|
|
-export default {
|
|
|
- name: "chart",
|
|
|
- props: {
|
|
|
- list: {
|
|
|
- type: Array,
|
|
|
- default: () => [],
|
|
|
- },
|
|
|
- type: {
|
|
|
- type: String,
|
|
|
- default: "1h",
|
|
|
- },
|
|
|
- showListBtn: {
|
|
|
- type: Boolean,
|
|
|
- default: false,
|
|
|
- }
|
|
|
- },
|
|
|
- components: {},
|
|
|
- data() {
|
|
|
- return {
|
|
|
- setInterval: undefined,
|
|
|
- setTimeout: undefined,
|
|
|
- chart: undefined,
|
|
|
- btnG: [
|
|
|
- {
|
|
|
- name: "1小时",
|
|
|
- type: "6m",
|
|
|
- },
|
|
|
- {
|
|
|
- name: "24小时",
|
|
|
- type: "24h",
|
|
|
- },
|
|
|
- {
|
|
|
- name: "7天",
|
|
|
- type: "7d",
|
|
|
- },
|
|
|
- {
|
|
|
- name: "30天",
|
|
|
- type: "30d",
|
|
|
- },
|
|
|
- ],
|
|
|
- canvasRegion: {
|
|
|
- width: 0,
|
|
|
- height: 0,
|
|
|
- },
|
|
|
- };
|
|
|
- },
|
|
|
- mounted() {
|
|
|
- let w = document.body.clientWidth || 0;
|
|
|
- let h = (document.body.clientHeight - 134) / 3;
|
|
|
- this.canvasRegion = {
|
|
|
- width: w > 1200 ? 1200 : w,
|
|
|
- height: h,
|
|
|
- };
|
|
|
- if (!this.list.length) return;
|
|
|
- this.$nextTick(() => {
|
|
|
- this.init();
|
|
|
- });
|
|
|
- },
|
|
|
- watch: {
|
|
|
- list(n) {
|
|
|
- this.setTimeout && clearTimeout(this.setTimeout);
|
|
|
- this.setInterval && clearTimeout(this.setInterval);
|
|
|
- this.chart.changeData(n);
|
|
|
- this.chart.repaint();
|
|
|
- this.init();
|
|
|
- },
|
|
|
- },
|
|
|
- destroyed() {
|
|
|
- console.log("--销毁");
|
|
|
- },
|
|
|
- methods: {
|
|
|
- init() {
|
|
|
- this.chart = new F2.Chart({
|
|
|
- el: this.$refs.chart,
|
|
|
- pixelRatio: window.devicePixelRatio,
|
|
|
- });
|
|
|
- this.chart.source(this.list);
|
|
|
- this.chart.scale("date", {
|
|
|
- range: [0, 1],
|
|
|
- type: "cat",
|
|
|
- tickCount: 3,
|
|
|
- });
|
|
|
- this.chart.tooltip({
|
|
|
- custom: true,
|
|
|
- triggerOn: ["touchstart"],
|
|
|
- showCrosshairs: true,
|
|
|
- layout: "vertical",
|
|
|
- snap: true,
|
|
|
- crosshairsType: "xy",
|
|
|
- crosshairsStyle: {
|
|
|
- lineDash: [2],
|
|
|
- },
|
|
|
- showXTip: true,
|
|
|
- onChange: (obj) => {
|
|
|
- const legend = this.chart.get("legendController").legends.top[0];
|
|
|
- const tooltipItems = obj.items;
|
|
|
- const legendItems = legend.items;
|
|
|
- const map = {};
|
|
|
- legendItems.forEach(function (item) {
|
|
|
- map[item.name] = JSON.parse(JSON.stringify(item));
|
|
|
- });
|
|
|
- tooltipItems.forEach(function (item) {
|
|
|
- const name = item.name;
|
|
|
- const value = item.value;
|
|
|
- if (map[name]) {
|
|
|
- map[name].value = value;
|
|
|
- }
|
|
|
- });
|
|
|
- let keys = Object.keys(map);
|
|
|
- let listVal = [];
|
|
|
- keys.map((v) => {
|
|
|
- listVal.push(map[v]);
|
|
|
- });
|
|
|
- legend.setItems(listVal);
|
|
|
- },
|
|
|
- onHide: () => {
|
|
|
- const legend = this.chart.get("legendController").legends.top[0];
|
|
|
- legend.setItems(this.chart.getLegendItems().country);
|
|
|
- },
|
|
|
- });
|
|
|
- this.chart.axis("date", {
|
|
|
- label: function label(text, index, total) {
|
|
|
- // 只显示每一年的第一天
|
|
|
- const textCfg = {
|
|
|
- fill: "#ccc",
|
|
|
- fontSize: 14,
|
|
|
- };
|
|
|
- if (index === 0) {
|
|
|
- textCfg.textAlign = "left";
|
|
|
- } else if (index === total - 1) {
|
|
|
- textCfg.textAlign = "right";
|
|
|
- }
|
|
|
- return textCfg;
|
|
|
- },
|
|
|
- });
|
|
|
- this.chart.axis("value", {
|
|
|
- label: function label(text) {
|
|
|
- // 只显示每一年的第一天
|
|
|
- const textCfg = {};
|
|
|
- if(text >= 100000000){
|
|
|
- textCfg.text = text / 100000000 + "亿"
|
|
|
- }else if(text >= 10000){
|
|
|
- textCfg.text = text / 10000 + "万"
|
|
|
- } else{
|
|
|
- textCfg.text = text
|
|
|
- }
|
|
|
- return textCfg;
|
|
|
- },
|
|
|
- });
|
|
|
- this.chart
|
|
|
- .line()
|
|
|
- .shape("smooth")
|
|
|
- .position("date*value")
|
|
|
- .color("type", ["#7aa6e1", "#FF9933"])
|
|
|
- .size("type", [1.5, 1, 1]);
|
|
|
- // this.chart
|
|
|
- // .point()
|
|
|
- // .position("date*value")
|
|
|
- // .color("type", ["#7aa6e1", "#FF9933"])
|
|
|
- // .shape("smooth")
|
|
|
- // .style({
|
|
|
- // fill: "#fff",
|
|
|
- // size: 2,
|
|
|
- // lineWidth: 1,
|
|
|
- // });
|
|
|
- this.chart
|
|
|
- .area()
|
|
|
- .shape("smooth")
|
|
|
- .position("date*value")
|
|
|
- .color("type", [
|
|
|
- "l(90) 0:#1677ff 1:#FFFFFF",
|
|
|
- "l(90) 0:#FF9933 1:#FFFFFF",
|
|
|
- ]);
|
|
|
- this.chart.render();
|
|
|
- },
|
|
|
- clickBtn(item) {
|
|
|
- if (this.type === item.type) return;
|
|
|
- this.chart.changeData(this.list);
|
|
|
- this.chart.repaint();
|
|
|
- if (item.type === "24h" || item.type === "6m") this.countFun(item);
|
|
|
- this.$emit("select", item);
|
|
|
- },
|
|
|
- countFun(item) {
|
|
|
- let time = new Date();
|
|
|
- let min = time.getMinutes();
|
|
|
- let remainder = min % 10;
|
|
|
- this.setTimeout && clearTimeout(this.setTimeout);
|
|
|
- this.setInterval && clearTimeout(this.setInterval);
|
|
|
- if (remainder === 1) {
|
|
|
- this.setInterval = setInterval(() => {
|
|
|
- console.log("定时");
|
|
|
- this.$emit("select", item);
|
|
|
- }, 600000);
|
|
|
- } else {
|
|
|
- this.setTimeout = setTimeout(() => {
|
|
|
- clearTimeout(this.setTimeout);
|
|
|
- this.$emit("select", item);
|
|
|
- this.setInterval = setInterval(() => {
|
|
|
- console.log("定时");
|
|
|
- this.$emit("select", item);
|
|
|
- }, 600000);
|
|
|
- }, (11 - remainder) * 1000 * 60);
|
|
|
- }
|
|
|
- },
|
|
|
- },
|
|
|
-};
|
|
|
-</script>
|
|
|
-
|
|
|
-<style lang="scss" scoped>
|
|
|
-.chart {
|
|
|
- position: relative;
|
|
|
- .textGroup {
|
|
|
- position: absolute;
|
|
|
- overflow: hidden;
|
|
|
- top: 15px;
|
|
|
- right: 10px;
|
|
|
- font-size: 0;
|
|
|
- color: #1989fa;
|
|
|
- &::after {
|
|
|
- border-radius: 3px;
|
|
|
- border-color: #1989fa;
|
|
|
- }
|
|
|
- .text {
|
|
|
- padding: 0 3px;
|
|
|
- display: inline-block;
|
|
|
- text-align: center;
|
|
|
- cursor: pointer;
|
|
|
- font-size: 14px;
|
|
|
- }
|
|
|
- .act {
|
|
|
- background-color: #1989fa;
|
|
|
- color: #fff;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-</style>
|