html - 如何使用Morris.Bar函数在图表中显示Y轴各类别的计数值?

标签 html razor morris.js

我正在以图形格式显示数据,并且在我的 cshtml 页面中使用 Morris.Bar 函数。 Y 轴具有以下类别:性能、可维护性、其他、可移植性、可靠性和安全性。

我正在使用以下功能:

Morris.Bar({
    element: 'category-bar-chart',
    data: JSON.parse(''[{"y":"Performance","a":23},{"y":"Maintainability","a":106},{"y":"Others","a":98},{"y":"Portability","a":27},{"y":"Reliability","a":87},{"y":"Security","a":14}]'),'),
    xkey: 'y',
    ykeys: ['a'],
    labels: ['Violation'],
    xLabelAngle: 43,            
});

但目前它没有在每个栏的顶部显示每个类别的值。我可以知道我可以添加什么属性来获取每个栏顶部的值吗?

最佳答案

没有内置参数在每个 Bar 顶部显示值。

但是你可以扩展 Morris 添加这个参数。我扩展了 Morris,为条形图添加了 labelTop 属性。如果设置为 true,则会在每个 Bar 的顶部添加一个带有值的标签(我将此属性限制为非堆叠 Bar,因为堆叠 Bar 有多个值)。

用法:

labelTop: true

请尝试下面的代码片段以查看工作示例:

(function() {
  var $, MyMorris;

  MyMorris = window.MyMorris = {};
  $ = jQuery;

  MyMorris = Object.create(Morris);

  MyMorris.Bar.prototype.defaults["labelTop"] = false;

  MyMorris.Bar.prototype.drawLabelTop = function(xPos, yPos, text) {
    var label;
    return label = this.raphael.text(xPos, yPos, text).attr('font-size', this.options.gridTextSize).attr('font-family', this.options.gridTextFamily).attr('font-weight', this.options.gridTextWeight).attr('fill', this.options.gridTextColor);
  };

  MyMorris.Bar.prototype.drawSeries = function() {
    var barWidth, bottom, groupWidth, idx, lastTop, left, leftPadding, numBars, row, sidx, size, spaceLeft, top, ypos, zeroPos;
    groupWidth = this.width / this.options.data.length;
    numBars = this.options.stacked ? 1 : this.options.ykeys.length;
    barWidth = (groupWidth * this.options.barSizeRatio - this.options.barGap * (numBars - 1)) / numBars;
    if (this.options.barSize) {
      barWidth = Math.min(barWidth, this.options.barSize);
    }
    spaceLeft = groupWidth - barWidth * numBars - this.options.barGap * (numBars - 1);
    leftPadding = spaceLeft / 2;
    zeroPos = this.ymin <= 0 && this.ymax >= 0 ? this.transY(0) : null;
    return this.bars = (function() {
      var _i, _len, _ref, _results;
      _ref = this.data;
      _results = [];
      for (idx = _i = 0, _len = _ref.length; _i < _len; idx = ++_i) {
        row = _ref[idx];
        lastTop = 0;
        _results.push((function() {
          var _j, _len1, _ref1, _results1;
          _ref1 = row._y;
          _results1 = [];
          for (sidx = _j = 0, _len1 = _ref1.length; _j < _len1; sidx = ++_j) {
            ypos = _ref1[sidx];
            if (ypos !== null) {
              if (zeroPos) {
                top = Math.min(ypos, zeroPos);
                bottom = Math.max(ypos, zeroPos);
              } else {
                top = ypos;
                bottom = this.bottom;
              }
              left = this.left + idx * groupWidth + leftPadding;
              if (!this.options.stacked) {
                left += sidx * (barWidth + this.options.barGap);
              }
              size = bottom - top;
              if (this.options.verticalGridCondition && this.options.verticalGridCondition(row.x)) {
                this.drawBar(this.left + idx * groupWidth, this.top, groupWidth, Math.abs(this.top - this.bottom), this.options.verticalGridColor, this.options.verticalGridOpacity, this.options.barRadius, row.y[sidx]);
              }
              if (this.options.stacked) {
                top -= lastTop;
              }
              this.drawBar(left, top, barWidth, size, this.colorFor(row, sidx, 'bar'), this.options.barOpacity, this.options.barRadius);
              _results1.push(lastTop += size);

              if (this.options.labelTop && !this.options.stacked) {
                label = this.drawLabelTop((left + (barWidth / 2)), top - 10, row.y[sidx]);
                textBox = label.getBBox();
                _results.push(textBox);
              }
            } else {
              _results1.push(null);
            }
          }
          return _results1;
        }).call(this));
      }
      return _results;
    }).call(this);
  };
}).call(this);

Morris.Bar({
  element: 'category-bar-chart',
  data: [
    { "y": "Performance", "a": 23 },
    { "y": "Maintainability", "a": 106 },
    { "y": "Others", "a": 98 },
    { "y": "Portability", "a": 27 },
    { "y": "Reliability", "a": 87 },
    { "y": "Security", "a": 14 }],
  xkey: 'y',
  ykeys: ['a'],
  labels: ['Violation'],
  xLabelAngle: 43,
  labelTop: true
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css" rel="stylesheet" />

<div id="category-bar-chart"></div>

关于html - 如何使用Morris.Bar函数在图表中显示Y轴各类别的计数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41009961/

相关文章:

javascript - 通过弹窗搜索MySQL数据

javascript - JavaScript 数组中的字符串

c# - Kendo UI 引用在 Razor View 中不起作用

.net - _ViewStart.cshtml 布局文件在哪里以及如何链接?

javascript - Morris.js 条形图的自定义悬停图例

javascript - xLabel的个数大于数据的长度

html - 调整浏览器大小时图像超出 div

jquery - 选择这个和里面的div

JavaScript,添加而不是替换

javascript - 如何将 JSON 数组从 php 传递到 JS?