Highcharts 饼图 数据字段过长的处理方法

在正常情况下,Highcharts的饼图显示效果良好,如下图所示

 

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Highcharts Example</title>

		<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
		<style type="text/css">
${demo.css}
		</style>
		<script type="text/javascript">
$(function () {
    $('#container').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: 'Browser market shares January, 2015 to May, 2015'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            name: "Brands",
            colorByPoint: true,
            data: [{
                name: "Microsoft Internet Explorer",
                y: 56.33
            }, {
                name: "Chrome",
                y: 24.03,
                sliced: true,
                selected: true
            }, {
                name: "Firefox",
                y: 10.38
            }, {
                name: "Safari",
                y: 4.77
            }, {
                name: "Opera",
                y: 0.91
            }, {
                name: "Proprietary or Undetectable",
                y: 0.2
            }]
        }]
    });
});
		</script>
	</head>
	<body>
<script src="js/highcharts.js"></script>

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

    </body>
</html>

但如果data中的name,也就是后台传入的数据字段很长的时候,就会出现一个问题,如下图所示

 

这是神马怪异的现象?

对于这种问题,解决的方法就是给dataLabels加个样式,让宽度有个限制,使得文字能够自动换行,局部代码如下

 

dataLabels: {
        enabled: true,
        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
        style: {
        width: '100px', // 重点在此
                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
        }
}


效果如下

 

但是,对于英文,这种方法可行,但是遇到中文,就行不通了

因为中文词与词之间可没有空格。这时只能让他强制换行,代码如下(由于要重写dataLabel的展现,必须用到 formatter)

 

dataLabels: {
        enabled: true,
        useHTML: true, // 一定要加上
        formatter : function() {
          return "<p style='width: 100px; display:inline-block; white-space:pre-wrap;'><b>"+this.point.name+"</b>: "+this.percentage.toFixed(2)+" %</p>"; // 重点在white-space:pre-wrap
        }
}

 

 

 

效果如下

总结一下,最主要的是white-space:pre-wrap;的使用,其他的也没什么。

 

给予了灵感的链接:

1. Highcharts API

http://www.hcharts.cn/api/index.php#plotOptions.pie.dataLabels

2. Highcharts Pie Chart.How to set labels in two lines

http://stackoverflow.com/questions/18329478/highcharts-pie-chart-how-to-set-labels-in-two-lines

3. CSS white-space 属性

http://www.w3school.com.cn/cssref/pr_text_white-space.asp

 

所用示例为Highcharts官方examples下的pie-basic。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Highcharts饼图的配置项包括以下内容: 1. `legend`:图例的设置,可以控制图例的显示、布局、背景颜色等。 2. `plotOptions`:绘图选项,可以设置饼图的大小、内圈直径等。 3. `tooltip`:提示框的设置,可以定义提示框的格式。 4. `chart`:图表的设置,包括类型、背景颜色、边距等。 5. `title`:标题的设置。 6. `series`:数据列的设置,可以定义饼图数据以及数据标签的样式。 这些配置项可以通过Javascript对象的方式进行设置,例如: ``` let option = { chart: { type: "pie", backgroundColor: "rgba(0, 0, 0, 0)", marginTop:10, options3d: { enabled: true, alpha: 50, beta: 0, }, }, title: { text: "", }, plotOptions: { pie: { borderColor: "#000", borderWidth: 1, allowPointSelect: true, cursor: "pointer", depth: 24, dataLabels: { enabled: true, format: "{point.name}", style: { textOutline: "none", color: "rgba(45,153,255,1)", fontSize: 11, }, }, }, }, series: [ { type: "pie", data: [ ["邮政包裹", 30], ["圆通快递", 20], ["京东快递", 30], ["申通快递", 50], ["德邦物流", 20], ["天天快递", 20], ["其他", 30], ["顺丰快递", 10], ["中通快递", 30], ["韵达快递", 20], ], }, ], }; Highcharts.chart("DispatchProportion", option); ``` 以上就是绘制Highcharts饼图所需的配置项。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Highcharts 3D饼图(1)上手使用配置](https://blog.csdn.net/kkk_xxx/article/details/109380972)[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%"] - *2* [highcharts 饼图常用设置项](https://blog.csdn.net/qq_26769677/article/details/80229195)[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 ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值