如何向轴添加标签

时间:2016-11-04 08:57:38

标签: javascript highcharts legend

我的图表与此示例非常相似:http://jsfiddle.net/MrFox1/n6vwqafg/

<script src="https://code.highcharts.com/maps/highmaps.js"></script>
<script src="https://code.highcharts.com/maps/modules/data.js"></script>
<script src="https://code.highcharts.com/mapdata/countries/us/us-all.js"></script>

<div id="container" style="height: 500px; min-width: 410px; max-width: 600px; margin: 0
auto">
</div>

$(function () {

    $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=us-population-density.json&callback=?', function (data) {

        // Make codes uppercase to match the map data
        $.each(data, function () {
            this.code = this.code.toUpperCase();
        });

        // Instanciate the map
        Highcharts.mapChart('container', {

            chart: {
                borderWidth: 1
            },

            title: {
                text: 'US population density (/km²)'
            },

            legend: {
                layout: 'vertical',
                borderWidth: 0,
                backgroundColor: 'rgba(255,255,255,0.85)',
                floating: true,
                verticalAlign: 'middle',
                align: 'right',
                y: 25
            },

            mapNavigation: {
                enabled: true
            },

            colorAxis: {
                min: 1,
                type: 'logarithmic',
                minColor: '#EEEEFF',
                maxColor: '#000022',
                stops: [
                    [0, '#EFEFFF'],
                    [0.67, '#4444FF'],
                    [1, '#000022']
                ]
            },

            series: [{
                animation: {
                    duration: 1000
                },
                data: data,
                mapData: Highcharts.maps['countries/us/us-all'],
                joinBy: ['postal-code', 'code'],
                dataLabels: {
                    enabled: true,
                    color: '#FFFFFF',
                    format: '{point.code}'
                },
                name: 'Population density',
                tooltip: {
                    pointFormat: '{point.code}: {point.value}/km²'
                }
            }]
        });
    });
});

我需要在轴上添加一个标签,显示所有数据的平均值。 理想情况下,标记符号位于图例中。将鼠标悬停在地图上时,轴上显示的标记相同,表示您指向的颜色与图例的关系。

已经计算了所有数字的平均值,它只是关于如何在视觉上显示它。

1 个答案:

答案 0 :(得分:0)

您可以通过colorAxis.tickPositions(数组)或colorAxis.tickPositioner(回调)在颜色轴中设置刻度线位置。它将为指定值创建一个tick。如果使用对数轴,那么这些值需要正确缩放。

colorAxis: {

  tickPositions: [Math.log10(1), Math.log10(10), Math.log10(calculatedAverage), Math.log10(100), Math.log10(1000)], 

然后使用colorAxis.labels.formatter定义特定标记的文本。

labels: {
            formatter: function () {
              return this.value == calculatedAverage 
                ? 'avg (' + this.value + ')'
                : this.value;
              }
            }

示例:http://jsfiddle.net/n6vwqafg/9/