上图是最终实现的效果,话不多说,直接上代码记录下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>渐变柱状</title>
<!-- 引入 echarts.js -->
<script src="./js/echarts4.60.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
</head>
<style>
body{
background-color: #00062E;
}
</style>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
var data = [50, 200, 400, 600];
var sum = 0;
for(var i = 0;i < data.length;i++){
sum += data[i];
}
// 指定图表的配置项和数据
myChart.setOption({
title: {
// text: '柱状图渐变'
},
tooltip: {},
legend: {
// data: ['销量']
},
xAxis: {
data: ["上年同期", "本年计划", "本年实际"],
axisTick: {
alignWithLabel: true,
show: false // 纵轴
},
axisLine: { // 横轴
show: true,
lineStyle: {
color:'#212A51'
}
},
axisLabel: {
textStyle: {
show:true,
fontFamily:'微软雅黑',
color: "#fff",
fontSize: '14',
},
}
},
yAxis: {
type: 'value',
splitLine: {
show: true,
lineStyle: {
type: 'dashed',
color:["#20294F"],
width:2.5,
}
},
axisLabel: {
textStyle: {
show:true,
fontFamily:'微软雅黑',
color: "#fff",
fontSize: '12',
},
},
axisTick: {
alignWithLabel: false,
show: false
},
axisLine: {
show: false
},
},
series: [{
// name: '销量',
type: 'bar',
barWidth:20,
barGap:'80%',/*多个并排柱子设置柱子之间的间距*/
barCategoryGap:'50%',/*多个并排柱子设置柱子之间的间距*/
itemStyle: {
normal: {
barBorderRadius:[15, 15, 0, 0],柱形图圆角,初始化效果
width:5,
label: {
show: true, //开启显示
position: 'top', //在上方显示
textStyle: { //数值样式
color: '#FFCA4A',
fontSize: 12
},
distance:20,
formatter: function(params) {//核心部分 formatter 可以为字符串也可以是回调
if(params.value){//如果当前值存在则拼接
return params.value + '万元'
}else{//否则返回个空
return '';
}
},
},
color: { //图形渐变颜色方法,四个数字分别代表,右,下,左,上,offset表示0%到100%
type: 'linear',
x: 0,
y: 1,
x2: 0, //从左到右 0-1
y2: 0,
colorStops: [
{
offset: 0,
color: "#FFCA4A" // 0% 处的颜色
},
{
offset: 0.2,
color: "#FFCA4A" // 60% 处的颜色
},
{
offset: 0.4,
color: "#FFCA4A" // 60% 处的颜色
},
{
offset: 0.6,
color: "#FFCA4A" // 60% 处的颜色
},
{
offset: 0.8,
color: "#88DDA2" // 60% 处的颜色
},
{
offset: 1,
color: "#30EBE2" // 100% 处的颜色
}
],
},
// color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
// {
// offset: 0,
// color: "#FFCA4A" // 0% 处的颜色
// },
// {
// offset: 0.2,
// color: "#FFCA4A" // 60% 处的颜色
// },
// {
// offset: 0.4,
// color: "#FFCA4A" // 60% 处的颜色
// },
// {
// offset: 0.6,
// color: "#FFCA4A" // 60% 处的颜色
// },
// {
// offset: 0.8,
// color: "#88DDA2" // 60% 处的颜色
// },
// {
// offset: 1,
// color: "#30EBE2" // 100% 处的颜色
// }
// ], false)
},
},
data: data,
markPoint: {
symbol:'arrow',
// symbol:'image://http://echarts.baidu.com/images/favicon.png',
itemStyle:{
normal:{
label:{
show:true,
color:'#fff',
},
color:'#00F5F0',
}
},
symbolSize:[8, 8],
symbolOffset:[0,-10],
data:[
{name:'上年同期',coord:[0,50]},
{name:'本年计划',coord:[1,200]},
{name:'本年实际',coord:[2,400]},
],
},
markLine : {
data : [
{
yAxis: 400,
name: '标杆水平',
lineStyle: {
type: 'solid',
color: '#C14833'
},
label:{
show:true,
position:'end',
// formatter:"平均值 : "+sum/data.length,
formatter:"标杆水平",
}
},
// {
// yAxis: 100,
// name: '上限',
// lineStyle: {
// type: 'dashed',
// color: '#b17063'
// },
{
type :'average',
name: '平均值',
label:{
show:true,
position:'end',
// formatter:"平均值 : "+sum/data.length,
formatter:"平均水平",
}
},
],
symbol: ['none', 'none'],
position:"insideTopCenter",
itemStyle:{
normal:{
lineStyle:{
type:'solid',
color:'#CCA607'
},
// label:{
// sh ow:true,
// position:'end',
// // formatter:"平均值 : "+sum/data.length,
// // formatter:"平均水平",
// }
}
},
large:true,
effect:{
show: false,
loop: true,
period: 0,
scaleSize : 2,
color : null,
shadowColor : null,
shadowBlur : null
},
},
// markLine: {
// data: [
// {type: 'average', name: '平均值'}
// ]
// }
}]
});
</script>
</body>
</html>