|
|
@@ -0,0 +1,126 @@
|
|
|
+<template>
|
|
|
+ <div
|
|
|
+ class="sortChart"
|
|
|
+ :style="{'width':width + 'px', 'height': height+ 'px'}"
|
|
|
+ >
|
|
|
+ <div class="container">
|
|
|
+ <div
|
|
|
+ v-for="(item,idx) in list"
|
|
|
+ :key="'k-'+idx"
|
|
|
+ class="bar"
|
|
|
+ :style="{'height': getHeight(item.value)+ 'px'
|
|
|
+ ,'background-color': getColor(item.color)
|
|
|
+ ,'top': getTop(item.value) + 'px'
|
|
|
+ ,'width': itemWidth + 'px'
|
|
|
+ }"
|
|
|
+ >
|
|
|
+ <div
|
|
|
+ class="value"
|
|
|
+ :style="{'left': itemWidth / 2 - 7 + 'px'}"
|
|
|
+ >{{Math.abs(item["value"])}}</div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+ /**
|
|
|
+ * @name sortChart (组件名称)
|
|
|
+ * @desc 柱状图,用于排序
|
|
|
+ * @author Li Xuesong
|
|
|
+ * @param list 传入要显示的内容
|
|
|
+ * @param height 柱状图高度
|
|
|
+ * @param itemContainerWidth 柱状图相邻两个柱子的宽度
|
|
|
+ * @param itemWidth 柱子的宽度
|
|
|
+ * @date
|
|
|
+ * @example 调用示例
|
|
|
+ * <sortChart></sortChart>
|
|
|
+ */
|
|
|
+
|
|
|
+ export default {
|
|
|
+ name: 'sortChart',
|
|
|
+ components: {},
|
|
|
+ props: {
|
|
|
+ list: {
|
|
|
+ default: () => [],
|
|
|
+ },
|
|
|
+ height: {
|
|
|
+ default: 150
|
|
|
+ },
|
|
|
+ itemContainerWidth: {
|
|
|
+ default: 60
|
|
|
+ },
|
|
|
+ itemWidth: {
|
|
|
+ default: 40
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+ data () {
|
|
|
+ return {
|
|
|
+ max: 20,
|
|
|
+ width: 200
|
|
|
+ }
|
|
|
+ },
|
|
|
+ created () {
|
|
|
+ },
|
|
|
+ watch: {
|
|
|
+ list: function (data) {
|
|
|
+ this.max = Math.max(...data.map(v => v["value"]));
|
|
|
+ this.width = data.length * this.itemContainerWidth;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted () { },
|
|
|
+ methods: {
|
|
|
+ getTop (value) {
|
|
|
+ return value > 0 ? 0 : this.height;
|
|
|
+ },
|
|
|
+ getHeight (value) {
|
|
|
+ return (value < 0 ? -value : value) / this.max * this.height;
|
|
|
+ },
|
|
|
+ getColor (color) {
|
|
|
+ switch (color) {
|
|
|
+ case 'green': return '#00cec9';
|
|
|
+ case 'orange': return '#e17055';
|
|
|
+ case 'red': return '#ff7675';
|
|
|
+ case 'purple': return '#a29bfe';
|
|
|
+ case 'yellow': return '#fdcb6e';
|
|
|
+ case 'white': return '#dfe6e9';
|
|
|
+ case 'blue': return '#0984e3'
|
|
|
+ }
|
|
|
+ return color;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ watch: {}
|
|
|
+ }
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped>
|
|
|
+ .sortChart {
|
|
|
+ /* width: 800px; */
|
|
|
+ height: 300px;
|
|
|
+ }
|
|
|
+ .container {
|
|
|
+ display: flex;
|
|
|
+ width: 100%;
|
|
|
+ height: 100%;
|
|
|
+ align-items: flex-end;
|
|
|
+ justify-content: space-around;
|
|
|
+ flex-wrap: nowrap;
|
|
|
+ flex-direction: row;
|
|
|
+ }
|
|
|
+ .bar {
|
|
|
+ display: flex;
|
|
|
+ width: 40px;
|
|
|
+ position: relative;
|
|
|
+ }
|
|
|
+ .value {
|
|
|
+ position: absolute;
|
|
|
+ bottom: 10px;
|
|
|
+ font-size: 14px;
|
|
|
+ font-weight: 600;
|
|
|
+ -moz-user-select: none; /* 火狐 */
|
|
|
+ -webkit-user-select: none; /* 谷歌 */
|
|
|
+ -ms-user-select: none; /* IE */
|
|
|
+ user-select: none;
|
|
|
+ }
|
|
|
+</style>
|