Skip to content

Commit

Permalink
feat(lineChart,pieChart): 升级图标配色 文字超出隐藏等
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed Mar 6, 2020
1 parent 50d7fee commit 907ec24
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 92 deletions.
143 changes: 85 additions & 58 deletions src/components/Charts/lineChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
<div
:class="className"
ref="chart"
:style="{height:height,width:width}"/>
:style="{height:'100%',width:width,minHeight:height}"/>
</template>

<script>
import echarts from 'echarts';
import 'echarts/theme/macarons';
export default {
props: {
Expand All @@ -44,6 +43,10 @@ export default {
type: String,
default: '300px',
},
type: {
type: String,
default: 'bar',
},
data: {
type: Array,
default: () => ([]),
Expand Down Expand Up @@ -84,86 +87,110 @@ export default {
this.chart.dispose();
this.chart = null;
},
computed: {
legand() {
const arr = [];
this.data.forEach((item) => {
arr.push(item.name);
});
return arr;
},
},
methods: {
initChart() {
this.chart = echarts.init(this.$refs.chart);
this.chart.setOption({
color: ['#FF7744', '#87CEFA'],
color: ['#60acfc', '#32d3eb', '#5bc49f', '#feb64d', '#ff7c7c', '#9287e7'],
tooltip: {
trigger: 'axis',
},
title: {
text: this.title,
left: '15%',
top: '0',
textAlign: 'center',
textStyle: {
fontSize: 14,
},
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true,
},
legend: {
data: ['案件', '线索'],
},
xAxis: [{
type: 'category',
boundaryGap: false,
axisTick: {
alignWithLabel: true,
},
data: this.legand,
axisLabel: { // 坐标轴刻度标签的相关设置。
interval: 0, // 横轴信息全部显示
rotate: -30, // -30度角倾斜显示
formatter(params) {
let newParamsName = '';// 最终拼接成的字符串
const paramsNameNumber = params.length;// 实际标签的个数
const provideNumber = 5;// 每行能显示的字的个数
const rowNumber = Math.ceil(paramsNameNumber / provideNumber);// 换行的话,需要显示几行,向上取整
/**
* 判断标签的个数是否大于规定的个数, 如果大于,则进行换行处理 如果不大于,即等于或小于,就返回原标签
*/
// 条件等同于rowNumber>1
if (paramsNameNumber > provideNumber) {
/** 循环每一行,p表示行 */
for (let p = 0; p < rowNumber; p += 1) {
let tempStr = '';// 表示每一次截取的字符串
const start = p * provideNumber;// 开始截取的位置
const end = start + provideNumber;// 结束截取的位置
// 此处特殊处理最后一行的索引值
if (p === rowNumber - 1) {
// 最后一次不换行
tempStr = params.substring(start, paramsNameNumber);
} else {
// 每一次拼接字符串并换行
tempStr = `${params.substring(start, end)}\n`;
}
newParamsName += tempStr;// 最终拼成的字符串
}
} else {
// 将旧标签的值赋给新标签
newParamsName = params;
}
// 将最终的字符串返回
return newParamsName;
},
},
data: this.date,
}],
yAxis: [{
type: 'value',
name: '人数',
position: 'left',
// axisLabel: {
// formatter: '{value} '
// }
name: '数量',
}],
series: [{
name: '线索',
type: 'line',
stack: '线索总量',
areaStyle: { normal: {} },
label: {
barWidth: 30, // 柱图宽度
name: '数量',
type: this.type,
data: this.data,
itemStyle: {
normal: {
show: true,
position: 'top',
},
},
lineStyle: {
normal: {
width: 1,
shadowColor: 'rgba(0,0,0,0.4)',
shadowBlur: 2,
shadowOffsetY: 2,
},
},
data: this.data[0],
}, {
name: '案件',
type: 'line',
stack: '案件总量',
label: {
normal: {
show: true,
position: 'top',
},
},
areaStyle: { normal: {} },
lineStyle: {
color: '#8cd5c2', // 改变折线颜色
},
// 每根柱子颜色设置
color(params) {
const colorList = ['#60acfc', '#32d3eb', '#5bc49f', '#feb64d', '#ff7c7c', '#9287e7'];
return colorList[params.dataIndex];
},
label: { show: true, color: '#000' },
lineStyle: {
normal: {
width: 1,
shadowColor: 'rgba(0,0,0,0.4)',
shadowBlur: 2,
shadowOffsetY: 2,
},
},
data: this.data[1],
}],
});
}, true);
},
},
watch: {
data: {
deep: true,
handler() {
this.initChart();
},
},
},
};
Expand Down
91 changes: 57 additions & 34 deletions src/components/Charts/pieChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,30 @@
-->
<template>
<div
:class="className"
ref="chart"
:style="{height:height,width:width}"/>
<div :class="className"
ref="chart"
:style="{height:'100%',minHeight:height,width:width}" />
</template>

<script>
import echarts from 'echarts';
import 'echarts/theme/macarons';
export default {
props: {
className: {
type: String,
},
id: {
type: String,
default: 'chart',
},
width: {
type: String,
default: '500px',
},
height: {
type: String,
default: '300px',
default: '250px',
},
data: {
type: Array,
default: () => ([]),
default: () => [],
},
title: {
type: String,
Expand Down Expand Up @@ -91,55 +85,75 @@ export default {
initChart() {
this.chart = echarts.init(this.$refs.chart);
this.chart.setOption({
color: [
'#1890FF',
'#13C2C2',
'#2FC25B',
'#FACC14',
'#F04864',
'#8543E0',
'#CA8622',
'#CBB6B0',
'#6E7074',
'#C4CCD3',
],
color: ['#60acfc', '#32d3eb', '#5bc49f', '#feb64d', '#ff7c7c', '#9287e7'],
title: {
text: this.title,
subtext: this.subtitle,
left: '29%',
top: '43%',
top: '44%',
textAlign: 'center',
textStyle: {
fontSize: 14,
},
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b} : {c} ({d}%)',
formatter: '{a}{b} : {c} ({d}%)',
},
legend: {
type: 'scroll',
icon: 'circle',
orient: 'vertical',
show: true,
itemWidth: 10, // 设置宽度
itemHeight: 10, // 设置高度
right: 0,
top: 35,
top: 40,
bottom: 20,
textStyle: {
fontSize: 12,
},
data: this.legand,
formatter: (params) => {
let newParamsName = '';// 最终拼接成的字符串
const paramsNameNumber = params.length;// 实际标签的个数
const provideNumber = 20;// 每行能显示的字的个数
const rowNumber = Math.ceil(paramsNameNumber / provideNumber);// 换行的话,需要显示几行,向上取整
/**
* 判断标签的个数是否大于规定的个数, 如果大于,则进行换行处理 如果不大于,即等于或小于,就返回原标签
*/
// 条件等同于rowNumber>1
if (paramsNameNumber > provideNumber) {
/** 循环每一行,p表示行 */
for (let p = 0; p < rowNumber; p += 1) {
let tempStr = '';// 表示每一次截取的字符串
const start = p * provideNumber;// 开始截取的位置
const end = start + provideNumber;// 结束截取的位置
// 此处特殊处理最后一行的索引值
if (p === rowNumber - 1) {
// 最后一次不换行
tempStr = params.substring(start, paramsNameNumber);
} else {
// 每一次拼接字符串并换行
tempStr = `${params.substring(start, end)}\n`;
}
newParamsName += tempStr;// 最终拼成的字符串
}
} else {
// 将旧标签的值赋给新标签
newParamsName = params;
}
// 将最终的字符串返回
return `${newParamsName} (${this.data.find(item => item.name === params).value})`;
},
},
series: [
{
name: '',
type: 'pie',
radius: ['50%', '80%'],
radius: ['40%', '64%'],
center: ['30%', '50%'],
data: this.data,
label: {
Expand All @@ -149,7 +163,6 @@ export default {
fontSize: 16,
},
show: false,
},
},
labelLine: {
Expand All @@ -162,14 +175,24 @@ export default {
normal: {
borderWidth: 3,
borderColor: '#fff',
},
},
},
],
}, true);
// 此处绑定饼图点击事件,把图例内容抛出,用于数据钻取
this.chart.on('click', (obj) => {
this.$emit('click', obj);
});
},
},
watch: {
data: {
deep: true,
handler() {
this.initChart();
},
},
},
};
</script>

0 comments on commit 907ec24

Please sign in to comment.