123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <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: "radar-chart",
- componentName: "radar-chart",
- props: {
- // 宽度 默认9.722vh
- width: {
- type: String,
- default: "100%",
- },
- // 高度 默认9.722vh
- height: {
- type: String,
- default: "7.4074vh",
- },
- // 标题
- title: {
- type: Array,
- default: () => ["-", "-"],
- },
- // 值
- value: {
- type: Array,
- default: () => {
- return [
- {
- indicator: ["N0", "N1", "N2", "N3", "N4", "N5"],
- data: [
- {
- value: [44200, 14200, 20000, 35000, 50000, 38000],
- name: "风机一号",
- },
- ],
- },
- ];
- },
- },
- showLegend: {
- type: Boolean,
- default: true,
- },
- },
- data() {
- return {
- id: "",
- chart: null,
- lineStyles: [],
- green:{
- areaStyle: {
- color: "rgba(165,228,175, 0.9)",
- },
- lineStyle: {
- color: "rgba(255,255,255, 0.85)",
- },
- itemStyle: {
- color: "rgba(165,228,175, 0.5)",
- borderColor: "rgba(255,255,255, 0.5)",
- borderWidth: 0.5,
- },
- },
- blue:{
- areaStyle: {
- color: 'rgba(75,85,174, 0.9)',
- },
- lineStyle: {
- color: "rgba(255,255,255, 0.85)",
- },
- itemStyle: {
- color:'rgba(75,85,174, 0.9)',
- borderColor: "rgba(255,255,255, 0.5)",
- borderWidth: 0.5,
- },
- }
- };
- },
- methods: {
- renderValue() {
- let result = [];
- this.value.forEach((value, index) => {
- result.push({
- name: this.title[index],
- type: "radar",
- data: value.data,
- });
- });
- return result;
- },
- initChart() {
- let themeName = '';
- let theme = this.$store.state.themeName;
- if(theme == 'dark' || theme == 'light'){
- themeName = theme;
- }else{
- themeName = theme.split(' ')[1];
- }
- let chart = echarts.init(this.$el);
- let maxValue = -1;
-
- if(themeName === "dark"){
- this.lineStyles = [this.green,this.blue];
- }else{
- this.lineStyles = [this.blue,this.green];
- }
-
- if (this.value.length > 0)
- this.value[0].data.forEach((item, index) => {
- item.value.forEach((value) => {
- if (value > maxValue) {
- maxValue = value;
- }
- });
- item.areaStyle =
- this.lineStyles[index % this.lineStyles.length].areaStyle;
- item.lineStyle =
- this.lineStyles[index % this.lineStyles.length].lineStyle;
- item.itemStyle =
- this.lineStyles[index % this.lineStyles.length].itemStyle;
- });
- maxValue *= 1.5;
- let indicator = [];
- if (this.value.length > 0)
- this.value[0].indicator.forEach((item) => {
- indicator.push({ name: item, max: maxValue });
- });
- let option = {
- grid: {
- left: 150,
- right: 150,
- bottom: 150,
- top: 150,
- },
- tooltip: {
- trigger: "item",
- backgroundColor:
- themeName === "dark"
- ? "rgba(0,0,0,0.4)"
- : "rgba(255,255,255,0.5)",
- borderColor:
- themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- textStyle: {
- color: themeName === "dark" ? "#fff" : "#000",
- fontSize: util.vh(16),
- },
- },
- legend: {
- show: this.showLegend,
- bottom: 16,
- inactiveColor:
- themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- textStyle: {
- fontSize: 12,
- color:
- themeName === "dark"
- ? partten.getColor("grayl")
- : "#000",
- },
- },
- radar: [
- // 最低层 80
- {
- radius: "70%",
- center: ["50%", "50%"],
- splitNumber: 1,
- nameGap: "16",
- name: {
- textStyle: {
- color: themeName === "dark"
- ? partten.getColor("gray") + 99
- : "#000",
- fontSize: 12,
- },
- },
- axisLine: {
- lineStyle: {
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- },
- },
- splitLine: {
- lineStyle: {
- width: 1,
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- },
- },
- splitArea: {
- areaStyle: {
- color: "transparent",
- },
- },
- indicator: indicator,
- },
- // 次外层 70 - 80
- {
- radius: ["60%", "70%"],
- center: ["50%", "50%"],
- startAngle: 90,
- splitNumber: 2,
- name: {
- show: false,
- },
- axisLine: {
- lineStyle: {
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- shadowBlur: 1,
- shadowColor: "#fff",
- shadowOffsetX: 0.5,
- shadowOffsetY: 1,
- },
- },
- splitLine: {
- lineStyle: {
- width: 1,
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- shadowColor: "#fff",
- shadowBlur: 0,
- shadowOffsetX: 0.5,
- shadowOffsetY: 0.5,
- },
- },
- splitArea: {
- areaStyle: {
- color: "transparent",
- },
- },
- indicator: indicator,
- },
- // 渐变层 40 - 70
- {
- radius: ["35%", "60%"],
- center: ["50%", "50%"],
- splitNumber: 1,
- name: {
- show: false,
- },
- axisLine: {
- lineStyle: {
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- },
- },
- splitLine: {
- lineStyle: {
- width: 1,
- color: themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- },
- },
- splitArea: {
- areaStyle: {
- shadowBlur: 4,
- color: {
- type: "radial",
- x: 0.5,
- y: 0.5,
- r: 0.5,
- colorStops: [
- {
- offset: 0.5,
- color: "transparent", // 0% 处的颜色
- },
- {
- offset: 1,
- color: themeName === "dark"
- ? partten.getColor("green") + 60
- : partten.getColor("deepblue") + 60, // 100% 处的颜色
- },
- ],
- global: false, // 缺省为 false
- },
- },
- },
- indicator: indicator,
- },
- // 渐变层 0 - 40
- {
- radius: ["0%", "35%"],
- center: ["50%", "50%"],
- splitNumber: 1,
- name: {
- show: false,
- },
- axisLine: {
- lineStyle: {
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- },
- },
- splitLine: {
- lineStyle: {
- width: 1,
- color: themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- },
- },
- splitArea: {
- areaStyle: {
- shadowBlur: 4,
- color: {
- type: "radial",
- x: 0.5,
- y: 0.5,
- r: 0.5,
- colorStops: [
- {
- offset: 0.5,
- color: "transparent", // 0% 处的颜色
- },
- {
- offset: 1,
- color: themeName === "dark"
- ? partten.getColor("green") + 60
- : partten.getColor("deepblue") + 60, // 100% 处的颜色
- },
- ],
- global: false, // 缺省为 false
- },
- },
- },
- indicator: indicator,
- },
- // 内层 0 - 50
- {
- radius: "35%",
- center: ["50%", "50%"],
- splitNumber: 1,
- name: {
- show: false,
- },
- axisLine: {
- lineStyle: {
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- },
- },
- splitLine: {
- lineStyle: {
- width: 1,
- color: themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- },
- },
- splitArea: {
- areaStyle: {
- shadowBlur: 4,
- color: "transparent",
- },
- },
- indicator: indicator,
- },
- // 内层 0 - 45
- {
- radius: "35%",
- center: ["50%", "50%"],
- splitNumber: 1,
- name: {
- show: false,
- },
- axisLine: {
- lineStyle: {
- color: themeName === "dark"
- ? partten.getColor("gray") + 40
- : "#000" + 40,
- },
- },
- splitLine: {
- lineStyle: {
- width: 1,
- color: themeName === "dark"
- ? partten.getColor("gray")
- : "#000",
- },
- },
- splitArea: {
- areaStyle: {
- shadowBlur: 4,
- color: "transparent",
- },
- },
- indicator: indicator,
- },
- ],
- series: this.renderValue(),
- };
- chart.setOption(option);
- },
- },
- created() {
- this.id = "pie-chart-" + util.newGUID();
- },
- mounted() {
- this.$nextTick(() => {
- this.$el.style.width = this.width;
- this.$el.style.height = this.height;
- this.initChart();
- });
- },
- updated() {
- this.$nextTick(() => {
- this.initChart();
- });
- },
- watch: {
- "$store.state.themeName"() {
- this.initChart();
- },
- },
- };
- </script>
- <style lang="less" scoped>
- .chart {
- width: 100%;
- height: 100%;
- display: block;
- margin: auto;
- }
- </style>
|