使用Morris.js跳过空值

时间:2016-01-05 18:19:07

标签: svg morris.js

我正在使用Morris.js绘制面积图。 我的数据声明如下:

var data = [
            { y: 'LUN', a: 1 },
            { y: 'MAR', a: 2},
            { y: 'MER', a: null },
            { y: 'JEU', a: 2 },
            { y: 'VEN', a: 1},
            { y: 'SAM', a: 0},
            { y: 'DIM', a: 1 }
        ];

当我使用Morris绘制这些数据时,我得到了以下图表:

enter image description here

代码来源基于以下问题:

Adding buttons to a chart - svg

我的问题是:是否可以跳过空值而不是绘制它们?

如果我们跳过空值,图表将如下图所示:

enter image description here

这是一个有效的代码:

(function () {
    var $, MyMorris;

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

    MyMorris = Object.create(Morris);

    MyMorris.Grid.prototype.gridDefaults["lineStyle"] = "";

    MyMorris.Line.prototype.drawLinePath = function (path, lineColor, lineIndex) {
        return this.raphael.path(path).attr('stroke', lineColor).attr('stroke-width', this.lineWidthForSeries(lineIndex)).attr('stroke-dasharray', this.options.lineStyle);
    };
}).call(this);

var data = [
    { y: 'LUN', a: 1 },
    { y: 'MAR', a: 2},
    { y: 'MER', a: null },
    { y: 'JEU', a: 2 },
    { y: 'VEN', a: 1},
    { y: 'SAM', a: 0},
    { y: 'DIM', a: 1 }
];

Morris.Area({
    element: 'chart',
    data: data,
    xkey: 'y',
    ykeys: ['a'],
    //labels: ['Label 1', 'Label 2'],
    fillOpacity: 0.3,
    hideHover: 'auto',
    behaveLikeLine: false,
    resize: true,
    pointFillColors: ['#ffffff'],
    pointStrokeColors: ['black'],
    lineColors: ['blue'],
    lineStyle: "-",
    parseTime: false,
    smooth: false,
    continuousLine: true
});

var indexNulls = [];

for (var i = 0; i < data.length; i++) {
    if (data[i].a == null) {
        indexNulls.push(i);
    }
}

for (var i = 0; i < indexNulls.length; i++) {
    var circleElement = $("#chart").find("circle")[indexNulls[i]];
    var divPosition = $(circleElement).attr("cx") - 20;
    var divEdit = $("<div/>").css({ "display": "inline-block", "position": "absolute", "left": divPosition + "px" });
    var btnEdit = $("<img/>").attr("src", "http://i.stack.imgur.com/Z2AxP.png").addClass("morrisEdit").css({ "cursor": "pointer" }).attr("onclick", "editAction();");
    divEdit.append(btnEdit);
    $("#edits").append(divEdit);
}

function editAction() {
    alert("Edit Clicked");

    // Do some actions
}
<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="http://cdn.oesmith.co.uk/morris-0.5.1.min.js"></script>
<link href="http://cdn.oesmith.co.uk/morris-0.5.1.css" rel="stylesheet"/>

<div id="chart"></div>
<div id="edits" style="width: 100%; margin-top: -150px; position: relative;">

2 个答案:

答案 0 :(得分:2)

这在Morris.js中从来都不是内置选项。在版本0.4.3之前有一个continuousLine参数会打破空值的行,但它仅适用于Line图表,而不是Area图表:

&#13;
&#13;
Morris.Line({
  element: 'chart',
  data: [
    { y: '2006', a: 100, b: 30 },
    { y: '2007', a: 75, b: 40 },
    { y: '2008', a: null, b: 20 },
    { y: '2009', a: 75, b: 45 },
    { y: '2010', a: 50, b: null },
    { y: '2011', a: 75, b: 65 },
    { y: '2012', a: 100, b:95 }
  ],
  xkey: 'y',
  ykeys: ['a', 'b'],
  labels: ['Label A', 'Label B'],
  continuousLine: false
});
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.4.3/morris.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css" rel="stylesheet"/>

<div id="chart"></div>
&#13;
&#13;
&#13;

您可以尝试扩展最新的Morris.js版本0.5.1并修改绘制线条的代码,但这很难实现;我尝试没有成功。

答案 1 :(得分:1)

文档指示在v0.5.0中删除了continousLine属性,其中空值导致行/面积图(http://morrisjs.github.io/morris.js/lines.html)中断。但是,GitHub显示区域图存在未解决的空值(https://github.com/morrisjs/morris.js/issues/347)存在问题。