# K 线图

查看代码详情
<template>
  <div ref="candlestick" style="height: 100%"></div>
</template>
  
<script>
export default {
  name: "WebCandlestick",
  props: {
    config: {
      type: Function,
    },
    data: {
      type: [Array, Object],
      default: () => [],
    },
    styles: {
      type: String,
      default: "height: 100%;width:800px;",
    },
    title: {
      type: [Array, Object],
      default: () => [
        {
          text: "统计数据",
          subtext: "单位:个",
        },
      ],
    },
    tooltip: {
      type: [Array, Object],
      default: () => [
        {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
      ],
    },
    grid: {
      type: [Array, Object],
      default: () => [
        {
          top: "250",
          bottom: 30,
        },
      ],
    },
    legend: {
      type: [Array, Object],
      default: () => [
        {
          orient: "vertical",
          left: "0%",
          top: "0%",
          bottom: "center",
          data: ["<10w", "10w-50w", "50w-100w", "100w-500w", ">500w"],
        },
      ],
    },
    xAxis: {
      type: [Array, Object],
      default: () => [
        {
          show: false,
        },
      ],
    },
    yAxis: {
      type: [Array, Object],
      default: () => [
        {
          show: false,
        },
      ],
    },
    series: {
      type: [Array, Object],
      default: () => [
        {
          type: "candlestick",
          radius: ["58%", "68%"],
        },
      ],
    },
  },
  data() {
    return {
      option: {
        title: this.titleTransform(this.title),
        tooltip: this.tooltip,
        grid: this.grid,
        legend: this.legend,
        series: this.series,
      },
      charts: null,
    };
  },
  methods: {
    titleTransform({ text, subtext, ...others }) {
      let arr = [];
      let target = {};
      if (text) {
        target = {
          text: "{style1|}{style2|}{style3|}" + text,
          textStyle: {
            fontWeight: "800",
            color: "#333",
            fontSize: 18,
            rich: {
              style1: {
                height: 20,
                width: 4,
                backgroundColor: "#2d65f2",
              },
              style2: {
                height: 20,
                width: 4,
                backgroundColor: "#b2c2ff",
              },
              style3: {
                width: 10,
              },
            },
          },
          left: 0,
          top: 0,
          ...others,
        };
        arr.push(target);
      }
      if (subtext) {
        target = {
          subtext: "{style1|}" + subtext,
          subtextStyle: {
            align: "right",
            verticalAlign: "top",
            color: "#666",
            fontSize: "18",
            rich: {
              style1: {},
            },
          },
          right: 0,
          top: -10,
          ...others,
        };
      }
      arr.push(target);
      return arr;
    },
  },
  mounted() {
    this.charts = this.$echarts.init(this.$refs.candlestick);
    if (!this.config) {
      this.charts.setOption(this.option);
    }
  },
  watch: {
    data: {
      handler(val) {
        let { title, ...option } = this.config(val);
        this.charts.setOption({
          title: title ? this.titleTransform(title) : [],
          ...option,
        });
      },
      deep: true,
    },
  },
};
</script>
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

# 1.普通散点图

查看代码详情
<template>
  <WebCandlestick :config="getOptions" :data="data"></WebCandlestick>
</template>

<script>
export default {
  data() {
    return {
      data: [],
    };
  },
  async created() {
    let res = await this.$api.getCandlestick(1);
    if (res.data) {
      this.data = res.data.map((item) => ({ ...item, value: item.descript }));
    }
  },
  methods: {
    getOptions(data) {
      return {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        legend: {
          data: ["日K", "MA5", "MA10", "MA20", "MA30"],
        },
        grid: {
          left: "10%",
          right: "10%",
          bottom: "15%",
        },
        xAxis: {
          type: "category",
          data: [
            "2013/1/24",
            "2013/1/25",
            "2013/1/28",
            "2013/1/29",
            "2013/1/30",
            "2013/1/31",
            "2013/2/1",
            "2013/2/4",
            "2013/2/5",
            "2013/2/6",
            "2013/2/7",
            "2013/2/8",
            "2013/2/18",
            "2013/2/19",
            "2013/2/20",
            "2013/2/21",
            "2013/2/22",
            "2013/2/25",
            "2013/2/26",
            "2013/2/27",
            "2013/2/28",
            "2013/3/1",
            "2013/3/4",
            "2013/3/5",
            "2013/3/6",
            "2013/3/7",
            "2013/3/8",
            "2013/3/11",
            "2013/3/12",
            "2013/3/13",
            "2013/3/14",
            "2013/3/15",
            "2013/3/18",
            "2013/3/19",
            "2013/3/20",
            "2013/3/21",
            "2013/3/22",
            "2013/3/25",
            "2013/3/26",
            "2013/3/27",
            "2013/3/28",
            "2013/3/29",
            "2013/4/1",
            "2013/4/2",
            "2013/4/3",
            "2013/4/8",
            "2013/4/9",
            "2013/4/10",
            "2013/4/11",
            "2013/4/12",
            "2013/4/15",
            "2013/4/16",
            "2013/4/17",
            "2013/4/18",
            "2013/4/19",
            "2013/4/22",
            "2013/4/23",
            "2013/4/24",
            "2013/4/25",
            "2013/4/26",
            "2013/5/2",
            "2013/5/3",
            "2013/5/6",
            "2013/5/7",
            "2013/5/8",
            "2013/5/9",
            "2013/5/10",
            "2013/5/13",
            "2013/5/14",
            "2013/5/15",
            "2013/5/16",
            "2013/5/17",
            "2013/5/20",
            "2013/5/21",
            "2013/5/22",
            "2013/5/23",
            "2013/5/24",
            "2013/5/27",
            "2013/5/28",
            "2013/5/29",
            "2013/5/30",
            "2013/5/31",
            "2013/6/3",
            "2013/6/4",
            "2013/6/5",
            "2013/6/6",
            "2013/6/7",
            "2013/6/13",
          ],
          scale: true,
          boundaryGap: false,
          axisLine: {
            onZero: false,
          },
          splitLine: {
            show: false,
          },
          splitNumber: 20,
          min: "dataMin",
          max: "dataMax",
        },
        yAxis: {
          scale: true,
          splitArea: {
            show: true,
          },
        },
        dataZoom: [
          {
            type: "inside",
            start: 50,
            end: 100,
          },
          {
            show: true,
            type: "slider",
            top: "90%",
            start: 50,
            end: 100,
          },
        ],
        series: [
          {
            name: "日K",
            type: "candlestick",
            data,
          },
        ],
      };
    },
  },
};
</script>
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