之前做数据表格时用过echart,发现颜色不喜欢随机生成的,在这里简单记录一下自定义线条颜色。
1.折线图修改颜色:
option2 = {
xAxis: {
type: 'category',
name: 'X',
data: ['2013', '2014', '2015', '2016', '2017']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
yAxis: {
type: 'log',
name: 'Y',
},
series: [
{
name: '第一个',
type: 'line',
areaStyle: {normal: {}},
lineStyle: {
normal: {
width: 1,
color:'#e6b459'
}
},
itemStyle:{
normal:{
label: {
show: true,
textStyle: {
color: '#e6b459'
}
},
barBorderColor:'#e6b459',
color:'#f5e1bd'
},
emphasis:{
color:'white'
}
},
data: [154, 153, 159, 157, 156]
},
{
name: '第二个',
type: 'line',
},
data: [114, 133, 119, 117, 113]
},
{
name: '第三个',
type: 'line',
},
data: [94, 93, 99, 77, 97]
}
]
};
series 中的
areaStyle,lineStyle,itemStyle,textStyle中可设置自定义折线及区域颜色。
2.环形图修改颜色:
function queryData2(){
var i=0;
var colors=['#393939','#f5b031','#fad797','#59ccf7','#c3b4df'];
myChart2 = echarts.init(document.getElementById('main2'));
option2 = {
tooltip : {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient : 'vertical',
x : 'left',
data:['女','男']
},
toolbox: {
show : true,
feature : {
saveAsImage : {show: true}
}
},
calculable : true,
series : [
{
name:'性别结构',
type:'pie',
radius : ['30%', '70%'],
itemStyle : {
normal : {
color:function(){
return colors[i++];
},
label : {
show : false
},
labelLine : {
show : false
}
},
emphasis : {
label : {
show : true,
position : 'center',
textStyle : {
fontSize : '30',
fontWeight : 'bold'
}
}
}
},
data:[]
}
]
};
}
其中 函数开始定义了一个 colors 对象这里保存的都是颜色值,而在series中的itemStyle中的normal 中定义了一个color:function(){ return colors[i++]} 函数,这个函数的作用就是随机获取颜色值。这样就修改了。
3、柱状图:
yAxis : [
{
type : 'value'
}
],
series : [
{
name:'部门人数',
type:'bar',
data:[],
//颜色
itemStyle:{
normal:{
color:'#f5b031',
}},
markPoint : {
data : [
{type : 'max', name: '最大值'},
{type : 'min', name: '最小值'}
]
},
markLine : {
data : [
{type : 'average', name: '平均值'}
]
}
}
]
颜色的修改还是在series 中的itemStyle 中的normal 中的color这个值。
4、饼图颜色修改:
var option = {
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b}: {c} ({d}%)"
},
//设置饼图的颜色
color:['#f6da22','#bbe2e8','#6cacde'],
legend: {
orient: 'vertical',
x: 'left',
data:['柴油','汽油','附属油'],
show:false
},
饼图的颜色修改和折线图 ,环形图不同,颜色是单独出来的!
如上仅个人解决方案,试了可行,也许不是最优解决方案。
如有错误:望各位高手不吝赐教,我会及时修改,能获得成长!