将文本居中对齐在HighCharts蜘蛛网图(SpiderWeb)x轴类别中应如何处理?

huangapple go评论67阅读模式
英文:

How do I align text center in HighCharts spiderWeb xAxis Categories?

问题

这是我的图表图像

将文本居中对齐在HighCharts蜘蛛网图(SpiderWeb)x轴类别中应如何处理?

我尝试使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>&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
            }
        }
    ],
}
英文:

This is my chart image

将文本居中对齐在HighCharts蜘蛛网图(SpiderWeb)x轴类别中应如何处理?

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: &#39;container&#39;,
polar: true,
type: &#39;area&#39;,
style: {
fontFamily: &#39;SUIT&#39;
}
},
xAxis: {
categories: [
&#39;&lt;span style=&quot;text-align: center&quot;&gt;&lt;span style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[0] +
&#39;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;font-size:20px;color:#002d84; font-weight:700;&quot;&gt;&#39; +
radarData[0] +
&#39;&lt;/span&gt;&lt;/span&gt;&#39;,
&#39;&lt;span style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[1] +
&#39;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;font-size:20px;color:#002d84; font-weight:700; text-align: center; width: 100%&quot;&gt;&#39; +
radarData[1] +
&#39;&lt;/span&gt;&#39;,
&#39;&lt;span style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[2] +
&#39;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;font-size:20px;color:#002d84; font-weight:700;&quot;&gt;&amp;nbsp&#39; +
radarData[2] +
&#39;&lt;/span&gt;&#39;,
&#39;&lt;span style=&quot;opacity:0;&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[3] +
&#39;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;font-size:20px;color:#002d84; font-weight:700;&quot;&gt;&#39; +
radarData[3] +
&#39;&lt;/span&gt;&#39;,
&#39;&lt;span style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[4] +
&#39;&lt;/span&gt;&amp;nbsp&lt;br&gt;&lt;span style=&quot;font-size:20px;color:#002d84; font-weight:700;&quot;&gt;&#39; +
radarData[4] +
&#39;&lt;/span&gt;&#39;,
&#39;&lt;span style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[5] +
&#39;&lt;/span&gt;&amp;nbsp&lt;br&gt;&lt;span style=&quot;font-size:20px;color:#002d84; font-weight:700;&quot;&gt;&#39; +
radarData[5] +
&#39;&lt;/span&gt;&#39;
],
tickmarkPlacement: &#39;on&#39;,
lineWidth: 0,
labels: {
distance: 30
},
series: [
{
enableMouseTracking: false,
data: radarData,
pointPlacement: &#39;on&#39;,
marker: {
radius: 1
}
}
],
},
};

答案1

得分: 2

正如您可以从文档和检查页面源代码中看到的那样,HTML代码被转换为SVG文本元素,特别是span被转换为tspan。有关在SVG中使用tspan对齐多行文本的解决方案,可以在这个 Stack Overflow 帖子 中找到。

Highcharts 足够智能,可以同时使用spantspan,但我更喜欢将正确的属性设置给正确的元素,所以我会使用tspan

   '&#39;&lt;tspan style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[1] +
'&#39;&lt;/tspan&gt;&lt;tspan x=&quot;0&quot; dy=&quot;21&quot; style=&quot;font-size:20px;color:#002d84; font-weight:700; text-align: center; width: 100%&quot;&gt;&#39; +
radarData[1] +
'&#39;&lt;/tspan&gt;&#39;,

请注意,第一个标签中缺少&lt;br&gt;和外部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:

   &#39;&lt;tspan style=&quot;color:#404040; font-size:16px; font-weight:500;&quot;&gt;&#39; +
radarDataTitle[1] +
&#39;&lt;/tspan&gt;&lt;tspan x=&quot;0&quot; dy=&quot;21&quot; style=&quot;font-size:20px;color:#002d84; font-weight:700; text-align: center; width: 100%&quot;&gt;&#39; +
radarData[1] +
&#39;&lt;/tspan&gt;&#39;,

Note the missing &lt;br&gt; 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 tspans, 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: &#39;area&#39;,
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.

huangapple
  • 本文由 发表于 2023年5月25日 08:35:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定