123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <template>
- <div id="barChart" ref="barChart"></div>
- </template>
- <script>
- import * as echarts from "echarts";
- export default {
- name: "barChart",
- data() {
- return {
- barChart: null,
- };
- },
- props: {
- categoryData: {
- type: Array,
- default() {
- return [];
- },
- },
- soilValueData: {
- type: Array,
- default() {
- return [];
- },
- },
- waterValueData: {
- type: Array,
- default() {
- return [];
- },
- },
- forestryValueData: {
- type: Array,
- default() {
- return [];
- },
- },
- sourceType: {
- type: String,
- default: "A",
- },
- unit: {
- type: String,
- default: "公顷",
- },
- },
- mounted() {
- this.initEchart(
- this.categoryData,
- this.soilValueData,
- this.waterValueData,
- this.forestryValueData
- );
- },
- watch: {
- sourceType(val) {
- // console.log(val, "sourceType");
- this.initEchart(
- this.categoryData,
- this.soilValueData,
- this.waterValueData,
- this.forestryValueData
- );
- },
- },
- methods: {
- initEchart(categoryData, soil, water, forestry) {
- if (!this.barChart) {
- this.barChart = echarts.init(this.$refs.barChart);
- }
- let option = {
- tooltip: {
- trigger: "axis",
- axisPointer: {
- // Use axis to trigger tooltip
- type: "shadow", // 'shadow' as default; can also be 'line' or 'shadow'
- },
- formatter: function (params) {
- let relVal = params[0].name;
- for (let i = 0; i < params.length; i++) {
- if (params[i].value === 0) {
- relVal += `<br/>${params[i].marker}${params[i].seriesName} ${params[i].value}`;
- } else {
- relVal += `<br/>${params[i].marker}${params[i].seriesName} ${params[i].value}公顷`;
- }
- }
- return relVal;
- },
- },
- legend: {
- bottom: "0%",
- right: "-1%",
- orient: "vertical",
- textStyle: {
- color: "#FFF",
- fontSize: "14",
- fontFamily: "PingFang SC",
- },
- },
- grid: {
- top: "5%",
- left: "0",
- right: "19%",
- bottom: "-5%",
- containLabel: true,
- },
- xAxis: {
- type: "value",
- show: false,
- },
- yAxis: {
- axisLine: {
- lineStyle: {
- color: "#FFF",
- },
- },
- // axisLine: "none",
- axisTick: "none",
- axisLabel: {
- // inside:true,
- },
- type: "category",
- data: categoryData,
- // data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
- },
- series: [
- {
- name: "土地",
- type: "bar",
- stack: "total",
- // label: {
- // show: true
- // },
- emphasis: {
- focus: "series",
- },
- data: soil,
- // itemStyle:{
- // color:"#f00"
- // }
- // data: [320, 302, 301, 334, 390, 330, 320],
- },
- {
- name: "水",
- type: "bar",
- stack: "total",
- // label: {
- // show: true
- // },
- emphasis: {
- focus: "series",
- },
- data: water,
- // itemStyle:{
- // color:"#f00"
- // }
- // data: [120, 132, 101, 134, 90, 230, 210],
- },
- {
- name: "林地",
- type: "bar",
- stack: "total",
- // label: {
- // show: true
- // },
- emphasis: {
- focus: "series",
- },
- data: forestry,
- // itemStyle:{
- // color:"#f00"
- // }
- // data: [220, 182, 191, 234, 290, 330, 182],
- },
- ],
- };
- this.barChart.setOption(option);
- },
- },
- };
- </script>
|