英文:
How do I align text center in HighCharts spiderWeb xAxis Categories?
问题
这是我的图表图像
我尝试使x轴类别文本居中对齐,所以我在某些方法中使用了样式text-align: center
,但都没有成功。例如,像BBBBBB和100这样的值没有居中对齐。那么有没有办法使它居中呢?
图表选项的代码如下:
{
chart: {
renderTo: 'container',
polar: true,
type: 'area',
style: {
fontFamily: 'SUIT'
}
},
xAxis: {
categories: [
'<span style="text-align: center"><span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[0] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[0] +
'</span></span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[1] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700; text-align: center; width: 100%;">' +
radarData[1] +
'</span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[2] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[2] +
'</span>',
'<span style="opacity:0;">2</span><br><span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[3] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[3] +
'</span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[4] +
'</span> <br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[4] +
'</span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[5] +
'</span> <br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[5] +
'</span>'
],
tickmarkPlacement: 'on',
lineWidth: 0,
labels: {
distance: 30
}
},
series: [
{
enableMouseTracking: false,
data: radarData,
pointPlacement: 'on',
marker: {
radius: 1
}
}
],
}
英文:
This is my chart image
I tried to make the xAxis Category text align, so I used style text align: center in some ways, but none of them went well. For example, values like BBBBBB and 100 are not aligned in the center. So is there a way to make it the center?
the chartoption code is like this
{
chart: {
renderTo: 'container',
polar: true,
type: 'area',
style: {
fontFamily: 'SUIT'
}
},
xAxis: {
categories: [
'<span style="text-align: center"><span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[0] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[0] +
'</span></span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[1] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700; text-align: center; width: 100%">' +
radarData[1] +
'</span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[2] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700;">&nbsp' +
radarData[2] +
'</span>',
'<span style="opacity:0;">2</span><br><span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[3] +
'</span><br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[3] +
'</span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[4] +
'</span>&nbsp<br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[4] +
'</span>',
'<span style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[5] +
'</span>&nbsp<br><span style="font-size:20px;color:#002d84; font-weight:700;">' +
radarData[5] +
'</span>'
],
tickmarkPlacement: 'on',
lineWidth: 0,
labels: {
distance: 30
},
series: [
{
enableMouseTracking: false,
data: radarData,
pointPlacement: 'on',
marker: {
radius: 1
}
}
],
},
};
答案1
得分: 2
正如您可以从文档和检查页面源代码中看到的那样,HTML代码被转换为SVG文本元素,特别是span
被转换为tspan
。有关在SVG中使用tspan
对齐多行文本的解决方案,可以在这个 Stack Overflow 帖子 中找到。
Highcharts 足够智能,可以同时使用span
或tspan
,但我更喜欢将正确的属性设置给正确的元素,所以我会使用tspan
:
''<tspan style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[1] +
''</tspan><tspan x="0" dy="21" style="font-size:20px;color:#002d84; font-weight:700; text-align: center; width: 100%">' +
radarData[1] +
''</tspan>',
请注意,第一个标签中缺少<br>
和外部span
。
在第一个tspan
的结束和第二个tspan
的开始之间不应有空格。
如果要将tspan
左移或右移,您应该使用dx
属性(请参阅我的示例中的CCCCCC案例),不要更改x
属性,因为Highcharts会将0或缺少的x更改为实际的绝对位置,但无法使用其他值进行计算。
这是一个jsFiddle示例。
英文:
As you can see from the docs and by inspecting the page source, html code is transformed to svg text elements, in particular span
is transformed to tspan
. A solution to align multiline text in svg with tspan
can be found in this SO thread.
Highcharts is smart enough to use both span
or tspan
, but I'm more comfortable with setting the right attributes to the right element, so I'd use tspan
:
'<tspan style="color:#404040; font-size:16px; font-weight:500;">' +
radarDataTitle[1] +
'</tspan><tspan x="0" dy="21" style="font-size:20px;color:#002d84; font-weight:700; text-align: center; width: 100%">' +
radarData[1] +
'</tspan>',
Note the missing <br>
and the missing outer span that you have in the first label only.
There should be no space between the closing first tspan
and the opening second tspan
.
If you want to move to left or right the tspan
s, you should use dx
attribute (see the CCCCCC case in my example), don't change the x
attribute since highcharts is changing 0 or missing x to the actual absolute position, but is not able to perform calculations using other values.
Here's a jsFiddle example.
答案2
得分: 1
请尝试一下这个吗?
这是更新后的代码。
chart: {
type: 'area',
margin: [50, 50, 50, 50], // 根据需要调整边距
spacingTop: 50, // 根据需要调整顶部间距
spacingBottom: 50, // 根据需要调整底部间距
spacingLeft: 50, // 根据需要调整左侧间距
spacingRight: 50 // 根据需要调整右侧间距
},
这是另一种方式,
我不知道这是否会起作用,但您可以尝试通过 chart.margin 和 chart.spacing 属性来调整图表的位置。
英文:
Can you please try this?
Here's an updated code.
chart: {
type: 'area',
margin: [50, 50, 50, 50], // Adjust the margins as needed
spacingTop: 50, // Adjust the top spacing as needed
spacingBottom: 50, // Adjust the bottom spacing as needed
spacingLeft: 50, // Adjust the left spacing as needed
spacingRight: 50 // Adjust the right spacing as needed
},
This is alternative ways,
I don't know if this will work, but you can try adjusting the chart's positioning using the chart.margin and chart.spacing properties.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论