javascript - 处理具有很长类别名称的 highcharts 条形图

标签 javascript graph charts highcharts

我有一个用 Highcharts 构建的条形图,它使用类别作为其 x 轴——非常冗长的类别。 我想不出一种确保类别始终保持在一行上的好方法。我不能缩写它们,除非我可以使用工具提示或其他东西在鼠标悬停或其他一些直观的用户交互时显示长版本。当类别换行时,它开始看起来像一堵文字墙。

对于以干净的方式显示长类别和数据有什么想法?我愿意考虑使用不同类型的图表,只要它能以清晰美观的方式显示数据。我想坚持使用 Highcharts,但前提是可能。

编辑: 经过多方努力,我放弃了以跨浏览器 (IE6+) 的方式向 x 轴类别标签添加工具提示的想法。即使使用 JQuery,它似乎也不可能或不实用。我仍在寻找任何可以让我很好地显示这些长类别的解决方案(我对我之前创建的 fiddle 不满意,因为悬停在数据栏上对用户来说不够明显)。

问题图的图片,类别被涂黑了: Labels too long, going to next line

JSFiddle 代码:

HTML:

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

Javascript:

$(function() {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'bar'
            },
            title: {
                text: 'Historic World Population by Region'
            },
            subtitle: {
                text: 'Source: Wikipedia.org'
            },
            xAxis: {
                categories: ['Africa blahblahblah blah blah blah ', 'America blahblahblah blah blah blah ', 'Asia blahblahblah blah blah blah', 'Europe blahblahblah blah blah blah ', 'Oceania blahblahblah blah blah blah '],
                    title: {
                        text: null
                    },
                    labels: {
                        formatter: function() {
                            return(this.value.substring(0,10) + "...");
                        }
                    }                            
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: 'Population (millions)',
                        align: 'high'
                    },
                    labels: {
                        overflow: 'justify'
                    }
                },
                tooltip: {
                    formatter: function() { 
                       $("#mytoolTip").html(this.x + 'and the value is ' + this.y) ; 
                       return false ; 
                    }
                },
                plotOptions: {
                    bar: {
                        dataLabels: {
                            enabled: true
                        }
                    }
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'top',
                    x: -100,
                    y: 100,
                    floating: true,
                    borderWidth: 1,
                    backgroundColor: '#FFFFFF',
                    shadow: true
                },
                credits: {
                    enabled: false
                },
                series: [{
                    name: 'Year 1800',
                    data: [107, 31, 635, 203, 2]
                }, {
                    name: 'Year 1900',
                    data: [133, 156, 947, 408, 6]
                }, {
                    name: 'Year 2008',
                    data: [973, 914, 4054, 732, 34]
                }]
            });
        });
    });

最佳答案

试试这个 fiddle : http://jsfiddle.net/a6zsn/70/

我们有一个类似的问题,我们最终通过允许标签使用 HTML 解决了这个问题。虽然我们没有采用下面发布的确切解决方案,但这应该适合您,因为它使用 jQueryUI 工具提示小部件在悬停时显示完整文本。

请注意我是如何定义 xAxis.labels 对象的。

$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'bar',
                events: {
                    load: function (event) {
                        $('.js-ellipse').tooltip();
                    }
                }
            },
            title: {
                text: 'Historic World Population by Region'
            },
            subtitle: {
                text: 'Source: Wikipedia.org'
            },
            xAxis: {
                categories: ['Africa is the best place to do safari.  Label is soooo big that it iss ugly now.  =(.  -38023-8412-4812-4812-403-8523-52309583409853409530495340985 ', 'America is the best place you can ever live in ', 'Asia is the best food ever ', 'Europe best chicks ever on earth ', 'Oceania i dont know any thing about this place '],
                title: {
                    text: null
                },
                labels: {
                    formatter: function () {
                        var text = this.value,
                            formatted = text.length > 25 ? text.substring(0, 25) + '...' : text;

                        return '<div class="js-ellipse" style="width:150px; overflow:hidden" title="' + text + '">' + formatted + '</div>';
                    },
                    style: {
                        width: '150px'
                    },
                    useHTML: true
            }
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Population (millions)',
                    align: 'high'
                },
                labels: {
                    overflow: 'justify'
                }
            },
            tooltip: {
                formatter: function() {
                   $("#mytoolTip").html(this.x + 'and the value is ' + this.y) ; 
                    return false ;
                }
            },
            plotOptions: {
                bar: {
                    dataLabels: {
                        enabled: true
                    }
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -100,
                y: 100,
                floating: true,
                borderWidth: 1,
                backgroundColor: '#FFFFFF',
                shadow: true
            },
            credits: {
                enabled: false
            },
            series: [{
                name: 'Year 1800',
                data: [107, 31, 635, 203, 2]
            }, {
                name: 'Year 1900',
                data: [133, 156, 947, 408, 6]
            }, {
                name: 'Year 2008',
                data: [973, 914, 4054, 732, 34]
            }]
        });
    });

});

关于javascript - 处理具有很长类别名称的 highcharts 条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11543357/

相关文章:

javascript - 如何从 jQuery 延迟传递参数

javascript - 如何获取当前 map 坐标?

javascript - 获取 aria 扩展值

javascript - 带有 html5 Canvas 的心电图

java - 使用neo4j spring数据从一个sql表建立关系

javascript - 在图表 js 中使标签响应

java - GWT &lt;script&gt; 标记未正确加载

python - 在一条边上连接两个 networkx 图

javascript - 更改 Chart.js 工具提示插入符号位置

javascript - 如何在D3条形图中添加分隔符