【问题标题】:How to format column chart data labels如何格式化柱形图数据标签
【发布时间】:2016-11-08 17:19:31
【问题描述】:

如何格式化如图所示的柱形图数据标签,只需要格式化第二条。

我尝试过的代码如下所示

[$('#voo-accepted-orders-graph').highcharts({
    chart: {
        type: 'column',
        backgroundColor: '#9a97de',
        minPadding: 0.08,
        maxPadding: 0.08,
    },
    title: {
        text: ''
    },
    legend: {
        enabled: false,
    },
    exporting: {
        enabled: false
    },
    credits: {
        enabled: false
    },
    xAxis: {
        tickWidth: 0,
        gridLineWidth: 0,
        gridLineColor: "#5c6bc0",
        categories: \['accepted', 'auto-accept', 'sent to PO', 'transfers'\],
        labels: {
            style: {
                color: '#ffffff',
                fontFamily: "Avenir LT std light",
                fontSize: "12px",
                zIndex: 1000
            }

        }
    },
    yAxis: {
        allowDecimals: false,
        title: {
            text: ''
        },
        gridLineWidth: 0,
        labels: {
            enabled: false
        },
        min: 0,

        tickInterval: 20,

    },
    tooltip: {
        pointFormat: '{series.name} <b>{point.y:,.0f}</b><br/>'

    },
    plotOptions: {
        column: {
            color: '#8381cc',
            borderColor: '#8381cc',
        },
        series: {
            borderWidth: 0,
            dataLabels: {
                enabled: true,
                zIndex: 10,
                color: "#fff",                
                style: {
                    textShadow: false,
                    fontWeight: "normal",
                }

            }
        }
    },

    series: \[{
        name: 'orders',
        data: \[200, 72, 36, 15\]

    }, {
        name: 'price',
        data: \[90, 150, 120, 50\]

    }\]
});][1]

还有 1. 只需要格式化第二条。 2.有没有可能得到垂直线。

【问题讨论】:

  • 能否将您提到的图片添加到您的问题中?它将帮助以后阅读此页面的人更好地理解您要解决的问题。谢谢!

标签: jquery highcharts


【解决方案1】:
/* bar bg */
.highcharts-series-1 rect:nth-child(1) {
  fill: #333;
}

/* text color above bar */
.highcharts-series-1 g:nth-child(1) text {
  fill: red !important;
}

/* x axis text colors */
.highcharts-xaxis-labels text:nth-child(1) {
  fill: green !important;
}
.highcharts-xaxis-labels text:nth-child(2) {
  fill: blue !important;
}
.highcharts-xaxis-labels text:nth-child(3) {
  fill: black !important;
}
.highcharts-xaxis-labels text:nth-child(4) {
  fill: pink !important;
}

CODEPEN

【讨论】:

  • 问题:如图所示,如何为起点到终点的x轴标签应用颜色?
【解决方案2】:

2016 年 7 月 8 日:我已更新此答案以解决原始发帖人关于在 x 轴标签后面添加背景的后续问题。 p>

如果您只想更改一列数据的标签,只需将更多属性添加到该系列中的特定点即可。

使用您问题中的代码,我更改了“价格”系列的第一点,使其标签为红色和粗体:

series: [{
    name: 'orders',
    data: [200, 72, 36, 15]

}, {
    name: 'price',
    data: [
        { // change the values for just this point
            y: 90, 
            dataLabels: { 
                color: 'red',
                style: {
                    fontWeight: 'bold'
                }
            } 
        },
        150, 120, 50]
}]

关于在 x 轴标签后面添加背景的后续问题,我参考了帖子 Highchart: Background color of Axis。这是我使用的代码:

首先,在定义图表选项之前,我添加了一个函数,用于在 x 轴标签后面绘制一个矩形。

  // the following code is derived from Mark's answer on:
  // https://stackoverflow.com/questions/20242302/highchart-background-color-of-axis
  // and using the setExtremes() function found in the Highcharts API documentation at:
  // http://api.highcharts.com/highcharts#Axis.getExtremes
  var rect = null;
  function drawRect(chart){
    if (rect){
      rect.element.remove();   
    }
    // this code draws a rectangle based on the chart's dimensions:
    // 1st attribute: left edge = 0
    // 2nd attribute: top edge = chart's height - (chart's bottom margin - 1) ... -1 to show axis baseline
    // 3rd attribute: width = chart's width
    // 4th attribute: height = chart's bottom margin
    // 5th attribute: corner radius = 0 ... no corners
    rect = chart.renderer.rect(0, chart.chartHeight - (chart.marginBottom-1), chart.chartWidth , chart.marginBottom, 0)
      .attr({
      'stroke-width': 0,
      stroke: '#888888',
      fill: '#888888',
      zIndex: 3
    })
      .add();
  }

接下来,我在您的图表选项中设置了一些事件:

chart: {
  type: 'column',
  backgroundColor: '#9a97de',
  minPadding: 0.08,
  maxPadding: 0.08,
  // events code from https://stackoverflow.com/questions/20242302/highchart-background-color-of-axis
  events: {
    load: function() {
      drawRect(this);
    },
    redraw: function(){
      drawRect(this);
    }
  }   
},

结果如下:

你可以在这里找到这个例子的工作小提琴:http://jsfiddle.net/brightmatrix/0yLn5bky/

希望对你有帮助。

【讨论】:

  • 如图所示,是否可以获得 x 轴标签的背景颜色?
  • @M.VamsiKrishna 图片不再是你的问题,所以我没有什么可参考的。另外,您是指 x 轴标签后面的背景颜色,还是标签本身的颜色?
  • 是的,x轴后面的背景色
  • @M.VamsiKrishna 这篇文章可能对您有所帮助。查看两个答案以获取替代方案:stackoverflow.com/questions/20242302/…
  • 嗨,谢谢你的链接,我在我的代码中尝试了同样的方法,但它不适合我,我不明白为什么它不适合我,可以帮忙吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 2017-07-08
相关资源
最近更新 更多