123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <template>
- <div class="new-line-single">
- <div :id="index" class="base">堆叠折线图</div>
- <div
- class="zuan"
- v-show="config.zuanFlag && config.zuanLevel > 0"
- :style="`color:${option.zuanFontColor}`"
- >
- <i
- class="el-icon-back"
- @click="onZuanBack"
- style="cursor: pointer; margin-right: 10px"
- >
- </i>
- {{ config.zuanStack.join(` ${option.zuanFenge} `) }}
- </div>
- </div>
- </template>
- <script>
- import {
- countLabel,
- countLine,
- grid,
- legend,
- title,
- countLegendSelect,
- countXlabel,
- } from "../../newStyles/styleCommon";
- export default {
- name: "new-line-single",
- props: ["option", "config", "modalSet", "index", "site"],
- data() {
- return {
- originData: [],
- chartOption: {
- tooltip: {
- trigger: "axis",
- axisPointer: { type: "shadow" },
- },
- legend: { data: [] },
- grid: { left: "3%", right: "3%", bottom: "3%", containLabel: true },
- xAxis: {
- type: "category",
- data: [],
- },
- yAxis: [{ type: "value" }],
- series: [],
- },
- chart: null,
- };
- },
- watch: {
- site: {
- handler() {
- if (this.chart) {
- this.chart.resize();
- }
- },
- deep: true,
- },
- option: {
- handler() {
- if (this.originData.length) {
- this.refresh();
- }
- },
- deep: true,
- },
- },
- methods: {
- handleClick(event) {
- if (this.config.zuanLevel + 1 >= this.config.zuan.length) {
- return this.$message({
- type: "warning",
- message: "已经钻取到最后一层了",
- });
- }
- let key = this.modalSet.find((i) => i.zuan).key;
- let index = this.config[key].findIndex((i) => i.zuan);
- this.config.zuanStack.push(event.data.name[index]);
- this.config.zuanLevel += 1;
- this.$emit("preview");
- },
- onZuanBack() {
- if (this.config.zuanLevel <= 0) return;
- this.config.zuanLevel -= 1;
- this.config.zuanStack = this.config.zuanStack.slice(
- 0,
- this.config.zuanLevel
- );
- this.$emit("preview");
- },
- refresh() {
- if (this.config.serie.length) {
- this.refreshSingle();
- } else {
- this.refreshNormal();
- }
- },
- refreshNormal() {
- let { dimension, measure } = this.config;
- dimension = [...dimension].filter((i) => !i.extJson.hidden);
- measure = measure.filter((i) => !i.extJson.hidden);
- if (this.config.zuanFlag) {
- let zuanField = this.config.zuan[this.config.zuanLevel];
- let zuanIndex = dimension.findIndex((i) => i.zuan);
- dimension.splice(zuanIndex, 1, zuanField);
- }
- let clearData = this.originData.map((i) => {
- let item = { key: [], value: [] };
- dimension.reduce((r, d) => {
- r.key.push(i[d.columnTitle]);
- return r;
- }, item);
- measure.reduce((r, d) => {
- r.value.push(parseFloat(i[d.columnTitle] || 0));
- return r;
- }, item);
- return item;
- });
- this.chartOption.grid = grid(this.option);
- //标题设置
- if (this.option.isTitleShow) {
- this.chartOption.title = title(this.option);
- }
- // 图例设置
- this.chartOption.legend = legend(this.option);
- let chartOption = { ...this.chartOption };
- let xAxisData = clearData.map((i) => i.key);
- chartOption.xAxis.data = xAxisData.map((i) => i.join("-"));
- let legengData = [];
- let seriesLabel = countLabel(this.option);
- chartOption.series = measure.map((m, i) => {
- legengData.push(m.columnTitle);
- let serie = {
- name: m.columnTitle,
- type: "line",
- data: clearData.map((d, index) => ({
- value: d.value[i],
- name: xAxisData[index],
- })),
- label: seriesLabel,
- itemStyle: {},
- };
- return serie;
- });
- chartOption.legend.data = legengData;
- chartOption.xAxis.axisLabel = countXlabel(this.option);
- this.setYlabel(chartOption);
- this.setSerieColor(chartOption);
- countLegendSelect(this.option, chartOption);
- this.chart = echarts.init(document.getElementById(this.index));
- this.chart.off("click", this.handleClick);
- if (this.config.zuanFlag) {
- this.chart.on("click", this.handleClick);
- }
- this.chart.setOption(chartOption, true);
- },
- refreshSingle() {
- let { dimension, measure, serie } = this.config;
- dimension = [...dimension].filter((i) => !i.extJson.hidden);
- measure = measure.filter((i) => !i.extJson.hidden);
- serie = serie.filter((i) => !i.extJson.hidden);
- if (this.config.zuanFlag) {
- let zuanField = this.config.zuan[this.config.zuanLevel];
- let zuanIndex = dimension.findIndex((i) => i.zuan);
- dimension.splice(zuanIndex, 1, zuanField);
- }
- let { stackSwitch } = this.option;
- this.chartOption.grid = grid(this.option);
- if (this.option.isTitleShow) {
- this.chartOption.title = title(this.option);
- }
- this.chartOption.legend = legend(this.option);
- let xAxisData = [];
- let legendData = [];
- let valueKey = measure[0].columnTitle;
- let serieKey = serie[0].columnTitle;
- let clearData = this.originData.map((i) => {
- let item = { key: "", value: "", label: "" };
- let keyArr = dimension.map((d) => i[d.columnTitle]);
- let key = keyArr.join("-");
- item.keyArr = keyArr;
- item.key = key;
- if (!xAxisData.includes(key)) {
- xAxisData.push(key);
- }
- if (!legendData.includes(i[serieKey])) {
- legendData.push(i[serieKey]);
- }
- item.value = i[valueKey];
- item.label = i[serieKey];
- return item;
- });
- let chartOption = { ...this.chartOption };
- chartOption.xAxis.data = xAxisData;
- let seriesLabel = countLabel(this.option);
- let seriesLine = countLine(this.option);
- chartOption.series = legendData.map((name) => {
- let serieItem = {
- name,
- data: xAxisData.map((i) => {
- let item = clearData.find((j) => j.key == i && j.label == name);
- let nameItem = clearData.find((j) => j.key == i);
- let value = item ? parseFloat(item.value) : 0;
- return { value, name: nameItem.keyArr };
- }),
- type: "line",
- label: seriesLabel,
- itemStyle: {},
- lineStyle: seriesLine,
- smooth: this.option.lineSmooth,
- };
- if (stackSwitch) {
- serieItem.areaStyle = {};
- }
- return serieItem;
- });
- if (stackSwitch) {
- chartOption.series.forEach((item) => (item.stack = "stack"));
- }
- chartOption.legend.data = legendData;
- chartOption.xAxis.axisLabel = countXlabel(this.option);
- this.setYlabel(chartOption);
- this.setSerieColor(chartOption);
- countLegendSelect(this.option, chartOption);
- this.chart = echarts.init(document.getElementById(this.index));
- this.chart.off("click", this.handleClick);
- if (this.config.zuanFlag) {
- this.chart.on("click", this.handleClick);
- }
- this.chart.setOption(chartOption, true);
- },
- // y轴设置
- setYlabel(chartOption) {
- let { ylabelFontSize, ylabelColor } = this.option;
- chartOption.yAxis[0].axisLabel = {
- fontSize: ylabelFontSize,
- color: ylabelColor,
- };
- },
- // 设置系列颜色
- setSerieColor(chartOption) {
- let list = this.option.colorList.slice(0, chartOption.series.length);
- list.forEach(({ color1, color2, direction }, index) => {
- direction = direction || "top";
- if (color1 && color2) {
- chartOption.series[index].itemStyle.color = {
- x: direction == "left" ? 1 : 0,
- y: direction == "top" ? 1 : 0,
- x2: direction == "right" ? 1 : 0,
- y2: direction == "bottom" ? 1 : 0,
- colorStops: [
- { offset: 0, color: color1 },
- { offset: 1, color: color2 },
- ],
- };
- } else if (color1 && !color2) {
- chartOption.series[index].itemStyle.color = color1;
- }
- });
- },
- update(data) {
- this.originData = data;
- this.refresh();
- },
- initRender() {
- let option = {
- tooltip: { trigger: "axis", axisPointer: { type: "shadow" } },
- legend: { data: ["大", "小", "中"] },
- grid: { left: "3%", right: "3%", bottom: "3%", containLabel: true },
- xAxis: {
- type: "category",
- data: ["老年", "青年", "少年", "中年"],
- axisLabel: {
- fontSize: 12,
- color: "#333333",
- interval: "0",
- rotate: 0,
- },
- },
- yAxis: [
- { type: "value", axisLabel: { fontSize: 12, color: "#333333" } },
- ],
- series: [
- {
- name: "大",
- data: [
- { value: 2614, name: ["老年"] },
- { value: 5480, name: ["青年"] },
- { value: 2771, name: ["少年"] },
- { value: 2578, name: ["中年"] },
- ],
- type: "line",
- barWidth: 20,
- label: {
- show: true,
- color: "#333333",
- fontSize: 12,
- position: "inside",
- textBorderWidth: 0,
- textBorderColor: "#ffffff",
- rotate: 0,
- },
- itemStyle: { borderRadius: 0 },
- areaStyle: {},
- stack: "stack",
- },
- {
- name: "小",
- data: [
- { value: 2570, name: ["老年"] },
- { value: 5605, name: ["青年"] },
- { value: 2906, name: ["少年"] },
- { value: 2766, name: ["中年"] },
- ],
- type: "line",
- label: {
- show: true,
- color: "#333333",
- fontSize: 12,
- position: "inside",
- textBorderWidth: 0,
- textBorderColor: "#ffffff",
- rotate: 0,
- },
- itemStyle: { borderRadius: 0 },
- areaStyle: {},
- stack: "stack",
- },
- {
- name: "中",
- data: [
- { value: 5066, name: ["老年"] },
- { value: 11154, name: ["青年"] },
- { value: 5371, name: ["少年"] },
- { value: 5364, name: ["中年"] },
- ],
- type: "line",
- barWidth: 20,
- label: {
- show: true,
- color: "#333333",
- fontSize: 12,
- position: "inside",
- textBorderWidth: 0,
- textBorderColor: "#ffffff",
- rotate: 0,
- },
- itemStyle: { borderRadius: 0 },
- areaStyle: {},
- stack: "stack",
- },
- ],
- };
- this.chart = echarts.init(document.getElementById(this.index));
- this.chart.setOption(option, true);
- this.$nextTick(this.chart.resize);
- },
- },
- };
- </script>
- <style lang="scss">
- .new-line-single {
- .base {
- width: 100%;
- height: calc(100% - 20px);
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .zuan {
- position: absolute;
- left: 0;
- bottom: 0;
- width: 100%;
- line-height: 20px;
- padding: 0 10px;
- font-size: 12px;
- }
- }
- </style>
|