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