123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <template>
- <div id="lineChart" ref="line"></div>
- </template>
- <script>
- export default {
- name: "LineChart",
- data() {
- return {
- lineChart: null
- };
- },
- props: {
- categoryData: {
- type: Array,
- default() {
- return [];
- }
- },
- valueData: {
- type: Array,
- default() {
- return [];
- }
- },
- unit: {
- type: String,
- default() {
- return "";
- }
- },
- title: {
- type: String,
- default() {
- return "";
- }
- }
- },
- watch: {
- valueData: {
- handler(val) {
- this.$nextTick(() => {
- if (this.valueData) {
- this.initEchart(this.categoryData, this.valueData);
- }
- });
- },
- deep: true,
- immediate: true
- }
- },
- mounted() {
- this.$nextTick(() => {
- if (this.valueData) {
- this.initEchart(this.categoryData, this.valueData);
- }
- });
- },
- methods: {
- initEchart(xData, seriesData) {
- if (!this.lineChart) {
- this.lineChart = this.$Echarts.init(this.$refs.line);
- }
- let unit = this.unit;
- let title = this.title;
- let dataMin = Math.min(...seriesData);
- let dataMax = Math.max(...seriesData);
- let homeSpecialTown = this.$store.state.homeSpecialTown;
- let option = {
- tooltip: {
- show: true,
- trigger: "axis",
- formatter: function (params) {
- return (
- params[0].marker +
- params[0].name +
- "年" +
- "(" +
- homeSpecialTown +
- ")" +
- "<br />" +
- title +
- ":" +
- (unit == "条" || unit == "个" ? parseFloat(params[0].data).toFixed(0) : parseFloat(params[0].data).toFixed(2)) +
- unit
- );
- }
- },
- grid: {
- left: "3%",
- right: "4%",
- bottom: "3%",
- top: "20px",
- containLabel: true
- },
- xAxis: {
- type: "category",
- data: xData,
- axisTick: {
- show: false // 不显示坐标轴刻度线
- },
- axisLine: {
- show: true,
- lineStyle: {
- color: "rgba(219,225,255,1)",
- width: 1,
- type: "solid"
- }
- },
- axisLabel: {
- //x轴文字的配置
- show: true,
- color: "rgba(219,225,255,1)"
- }
- },
- yAxis: {
- min:
- dataMax === dataMin
- ? dataMax
- : parseFloat(dataMin - (dataMax - dataMin) / 2).toFixed(2) < 0
- ? 0
- : unit == "条" || unit == "个"
- ? dataMin
- : parseFloat(dataMin - (dataMax - dataMin) / 2).toFixed(2),
- max: unit == "条" || unit == "个" ? parseFloat(dataMax).toFixed(0) : parseFloat(dataMax).toFixed(2),
- interval:
- dataMax === dataMin
- ? parseFloat(dataMax).toFixed(0)
- : parseFloat(dataMax - dataMin).toFixed(2) < 0
- ? 0
- : unit == "条" || unit == "个"
- ? parseFloat(dataMax - dataMin).toFixed(0)
- : parseFloat(dataMax - dataMin).toFixed(2),
- type: "value",
- scale: true,
- splitLine: {
- show: false // 不显示网格线
- },
- axisLine: {
- show: false // 不显示坐标轴线
- },
- axisLabel: {
- //y轴文字的配置
- color: "rgba(219,225,255,1)",
- margin: 15
- }
- },
- series: [
- {
- data: seriesData,
- type: "line",
- smooth: true,
- itemStyle: {
- color: "#00aaff"
- },
- areaStyle: {
- color: new this.$Echarts.graphic.LinearGradient(0, 0, 0, 1, [
- {
- offset: 0,
- color: "#00aaff"
- },
- {
- offset: 1,
- color: "#00aaff00"
- }
- ])
- }
- }
- ]
- };
- this.lineChart.setOption(option);
- }
- }
- };
- </script>
- <style>
- #lineChart {
- width: 100%;
- height: 100%;
- }
- </style>
|