kendo ui - 条形图标签位置根据条件DEMO分配

问题描述 投票:0回答:1

我想根据条件选择条形图标签位置。由于数据量的比率不同,存在标签在小数据中被切断的现象。

当前标签的位置是"insideEnd"。如果状态值为2,我可以选择外部结束吗?

这是我的代码

function createExecutionToday(type) {

  $("#" + type).kendoChart({
    dataSource: {
      transport: {
        read: {
          url: "dashboard/" + type + ".json",
          dataType: "json"
        }
      },
      schema: {
        data: function(response) {
          for (i in response) {
            var status = response[i].status;
            response[i].status = toKorean(status);
          }
          return response;
        }
      },
    },
    legend: {
      visible: false
    },
    chartArea: {
      border: {
        width: 10,
        color: "white"

      },
      margin: {
        top: 40,
        left: 20,
        bottom: 30
      },

      background: "#ffffff",
    },
    seriesDefaults: {
      type: "bar",
      labels: {
        visible: true,
        position: "insideEnd",

        template: "#if (value > 0) {# #: value # #}#",
        font: "bold 12px arial",
        background: "transparent"

      },
      overlay: null
    },
    series: [{
      field: "count",
      color: function(status) {
        var colors = ["#DB7196", "#66CCFF", "#E5B055"];
        return colors[status.index];
      },
      border: {
        width: 0
      }
    }],

    categoryAxis: {
      categories: [success, running, check],
      majorGridLines: {
        visible: false
      },
      line: {
        visible: false
      },
      labels: {
        font: "bold 12px arial",
        color: "black",
      }
    },
    valueAxis: {

      labels: {
        format: "{0}%"
      },
      line: {
        visible: false
      },
      labels: {
        visible: false
      },
      majorGridLines: {
        visible: false
      },
    },

  });
}
kendo-ui bar-chart kendo-chart
1个回答
0
投票

您可以使用标签的Visual property来重新定位它,具体取决于状态值甚至条形图的宽度。

  labels: {
    visible: true,
    position: "insideEnd",
    template: "#if (value > 0) {# #: value # #}#",
    font: "bold 12px arial",
    background: "transparent",
    visual: function(e) {
        if (e.dataItem.status == 2 || e.rect.size.width < 40){
            var x = e.rect.origin.x + e.rect.size.width + 4;
            var y = e.rect.origin.y + (e.rect.size.height - 12)/2;
            return new kendo.drawing.Text(e.text, [x, y], {
                font: "bold 12px arial",
                fill: {
                    color: "black",                             
                }
            });
        } else {
            return e.createVisual();
        }
    }
  },

注意:return e.createVisual();绘制默认标签

DEMO

© www.soinside.com 2019 - 2024. All rights reserved.