Highcharts 问题 - 在可缩放图表中显示标签

发布于 2024-11-03 18:16:46 字数 390 浏览 1 评论 0原文

我有一个缩放柱形图,xAxis 中有 200 多个类别。因此,当它处于初始状态(比例1:1)时,所有这些家伙都显示在X轴下方,即使我将它们垂直放置,也无法读取任何内容。我需要缩放图表以使标签可见。

这是问题的屏幕截图: 在此处输入图像描述

这是我的代码: http://jsfiddle.net/sherlock85/hJcQm/

是否可以调节浓度标签(也许会自动更改步骤)取决于缩放级别?

我非常感谢你的帮助。

谢谢, 安杰伊

I have a zoom column chart with more then 200 categories in xAxis. Consequently, when it is in the initial state (scale 1:1), all these guys are shown under the X axis, and it's impossible to read anything even if I place them vertically. I need to zoom the chart to make the labels visible.

Here's screenshot of the problem:
enter image description here

Here's my code:
http://jsfiddle.net/sherlock85/hJcQm/

Is it possible to adjust the concentration of the labels (perhaps change the step automatically) depending on a zoom level?

I would really appreciate your help.

Thanks,
Andrzej

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的那么颓废 2024-11-10 18:16:46

按照从最简单到最困难的顺序...

由于您的类别主要是数字(例如“mir001”),因此您可以省略类别,这将阻止它们全部显示。然后他们会显示 1、2、3 等等。

或者,您可以删除类别并使用标签格式化程序。在这种情况下,您可以执行类似的操作,再次利用标签看起来是数字并递增的事实:

chart = new Highcharts.Chart({
    xaxis: {
        labels: {
            formatter: function() {
                return "mir" + this.value;
            }
        }
    }
});

以上两种方法都会减少标签的数量(标签将遵循某种模式,例如 1、50、 100,直到缩放)。

如果您想对标签非常挑剔,则必须使用标签格式化程序并进行一些数学运算来找到图形的比例,以确定标签的显示方式。例如,您可以做这样的事情,尽管它非常复杂:

var categories = [] //have your categories as a separate list
var buckets = 4; 
var labelFormatter = function(frequency) {
    var count = 0;
    frequency = frequency || 0;
    return function () {
        count++;
        if ((count > frequency) || (frequency < 1)) {
                return this.value
        }
    }
};

chart = new Highcharts.Chart({
    zoomtype: 'x',
    events: {
        selection: function(event) {
            if (event.xAxis) {
                var scale = event.xAxis[0];
                var frequency = Math.floor((scale.max - scale.min) / buckets)
                this.xaxis[0].labels.formatter = labelFormatter(frequency);
            }
        }
    },
    xaxis: {
        labels: {
            formatter: labelFormatter(Math.floor(categories.length/buckets))
        }
    }
});

我应该指出最后一个解决方案并不完全有效,进一步说明了它有多么困难。

In order from easiest to most difficult...

Since your categories are mostly numeric (e.g. 'mir001'), you could omit the categories, which would prevent them from all being displayed. They would then show 1, 2, 3, and so on.

Alternatively, you could remove the categories and use a label formatter. In this case, you could do something like this, again taking advantage of the fact that the labels appear to be numeric and increasing:

chart = new Highcharts.Chart({
    xaxis: {
        labels: {
            formatter: function() {
                return "mir" + this.value;
            }
        }
    }
});

Both of the above would reduce the number of labels (the labels would follow some pattern such as 1, 50, 100, until zoomed).

If you wanted to be very particular about the labels, you would have to use the label formatter and do some math to find the scale of the graph to determine how the labels should be displayed. You could, for example, do something like this, though it is quite convoluted:

var categories = [] //have your categories as a separate list
var buckets = 4; 
var labelFormatter = function(frequency) {
    var count = 0;
    frequency = frequency || 0;
    return function () {
        count++;
        if ((count > frequency) || (frequency < 1)) {
                return this.value
        }
    }
};

chart = new Highcharts.Chart({
    zoomtype: 'x',
    events: {
        selection: function(event) {
            if (event.xAxis) {
                var scale = event.xAxis[0];
                var frequency = Math.floor((scale.max - scale.min) / buckets)
                this.xaxis[0].labels.formatter = labelFormatter(frequency);
            }
        }
    },
    xaxis: {
        labels: {
            formatter: labelFormatter(Math.floor(categories.length/buckets))
        }
    }
});

I should point out that the last solution doesn't exactly work, further illustrating how difficult it is.

Hello爱情风 2024-11-10 18:16:46

我的解决方案:

xAxis:
{
    categories: [ <!-- your categories -->],
    tickInterval: 5,        // Cada 5 valores muestra valor (five jump interval)
    labels:
    {
        rotation: -35,
        style:
        {
            font: 'normal 12px Verdana, sans-serif'
        }
    }
},

My solution:

xAxis:
{
    categories: [ <!-- your categories -->],
    tickInterval: 5,        // Cada 5 valores muestra valor (five jump interval)
    labels:
    {
        rotation: -35,
        style:
        {
            font: 'normal 12px Verdana, sans-serif'
        }
    }
},
烟柳画桥 2024-11-10 18:16:46

对于最初的问题来说可能有点太晚了,但我正在使用 Highcharts 开发一些东西,并且我正在寻找正是这个目的。

以下是我的做法,以供将来参考:

xAxis: {
    labels: {
        step: 5
    },
    events: {
        setExtremes: function(event) {
            if (event.max) {
                var frequency = Math.floor((event.max - event.min) / 17);
                this.options.labels.step = frequency;
            } else {
                this.options.labels.step = 5;
            }
        }
    }
},

它基本上检查我们是否正在缩放 - if (event.max) - 或重置。 17 是我在缩小时得到的标签数量,标签步长设置为 5,并且使用这个特定的数据集(我很确定这一切都可以更加完善 - 但它有效)。

Probably a bit too late for the original question, but I'm developing something with Highcharts and I was looking for exactly for this.

Here's how I did it, for future reference:

xAxis: {
    labels: {
        step: 5
    },
    events: {
        setExtremes: function(event) {
            if (event.max) {
                var frequency = Math.floor((event.max - event.min) / 17);
                this.options.labels.step = frequency;
            } else {
                this.options.labels.step = 5;
            }
        }
    }
},

It basically checks if we're zooming - if (event.max) - or resetting. 17 is the number of labels I get when zoomed out, with labels step set to 5 and with this particular data set (I'm pretty sure this could all be more polished - but it works).

娇女薄笑 2024-11-10 18:16:46

这就是我解决这个问题的方法:

xAxis: {
   tickPositioner: function () {
      var positions = [],
          min = Math.floor(this.min),
          max = Math.ceil(this.max),
          tick = min,
          increment = Math.ceil((max - min) / 30);

      for (; tick < max; tick += increment) {
         positions.push(tick);
      }
      positions.push(max);
      return positions;
   }
}

在重绘时调用tickPositioner。最小值和最大值包含最左边和最右边(x 轴)的包含值。

This is I how solved this problem:

xAxis: {
   tickPositioner: function () {
      var positions = [],
          min = Math.floor(this.min),
          max = Math.ceil(this.max),
          tick = min,
          increment = Math.ceil((max - min) / 30);

      for (; tick < max; tick += increment) {
         positions.push(tick);
      }
      positions.push(max);
      return positions;
   }
}

The tickPositioner get called on a redraw. The min and max contain the left and right most (x-axis) inclusive values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文