Cirry's Blog

Highcharts给X轴添加滚动条

May 29, 2023
技术 js
3分钟
471字

效果预览

default

default

直接配置

Highcharts使用柱状图,在x轴的数据太多的时候,不会出现滚动条而是挤压在一起,可以通过一些配置,给X轴添加上滚动条。

chart: {
type: 'column',
zoomType: 'x',
},
scrollablePlotArea: {
minWidth: 600
},
xAxis: {
max:10,
categories: data.categories, // 自定义x轴数据
crosshair: true,
labels: {
autoRotationLimit: 100,// 设置倾斜的点
autoRotation: [45], // 向右倾斜45度,默认为[-45]
}
11 collapsed lines
},
scrollbar:{// 设置滚动条样式
enabled: true,
height: 8,
barBackgroundColor: "#d1d1d1",
barBorderColor: "#d1d1d1",
rifleColor: "#d1d1d1",
barBorderRadius: 5,
buttonBorderWidth: 0,
buttonArrowColor: "rgba(0,0,0,0)",
},

如果确定数据量一定是大于xAxis.max条数的话,那你是可以这样直接写死设置的。

否则在数据不足够的时候,仍然会展示滚动条且highcharts会自动补齐数据项直到满足xAxis.max的数据量。

动态配置

为了满足在数据不足的时候,更加优雅的显示图表,建议自己判断数据是否需要显示滚动条。

let options = {
colors: ['#77A5FF', '#FF9536', '#FF5858', '#14C9B7', '#4486FF', '#4c4c4c'],
lang: {
thousandsSep: ''
},
title: {
text: ''
},
chart:{
type: 'column',
},
credits: {
enabled: false
},
legend: {
57 collapsed lines
enabled: false,
},
xAxis: {
categories: data.categories
}
yAxis: {
maxPadding: 0.1,
allowDecimals: false,
title: {
text: ''
},
gridLineDashStyle: 'dash',
},
tooltip: {
enabled: false,
},
plotOptions: {
column: {
pointPadding: 0,
groupPadding: 0.05,
maxPointWidth: 62,
colorByPoint: true,
dataLabels: {
enabled: true, //显示数量提示
inside: false,
useHTML: true,
crop: false,
overflow: 'none',
y: 6,
}
}
},
series: []
}
if (data.length >= 10) {
options.chart.zoomType= 'x'
options.xAxis.max = 10
options.xAxis.crosshair = true
options.xAxis.labels = {
autoRotationLimit: 100,// 设置倾斜的点
autoRotation: [45], // 向右倾斜45度,默认为[-45]
}
options.scrollablePlotArea = {
minWidth: 600
}
options.scrollbar = {// 设置滚动条样式
enabled: true,
height: 8,
barBackgroundColor: "#d1d1d1",
barBorderColor: "#d1d1d1",
rifleColor: "#d1d1d1",
barBorderRadius: 5,
buttonBorderWidth: 0,
buttonArrowColor: "rgba(0,0,0,0)",
}
}
Highcharts.chart('element_id', options ) ; // 传入显示元素id和配置项

使用这种方式可以在数据项不足的时候,使用原来的样式,在数据量大于max的时候使用滚动条样式。

本文标题:Highcharts给X轴添加滚动条
文章作者:Cirry
发布时间:May 29, 2023
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode
总访问量
总访客数人次