echarts上加横线标线_echarts 添加标线,设置颜色

这篇博客介绍了如何在Echarts图表中添加横线(警示线)并设置颜色,通过示例展示了针对不同数据段设置不同颜色的方法,包括条形图和折线图的配置,以及使用markLine和itemStyle进行个性化定制。
摘要由CSDN通过智能技术生成

< script src ="assets/js/jquery-1.8.3.min.js" > script > echarts怎么加警戒线

< script src ="assets/js/jquery-1.8.3.min.js" > script >< script type ="text/javascript" src ="js/echarts.common.min.js" > script >< div id ="main1" style ="width:45%;height:400px;margin-top: 40px;float: left;" > div >< div id ="main2" style ="width:53%;height:400px;margin-top: 40px;float: right;" > div >< script > // var myChart1 = echarts.init(document.getElementById( ' main1 ' )); var data1 = [ ' 10.2 ' , ' 8.6 ' , ' 9.3 ' , ' 7.5 ' ]; // 预警值 数组 var seriesData = [ 12 , 7.8 , 14.2 , 6.2 ]; // 总能耗数值数组 option1 = { title: { text: ' 分车型能耗预警 ' }, color: [ ' #e7505a ' ], tooltip: { trigger: ' axis ' , axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: ' shadow ' // 默认为直线,可选为:'line' | 'shadow' } }, grid: { right: ' 4% ' , bottom: ' 3% ' , containLabel: true }, xAxis: [{ type: ' category ' , data: [ ' 伊兰特汽油 ' , ' 伊兰特CNG ' , ' B50汽油 ' , ' 凯美瑞混合动力 ' ], axisTick: { alignWithLabel: true } }], yAxis: [{ type: ' value ' , name: ' 百公里能耗L/100KM ' , min: 0 , max: 15 , interval: 3 , axisLabel: { formatter: ' {value} ' } }], series: [{ name: '' , type: ' bar ' , barWidth: ' 45% ' , data:seriesData, markLine: { itemStyle: { normal: { borderWidth: 1 , lineStyle: { type: ' dash ' , color: ' #333 ' , width: 2 , }, label: { formatter: '' , textStyle: { fontSize: 16 , fontWeight: " bolder " , }, } }, }, data: [ [{ coord: [ ' 伊兰特汽油 ' , 0 ] }, { coord: [ ' 伊兰特汽油 ' , 10.2 ] }], [{ coord: [ ' 伊兰特CNG ' , 0 ] }, { coord: [ ' 伊兰特CNG ' , 8.6 ] }], [{ coord: [ ' B50汽油 ' , 0 ] }, { coord: [ ' B50汽油 ' , 9.3 ] }], [{ coord: [ ' 凯美瑞混合动力 ' , 0 ] }, { coord: [ ' 凯美瑞混合动力 ' , 7.5 ] // 平行于Y轴的 虚线 }] ], }, }, { name: ' 预警值 ' , type: ' line ' , color: ' #333 ' ,

symbol:'triangle', //起点标记图形 (三角形),详细请见 http://echarts.baidu.com/option.html#series-line.markLine.data.0.symbol itemStyle: { normal: { borderWidth: 1 , lineStyle: { type: ' dash ' , color: ' transparent ' , width: 2 , }, label: { formatter: '' , textStyle: { color: ' #333 ' , fontSize: 16 , fontWeight: " bolder " , }, } }, }, data: data1.map( function (val) { return val ; }), label: { normal: { show: true , formatter: function (params) { return params.value ; // 预警值 对应显示 data1 预警值 } } } } ], }; // 判断数值动态设置颜色 $(document).ready( function (params){ for ( var i = 0 ; i < seriesData.length ; i ++ ){ if (seriesData[i].value){ seriesData[i] = seriesData[i].value; } if (seriesData[ 0 ] > 10.2 ){ seriesData[ 0 ] = { value : seriesData[ 0 ], itemStyle:{ normal:{ color : ' #c23531 ' } } } } else if (seriesData[ 0 ] <= 10.2 ){ seriesData[ 0 ] = { value : seriesData[ 0 ], itemStyle:{ normal:{ color : ' #c5bf66 ' } } } } if (seriesData[ 1 ] > 8.6 ){ seriesData[ 1 ] = { value : seriesData[ 1 ], itemStyle:{ normal:{ color : ' #c23531 ' } } } } else if (seriesData[ 1 ] <= 8.6 ){ seriesData[ 1 ] = { value : seriesData[ 1 ], itemStyle:{ normal:{ color : ' #c5bf66 ' } } } } if (seriesData[ 2 ] > 9.3 ){ seriesData[ 2 ] = { value : seriesData[ 2 ], itemStyle:{ normal:{ color : ' #c23531 ' } } } } else if (seriesData[ 2 ] <= 9.3 ){ seriesData[ 2 ] = { value : seriesData[ 2 ], itemStyle:{ normal:{ color : ' #c5bf66 ' } } } } if (seriesData[ 3 ] > 7.5 ){ seriesData[ 3 ] = { value : seriesData[ 3 ], itemStyle:{ normal:{ color : ' #c23531 ' } } } } else if (seriesData[ 3 ] <= 7.5 ){ seriesData[ 3 ] = { value : seriesData[ 3 ], itemStyle:{ normal:{ color : ' #c5bf66 ' } } } } } // console.log(seriesData); // option1.series[0].data=seriesData myChart1.setOption(option1, true ); }) myChart1.setOption(option1); var myChart2 = echarts.init(document.getElementById( ' main2 ' )); option2 = { title: { text: ' 分车龄能耗预警 ' }, color: [ ' #F8CB00 ' ], tooltip: { trigger: ' axis ' , axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: ' shadow ' // 默认为直线,可选为:'line' | 'shadow' } }, grid: { right: ' 4% ' , bottom: ' 3% ' , containLabel: true }, xAxis: [{ type: ' category ' , data: [ ' 1年 ' , ' 2年 ' , ' 3年 ' , ' 4年 ' , ' 5年 ' , ' 6年 ' , ' 7年 ' , ' 8年 ' ], axisTick: { alignWithLabel: true } }], yAxis: [{ type: ' value ' , name: ' 百公里能耗L/100KM ' , min: 0 , max: 15 , interval: 3 , axisLabel: { formatter: ' {value} ' } }], series: [{ name: '' , type: ' bar ' , barWidth: ' 65% ' , data: [ 8.1 , 13.3 , 1.5 , 14.6 , 10.8 , 9.0 , 9.2 , 9.3 ], markLine: { // 开始标预警线 itemStyle: { normal: { borderWidth: 1 , lineStyle: { type: ' dash ' , color: ' #333 ' , width: 2 , }, label: { formatter: ' 12.6 ' , textStyle: { fontSize: 16 , fontWeight: " bolder " , }, } }, }, data: [ [{ coord: [ ' 1年 ' , 12.6 ] }, { coord: [ ' 8年 ' , 12.6 ] }] ], }, itemStyle: { normal: { color: function (params) { // 根据预警线的值 显示对应的颜色 // build a color map as your need. var colorList = [ ' #c23531 ' , ' #c5bf66 ' ]; if (params.data > 12.6 ) { return colorList[ 0 ]; } else if (params.data < 12.6 ) { return colorList[ 1 ]; } } }, } }], }; myChart2.setOption(option2); script >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值