【问题标题】:How to reduce the space between Bars with a fixed bar width in Highcharts column chart?如何减少Highcharts柱形图中固定条宽的条之间的空间?
【发布时间】:2019-05-24 05:28:42
【问题描述】:

我有一个固定宽度和固定条宽的柱形图。当数据较少时,条形之间的空间会增加。我想减少空间,我不介意条形图是从图表的开头还是中间显示,但应该减小条形的空间。 10px 的空间看起来不错。任何帮助深表感谢。这是一个演示 -

Highcharts.chart('container', {
    chart: {
        type: 'column',
        width:1200
    },
    title: {
        text: 'Monthly Average Rainfall'
    },
    subtitle: {
        text: 'Source: WorldClimate.com'
    },
    xAxis: {
        categories: [
            'Jan',
            'Feb'
        ],
        crosshair: true
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Rainfall (mm)'
        }
    },
    tooltip: {
        headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
        pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
            '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
        footerFormat: '</table>',
        shared: true,
        useHTML: true
    },
    plotOptions: {
        column: {
            pointWidth:30
        }
    },
    series: [{
        name: 'Tokyo',
        data: [49.9, 71.5]

    }]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

【问题讨论】:

  • 如果你想有一个固定的列宽(30px)和它们之间的空间(10px),那么图表宽度必须根据点数来计算。否则,列以相等的间隔呈现,并且它们之间的空间是匹配的。

标签: javascript highcharts column-chart


【解决方案1】:

我唯一能想到的就是添加一堆假列来占用额外的空间。 Highcharts 似乎不是这样工作的。根据docs,使用pointWidthpointPadding,这取决于您提供哪一个,但不能同时使用两者。

Highcharts.chart('container', {
    chart: {
        type: 'column',
        width:1200
    },
    title: {
        text: 'Monthly Average Rainfall'
    },
    subtitle: {
        text: 'Source: WorldClimate.com'
    },
    xAxis: {
        categories: [
            'Jan',
            'Feb',
            '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''
        ],
        crosshair: true
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Rainfall (mm)'
        }
    },
    tooltip: {
        headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
        pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
            '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
        footerFormat: '</table>',
        shared: true,
        useHTML: true
    },
    plotOptions: {
        column: {
            pointWidth: 30,
        },
    },
    series: [{
        name: 'Tokyo',
        data: [49.9, 71.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    }]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

【讨论】:

  • 是的,我已经检查过了,但我必须将点宽度设置为 30,正如我所提到的。不过感谢您的建议。
  • 好的,用另一种可能的解决方案更新了我的答案
  • 感谢您的回答,但有没有其他方法不放那些虚拟数据?
  • 我看不到。我什至想不出是否有任何真正的用例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多