Highcharts水平条形图,栏内有文字

时间:2017-03-05 23:18:38

标签: javascript highcharts bar-chart

我是Highcharts的新手,需要设计一个与下图完全相同的水平条形图。

Horizontal-Bar-Chart

我试过摆弄并设法在栏内显示4和2。但是,我无法在第一个栏上方显示“已完成”文本,在第二个栏下方显示“待处理”文本。我也想消除酒吧之间的差距。以下是我目前的代码。

var chart = new Highcharts.Chart({
chart: {
    renderTo:'container',
    //marginLeft:120,
    type:'bar'
},
legend: { enabled: false},
colors:['#173c64'],
xAxis: {
    categories: ['4','2'],
    labels: {
        align:'left',
        x:5,
        style: {
            fontSize: '4em',
            color:'#fff'
        }
    },
    lineWidth: 0,
    gridLineWidth: 0,
    lineColor: 'transparent',
    minorTickLength: 0,
    tickLength: 0,
    title: {
           enabled: false
    }

},
yAxis: {
     lineWidth: 0,
     gridLineWidth: 0,
     lineColor: 'transparent',      
     labels: {
         enabled: false
     },
     minorTickLength: 0,
     tickLength: 0,
     title: {
            enabled: false
     }
},
title: {
   margin:0,
   useHTML: true,
   text: "This Month",
   style: {"color": "#333333", "fontSize": "1.5rem","fontWeight": "bold"}
},
series:[{
    data:[{y: 20, color:'#0091dc'},{y: 17, color:'#173c64'}]
}]      

});

1 个答案:

答案 0 :(得分:0)

已完成添加了字幕,并为要操作添加了自定义标签到页脚,并删除了添加plotOptions的栏之间的空格 图表中的groupPadding: 0,pointPadding: 0,

这几乎符合您的要求。希望这有帮助



var chart = new Highcharts.Chart({
  chart: {
    renderTo: 'container',
    //marginLeft:120,
    type: 'bar',
    events: {
      load: function() {
        var label = this.renderer.label("To Action")
          .css({
            width: '100px',
            fontSize: '1.5rem'
          })
          .add();

        label.align(Highcharts.extend(label.getBBox(), {
          align: 'left',
          x: 0, // offset
          verticalAlign: 'bottom',
          y: -30 // offset
        }), null, 'spacingBox');

      }
    },
    marginBottom: 120
  },
  legend: {
    enabled: false
  },
  colors: ['#173c64'],
  xAxis: {
    categories: ['4', '2'],
    labels: {
      align: 'left',
      x: 5,
      style: {
        fontSize: '4em',
        color: '#fff'
      }
    },
    lineWidth: 0,
    gridLineWidth: 0,
    lineColor: 'transparent',
    minorTickLength: 0,
    tickLength: 0,
    title: {
      enabled: false
    }

  },
  yAxis: {
    lineWidth: 0,
    gridLineWidth: 0,
    lineColor: 'transparent',
    labels: {
      enabled: false
    },
    minorTickLength: 0,
    tickLength: 0,
    title: {
      enabled: false
    }
  },
  plotOptions: {
    bar: {
      stacking: "normal",
      groupPadding: 0, //add here
      pointPadding: 0, //add here
    }
  },
  title: {
    margin: 0,
    useHTML: true,
    text: "This Month",
    style: {
      "color": "#333333",
      "fontSize": "1.5rem",
      "fontWeight": "bold"
    }
  },
  subtitle: {
    align: 'left',
    y: 40, //offset
    useHTML: true,
    text: 'Completed',
    style: {
      "color": "#333333",
      "fontSize": "1.5rem"
    }
  },
  series: [{
    data: [{
      y: 20,
      color: '#0091dc'
    }, {
      y: 17,
      color: '#173c64'
    }]
  }]
})

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
&#13;
&#13;
&#13;