如何在Highcharts中向图表添加文本

时间:2017-09-07 15:12:31

标签: jquery highcharts

我正在使用highcharts,我希望实现下面你可以看到的相同图形(负条)。你可以看到我的代码。如何在最后一个栏的侧面添加灰色文本? (浅绿色条)。我也想摆脱0%,不知怎的,我在第二个酒吧。提前谢谢。

$(function () {
    $('#emmisions2015_2050').highcharts({

        chart: {
            type: 'column'
        },

        title: {
            text: ''
        },

        xAxis: {
            opposite: true,
            categories: ['Transport', 'Purchased Electricity', 'Direct Emissions']
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.x + '</b><br/>' + this.series.name + ': ' + this.y + '<br/>' +
                    'Total: ' + this.point.stackTotal;
            }
        },

        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true,
                    rotation: 0,
                    align: 'right',
                    style: {
                        textOutline: false 
                    },
                     borderWidth: 1,
                     formatter: function() {
                       if (this.point.isInside == true) {
                         return '<span style="color: white">' + this.y + '%</span>';
                       } else {
                         return '<span style="color: black">' + this.y + '%</span>';
                       }
                     }
                }
            },
            column: {
                stacking: 'normal',
                format: '{point.x:.1f} Billion Euro'
            }
        },

        series: [{
            name: 'Emerging & Breakthrough',
            data: [0, 0, { y: -5, color: '#8cc640'}]
        },{
            name: 'Demand-side Flexibility',
            data: [0, 0, { y: -2, color: '#8cc640'}]
        },{
            name: 'Fuel Switch',
            data: [0, 0, { y: -8, color: '#8cc640'}]
        },{
            name: 'Energy Efficiency',
            data: [{ y: -4, color: '#51a332'},{ y: -11, color: '#26b6cc'},{ y: -7, color: '#8cc640'}]
        }]
    });
});
#emmisions2015_2050 .highcharts-legend{
    display: none;
}
.highcharts-button, .highcharts-axis, .highcharts-credits{
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div class="emisions" id="emmisions2015_2050"></div>

Así se debería ver

2 个答案:

答案 0 :(得分:1)

您可以删除0并为点设置x属性 - 它会删除不必要的标签。

name: 'Emerging & Breakthrough',
data: [{
  x: 2,
  y: -5,
  color: '#8cc640'
}]

您可以在右侧添加文字作为情节线标签,请参阅plotLines.label

你不应该通过CSS隐藏元素,而是使用Highcharts API,例如积分,传奇可以通过enabled: false属性禁用。

实例和输出

https://jsfiddle.net/bb83jw29/

plot lines

答案 1 :(得分:1)

您可以通过向图表添加相反的yAxis并创建一些函数来实现此效果,该函数定义标签在此轴上的外观以及它们将从何处获取名称。看看下面的代码:

var tickPositions = [-19, -16, -12, -4],   // Define here positions of labels (position point can't be equal than extreme points of axis)
    extremes = chart.yAxis[0].getExtremes(),
    counter = 0,

createTicks = function(ticks, axisExtremes) {
    var ticksArray = [axisExtremes.min, ...ticks, axisExtremes.max];
    return ticksArray;
    };

chart.yAxis[1].setExtremes(extremes.min ,extremes.max);

chart.yAxis[1].update({
    tickPositions: createTicks(tickPositions, extremes),
labels: {
    formatter: function() {

        if(this.value === extremes.min) {
            return;
        } else if(counter < tickPositions.length) {
            return this.chart.series[counter++].name;
        } else if(this.value === extremes.max) {
            counter = 0
            return;
        }

    },
    style: {
        "color": "#aaa",
        "font-weight": "bold"
    }
}

})

如果你想看看它是如何运作的,这里是JSFiddle