123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <template>
- <div>
- <div
- class="chart"
- v-for="index of list.length"
- :key="index"
- :id="id + index"
- ></div>
- </div>
- </template>
- <script>
- import util from "@/helper/util.js";
- import partten from "@/helper/partten.js";
- import * as echarts from "echarts";
- export default {
- name: "percent-pie",
- componentName: "percent-pie",
- props: {
- width: {
- type: String,
- default: "100%",
- },
- height: {
- type: String,
- default: "18.519vh",
- },
- // 传入数据
- list: {
- type: Array,
- default: () => [
- {
- name: "当日预测电量",
- value: 103.62,
- total: 200,
- },
- {
- name: "实际发电量",
- value: 98.62,
- total: 100,
- },
- {
- name: "当月预测电量",
- value: 113.27,
- total: 100,
- },
- {
- name: "实际发电量",
- value: 136.72,
- total: 100,
- },
- ],
- },
- total: {
- type: Number,
- default: 150,
- },
- colors: {
- type: Array,
- default: () => ["green", "purple"],
- },
- },
- data() {
- return {
- id: "",
- chart: null,
- firstAnimation: true,
- };
- },
- created() {
- console.log("this.list", this.list);
- },
- computed: {
- datas() {
- return this.list.map((t) => {
- return t.value;
- });
- },
- },
- methods: {
- resize() {},
- initChart(value, index) {
- var currColor = this.colors[index % 2];
- var $dom = document.getElementById(this.id + (index + 1));
- $dom.style.width = this.width;
- $dom.style.height = `calc(${this.height} / ${this.list.length} - 4px)`;
- let chart = echarts.init($dom);
- let option = {
- xAxis: {
- max: value.total,
- splitLine: {
- show: false,
- },
- axisLine: {
- show: false,
- },
- axisLabel: {
- show: false,
- },
- axisTick: {
- show: false,
- },
- },
- grid: {
- left: 16,
- top: 16, // 设置条形图的边s距
- right: 110,
- bottom: 0,
- containLabel: true,
- },
- yAxis: [
- {
- type: "category",
- inverse: true,
- data: [value],
- axisLine: {
- show: false,
- },
- axisTick: {
- show: false,
- },
- axisLabel: {
- show: false,
- },
- },
- ],
- series: [
- // 内
- {
- type: "bar",
- barWidth: 6,
- animation: this.firstAnimation,
- // legendHoverLink: false,
- // silent: true,
- itemStyle: {
- normal: {
- color: function (params) {
- return {
- type: "linear",
- x: 0,
- y: 0,
- x2: 1,
- y2: 0,
- colorStops: [
- {
- offset: 0,
- color: partten.getColor(currColor), // 0% 处的颜色
- },
- {
- offset: 1,
- color: partten.getColor(currColor), // 100% 处的颜色
- },
- ],
- };
- },
- shadowBlur: 10,
- shadowColor: "rgba(255, 255, 255, 0.30)",
- },
- },
- label: {
- normal: {
- show: true,
- position: [0, util.vh("-20")],
- formatter: function (param) {
- return param.data.name;
- },
- textStyle: {
- color:
- this.$store.state.themeName === "dark" ? "#7a8385" : "#000",
- fontSize: 12,
- },
- },
- },
- data: [value],
- z: 1,
- animationEasing: "elasticOut",
- },
- // 三角
- {
- type: "pictorialBar",
- symbolPosition: "end",
- animation: this.firstAnimation,
- data: [value],
- symbol: "triangle",
- symbolOffset: [0, -10],
- symbolSize: [5, 5],
- symbolRotate: 180,
- itemStyle: {
- normal: {
- borderWidth: 0,
- color: function (params) {
- return partten.getColor(currColor);
- },
- // shadowBlur: 2,
- // shadowColor: "rgba(255, 255, 255, 0.80)",
- },
- },
- },
- // 分隔
- {
- type: "pictorialBar",
- itemStyle: {
- normal: {
- color:
- this.$store.state.themeName === "dark" ? "#20314f" : "#000",
- },
- },
- animation: this.firstAnimation,
- symbolRepeat: "fixed",
- symbolMargin: 4,
- symbol: "rect",
- symbolClip: true,
- symbolSize: [1, 8],
- symbolPosition: "start",
- symbolOffset: [8, -1],
- symbolBoundingData: value.total,
- symbolRotate: -15,
- data: [value],
- z: 2,
- animationEasing: "elasticOut",
- },
- // 外边框
- {
- type: "pictorialBar",
- animation: this.firstAnimation,
- symbol: "rect",
- symbolBoundingData: value.total,
- itemStyle: {
- normal: {
- color: "none",
- },
- },
- label: {
- normal: {
- formatter: (params) => {
- return "{gm|}{f| " + params.data + "}";
- },
- rich: {
- f: {
- color:
- this.$store.state.themeName === "dark" ? "#fff" : "#000",
- fontSize: 14,
- lineHeight: 20,
- fontFamily: "Bicubik",
- },
- gm: {
- backgroundColor: partten.getColor(currColor),
- width: 4,
- height: 4,
- lineHeight: 20,
- verticalAlign: "middle",
- borderRadius: [50, 50, 50, 50],
- },
- },
- position: "right",
- distance: 8, // 向右偏移位置
- show: true,
- },
- },
- data: [value.value],
- },
- // 外框
- {
- type: "bar",
- animation: this.firstAnimation,
- name: "外框",
- barGap: "-120%", // 设置外框粗细
- data: [
- {
- value: value.total,
- itemStyle: {
- normal: {
- color: "transparent",
- borderColor: partten.getColor(currColor), // [, "#333"],
- borderWidth: 1, // 边框宽度
- // barBorderRadius: 0, //圆角半径
- opacity: 0.5,
- label: {
- // 标签显示位置
- show: false,
- position: "top", // insideTop 或者横向的 insideLeft
- },
- },
- },
- },
- ],
- barWidth: 9,
- },
- ],
- };
- chart.clear();
- chart.setOption(option);
- this.resize = function () {
- chart.resize();
- };
- window.removeEventListener("resize", this.resize);
- window.addEventListener("resize", this.resize);
- },
- },
- created() {
- this.id = "pie-chart-" + util.newGUID();
- },
- mounted() {
- this.$nextTick(() => {
- this.$el.style.width = this.width;
- this.$el.style.height = this.height;
- this.list.forEach((value, index) => {
- this.initChart(value, index);
- });
- this.firstAnimation = false;
- });
- },
- updated() {
- this.$nextTick(() => {
- this.list.forEach((value, index) => {
- this.initChart(value, index);
- });
- });
- },
- unmounted() {
- window.removeEventListener("resize", this.resize);
- },
- watch: {
- "$store.state.themeName"() {
- this.list.forEach((value, index) => {
- this.initChart(value, index);
- });
- },
- },
- };
- </script>
- <style lang="less">
- .chart {
- width: 100%;
- height: 100%;
- display: inline-block;
- cursor: default;
- }
- </style>
|