123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- <template>
- <div class="chart" :id="id"></div>
- </template>
- <script>
- import util from "@/helper/util.js";
- import partten from "@/helper/partten.js";
- import * as echarts from "echarts";
- import dayjs from "dayjs";
- export default {
- name: "windChartCom",
- componentName: "windChartCom",
- props: {
- width: {
- type: String,
- default: "100%",
- },
- height: {
- type: String,
- default: "450px",
- },
- interval: {
- type: Number,
- default: 7,
- },
- // 单位
- units: {
- type: Array,
- default: () => ["", ""],
- },
- // 单位数据
- unitDatas: {
- type: Array,
- default: () => [
- { flag: false, min: 0, max: 0 },
- { flag: false, min: 0, max: 0 },
- ],
- },
- // 传入数据
- list: {
- type: Array,
- default: () => [
- {
- title: "绿线",
- smooth: true,
- value: [
- {
- text: "",
- value: 0,
- },
- {
- text: "0:00",
- value: 20,
- },
- {
- text: "10:00",
- value: 1,
- },
- {
- text: "11:00",
- value: 40,
- },
- {
- text: "12:00",
- value: 10,
- },
- {
- text: "13:00",
- value: 15,
- },
- {
- text: "14:00",
- value: 30,
- },
- {
- text: "15:00",
- value: 40,
- },
- {
- text: "",
- value: 10,
- },
- ],
- },
- {
- title: "黄线",
- smooth: true,
- value: [
- {
- text: "",
- value: 0,
- },
- {
- text: "0:00",
- value: 40,
- },
- {
- text: "10:00",
- value: 20,
- },
- {
- text: "11:00",
- value: 20,
- },
- {
- text: "12:00",
- value: 10,
- },
- {
- text: "13:00",
- value: 40,
- },
- {
- text: "14:00",
- value: 50,
- },
- {
- text: "15:00",
- value: 40,
- },
- {
- text: "",
- value: 10,
- },
- ],
- },
- ],
- },
- showLegend: {
- type: Boolean,
- default: false,
- },
- animate: {
- type: Boolean,
- default: true,
- },
- showXdata: {
- type: Boolean,
- default: false,
- },
- ratio: {
- type: Number,
- default: 1,
- },
- },
- data() {
- return {
- id: "",
- chart: null,
- color: [
- "#1985de",
- "#ff8300",
- "#06a746",
- "#3eede7",
- "#eb0f7c",
- "#dfdc0a",
- "#de0feb",
- "#cca4e3",
- ],
- newlist: null,
- emptyData: [],
- leftOffset: 20,
- seletedData: {},
- };
- },
- watch: {
- list: {
- handler(newValue, oldValue) {
- this.newlist = newValue;
- this.$nextTick(() => {
- this.newlist = this.list;
- this.initChart();
- });
- },
- deep: true,
- },
- "$store.state.themeName"() {
- this.initChart();
- },
- },
- computed: {
- getTimeStanp() {
- // 当日0点时间
- var timeStamp = [];
- let stamp = new Date(new Date().setHours(0, 0, 0, 0)).getTime();
- for (let i = 0; i < 96; i++) {
- timeStamp.push(dayjs(stamp).format("HH:mm"));
- this.emptyData.push("0");
- stamp = parseInt(stamp) + 15 * 60 * 1000;
- }
- timeStamp.push("24:00");
- return timeStamp;
- },
- colorValue() {
- return partten.getColor(this.color);
- },
- datas() {
- return this.newlist.map((t) => {
- return t.title;
- });
- },
- legend() {
- if (this.newlist) {
- return this.newlist.map((t) => {
- return t.title;
- });
- } else {
- return [];
- }
- },
- xdata() {
- var data;
- this.newlist.forEach((value, index) => {
- if (value.value != null && value.value.length > 0) {
- data = value;
- }
- });
- return data?.value.map((t, index) => {
- // if (index === this.newlist[0]?.value.length - 1) {
- // return "24:00";
- // } else {
- return dayjs(t.dateTime).format("MM-DD HH:mm");
- // }
- });
- },
- series() {
- let result = [];
- this.list.forEach((value, index) => {
- result.push({
- name: value.name || value.title,
- type: "line",
- smooth: value.smooth,
- symbol: "circle",
- showSymbol: true,
- zlevel: index,
- lineStyle: {
- normal: {
- color: this.color[index],
- width: 1,
- },
- },
- yAxisIndex: value.yAxisIndex,
- data: value.value.map((t) => {
- if (value.noRatio) {
- return t.value ? t.value.toFixed(2) : 0.0;
- } else {
- return t.value === "" && t.value !== 0
- ? ""
- : (t.value / this.ratio).toFixed(2);
- }
- }),
- });
- });
- return result;
- },
- yAxis() {
- let result = [];
- let i = 0;
- this.units.forEach((value, index) => {
- var option = {
- type: "value",
- // name: this.units[index],
- nameTextStyle: {
- color: this.color[index],
- fontSize: 12,
- },
- splitNumber: 11,
- // position:'left',
- axisLabel: {
- formatter: "{value}",
- textStyle: {
- color: this.color[index],
- fontSize: 12,
- },
- },
- axisLine: {
- lineStyle: {
- color: this.color[this.unitDatas[index].codex],
- width: 1,
- },
- },
- boundaryGap: false,
- //分格线
- splitLine: {
- show: false,
- lineStyle: {
- color: "#2D3338",
- width: 1, //轴线的宽度
- type: "dashed",
- },
- },
- };
- if (this.unitDatas[index].flag) {
- i++;
- option.min = this.unitDatas[index].min;
- option.max = this.unitDatas[index].max;
- if (i % 2 != 0) {
- option.position = "left";
- option.offset = (i - 1) * 35;
- if (i >= 3) {
- option.offset = (i - 2) * 35;
- }
- if (i >= 5) {
- option.offset = (i - 3) * 35;
- }
- if (i >= 7) {
- option.offset = (i - 4) * 35;
- }
- } else {
- if (i >= 4) {
- option.offset = (i - 3) * 35;
- }
- if (i >= 6) {
- option.offset = (i - 4) * 35;
- }
- if (i >= 8) {
- option.offset = (i - 5) * 35;
- }
- }
- result.push(option);
- } else if (index == 0) {
- if (!this.unitDatas[0].flag) {
- option.axisLine.lineStyle.color = "#525252";
- option.axisLabel.textStyle.color = "#828484";
- }
- if (
- this.unitDatas[index].min == 0 &&
- this.unitDatas[index].max == 0
- ) {
- result.push(option);
- } else {
- option.min = this.unitDatas[index].min;
- option.max = this.unitDatas[index].max;
- result.push(option);
- }
- }
- this.leftOffset = 35;
- if (i == 0) {
- this.leftOffset = 35;
- }
- });
- return result;
- },
- },
- methods: {
- childMethod(index) {
- this.chart.dispatchAction({
- type: "highlight",
- seriesIndex: 0,
- dataIndex: 0,
- });
- return this.chart.getModel().getComponent("yAxis", index).axis.scale
- ._extent[1];
- },
- changDateVisible(option) {
- this.seletedData = option;
- this.initChart();
- },
- resize() {},
- initChart() {
- const chart = echarts.init(this.$el, null, {
- renderer: "canvas",
- useDirtyRect: false,
- });
- let option = {
- animation: this.animate,
- color: this.color,
- tooltip: {
- trigger: "axis",
- backgroundColor: "rgba(0,0,0,0.4)",
- borderColor: partten.getColor("gray"),
- textStyle: {
- color: "#fff",
- fontSize: 14,
- },
- },
- legend:
- {
- show: this.showLegend,
- data: this.legend,
- padding: [10, 10],
- left: "center",
- bottom: -10,
- icon: "ract",
- itemWidth: 6,
- itemHeight: 6,
- inactiveColor: partten.getColor("gray"),
- textStyle: {
- color: partten.getColor("grayl"),
- fontSize: 12,
- },
- selected: this.seletedData,
- } || {},
- grid: {
- top: 16,
- left: this.leftOffset,
- right: 40,
- bottom: 30,
- containLabel: true,
- },
- xAxis:
- [
- {
- type: "category",
- boundaryGap: false,
- axisLabel: {
- interval: this.interval,
- formatter: "{value}",
- textStyle: {
- color: "#828484",
- fontSize: 12,
- },
- },
- axisTick: {
- alignWithLabel: true,
- },
- data: this.showXdata ? this.getTimeStanp : this.xdata,
- },
- ] || [],
- yAxis: this.yAxis || [],
- dataZoom: [
- {
- type: "slider",
- bottom: 0,
- height: 20,
- handleSize: "90%",
- start: 0,
- end: 100,
- handleColor: "#555B66",
- borderColor: "#545454",
- backgroundColor: "rgba(0, 70, 199, 0.2)",
- fillerColor: "#343A46",
- textStyle: { color: "white" },
- labelFormatter: (value) => {
- if (value) {
- return this.xdata[value];
- }
- },
- },
- ],
- series: this.series || {},
- };
- chart.clear();
- if (Object.values(this.series).length) {
- chart.setOption(option);
- }
- this.resize = function () {
- chart.resize();
- };
- window.addEventListener("resize", this.resize);
- this.chart = chart;
- },
- },
- created() {
- this.$nextTick(() => {
- this.id = "pie-chart-" + util.newGUID();
- });
- this.newlist = this.list;
- },
- mounted() {
- this.$nextTick(() => {
- this.$el.style.width = this.width;
- this.$el.style.height = this.height;
- this.initChart();
- });
- },
- updated() {
- this.$nextTick(() => {
- this.initChart();
- });
- },
- unmounted() {
- window.removeEventListener("resize", this.resize);
- },
- };
- </script>
- <style lang="less">
- .chart {
- width: 100%;
- height: 100%;
- display: inline-block;
- }
- </style>
|