ZhangXianQiang
2024-03-04 2b7b4e2ea52a17eda4eca7ada4a139ad20785631
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
<script setup lang="ts">
import { onMounted, reactive, ref, watch } from "vue";
import type { DefaultConfigType } from "./index.d";
import cloneDeep from "lodash/cloneDeep";
import merge from "lodash/merge";
const mergedConfig = ref<any>(null);
const capsuleLength = ref<any>([]);
const capsuleValue = ref<any>([]);
const labelData = ref<any>([]);
// const labelDataLength = ref<any>([]);
 
const defaultConfig = reactive<DefaultConfigType>({
    // Colors (hex|rgb|rgba|color keywords) ['#000', 'rgb(0, 0, 0)', 'rgba(0, 0, 0, 1)', 'red']
    colors: [
        "red",
        "#32c5e9",
        "#67e0e3",
        "#9fe6b8",
        "#ffdb5c",
        "#ff9f7f",
        "#fb7293",
    ],
    unit: "",
    showValue: false, // Show item value
});
const props = withDefaults(
    defineProps<{
        config: object | any;
        data: Array<{
            name: string;
            value: string | number;
        }>;
    }>(),
    {
        config: () => { },
        data: () => [],
    }
);
const calcData = () => {
    mergeConfig();
    calcCapsuleLengthAndLabelData();
};
const mergeConfig = () => {
    mergedConfig.value = merge(cloneDeep(defaultConfig), props.config || {});
};
const calcCapsuleLengthAndLabelData = () => {
    if (!props.data.length) return;
    const newcapsuleValue = props.data.map((item: any) => item.value);
    const maxValue = Math.max(...newcapsuleValue);
    capsuleValue.value = newcapsuleValue;
    capsuleLength.value = newcapsuleValue.map((v: any) =>
        maxValue ? v / maxValue : 0
    );
    const oneFifth = maxValue / 5;
    const newlabelData = Array.from(
        new Set(new Array(6).fill(0).map((v, i) => Math.ceil(i * oneFifth)))
    );
    labelData.value = newlabelData;
    // labelDataLength.value = Array.from(newlabelData).map((v) =>
    //     maxValue ? v / maxValue : 0
    // );
    // console.log(labelDataLength.value);
};
watch(
    () => props.data,
    (newval: any) => {
        calcData();
    },
);
watch(
    () => props.config,
    (newval: any) => {
        calcData();
    },
);
onMounted(() => {
    calcData();
});
</script>
 
<template>
    <div class="dv-capsule-chart">
        <template v-if="mergedConfig">
            <div class="label-column">
                <div v-for="item in data" :key="item.name">
                    {{ item.name }}
                </div>
                <div>&nbsp;</div>
            </div>
            <div class="capsule-container">
                <div class="capsule-item" v-for="(capsule, index) in capsuleLength" :key="index">
                    <div class="capsule-item-column" :style="`width: ${capsule * 100}%; background-color: ${mergedConfig.colors[index % mergedConfig.colors.length]
                    };`">
                        <div v-if="mergedConfig.showValue" class="capsule-item-value">
                            {{ capsuleValue[index] }}
                        </div>
                    </div>
                </div>
 
                <div class="unit-label">
                    <div v-for="(label, index) in labelData" :key="label + index">
                        {{ label }}
                    </div>
                </div>
            </div>
 
            <div class="unit-text" v-if="mergedConfig.unit">
                {{ mergedConfig.unit }}
            </div>
        </template>
    </div>
</template>
 
<style scoped lang="scss">
.dv-capsule-chart {
    position: relative;
    display: flex;
    flex-direction: row;
    box-sizing: border-box;
    padding: 10px;
    color: #fff;
    height: 90%!important;
    .label-column {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        box-sizing: border-box;
        padding-right: 10px;
        text-align: right;
        font-size: 12px;
 
        div {
            height: 20px;
            line-height: 20px;
        }
    }
 
    .capsule-container {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
 
    .capsule-item {
        //box-shadow: 0 0 3px #999;
      box-shadow:none;
        height: 10px;
        margin: 5px 0px;
        border-radius: 5px;
 
        .capsule-item-column {
          background: linear-gradient(to right, #9fe1fa, #f4edc9);
          position: relative;
            height: 15px;
            margin-top: 1px;
            border-radius: 5px;
            transition: all 0.3s;
            display: flex;
            justify-content: flex-end;
            align-items: center;
 
            .capsule-item-value {
                font-size: 12px;
                transform: translateX(100%);
            }
        }
    }
 
    .unit-label {
        height: 20px;
        font-size: 12px;
        position: relative;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
 
    .unit-text {
        text-align: right;
        display: flex;
        align-items: flex-end;
        font-size: 12px;
        line-height: 20px;
        margin-left: 10px;
    }
}
</style>