DashChartDemo.vue 4.3 KB
Newer Older
1 2
<template>
  <div :style="{ padding: '0 0 32px 32px' }">
JEECG低代码平台's avatar
JEECG低代码平台 已提交
3
    <v-chart :forceFit="true" :height="300" :data="chartData" :scale="scale">
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
      <v-coord type="polar" :startAngle="-202.5" :endAngle="22.5" :radius="0.75"></v-coord>
      <v-axis
        dataKey="value"
        :zIndex="2"
        :line="null"
        :label="axisLabel"
        :subTickCount="4"
        :subTickLine="axisSubTickLine"
        :tickLine="axisTickLine"
        :grid="null"
      ></v-axis>
      <v-axis dataKey="1" :show="false"></v-axis>
      <v-series
        gemo="point"
        position="value*1"
        shape="pointer"
        color="#1890FF"
        :active="false"
      ></v-series>
      <v-guide
        type="arc"
        :zIndex="0"
        :top="false"
        :start="arcGuide1Start"
        :end="arcGuide1End"
        :vStyle="arcGuide1Style"
      ></v-guide>
      <v-guide
        type="arc"
        :zIndex="1"
        :start="arcGuide2Start"
JEECG低代码平台's avatar
JEECG低代码平台 已提交
35
        :end="getArcGuide2End"
36 37 38 39 40 41 42 43 44 45 46 47
        :vStyle="arcGuide2Style"
      ></v-guide>
      <v-guide
        type="html"
        :position="htmlGuidePosition"
        :html="getHtmlGuideHtml()"
      ></v-guide>
    </v-chart>
  </div>
</template>

<script>
48
  import { registerShape } from 'viser-vue';
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

  registerShape('point', 'pointer', {
    draw(cfg, container) {
      let point = cfg.points[0];
      point = this.parsePoint(point);
      const center = this.parsePoint({
        x: 0,
        y: 0,
      });
      container.addShape('line', {
        attrs: {
          x1: center.x,
          y1: center.y,
          x2: point.x,
          y2: point.y + 15,
          stroke: cfg.color,
          lineWidth: 5,
          lineCap: 'round',
        }
      });
      return container.addShape('circle', {
        attrs: {
          x: center.x,
          y: center.y,
          r: 9.75,
          stroke: cfg.color,
          lineWidth: 4.5,
          fill: '#fff',
        }
      });
    }
  });

  const scale = [{
    dataKey: 'value',
    min: 0,
    max: 9,
    tickInterval: 1,
    nice: false,
  }];

90
  const data = [
JEECG低代码平台's avatar
JEECG低代码平台 已提交
91
    { value: 7.0 },
92 93 94
  ];

  export default {
95 96 97
    name:"DashChartDemo",
    props:{
      datasource:{
98
        type: Number,
JEECG低代码平台's avatar
JEECG低代码平台 已提交
99
        default:7
100 101 102 103 104 105
      },
      title: {
        type: String,
        default: ''
      }
    },
106 107 108 109 110 111
    created(){
      if(!this.datasource){
        this.chartData = data;
      }else{
        this.chartData = [
          { value: this.datasource },
112 113
        ];
      }
114
      this.getChartData()
115 116
    },
    watch: {
117 118 119
      'datasource': function (val) {
        this.chartData = [
          { value: val},
120
        ];
121
        this.getChartData();
122 123
      }
    },
124 125 126 127 128
    methods:{
      getChartData(){
        if(this.chartData && this.chartData.length>0){
          this.abcd = this.chartData[0].value * 10
        }else{
129 130 131
          this.abcd = 70
        }
      },
132
      getHtmlGuideHtml(){
133
        return '<div style="width: 300px;text-align: center;">\n' +
134 135
          '<p style="font-size: 14px;color: #545454;margin: 0;">'+this.title+'</p>\n' +
          '<p style="font-size: 36px;color: #545454;margin: 0;">'+this.abcd+'%</p>\n' +
136 137
          '</div>'
      },
138 139
      getArcGuide2End(){
        return [this.chartData[0].value, 0.945]
140 141 142 143
      }
    },
    data() {
      return {
144 145
        chartData:[],
        height: 400,
146
        scale: scale,
147
        abcd:70,
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
        axisLabel: {
          offset: -16,
          textStyle: {
            fontSize: 18,
            textAlign: 'center',
            textBaseline: 'middle'
          }
        },
        axisSubTickLine: {
          length: -8,
          stroke: '#fff',
          strokeOpacity: 1,
        },
        axisTickLine: {
          length: -17,
          stroke: '#fff',
          strokeOpacity: 1,
        },
        arcGuide1Start: [0, 0.945],
        arcGuide1End: [9, 0.945],
        arcGuide1Style: {
          stroke: '#CBCBCB',
          lineWidth: 18,
        },
        arcGuide2Start: [0, 0.945],
        arcGuide2Style: {
          stroke: '#1890FF',
          lineWidth: 18,
        },
        htmlGuidePosition: ['50%', '100%'],
        htmlGuideHtml: `
        <div style="width: 300px;text-align: center;">
          <p style="font-size: 14px;color: #545454;margin: 0;">${this.title}</p>
          <p style="font-size: 36px;color: #545454;margin: 0;">${this.abcd}%</p>
        </div>
      `,
      };
    },
  };
</script>