echarts 自定义区间 线段图(横向,纵向)

在这里插入图片描述
配置项
option = {
tooltip: {
trigger: ‘axis’,
axisPointer: {
type: ‘shadow’
}
},
title: {
text: ‘区间图’
},
legend: {
data: [‘平均值’]
},
xAxis: {
type: ‘value’,
},
yAxis: {
data:categoryData
},
series: [
{
type: ‘scatter’,
name: ‘average’,
data: pointData,
seriesLayoutBy:‘row’,
encode: {
x: [2,3],
y: 1
},
itemStyle: {
color: ‘#77bef7’
}
},
{
type: ‘custom’,
name: ‘平均值’,
renderItem: function renderItem(params, api) {
var yValue = api.value(0); //对应几个y轴上的项
var lowPoint = api.coord([api.value(2),yValue]); // 左起点【15,‘平均数’】【20,中位数】
var highPoint = api.coord([api.value(3),yValue]); // 右终点【156,‘平均数’】【180,中位数】
var halfWidth = 15
var style = api.style({
stroke: api.visual(‘color’),
fill: null
});

                        return {
                            type: 'group', //左边
                            children: [
                                {
                                    type: 'line',
                                    shape: {
                                    x1: lowPoint[0],
                                    y1: lowPoint[1]+halfWidth,
                                    x2: lowPoint[0],
                                    y2: lowPoint[1]-halfWidth
                                    },
                                    style: style
                                },
                                {
                                    //中间横线
                                    type: 'line',
                                    shape: {
                                        x1: lowPoint[0],//[15]
                                        y1: highPoint[1],//[中位数]
                                        x2: highPoint[0],//[156]
                                        y2: lowPoint[1]//[中位数]
                                    },
                                    style: style
                                },
                                {
                                    //右边
                                    type: 'line',
                                  shape: {
                                    x1: highPoint[0],
                                    y1: highPoint[1]+halfWidth,
                                    x2: highPoint[0],
                                    y2: highPoint[1]-halfWidth
                                    },
                                    style: style
                                }
                            ]
                        };


                    },
                     seriesLayoutBy:'row',
                    encode: {
                       x: [2,3],
                       y: 1,
                        tooltip: [ 2,3],
                    },
                    data: msgData,
                    z: 100,
                }
            ]
        };
        数据结构
        const dimensions = categoryData = [
          '平均数',
           '中位数'
              ];

// prettier-ignore
const pointData = [
[0, ‘平均数’,115],
[1, ‘中位数’,122.5],
];
const msgData = [
[0,‘平均数’,15, 156],
[1,‘中位数’,20,180],
];

在这里插入图片描述
option = {
tooltip: {
trigger: ‘axis’,
axisPointer: {
type: ‘shadow’
}
},
title: {
text: ‘区间图’
},
legend: {
data: [‘平均值’]
},

            xAxis: {
                data: categoryData
            },
            yAxis: {},
            series: [
                {
                    type: 'scatter',
                    name: 'average',
                    data: pointData,
                    encode: {
                        single: 3
                    },
                    itemStyle: {
                        color: '#77bef7'
                    }
                },
                {
                    type: 'custom',
                    name: '平均值',
                    renderItem: function renderItem(params, api) {
                        var xValue = api.value(0);
                        var lowPoint = api.coord([xValue, api.value(1)]); //得到下面线的中心坐标
                        var highPoint = api.coord([xValue, api.value(2)]); //得到上面线的中心坐标
                        var halfWidth = api.size([1, 0])[0] * 0.1; //根据x轴索引为1的数值得到所在的映射宽度*0.1
                        var style = api.style({
                            stroke: api.visual('color'),
                            fill: null
                        });

                        return {
                            type: 'group', //上面那条线
                            children: [
                                {
                                    type: 'line',
                                    shape: {
                                        x1: highPoint[0] - halfWidth,
                                        y1: highPoint[1],
                                        x2: highPoint[0] + halfWidth,
                                        y2: highPoint[1]
                                    },
                                    style: style
                                },
                                {
                                    //中间垂直线
                                    type: 'line',
                                    shape: {
                                        x1: highPoint[0],
                                        y1: highPoint[1],
                                        x2: lowPoint[0],
                                        y2: lowPoint[1]
                                    },
                                    style: style
                                },
                                {
                                    //下面那条线
                                    type: 'line',
                                    shape: {
                                        x1: lowPoint[0] - halfWidth,
                                        y1: lowPoint[1],
                                        x2: lowPoint[0] + halfWidth,
                                        y2: lowPoint[1]
                                    },
                                    style: style
                                }
                            ]
                        };


                    },
                    encode: {
                        x: [2],
                        y: [1, 2],
                    },
                    data: msgData,
                    z: 100,
                }
            ]
        };
数据类型
 var categoryData = [
                                    "2023-2-12",
                                    "2023-2-13",
                                    "2023-2-14",
                                    "2023-2-15"]
var pointData = [
                                    [0, 115],
                                    [1, 122.5],
                                    [2, 106],
                                    [3,106.5],
                                ]
var msgData = [
                                    [0, 74, 156],
                                    [1, 90, 155],
                                    [2, 77, 135],
                                    [3, 68, 145]
                                ]
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用echarts时,可以通过自定义例来改变例的样式。博主提到了通过修改legend.icon来实现自定义例样式的方法。echarts默认的几个例icon都是使用了矢量路径的方式来表示的。矢量路径是一种用数学公式描述形的方法,通过定义路径上的点和线段来绘制出复杂的形。在echarts中,可以通过修改legend.icon的矢量路径来实现自定义例样式。具体的实现代码可以参考中的示例代码。这些示例代码非常详细,对于想要学习或者在工作中使用echarts实现例自定义的人们来说是一个很好的参考。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [echarts 例(legend icon)标自定义的几种方式](https://blog.csdn.net/rudy_zhou/article/details/111474179)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [echarts实现词云自定义形状的示例代码](https://download.csdn.net/download/weixin_38695293/12946015)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值