简体   繁体   English

Highcharts 在柱形图上将 yaxis 值置于零

[英]Highcharts center yaxis value at zero on column chart

I want to center YAxis value at zero on Highcharts Column chart with positive & negative values like this: https://drive.google.com/open?id=0B_IZwAPijvleZzBhUnVaSFhBcDQ , these axis values are dynamic loaded.我想在 Highcharts 柱形图上将 YAxis 值以零为中心,正负值如下所示: https ://drive.google.com/open?id=0B_IZwAPijvleZzBhUnVaSFhBcDQ,这些轴值是动态加载的。 With jqPlot there is an option called forceTickAt0 at axis settings that can meet my requirements, I'd like to know if this is possible with Highcharts, if yes how to do?使用 jqPlot 时,轴设置中有一个名为 forceTickAt0 的选项可以满足我的要求,我想知道 Highcharts 是否可以实现,如果可以怎么办? Thanks.谢谢。

Update: Ok, in order to clearly explain my question, I've created a jsFiddle .更新:好的,为了清楚地解释我的问题,我创建了一个jsFiddle As you see on the chart, the max/min series value at y-axis is 5001/-4280, but Highcharts shows 15000/-5000 at y-axis boundary and I expect it to be 6000/-5000, and if you remove the first value 5001 from y-axis series, Highcharts will show 5000/-5000 at y-axis boundary that's what I expected.正如您在图表上看到的,y 轴的最大/最小系列值为 5001/-4280,但 Highcharts 在 y 轴边界显示 15000/-5000,我预计它是 6000/-5000,如果您删除y 轴系列的第一个值 5001,Highcharts 将在 y 轴边界显示 5000/-5000,这正是我所期望的。 So add a 5001 value at y-axis series causes the y-axis boundary becomes too far away.所以在 y 轴系列上添加 5001 值会导致 y 轴边界变得太远。

The key point is I don't want the biggest/smallest series value at y-axis got too much far away from chart max/min boundary value.关键是我不希望 y 轴上的最大/最小系列值离图表最大/最小边界值太远。

Following is the code, for explanation purpose, series data here use fixed values, In a real situation they are all dynamic loaded.下面是代码,为了便于说明,这里的系列数据使用固定值,在实际情况下它们都是动态加载的。

$(function() {
   $('#container').highcharts({
     chart: {
       zoomType: 'xy',
       plotBackgroundColor: '#fffdf6',
       alignTicks: true,
     },
     title: {
       text: 'Country Illegally Building Num Average Tax',
     },
     subtitle: {
       text: ''
     },
     xAxis: [{
       categories: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'],
       crosshair: true,
       title: {
         text: 'Country Code',
       }
     }],
     yAxis: [{ // Primary yAxis
       labels: {
         format: '{value} ',
       },
       title: {
         text: 'Illegally Building Num',
       },

     }, { // Secondary yAxis
       min: 0, //Required to start at zero
       title: {
         text: 'Average Tax ($)',
         style: {
           color: '#666'
         }
       },
       labels: {
         format: '{value} $',
         style: {
           color: '#666'
         }
       },
       opposite: true
     }],
     tooltip: {
       shared: true
     },
     legend: {
       enabled: false
     },
     series: [{
       color: 'red',
       negativeColor: 'green',
       name: 'Illegally Building Num',
       type: 'column',
       data: [5001, 369, 475, 891, 3585, 4235, -1711, -3648, -3749, -2555, -4280, -1017, 2001, -900],
       tooltip: {
         valueSuffix: ' '
       }
     }, {
       name: 'Average Tax',
       type: 'line',
       data: [140.5, 141, 139.5, 162, 151, 142, 147, 151, 154, 142, 146, 149, 153, 111.5],
       yAxis: 1,
       tooltip: {
         valueSuffix: ' $'
       }
     }]
   });
});

I don't believe there is a forceiTickAt0, but this functionality can easily be accomplished by setting the min/max of the y-axis.我不相信有 forceiTickAt0,但是可以通过设置 y 轴的最小值/最大值轻松实现此功能。 For example:例如:

yAxis: {
            min:-20,
            max:20,
            title: {
                text: 'Axis Title'
            },
            labels: {
                formatter: function () {
                    return this.value + '°';
                }
            },
            lineWidth: 2
        },

This would have a center point at 0, and depending on your data you can dynamically set the max to be the absolute value of the min.这将有一个中心点为 0,根据您的数据,您可以将最大值动态设置为最小值的绝对值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM