123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <template>
- <div class="chart" id="stand-alone-chart"></div>
- </template>
- <script>
- import util from "@/helper/util.js";
- import partten from "@/helper/partten.js";
- import * as echarts from "echarts";
- export default {
- name: "multiple-bar-chart",
- componentName: "multiple-bar-chart",
- props: {
- width: {
- type: String,
- default: "100%",
- },
- height: {
- type: String,
- default: "13.889vh",
- },
- // 传入数据
- barData: {
- type: Array,
- default: () => [],
- },
- lineData: {
- type: Object,
- default: () => {},
- },
- // 单位
- units: {
- type: Array,
- default: () => ["(万KWh)", "(风速)"],
- },
- // 显示 legend
- showLegend: {
- type: Boolean,
- default: true,
- },
- // 颜色
- color: {
- type: Array,
- default: () => ["#05bb4c", "#e17e23", "#ba3237", "#c531c7", "#1a93cf"],
- },
- showAnimation: {
- type: Boolean,
- default: true,
- },
- },
- data() {
- return {
- id: "",
- chart: null,
- firstAnimation: true,
- newbarData: [],
- newlineData: [],
- };
- },
- watch: {
- barData: {
- handler(newValue, oldValue) {
- this.newbarData = newValue;
- this.initChart();
- },
- deep: true,
- },
- lineData: {
- handler(newValue, oldValue) {
- this.newlineData = newValue;
- this.initChart();
- },
- deep: true,
- },
- "$store.state.themeName"() {
- this.initChart();
- },
- },
- computed: {
- legend() {
- return [
- ...this.newbarData.map((t) => {
- return t.title;
- }),
- this.newlineData.name,
- ];
- },
- xdata() {
- let result = [];
- if (
- this.newbarData &&
- this.newbarData.length > 0 &&
- this.newbarData[0].value.length > 0
- ) {
- result = this.newbarData[0].value.map((t) => {
- return t.text;
- });
- }
- return result;
- },
- ydata() {
- let result = [];
- this.units.forEach((value, index) => {
- let data = null;
- if (index == 0) {
- data = {
- type: "value",
- name: value,
- axisLabel: {
- formatter: "{value} ",
- fontSize: 12,
- },
- //分格线
- splitLine: {
- lineStyle: {
- color: "#5a6162",
- type: "dashed",
- },
- },
- };
- } else {
- data = {
- type: "value",
- name: value,
- axisLabel: {
- formatter: "{value}",
- fontSize: 12,
- },
- //分格线
- splitLine: {
- show: false,
- },
- };
- }
- result.push(data);
- });
- return result;
- },
- series() {
- let result = [];
- if (this.newbarData && this.newbarData.length > 0) {
- this.newbarData.forEach((value, index) => {
- result.push({
- name: value.title,
- type: "bar",
- barWidth: "10%",
- animation: this.firstAnimation && this.showAnimation,
- yAxisIndex: value.yAxisIndex,
- data: value.value.map((t) => {
- return t.value;
- }),
- });
- });
- }
- if (
- this.newlineData &&
- this.newlineData.data &&
- this.newlineData.data.length > 0
- ) {
- result.push({
- name: this.newlineData.name,
- type: "line",
- data: this.newlineData.data,
- smooth: true, //平滑展示
- yAxisIndex: 1,
- lineStyle: {
- color: "#f8de5b",
- },
- itemStyle: {
- color: "#f8de5b",
- },
- });
- }
- return result;
- },
- },
- methods: {
- resize() {},
- initChart() {
- let chart = echarts.init(this.$el);
- let option = {
- color: this.color,
- tooltip: {
- trigger: "axis",
- backgroundColor:
- this.$store.state.themeName === "dark"
- ? "rgba(0,0,0,0.4)"
- : "rgba(255,255,255,0.5)",
- borderColor:
- this.$store.state.themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- textStyle: {
- color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
- fontSize: 12,
- },
- },
- legend: {
- show: this.showLegend,
- data: this.legend,
- right: 56,
- icon: "ract",
- itemWidth: 8,
- itemHeight: 8,
- inactiveColor:
- this.$store.state.themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- textStyle: {
- color:
- this.$store.state.themeName === "dark"
- ? partten.getColor("grayl")
- : "#000",
- fontSize: 12,
- },
- },
- grid: {
- top: 32,
- left: 40,
- right: this.ydata.length > 1 ? 40 : 14,
- bottom: 24,
- },
- xAxis: [
- {
- type: "category",
- data: this.xdata,
- axisPointer: {
- type: "shadow",
- },
- axisLabel: {
- fontSize: 12,
- },
- },
- ],
- yAxis: this.ydata,
- series: this.series,
- };
- chart.clear();
- chart.setOption(option);
- this.resize = function () {
- chart.resize();
- };
- window.addEventListener("resize", this.resize);
- },
- },
- created() {
- // this.id = "pie-chart-" + util.newGUID();
- this.newbarData = this.barData;
- this.newlineData = this.lineData;
- },
- mounted() {
- this.$nextTick(() => {
- this.$el.style.width = this.width;
- this.$el.style.height = this.height;
- this.initChart();
- this.firstAnimation = false;
- });
- },
- updated() {
- this.$nextTick(() => {
- this.initChart();
- });
- },
- unmounted() {
- window.removeEventListener("resize", this.resize);
- },
- };
- </script>
- <style lang="less">
- .chart {
- width: 100%;
- height: 100%;
- display: inline-block;
- }
- </style>
|