123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- <template>
- <div class="echart-render chart-box">
- <h3 class="chart-title">{{ chartTitle }}</h3>
- <v-chart
- :options="chartOption"
- v-if="chartOption"
- autoresize
- ref="vueChart"
- ></v-chart>
- <p class="chart-none" v-else>暂无数据</p>
- </div>
- </template>
- <script>
- import echarts from "echarts/lib/echarts";
- export default {
- name: "echart-render",
- props: {
- chartType: {
- type: String,
- required: true,
- validator(value) {
- return (
- ["bar", "barReverse", "pie", "line", "barGroup", "lineGroup"].indexOf(
- value
- ) !== -1
- );
- }
- },
- chartData: {
- type: [Object, Array],
- required: true
- },
- chartTitle: {
- type: String,
- default: ""
- },
- chartColor: {
- type: Array,
- default() {
- return [
- "#22C0FF",
- "#41CD7D",
- "#FF7786",
- "#4E7CFF",
- "#6d32f9",
- "#dd7755"
- ];
- }
- }
- },
- data() {
- return {
- chartOption: null
- };
- },
- mounted() {
- this.getOptions();
- },
- methods: {
- getOptions() {
- const name = this.chartType[0].toUpperCase() + this.chartType.slice(1);
- this.chartOption = this[`get${name}Option`](this.chartData);
- },
- getLineOption(datas) {
- let labels = [],
- vals = [];
- datas.map(item => {
- labels.push(item.name);
- vals.push(item.value);
- });
- const linearColor = new echarts.graphic.LinearGradient(0, 1, 0, 0, [
- {
- offset: 1,
- color: "rgba(34,192,255,1)"
- },
- {
- offset: 0,
- color: "rgba(34,192,255,0)"
- }
- ]);
- return {
- grid: {
- top: "10%",
- bottom: "12%",
- left: "5%",
- right: "5%"
- },
- tooltip: {
- show: true,
- formatter: function(params) {
- return params.value + "%";
- }
- },
- xAxis: {
- type: "category",
- data: labels,
- splitLine: {
- show: true,
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLine: {
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLabel: {
- color: "#7C86A3",
- fontSize: 12,
- fontWeight: "bold"
- },
- axisTick: {
- show: false
- }
- },
- yAxis: {
- type: "value",
- splitLine: {
- show: true,
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLine: {
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLabel: {
- fontSize: 12,
- color: "#7C86A3",
- formatter: function(value, index) {
- return value + "%";
- }
- },
- axisTick: {
- show: false
- }
- },
- series: [
- {
- name: "数量",
- type: "line",
- smooth: true,
- data: vals,
- itemStyle: {
- color: "rgba(34, 192, 255, 1)"
- },
- lineStyle: {
- color: "rgba(34, 192, 255, 1)"
- },
- areaStyle: {
- color: linearColor
- }
- }
- ]
- };
- },
- getPieOption(datas) {
- if (!datas.chartLabels.length) return;
- const seriesData = datas.chartLabels.map(function(item, index) {
- return {
- name: item,
- value: datas.chartData[0][index]
- };
- });
- return {
- grid: {
- top: "24%",
- bottom: "10%"
- },
- tooltip: {
- trigger: "item",
- formatter: "{a} <br/>{b} : {c} ({d}%)"
- },
- legend: {
- show: true,
- itemGap: 20,
- itemWidth: 20,
- textStyle: {
- fontSize: 16
- }
- },
- series: [
- {
- name: "数量",
- type: "pie",
- radius: "70%",
- data: seriesData,
- label: {
- show: false
- },
- itemStyle: {
- emphasis: {
- shadowBlur: 10,
- shadowOffsetX: 0,
- shadowColor: "rgba(0, 0, 0, 0.5)"
- }
- }
- }
- ]
- };
- },
- getBarOption(datas) {
- if (!datas.names.length) return;
- return {
- color: this.chartColor,
- grid: {
- top: "20%",
- bottom: "10%",
- left: "8%",
- right: "5%"
- },
- tooltip: {
- show: true
- },
- xAxis: {
- type: "category",
- data: datas.names,
- axisLine: {
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLabel: {
- color: "#7C86A3",
- fontSize: 12,
- fontWeight: "bold"
- },
- axisTick: {
- show: false
- }
- },
- yAxis: {
- type: "value",
- splitLine: {
- show: true,
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLine: {
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLabel: {
- color: "#7C86A3",
- fontSize: 12,
- fontWeight: "bold"
- },
- axisTick: {
- show: false
- }
- },
- series: [
- {
- name: this.chartTitle || "值",
- type: "bar",
- barWidth: 16,
- data: datas.dataList,
- itemStyle: {
- barBorderRadius: 4
- }
- }
- ]
- };
- },
- getBarReverseOption(datas) {
- if (!datas.names.length) return;
- return {
- color: this.chartColor,
- grid: {
- top: "20%",
- bottom: "10%",
- left: "12%",
- right: "5%"
- },
- tooltip: {
- show: true
- },
- yAxis: {
- type: "category",
- data: datas.names,
- axisLine: {
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- splitLine: {
- show: true,
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLabel: {
- color: "#7C86A3",
- fontSize: 12,
- fontWeight: "bold"
- },
- axisTick: {
- show: false
- }
- },
- xAxis: {
- type: "value",
- splitLine: {
- show: false,
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLine: {
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLabel: {
- color: "#7C86A3",
- fontSize: 12,
- fontWeight: "bold"
- },
- axisTick: {
- show: false
- }
- },
- series: [
- {
- name: this.chartTitle || "值",
- type: "bar",
- barWidth: 16,
- data: datas.dataList,
- itemStyle: {
- barBorderRadius: 4
- }
- }
- ]
- };
- },
- getBarGroupOption(datas) {
- if (!datas.names.length) return;
- const xAxis = datas.dataList.map(function(item) {
- return item.name;
- });
- const series = datas.names.map(function(name, index) {
- const data = datas.dataList.map(function(item) {
- return item.data[index];
- });
- return {
- name: name,
- type: "bar",
- data: data,
- barMaxWidth: 8,
- itemStyle: {
- barBorderRadius: 2
- }
- };
- });
- const options = {
- color: this.chartColor,
- grid: {
- top: "20%",
- bottom: "10%",
- left: "8%",
- right: "5%"
- },
- tooltip: {
- show: true,
- trigger: "axis",
- axisPointer: {
- type: "shadow"
- },
- formatter: function(params) {
- const label = params[0].axisValueLabel;
- let infos = params.map(function(item) {
- return item.seriesName + ":" + item.value.toFixed(2) + "%";
- });
- infos.unshift(label);
- return infos.join("<br/>");
- }
- },
- legend: {
- data: datas.names,
- right: 0,
- itemWidth: 14,
- textStyle: {
- fontSize: 16
- }
- },
- xAxis: {
- type: "category",
- data: xAxis,
- axisLabel: {
- color: "#7C86A3",
- fontSize: 12,
- fontWeight: "bold"
- },
- axisTick: {
- show: false
- }
- },
- yAxis: {
- type: "value",
- splitLine: {
- show: true,
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLine: {
- lineStyle: {
- color: "rgba(231,234,241,1)"
- }
- },
- axisLabel: {
- color: "#7C86A3",
- fontSize: 12,
- fontWeight: "bold",
- formatter: function(value, index) {
- return value + "%";
- }
- },
- axisTick: {
- show: false
- }
- },
- series: series
- };
- return options;
- },
- getLineGroupOption(datas) {
- if (!datas.length) return;
- const names = datas.map(function(item) {
- return item.name;
- });
- const xaxis = datas[0].dataList.map(function(item, index) {
- return index;
- });
- const series = datas.map(function(item) {
- return {
- name: item.name,
- type: "line",
- symbol: "circle",
- smooth: true,
- itemStyle: {
- emphasis: {
- color: "#333"
- }
- },
- data: item.dataList.map(function(num) {
- return num * num;
- })
- };
- });
- return {
- grid: {
- top: "15%",
- bottom: "12%"
- },
- tooltip: {
- show: true,
- trigger: "axis",
- axisPointer: {
- type: "shadow"
- },
- formatter: function(params) {
- const label = params[0].axisValueLabel;
- let infos = params.map(function(item) {
- return item.seriesName + ":" + Math.sqrt(item.value);
- });
- infos.unshift(label);
- return infos.join("<br/>");
- }
- },
- legend: {
- data: names,
- right: 0,
- itemWidth: 14,
- textStyle: {
- fontSize: 16
- }
- },
- xAxis: {
- type: "category",
- data: xaxis,
- axisLabel: {
- show: false,
- fontSize: 14
- },
- axisTick: {
- show: false
- }
- },
- yAxis: {
- type: "value",
- interval: 1,
- splitLine: {
- show: false
- },
- axisLabel: {
- fontSize: 14,
- formatter: function(value) {
- const num = Math.sqrt(value);
- return num % 1 ? "" : parseInt(num);
- }
- },
- axisTick: {
- show: false
- }
- },
- dataZoom: [
- {
- type: "inside",
- start: 0,
- end: 30
- },
- {
- type: "slider",
- start: 0,
- end: 30
- }
- ],
- series: series
- };
- }
- }
- };
- </script>
|