设置Echarts折线图自定义属性

本文详细介绍了如何使用Echarts库来设置折线图的各种自定义属性,包括线条颜色、点的形状、数据标签等,帮助开发者创建个性化的数据可视化效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

var option = {
        backgroundColor: '#FFF0F5',
        title: {
          text: '折线图',
          subtext: '模拟数据',
          x: 'center'
        },
        legend: {
          // orient 设置布局方式,默认水平布局,可选值:'horizontal'(水平) ¦ 'vertical'(垂直)
          orient: 'horizontal',
          // x 设置水平安放位置,默认全图居中,可选值:'center' ¦ 'left' ¦ 'right' ¦ {number}(x坐标,单位px)
          x: 'left',
          // y 设置垂直安放位置,默认全图顶端,可选值:'top' ¦ 'bottom' ¦ 'center' ¦ {number}(y坐标,单位px)
          y: 'top',
          data: ['expected', 'actual']
        },
        //  图表距边框的距离,可选值:'百分比' || 数值(单位px)
        grid: {
            top: 10,   
            left: 10,
            right: 10,
            bottom: 20,
            top: 30,
            containLabel: true,
        }, 
        // 提示框,悬浮点
        tooltip: {
          trigger: 'axis',
          //当鼠标移动到数值时候,在X轴Y轴显示数值
          axisPointer: {
            type: 'cross'
          },         
        },
        //工具框,可以选择
        toolbox: {
            feature: {
                saveAsImage: {} //下载工具
            }
        },
        xAxis: {
          //网格样式
          splitLine: {
            show: true,
            lineStyle:{
              color: ['#fff'],
              width: 1,
              type: 'solid'
            }
          },
          //x轴颜色
          axisLine:{
            lineStyle:{
              color: ['#fff'],
              width: 1,
              type: 'solid'
            }
          },
          // 设置X轴数据旋转倾斜
          axisLabel: {
            rotate: 30, // 旋转角度
            interval: 0  //设置X轴数据间隔几个显示一个,为0表示都显示
            color: "#ff0" //刻度线标签颜色
          },
          // boundaryGap值为false的时候,折线第一个点在y轴上
          boundaryGap: false,
          data: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
        },
 
        yAxis: {
          name: '数值',
          type: 'value',
          min:0, // 设置y轴刻度的最小值
          max:1800,  // 设置y轴刻度的最大值
          splitNumber:9,  // 设置y轴刻度间隔个数
          axisLine: {
            lineStyle: {
              // 设置y轴颜色
              color: '#fff'
            }
          },
        },
        series: [
          {
            name: 'expected', itemStyle: {
	            normal: {
	              color: '#FF005A',
	              lineStyle: {
	                color: '#fff',//线条颜色
	                width: 2
	              }
	            }
	          },
            data: [820, 932, 301, 1434, 1290, 1330, 1320],            
	        itemStyle : {//每个坐标点圆点颜色
	         	normal : {
	              color:'red',//圆点外圈颜色
	              lineStyle:{
	                color:'#fff'
	              }
	            }
	        },
            type: 'line',
            // 设置小圆点消失
            // 注意:设置symbol: 'none'以后,拐点不存在了,设置拐点上显示数值无效
            symbol: 'none',
            // 设置折线弧度,取值:0-1之间
            smooth: 0.5,
          }, 
          {
            name: 'actual',
            data: [620, 732, 941, 834, 1690, 1030, 920],
            type: 'line',
            // 设置折线上圆点大小
            symbolSize:8,
            itemStyle:{
              normal:{
                // 拐点上显示数值
                label : {
                show: true
                },
                borderColor:'red',  // 拐点边框颜色
                lineStyle:{                 
                  width:5,  // 设置线宽
                  type:'dotted'  //'dotted'虚线 'solid'实线
                }
              }
            }
          },
        ],
        color: ['#00EE00', '#FF9F7F']
### ECharts 折线图自定义 Tooltip 示例及配置方法 #### 修改 Tooltip 默认样式 ECharts 提供了多种方式来自定义 `Tooltip` 的外观和行为。通过设置 `tooltip` 属性中的不同选项,可以实现高度定制化的提示框效果[^1]。 对于折线图而言,在初始化图表实例时可以通过如下方式进行基本配置: ```javascript var myChart = echarts.init(document.getElementById('main')); option = { tooltip: { trigger: 'axis', axisPointer: { type: 'cross' }, backgroundColor: '#282c34', // 背景颜色 borderColor: '#ffffff', // 边框颜色 borderWidth: 1, // 边框宽度 padding: [5, 10], // 内边距 textStyle: { color: '#fff' // 文字颜色 } }, }; myChart.setOption(option); ``` 此段代码设置了背景色、文字颜色以及指针样式等参数来改变默认的 Tooltip 外观。 #### 动态调整 X 轴标签与 Tooltip 显示内容不一致的情况 如果遇到 X 轴实际展示的内容同 Tooltip 中呈现的数据存在差异的情形,则可通过将数据存储为特定结构并利用 formatter 函数来进行处理[^2]。 具体做法是在准备数据阶段就构建好包含坐标、名称及时刻的信息列表,并将其作为 series 数据源的一部分;接着针对 xAxis 设置相应的 label 格式化函数,从而达到预期的效果。 以下是简化版的例子说明这一过程: ```javascript // 假设这是经过转换后的部分数据集 let data = [ ['2023-09-01T00:00:00Z', "Point A", new Date().getTime()], ... ]; option = { xAxis: { type: 'category', boundaryGap: false, data: data.map(item => item[1]), // 使用名称填充X轴 axisLabel: { formatter: function (value) { return value; } } }, yAxis: {}, series: [{ name: 'Series Name', type: 'line', data: data.map((item, index) => ({ coord: [index, Math.random() * 100], time: item[2] })), tooltip: { formatter: function (params) { let pointInfo = params.data; return ` 时间戳:<br/> ${new Date(pointInfo.time).toLocaleString()} `; } } }] }; myChart.setOption(option); ``` 上述代码片段展示了如何创建一个带有特殊格式的时间戳信息的 Tooltip ,并且确保这些信息不会直接反映到 X 轴上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值