ChartBarPic.vue 11.6 KB
Newer Older
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
<!-- 柱状 - 方格 -->
<template>
    <div :id="id" class="chart" />
</template>

<script>
    import ChartBase from '@/components/Base/ChartsBase.js';
    import {
        ChartStyle
    } from '@/common/const.js';
    import {
        getWarnColor,
        sliceString
    } from '@/common/util.js';

    export default {
        mixins: [ChartBase],
        methods: {
          setOptions(chartData) {
              if (!this.chart || !chartData) return;
              let option = {
                  tooltip: {
                      // formatter: params => {
                      //     if (!params.value) return '';
                      //     let str = params.value.toString();
                      //     return parseFloat(str.substr(0, str.length - 2));
                      // }
                  },
                  xAxis: {
                      type: 'category',
                      data: chartData.xAxis || chartData.xaxis,
                      axisLine: {
                          lineStyle: {
                              color: ChartStyle.axis.color,
                          }
                      },
                      axisTick: {
                          show: false
                      },
                      axisLabel: {
                          fontSize: ChartStyle.axis.fontSize,
                          lineHeight: ChartStyle.axis.lineHeight,
                          formatter(value, index) {
                              return sliceString(value, 6);
                          }
                      }
                  },
                  yAxis: {
liubinyu's avatar
liubinyu committed
49
                      splitNumber: 3,
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
                      axisLine: {
                          show: false
                      },
                      axisTick: {
                          show: false
                      },
                      axisLabel: {
                          color: ChartStyle.axis.color,
                          fontSize: ChartStyle.axis.fontSize
                      },
                      splitLine: {
                          lineStyle: {
                              color: ChartStyle.split.color,
                              type: 'dashed',
                              opacity: ChartStyle.split.opacity,
                          }
                      }
                  },
                  grid: {
                      top: '18%',
                      left: '2%',
                      right: '2%',
                      bottom: '1%',
                      containLabel: true
                  },
                  series: [],
                  animationDurationUpdate: 500,
              };
              for (let i = 0, len = chartData.dataList.length; i < len; i++) {
                  const item = chartData.dataList[i];
                  option.series.push({
                      type: 'bar',
                      data: item.value,
                      showBackground: true,
                      backgroundStyle: {
                          color: 'rgba(0, 0, 0, 0.1)'
                      },
                      itemStyle: {
                          color: 'transparent'
                      },
                      barMaxWidth: 25,
                  });
                  option.series.push({
                      type: 'pictorialBar',
                      symbol: 'rect',
                      symbolRepeat: true,
                      symbolSize: ['90%', '18%'],
                      symbolMargin: 1.5,
                      symbolOffset: [0, -2],
                      symbolRepeatDirection: 'end',
                      name: item.name,
                      data: item.value,
                      label: {
                          show: true,
                          position: 'top',
                          color: ChartStyle.label.color,
                          fontSize: ChartStyle.label.fontSize,
                          // formatter: params => {
                          //     if (!params.value) return '';
                          //     let str = params.value.toString();
                          //     return parseFloat(str.substr(0, str.length - 2));
                          // }
                      },
                      itemStyle: {
                          normal: {
                              // 最后两位为信号状态
                              color: params => {
                                  // return getWarnColor(params.value?.toString().substr(-2));
                                  return getWarnColor(item.state[params.dataIndex]);
                              }
                          }
                      },
                      barMaxWidth: 25,
                  });
                  option.series.push({
                      type: 'line',
                      name: item.name,
                      data: item.value,
                  });
              }
              this.chart.setOption(option);
          },
            // setOptions(chartData) {
            //     if (!this.chart || !chartData) return;
            //     let option = {
            //         tooltip: {
            //             formatter: params => {
            //                 if (!params.value) return '';
            //                 let str = params.value.toString();
            //                 return parseFloat(str.substr(0, str.length - 2));
            //             }
            //         },
            //         title: [],
            //         xAxis: [],
            //         yAxis: [],
            //         series: [],
            //         grid: [],
            //         animationDurationUpdate: 500,
            //     };
            //     // 找到最大值,作为单轴时y轴的最大值
            //     let yMax;
            //     for (let i = 0; i < chartData.length; i++) {
            //         for (let j = 0; j < chartData[i].dataList.length; j++) {
            //             for (let k = 0; k < chartData[i].dataList[j].value.length; k++) {
            //                 if(!yMax || yMax < chartData[i].dataList[j].value[k]) yMax = chartData[i].dataList[j].value[k];
            //             }
            //         }
            //     }
            //     for (let i = 0, len = chartData.length; i < len; i++) {
            //         option.title.push({
            //             text: '{a| }   ' + chartData[i].title,
            //             textStyle: {
            //                 color: ChartStyle.title.color,
            //                 fontSize: ChartStyle.title.fontSize,
            //                 // width: '100%',
            //                 fontWeight: 200,
            //                 rich: {
            //                     a: {
            //                         width: 5,
            //                         height: 5,
            //                         backgroundColor: ChartStyle.title.color,
            //                         borderRadius: 50,
            //                         shadowColor: 'rgba(147,252,255,0.30)',
            //                         shadowBlur: 5,
            //                     }
            //                 }
            //             },
            //             left: 100 / len / 2 + (100 / len) * i - 1 + '%',
            //             top: '3',
            //             textAlign: 'center'
            //         });
            //         option.xAxis.push({
            //             data: chartData[i].xAxis,
            //             axisLine: {
            //                 lineStyle: {
            //                     color: ChartStyle.axis.color,
            //                 }
            //             },
            //             axisTick: {
            //                 show: false
            //             },
            //             axisLabel: {
            //                 color: ChartStyle.axis.color,
            //                 fontSize: ChartStyle.axis.fontSize
            //             },
            //             gridIndex: i
            //         });
            //         option.yAxis.push({
            //             max: yMax * 1.2, // 单轴
            //             axisLine: {
            //                 show: false
            //             },
            //             axisTick: {
            //                 show: false
            //             },
            //             axisLabel: {
            //                 show: false
            //             },
            //             splitLine: {
            //                 show: false
            //             },
            //             gridIndex: i
            //         });
            //         option.grid.push({
            //             top: '20%',
            //             left: (100 / len) * i + 5 + '%',
            //             right: (100 / len) * (len - i - 1) + 5 + '%',
            //             bottom: '5%',
            //             containLabel: true
            //         });
            //         for (let j = 0; j < chartData[i].dataList.length; j++) {
            //             option.series.push({
            //                 type: 'bar',
            //                 data: chartData[i].dataList[j].value,
            //                 showBackground: true,
            //                 backgroundStyle: {
            //                     color: 'rgba(0, 0, 0, 0.1)'
            //                 },
            //                 itemStyle: {
            //                     color: 'transparent'
            //                 },
            //                 barMaxWidth: 25,
            //                 xAxisIndex: i,
            //                 yAxisIndex: i
            //             });
            //             option.series.push({
            //                 type: 'pictorialBar',
            //                 symbol: 'rect',
            //                 symbolRepeat: true,
            //                 symbolSize: ['90%', '18%'],
            //                 symbolMargin: 1.5,
            //                 symbolOffset: [0, -2],
            //                 symbolRepeatDirection: 'end',
            //                 name: chartData[i].dataList[j].name,
            //                 data: chartData[i].dataList[j].value,
            //                 // label: {
            //                 //     show: true,
            //                 //     position: 'top',
            //                 //     color: ChartStyle.label.color,
            //                 //     fontSize: ChartStyle.label.fontSize
            //                 //     formatter: params => {
            //                 //         if (!params.value) return '';
            //                 //         let str = params.value.toString();
            //                 //         return str.substr(0, str.length - 2);
            //                 //     }
            //                 // },
            //                 itemStyle: {
            //                     normal: {
            //                         // 最后两位为信号状态
            //                         color: params => {
            //                             return getWarnColor(params.value?.toString().substr(-2));
            //                         }
            //                     }
            //                 },
            //                 barMaxWidth: 25,
            //                 xAxisIndex: i,
            //                 yAxisIndex: i
            //             });
            //         }
            //     }
            //     this.chart.setOption(option);
            // },
        }
    };
</script>

<style lang="scss" scoped>
    .chart {
        width: 100%;
        height: 100%;
    }
</style>