Skipping null values with Morris.js

喜夏-厌秋 提交于 2020-01-06 04:43:25

问题


I'm using Morris.js to plot an area chart. My data is declared as follow:

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 }
        ];

When I plot this data using Morris I got the following chart:

The code source is based on the following asked question:

Adding buttons to a chart - svg

My question is: is it possible to skip the null values and not plotting them ?

If we skip the null values the chart will be like the following picture:

Here is a working code:

(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;">

回答1:


This was never a built-in option in Morris.js. There was a continuousLine parameter until version 0.4.3 that breaks the line for null values, but it was only for the Line chart, not the Area chart:

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
});
<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>

You can try to extend the latest Morris.js version 0.5.1 and modify the code that draw the lines, but it's a difficult one to achieve; I tried without success.




回答2:


Documentation indicates that the continousLine attribute was removed in v0.5.0 with null values causing a break in line/area chart (http://morrisjs.github.io/morris.js/lines.html). However, GitHub shows that there is an open issue with area chart not breaking on null values (https://github.com/morrisjs/morris.js/issues/347).



来源:https://stackoverflow.com/questions/34618601/skipping-null-values-with-morris-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!