简体   繁体   English

HighCharts:水平条形图的对数标度

[英]HighCharts: Logarithmic Scale for Horizontal Bar Charts

I am working with HighCharts to produce a bar chart. 我正在与HighCharts合作制作条形图。 My values can range from as minimal as 0 to as high as 100k (example). 我的值可以从最小0到最高100k(示例)。 Therefore, one bar of the graph can be very small and the other can be very long. 因此,图形的一条可以非常小而另一条可以非常长。 HighCharts has introduced the feature of "Logarithmic Scaling". HighCharts引入了“对数缩放”功能。 The example of which can be seen HERE 这里的例子可以在这里看到

My js code is written in this jsfiddle file. 我的js代码写在这个jsfiddle文件中。 I want to display my horizontal axis (x-Axis) logarithmically. 我想以对数方式显示我的水平轴(x轴)。 I have inserted the key type as shown in the example but the script goes into an infinite loop which has to be stopped. 我插入了键类型 ,如示例所示,但脚本进入无限循环,必须停止。

What is the flaw in the execution or is logarithmic scaling for HighCharts still not mature? 执行中的缺陷是什么,或者HighCharts的对数缩放仍然不成熟?

PS The commented line in jsfiddle is causing the issue PS jsfiddle中的注释行引起了问题

Since the "official" method is still buggy, you can achieve the log scale more manually by manipulating your input data with a base 10 log and masking your output data raising 10 to the output value. 由于“官方”方法仍然存在问题,因此您可以通过使用基本10日志操作输入数据并将输出数据屏蔽为输出值10来更加手动地实现日志比例。 See it in action here http://jsfiddle.net/7J6sc/ code below. 请参阅下面的http://jsfiddle.net/7J6sc/代码。

function log10(n) {
 return Math.log(n)/Math.log(10);   
}

chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        defaultSeriesType: 'bar',
        marginRight: 200,
        marginLeft: 10,
    },
    title: {
        text: 'Negative'
    },
    xAxis: {
        categories: [''],
        title: {
            text: null
        }
    },
    yAxis: {
        min: 0,
        title: {
            text: '',
            align: 'high',
        },
        labels: {
            formatter: function() {
             return Math.round(Math.pow(10,this.value));
            }
        }
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'top',
        x: -50,
        y: 100,
        floating: true,
        borderWidth: 1,
        shadow: true
    },
    tooltip: {
        formatter: function() {
            return '' + this.series.name + ': ' + Math.round(Math.pow(10,this.y)) + ' millions';
        }
    },
    plotOptions: {
        bar: {
            dataLabels: {
                enabled: true,
                formatter: function() {
                   return Math.round(Math.pow(10,this.y)); 
                }
            }
        }
    },
    credits: {
        enabled: false
    },
    series: [{
        "data": [log10(4396)],
        "name": "A"},
    {
        "data": [log10(4940)],
        "name": "B"},
    {
        "data": [log10(4440)],
        "name": "C"},
    {
        "data": [log10(2700)],
        "name": "D"},
    {
        "data": [log10(2400)],
        "name": "E"},
    {
        "data": [log10(6000)],
        "name": "F"},
    {
        "data": [log10(3000)],
        "name": "G"},
    {
        "data": [log10(15000)],
        "name": "E"}],

});

For those of you who are still looking for an answer : 对于那些仍在寻找答案的人:

JSFiddle : http://jsfiddle.net/TuKWT/76/ JSFiddle: http//jsfiddle.net/TuKWT/76/

Or SO snippet : 或者SO片段:

 chart = new Highcharts.Chart({ chart: { renderTo: 'container', defaultSeriesType: 'bar' }, title: { text: 'Negative' }, xAxis: { categories: ['A','B','C','D','E','F','G','H'], title: { text: null } }, yAxis: { type: 'logarithmic', //min: 0, <= THIS WILL CAUSE ISSUE title: { text: null, } }, legend: { enabled: false }, tooltip: { formatter: function() { return this.x + ':' + this.y + ' millions'; } }, plotOptions: { bar: { dataLabels: { enabled: false } } }, credits: { enabled: false }, series: [{ "data": [4396,4940,4440,2700,2400,6000,3000,15000], }], }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <div id="container" style="height: 300px"></div> 

It is still experimental according to the Official Documentation , so that might be the case: 根据官方文档 ,它仍然是实验性的,因此情况可能如此:

"The type of axis. Can be one of "linear" or "datetime". In a datetime axis, the numbers are given in milliseconds, and tick marks are placed on appropriate values like full hours or days. “轴的类型。可以是”线性“或”日期时间“之一。在日期时间轴中,数字以毫秒为单位,刻度标记放在适当的值上,如完整小时或天。

As of 2.1.6, "logarithmic" is added as an experimental feature, but it is not yet fully implemented. 从2.1.6开始,添加“对数”作为实验特征,但尚未完全实现。 Defaults to "linear". 默认为“线性”。

Try it: "linear", "datetime" with regular intervals, "datetime" with irregular intervals, experimental "logarithmic" axis." 试一试:“线性”,“日期时间”,定期间隔,“日期时间”,不规则间隔,实验“对数”轴。“

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

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