123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- <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";
- export default {
- name: "multiple-bar-chart",
- componentName: "multiple-bar-chart",
- props: {
- width: {
- type: String,
- default: "100%",
- },
- height: {
- type: String,
- default: "800px",
- },
- lineData: {
- type: Array,
- default: () => [
- {
- text: "日发电量",
- value: [
- {
- text: "1",
- value: 1,
- },
- {
- text: "2",
- value: 2,
- },
- {
- text: "3",
- value: 1,
- },
- {
- text: "4",
- value: 3,
- },
- {
- text: "5",
- value: 3,
- },
- {
- text: "6",
- value: 3,
- },
- {
- text: "7",
- value: 3,
- },
- {
- text: "8",
- value: 3,
- },
- {
- text: "9",
- value: 3,
- },
- {
- text: "10",
- value: 3,
- },
- {
- text: "11",
- value: 3,
- },
- {
- text: "12",
- value: 3,
- },
- {
- text: "13",
- value: 3,
- },
- {
- text: "14",
- value: 3,
- },
- {
- text: "15",
- value: 3,
- },
- {
- text: "16",
- value: 3,
- },
- ],
- },
- {
- text: "上网电量",
- value: [
- {
- text: "1",
- value: 1,
- },
- {
- text: "2",
- value: 2,
- },
- {
- text: "3",
- value: 1,
- },
- {
- text: "4",
- value: 3,
- },
- {
- text: "5",
- value: 4,
- },
- {
- text: "6",
- value: 5,
- },
- {
- text: "7",
- value: 6,
- },
- {
- text: "8",
- value: 7,
- },
- {
- text: "9",
- value: 8,
- },
- {
- text: "10",
- value: 7,
- },
- {
- text: "11",
- value: 9,
- },
- {
- text: "12",
- value: 2,
- },
- {
- text: "13",
- value: 3,
- },
- {
- text: "14",
- value: 5,
- },
- {
- text: "15",
- value: 12,
- },
- {
- text: "16",
- value: 11,
- },
- ],
- },
- ],
- },
- areaData: {
- type: Array,
- default: () => [
- {
- name: "1",
- start: 0,
- end: 100,
- state: "green",
- },
- {
- name: "1",
- start: 100,
- end: 200,
- state: "red",
- },
- {
- name: "1",
- start: 200,
- end: 300,
- state: "yellow",
- },
- {
- name: "2",
- start: 300,
- end: 800,
- state: "green",
- },
- {
- name: "3",
- start: 800,
- end: 9000,
- state: "green",
- },
- ],
- },
- // 单位
- units: {
- type: Array,
- default: () => ["健康趋势", "风机健康状态数量"],
- },
- // 显示 legend
- showLegend: {
- type: Boolean,
- default: true,
- },
- // 颜色
- color: {
- type: Array,
- default: () => ["#323E6F", "#1DA0D7", "#02BB4C", "#DB5520", "#EDB32F", "#EDEB2F"],
- },
- },
- data () {
- return {
- id: "",
- chart: null,
- };
- },
- computed: {
- legend () {
- let data = [];
- this.lineData.forEach((value, index) => {
- data.push(value.text);
- });
- return data;
- },
- xAxisData () {
- let data = [];
- if (this.lineData.length > 0)
- this.lineData[0].value.forEach((value, index) => {
- data.push(value.text);
- });
- return data;
- },
- areaChartData () {
- let data = [];
- for (var i = 0; i < this.areaData.length; i++) {
- let item = this.areaData[i];
- var color = item.color || item.state;
- data.push({
- name: item.name,
- value: [item.start, item.end, item.end - item.start],
- itemStyle: {
- normal: {
- color: color,
- },
- },
- exData: item,
- });
- }
- return data;
- },
- areaMax () {
- let max = 0;
- this.areaData.forEach((value) => {
- if (max < value.end) max = value.end;
- });
- return max;
- },
- },
- methods: {
- renderItem (params, api) {
- var start = api.coord([api.value(0)]);
- var end = api.coord([api.value(1)]);
- var height = api.size([0, 1])[1];
- var rectShape = echarts.graphic.clipRectByRect(
- {
- x: start[0],
- y: start[1] - height / 2,
- width: end[0] - start[0],
- height: height,
- },
- {
- x: params.coordSys.x,
- y: params.coordSys.y,
- width: params.coordSys.width,
- height: params.coordSys.height,
- }
- );
- return (
- rectShape && {
- type: "rect",
- transition: ["shape"],
- shape: rectShape,
- style: api.style(),
- }
- );
- },
- initChart () {
- console.log("units:",this.units)
- let that = this;
- let chart = echarts.init(this.$el);
- let option = {
- color: this.color,
- grid: {
- left: 40,
- right: 40,
- bottom: 40,
- top: 32,
- containLabel: true,
- },
- tooltip: {
- show: true,
- trigger: "axis",
- axisPointer: {
- type: "cross",
- },
- backgroundColor: "rgba(0,0,0,0.4)",
- borderColor: partten.getColor("gray"),
- textStyle: {
- color: "#fff",
- fontSize: 14,
- },
- },
- legend: {
- show: this.showLegend,
- data: this.legend,
- right: 120,
- icon: "ract",
- itemWidth: 8,
- itemHeight: 8,
- inactiveColor: partten.getColor("gray"),
- textStyle: {
- color: partten.getColor("grayl"),
- fontSize: 12,
- },
- },
- xAxis: [
- {
- type: "category",
- axisLabel: {
- color: partten.getColor("gray"),
- },
- axisLine: {
- show: false,
- },
- axisTick: {
- show: false,
- },
- data: this.xAxisData,
- },
- {
- show: false,
- min: 0,
- max: this.areaMax,
- axisLabel: {
- show: false,
- formatter: function (val) {
- return Math.max(0, val - 0) + " ms";
- },
- },
- },
- ],
- yAxis: [
- {
- type: "value",
- name: this.units[0],
- axisLabel: {
- formatter: "{value} ",
- color: partten.getColor("gray"),
- },
- axisLine: {
- type: "dashed",
- lineStyle: {
- color: partten.getColor("gray"),
- },
- width: 5,
- },
- axisTick: {
- show: false,
- },
- splitLine: {
- lineStyle: {
- type: "dashed",
- dashOffset: 10,
- color: partten.getColor("gray") + 80,
- },
- },
- },
- {
- data: [this.areaData[0].name],
- axisLabel: { show: false },
- },
- ],
- series: [],
- };
- // line data
- if (this.lineData.length > 0) {
- this.lineData.forEach((value, index) => {
- option.series.push({
- name: value.text,
- type: "line",
- data: value.value,
- smooth: true, //平滑展示
- yAxisIndex: 0,
- // lineStyle: {
- // color: partten.getColor("green"),
- // },
- // itemStyle: {
- // color: partten.getColor("green"),
- // },
- });
- });
- }
- // 区域
- if (this.areaData && this.areaData.length > 0) {
- option.series.push({
- type: "custom",
- renderItem: this.renderItem,
- yAxisIndex: 1,
- xAxisIndex: 1,
- itemStyle: {
- opacity: 0.2,
- },
- tooltip: {
- show: false,
- formatter: function (params) {
- return params.marker + params.name + ": " + params.value[2] + "s";
- },
- },
- encode: {
- x: [1, 2],
- y: 0,
- },
- data: this.areaChartData,
- });
- }
- chart.setOption(option);
- return chart;
- },
- },
- emits: {
- areaClick: null,
- },
- created () {
- this.id = "pie-chart-" + util.newGUID();
- },
- mounted () {
- this.$nextTick(() => {
- this.$el.style.width = this.width;
- this.$el.style.height = this.height;
- let that = this;
- let chart = this.initChart();
- chart.on("click", function(e, p) {
- if (e.seriesType == "custom") {
- that.$emit("areaClick", { data: e.data.exData });
- }
- });
- });
- },
- updated () {
- this.$nextTick(() => {
- this.initChart();
- });
- },
- };
- </script>
- <style lang="less">
- .chart {
- width: 100%;
- height: 100%;
- display: inline-block;
- }
- </style>
|