英文:
How to set up fixed spacing / bar width for bar chart
问题
我有一个带有动态系列数量的水平柱状图。我们已经设置了此图表,以一次显示最多8个柱子,并带有分页功能,当至少有8个柱子时,这个功能效果非常好。
但是,当要显示的柱子少于8个(或者如果您在最后一页且那里的柱子少于8个时),图表的显示就不好。例如,如果有两个柱子,它们会将柱子分开得很远:
我们的设计团队希望实现以下效果:
a) 所有柱子都精确地高度为8px(柱子不应比这更宽或更窄,但应始终保持这个宽度)
b) 所有柱子应该间隔24px。
c) 如果没有足够的柱子来填满整个空间,它们应该对齐到图表的顶部,并在下面留下空白。柱子之间始终应保持24像素的间隔。
实质上,一个好的图表应该是这个样子的:
我不需要Highcharts担心柱子是否会适应图表中。我已经控制了分页,并确保柱子能够适应提供的空间。我只想要固定的柱高和固定的柱间距。
我的问题是 - 使用Highcharts是否可能实现目标设计,还是我们需要寻找另一个库来完成这个任务?不得不在另一个框架中创建这个图表将会很不幸,但我们不能保留它,有两个巨大的柱子或两个微小的柱子之间有很大的间隙,这看起来不好。
我已经尝试使用pointPadding、groupPadding(我认为应该处理柱子之间的间距)以及以不同的组合设置pointWidth,还动态根据柱子数量设置图表的高度。这些选项都没有给我想要的效果:
-
通过设置pointWidth,您可以固定柱子的宽度,但然后无法控制它们之间的距离。
-
设置groupPadding,老实说,我从未能够得到正确的填充距离,即使未设置pointWidth。最好的情况下,我能够使柱子相隔约32px,仍然过大,高度会变得过大(有时高达50px)。
-
动态设置图表的高度,再次无法使组之间的填充小于32px。如果我使图表的高度小于此值,柱子将简单地溢出,而不会进一步减少其填充。更改groupPadding/pointPadding对此没有影响。
英文:
I have a horizontal bar chart with a dynamic number of series. We have set up this chart to display up to 8 bars at a time with pagination, which is working great when you have at least 8 bars.
However, when there are less than 8 bars to display (or if you're on the last page and there's less than 8 bars there), the display of the chart is just not good. For example, if there are two bars, it spreads the bars very far apart from each other:
Our design team wants to have:
a) All bars precisely 8px tall (bars should not get wider or narrower than this, but should always be exactly this width)
b) All bars should be spaced 24px apart.
c) If there aren't enough bars to fill the whole space, they should align to the top of the chart and leave whitespace below. The bars should always remain 24 px apart.
In essence, this is what a good chart would look like:
I don't need Highcharts to worry about whether the bars will fit in the chart or not. I'm already controlling for pagination and ensuring the bars will fit in the space provided. I just want a fixed bar height and fixed bar padding.
My question is - is it even possible to achieve the target design with highcharts, or do we need to look for another library for this? It would be unfortunate to have to do this one chart in a separate framework, but we can't leave it as is with two giant bars or two tiny bars with a big gap in between them, it just doesn't look good.
I've tried using the options for pointPadding, groupPadding (which I thought should handle the spacing between the bars, and pointWidth in various combinations, along with dynamically setting the height of the chart based on the # of bars. None of these options gave me what I was looking for:
-
By setting pointWidth, you can fix the width of the bars but then you can't control the distance between them
-
Setting groupPadding, honestly, was never able to get me the right padding distance, even with pointWidth unset. At best I was able to get the bars about 32px apart, still 33% too far. And with this, the width of the bars would become overly large (sometimes as much as 50px tall).
-
Setting the height of the chart dynamically, again I was unable to get the padding between the groups less than about 32px. If I made the chart shorter than this, the bars would simply overflow without reducing their padding further. Changing groupPadding / pointPadding had no impact on this.
答案1
得分: 2
找到了解决方法 - 还有另一个属性,chart.spacingTop / spacingBottom,似乎对柱子之间的间距有更直接的影响。您可以与pointWidth一起调整它,以实现所需的效果:
<!-- 开始代码段: js 隐藏: false 控制台: true 巴别: false -->
<!-- 语言: lang-js -->
chart: {
...
spacingTop: -15,
spacingBottom: (8 - (series[0]?.data?.length ?? 0)) * 55,
},
...
plotOptions: {
bar: {
...
pointWidth: 8,
<!-- 结束代码段 -->
从这个问题得到了灵感:https://stackoverflow.com/questions/26301028/highcharts-stacked-bar-how-to-remove-spacing-between-bars?rq=2
英文:
Figured this out - there's another property, chart.spacingTop / spacingBottom, which seems to have a much more direct impact on how much space goes between the bars. You can adjust this together with the pointWidth to achieve the desired effect:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
chart: {
...
spacingTop: -15,
spacingBottom: (8 - (series[0]?.data?.length ?? 0)) * 55,
},
...
plotOptions: {
bar: {
...
pointWidth: 8,
<!-- end snippet -->
Got the idea from this question: https://stackoverflow.com/questions/26301028/highcharts-stacked-bar-how-to-remove-spacing-between-bars?rq=2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论