Javascript 在Highcharts仪表图中,如何将plotband标签定位在中心位置?

Javascript 在Highcharts仪表图中,如何将plotband标签定位在中心位置?,javascript,css,highcharts,Javascript,Css,Highcharts,我试图在仪表图的绘图带内显示标签,但我不知道如何正确对齐文本。你知道我怎样才能把标签放在图画带的中间吗?或者是否有一种更简单/替代的方法来产生这种效果 注:我使用Highcharts的“样式化模式”版本 $(函数(){ //确保仪表在初始负载时具有正确的高度 计算重量(); //并确保在调整窗口大小时重新计算高度 $(窗口).on('load resize',function()){ 计算重量(); }); 变量设置={ 参数值:0, 仪表最大值:8000, gaugeStartValue:3

我试图在仪表图的绘图带内显示标签,但我不知道如何正确对齐文本。你知道我怎样才能把标签放在图画带的中间吗?或者是否有一种更简单/替代的方法来产生这种效果

注:我使用Highcharts的“样式化模式”版本

$(函数(){
//确保仪表在初始负载时具有正确的高度
计算重量();
//并确保在调整窗口大小时重新计算高度
$(窗口).on('load resize',function()){
计算重量();
});
变量设置={
参数值:0,
仪表最大值:8000,
gaugeStartValue:3000,
高仪三角:-180,
高更荡尔:180,
仪表更新间隔:500//ms
};
变量选项={
工具提示:{
已启用:false
},
图表:{
类型:“仪表”
},
标题:假,
窗格:{
startAngle:settings.gaugeStartAngle,
endAngle:settings.gaugeEndAngle
},
打印选项:{
仪表:{
拨号:{
半径:0
},
支点:{
半径:0
},
数据标签:{
边框宽度:0,
填充:0,
垂直排列:'中间',
风格:假,
格式化程序:函数(){
var输出=“”;
输出+=''+此.y+'';
输出+='';
返回输出;
},
useHTML:true
}
},
馅饼:{
数据标签:false,
动画:错,
startAngle:settings.gaugeStartAngle,
endAngle:settings.gaugeEndAngle,
中心:['50%,'50%'],
国家:{
悬停:{
已启用:false
}
}
}
},
//价值轴
亚克斯:{
偏移量:0,
最小值:设置值。最小值,
最大值:settings.gaugeMaxValue,
标题:假,
标签:假,
金额:0,
绘图带:[{
厚度:25,
外层:“100%”,
from:settings.gaugername值,
至:settings.gaugeStartValue,
标签:{}
}, {
厚度:25,
外层:“100%”,
from:settings.gaugeStartValue,
至:settings.gaugeMaxValue,
标签:{}
}]
},
系列:[{
类型:“仪表”,
数据:[设置.gaugeStartValue],
}, {
键入“pie”,
内部尺寸:“87%”,
类名:“比萨饼”,
数据:[{
y:settings.gaugeStartValue,
名称:“数据1”,
类名:“customSeries1”
}, {
y:settings.gaugeMaxValue-settings.gaugeStartValue,
名称:“数据2”,
类名:“customSeries2”
}]
}],
导航:{
按钮选项:{
已启用:false
}
},
学分:假
};
$(#gauge1')。高图(选项,构建图);
函数构建图(图表){
if(!chart.renderer.forExport){
setInterval(函数(){
var gaugePoint=图表.系列[0].点[0],
piePoint=图表系列[1],
yAxis=chart.yAxis[0],
纽瓦尔,
inc=数学圆((数学随机数()-0.5)*1500);
newVal=计量点y+inc;
如果(newValsettings.gaugeMaxValue){
newVal=gaugePoint.y-inc;
}
//更新数字仪表值
测量点更新(newVal);
//用当前值更新饼图
piePoint.points[0]。更新(newVal);
piePoint.points[1]。更新(settings.gaugeMaxValue-newVal);
yAxis.update({
绘图带:[{
厚度:25,
外层:“100%”,
from:settings.gaugername值,
致:纽瓦尔,
标签:{
文本:“文本1”,
y:15,
x:55,
轮调:290
}
}, {
厚度:25,
外层:“100%”,
发件人:纽瓦尔,
至:settings.gaugeMaxValue,
标签:{
文本:“文本2”,
y:chart.plotSizeY/2,
x:chart.plotSizeX*0.87,
轮调:290
}
}]
});
},设置。仪表更新间隔);
}
}
函数CalculateGaugehight(){
var div=$(“.gauge”);
分区高度(分区宽度());
}
});
/**
*@license Highcharts
*
*(c)2009-2016托尔斯坦荣誉
*
*许可证:www.highcharts.com/License
*/
.海图集装箱{
位置:相对位置;
溢出:隐藏;
宽度:100%;
身高:100%;
文本对齐:左对齐;
线高:正常;
z指数:0;
/* #1072 */
-webkit点击突出显示颜色:透明;
字体系列:“Lucida Grande”、“Lucida Sans Unicode”、Arial、Helvetica、Sans serif;
字体大小:12px;
}
.highcharts根文本{
笔画宽度:0;
}
.海图强劲{
字体大小:粗体;
}
.海图强调{
字体:斜体;
}
.海图背景{
填充:#ffffff;
}
.高图绘制边界,
.highcharts绘图背景{
填充:无;
}
.海图标签盒{
填充:无;
}
.海图按钮盒{
填充:继承;
}
/*头衔*/
.海图标题{
填充:#333333;
字号:1.5em;
}
.海图字幕{
填充:#666666;
}
/*斧头*/
.海图轴线{
填充:无;
冲程:#ccd6eb;
}
.海图yaxis.海图轴线{
笔画宽度:0;
}
.高轴标题{
填充:#666666;
}
.高轴标签{
填充:#666666;
游标:默认值;
字号:0.9em;
}
.海图网格线{
填充:无;
冲程:#e6;
}
.海图xaxis网格.海图网格线{
笔画宽度:0;
}
.高位图表滴答作响{
冲程:#ccd6eb;
}
.高度图表yaxis.高度图表勾选{
笔画宽度:0;
}
.highcharts次要网格线{
行程:#f2f2;
}
.高分十字线细{
笔画宽度:1px;
行程:#中交;
}
.高图表十字线类别{
冲程:#ccd6eb;
笔划不透明度:0.25;
}
/*学分*/
hig先生
function renderText(textStr, plotBand, i) {
  const id = `plot-band-${i}`
  const chart = plotBand.axis.chart
  const path = plotBand.svgElem
  const textRendered = chart.textRendered
  path.attr('id', id)

  if (!textRendered[i]) {
    const text = chart.renderer.createElement('text')
      .attr({
        zIndex: 99,
        dy: 20,
       'text-anchor': 'middle'
      })
      .css({
        color: '#4572A7',
        fontSize: '16px'
      }).add()


    const textPath = chart.renderer.createElement('textPath').attr({
      startOffset: '25%'
    }).add(text)

    textPath.element.setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#' + id)

    const textNode = document.createTextNode(textStr)
    textPath.element.appendChild(textNode)
    textRendered[i] = true
  }
}
chart: {
  type: 'gauge',
  events: {
    load: function() {
      this.textRendered = []
    }
  }
},
yAxis.plotLinesAndBands.forEach((plotBand, i) => {
  renderText('Custom Text', plotBand, i)
})
function calculateStartOffset(wrapper) {
  const len = wrapper.element.getTotalLength()

  const tempPath = document.createElementNS('http://www.w3.org/2000/svg', 'path')
  tempPath.setAttribute('d', wrapper.d.replace(/\sL.*/, '')) // upper line
  const len2 = tempPath.getTotalLength()

  return Math.round(len2 / 2 / len * 100)    
}