How utilize flot.js to generate a monthly attendance chart? - javascript

How to make use of flot.js to create a attendance chart with it? something similar to the image linked.attendance chart
The date axis shows perfectly. the problem lies in the time axis! how do I format the time, to pass it to the flot?
function gd(year, month, day) {
return new Date(year, month - 1, day).getTime();
}
function gt(year, month, day, hour, minute){
return new Date(year, month - 1, day, hour, minute).getTime();
}
function init_attendace_chart(){
if( typeof ($.plot) === 'undefined'){ return; }
console.log('attendance plotting');
var attendance_data = [
[gd(2017, 3, 1), gt(2017, 3, 1, 8, 45)], [gd(2017, 3, 2), gt(2017, 3, 1, 7, 48)], [gd(2017, 3, 3), gt(2017, 3, 1, 7, 50)],
[gd(2017, 3, 4), gt(2017, 3, 1, 7, 45)], [gd(2017, 3, 5), gt(2017, 3, 1, 7, 55)], [gd(2017, 3, 6), gt(2017, 3, 1, 7, 45)],
[gd(2017, 3, 7), gt(2017, 3, 1, 7, 45)], [gd(2017, 3, 8), gt(2017, 3, 1, 7, 45)], [gd(2017, 3, 9), gt(2017, 3, 1, 7, 45)]
];
var attendance_chart_settings = {
series: {
curvedLines: {
apply: true,
active: true,
monotonicFit: true
},
shadowSize: 5
},
xaxes: [{ //Edit this
mode: "time",
tickSize: [1, "day"]
}],
yaxes:[{
mode:"time",
tickSize: [1, "hour"]
}],
colors: ["#26B99A"],
lines: { show: true },
grid: {
borderWidth: {
top: 0,
right: 0,
bottom: 1,
left: 1,
hoverable: true
},
borderColor: {
bottom: "#7F8790",
left: "#7F8790"
}
}
};
if ($("#attendance_chart").length){
$.plot($("#attendance_chart"), [{
label: " Your Clock-In Times",
data: attendance_data,
lines: {
fillColor: "rgba(150, 202, 89, 0.12)"
},
points: {
fillColor: "#fff"
}
}], attendance_chart_settings);
// $.plot($("#attendance_chart"), dataset, options);
$("#attendance_chart").UseTooltip();
}
}

Related

Flot xaxis labels disappears on multiple charts

When displaying two separate bar graph charts, the xaxis labels disappear but when only 1 graph is displayed, the labels are displayed fine.
Do you have an idea about this?
Displaying two graphs:
Displaying one graph:
Below is the javascript used:
<script>
var statement = [
[gd(2018, 2, 1), 44578],
[gd(2018, 2, 2), 550],
[gd(2018, 2, 3), 600],
[gd(2018, 2, 4), 500],
[gd(2018, 2, 5), 700],
[gd(2018, 2, 6), 38339],
[gd(2018, 2, 7), 28518],
[gd(2018, 2, 8), 21629],
[gd(2018, 2, 9), 50716],
[gd(2018, 2, 10), 29774],
[gd(2018, 2, 11), 24562],
[gd(2018, 2, 12), 63659],
[gd(2018, 2, 13), 29186],
[gd(2018, 2, 14), 62470],
[gd(2018, 2, 15), 82143],
[gd(2018, 2, 16), 12774],
[gd(2018, 2, 17), 13607],
[gd(2018, 2, 18), 7058],
[gd(2018, 2, 19), 32801],
[gd(2018, 2, 20), 25184],
[gd(2018, 2, 21), 22912],
[gd(2018, 2, 22), 35300],
[gd(2018, 2, 23), 20038],
[gd(2018, 2, 24), 21566],
[gd(2018, 2, 25), 73290],
[gd(2018, 2, 26), 43411],
[gd(2018, 2, 27), 36330],
[gd(2018, 2, 28), 40766]
];
var dataset = [{
label: "Consumption",
data: statement,
color: "#ffa500",
bars: {
show: true,
align: "center",
barWidth: 24 * 60 * 60 * 600,
lineWidth: 2,
fill: 1
}
}];
var options = {
xaxis: {
mode: "time",
tickSize: [3, "day"],
timeformat: "%e %b",
tickLength: 0,
rotateTicks: 135,
axisLabel: "",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 1,
axisLabelFontFamily: "Calibri",
axisLabelPadding: 1,
color: "black"
},
yaxes: [{
position: "left",
color: "black",
axisLabel: "'000 kWh",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: "Calibri",
axisLabelPadding: 15,
align: "center",
tickFormatter: function numberWithCommas(x) {
return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}
}],
legend: {
container: $("#legendContainer"),
noColumns: 2,
labelBoxBorderColor: "#000000",
position: "nw"
},
grid: {
hoverable: true,
borderWidth: 1,
backgroundColor: {
colors: ["#ffffff", "#EDF5FF"]
}
}
};
$(document).ready(function() {
$.plot($("#graph-placeholder"), dataset, options);
});
function gd(year, month, day) {
return Date.UTC(year, month - 1, day);
}
<script>
var statementMonthly = [
[gdMonthly(2018, 1, 1), 0],
[gdMonthly(2018, 2, 1), 2351.8],
[gdMonthly(2018, 3, 1), 1209.6],
[gdMonthly(2018, 4, 1), 1205.6],
[gdMonthly(2018, 5, 1), 0],
[gdMonthly(2018, 6, 1), 515.382],
[gdMonthly(2018, 7, 1), 621.921],
[gdMonthly(2018, 8, 1), 0],
[gdMonthly(2018, 9, 1), 0],
[gdMonthly(2018, 10, 1), 551.3],
[gdMonthly(2018, 11, 1), 0],
[gdMonthly(2018, 12, 1), 0]
];
var datasetMonthly = [{
data: statementMonthly,
color: "#526270",
bars: {
show: true,
align: "center",
barWidth: 800 * 65 * 60 * 700,
lineWidth: 2,
fill: 1
}
}];
var optionsMonthly = {
xaxis: {
mode: "time",
tickSize: [1, "month"],
timeformat: " %b",
monthNames: ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"],
tickLength: 0,
rotateTicks: 179,
axisLabel: "",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 1,
axisLabelFontFamily: "Calibri",
axisLabelPadding: 1,
color: "black"
},
yaxes: [{
position: "left",
color: "black",
axisLabel: "'000 kWh",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: "Calibri",
axisLabelPadding: 15,
align: "center",
tickFormatter: function numberWithCommasMonthly(x) {
return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}
}],
legend: {
container: $("#legendContainerMonthly"),
noColumns: 2,
labelBoxBorderColor: "#000000",
position: "nw"
},
grid: {
hoverable: true,
borderWidth: 1,
backgroundColor: {
colors: ["#ffffff", "#EDF5FF"]
}
}
};
$(document).ready(function() {
$.plot($("#graph-placeholderMonthly"), datasetMonthly, optionsMonthly);
});
function gdMonthly(year, month, day) {
return Date.UTC(year, month - 1, day);
}
I don't understand why the xaxis labels disappear.
What did I miss?
Hope you can help me.
Found the cause of the problem.
The problem is caused by this plugin:
jquery.flot.tickrotor.js
I just removed the plugin on the second graph and the label is now working.

How can I add both plotLines and plotBands to a HighStocks graph using HighChart?

I've been trying to add my plotLines to this graph and I've followed the syntax online; however, the plotLine will not appear.
The plotBands appear just fine, so I am just trying to see what I may be missing to get it to work.
Below is a link to my jsFiddle:
https://jsfiddle.net/fbmoju7f/75/
$.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
Highcharts.stockChart('container', {
title: {
text: 'Morphine'
},
xAxis: {
type: 'datetime',
floor: Date.UTC(2017, 2, 2),
ceiling: Date.UTC(2017, 2, 9),
plotLine: {
value: Date.UTC(2017, 2, 8),
color: '#3A1231', //Purple
width: 2,
dashStyle: 'solid'
}
},
yAxis: { //Keep yAxis ----------------------------------------
floor: 0,
ceiling: 100,
plotBands: [{
color: '#F9F98E', //Yellow
from: 0,
to: 10
}, {
color: '#9DF98E', //Green
from: 10,
to: 30
}, {
color: '#F9958E', //Red
from: 30,
to: 100
}]
},
rangeSelector: {
buttons: [{
type: 'day',
count: 8,
text: '-7d'
}, {
type: 'day',
count: 4,
text: '-3d'
}, {
type: 'day',
count: 3,
text: '-2d'
}, {
type: 'day',
count: 2,
text: '-1d'
}],
selected: 2 //Initial view upon opening application
},
series: [{
type: 'spline',
color: '#4C91FA',
data: [ //Test data set
[Date.UTC(2017, 2, 1), 5.7],
[Date.UTC(2017, 2, 2), 7.3],
[Date.UTC(2017, 2, 3), 10.3],
[Date.UTC(2017, 2, 3), 15.6],
[Date.UTC(2017, 2, 4), 12.7],
[Date.UTC(2017, 2, 4), 14.0],
[Date.UTC(2017, 2, 4), 17.8],
[Date.UTC(2017, 2, 5), 19.1],
[Date.UTC(2017, 2, 5), 18.4],
[Date.UTC(2017, 2, 6), 19.5],
[Date.UTC(2017, 2, 6), 22.7],
[Date.UTC(2017, 2, 6), 25.1],
[Date.UTC(2017, 2, 7), 28.2],
[Date.UTC(2017, 2, 7), 30.4],
[Date.UTC(2017, 2, 8), 29.6],
[Date.UTC(2017, 2, 9), 29.0],
[Date.UTC(2017, 2, 10), 27.8]
]
}]
});
});
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/modules/stock/exporting.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>
You have the wrong property name and you are not enclosing the plotLines in brackets. It should be like:
xAxis: {
type: 'datetime',
floor: Date.UTC(2017, 2, 2),
ceiling: Date.UTC(2017, 2, 9),
plotLines: [{
value: Date.UTC(2017, 2, 8),
color: '#3A1231', //Purple
width: 2,
dashStyle: 'solid'
}]
},

Unable to reference json file in Data attribute but array list data works

I am incorporating cal-heatmap in my asp.net website and all works fine as long as i am referencing data through an array, initialized as variable above the init method, however things stop showing up as soon as i create new text file in visual studio and rename it as datas-years.json and reference it in 'data' attribute, please help me ?
below is the working code, manually entered data in array and referencing
var cal = new CalHeatMap();
var data = { "1420498800": 20, "1420585200": 40, "1420671600": 60, "1420758000": 1, "1421103600": 2, "1421190000": 90, "1421276400": 1, "1421362800": 1, "1421622000": 1, "1421708400": 1, "1422226800": 1, "1422313200": 1, "1422399600": 2, "1422486000": 1, "1422572400": 1, "1423695600": 3, "1424127600": 2, "1424214000": 1, "1424300400": 3, "1424386800": 1, "1424646000": 2, "1424732400": 1, "1424818800": 2, "1424905200": 2, "1424991600": 1, "1425337200": 1, "1425855600": 4, "1426201200": 2, "1426460400": 2, "1426546800": 1, "1426633200": 2, "1426719600": 1, "1426806000": 1, "1427065200": 1, "1427151600": 1, "1427238000": 2, "1427324400": 1, "1427670000": 2, "1428361200": 2, "1428447600": 2, "1428534000": 3, "1428620400": 3, "1428966000": 2, "1429138800": 2, "1429225200": 1, "1429484400": 2, "1429570800": 1, "1429657200": 2, "1429743600": 2, "1429830000": 3 };
cal.init({
data: data,
itemName: ["volunteer", "volunteers"],
start: new Date(2015, 0, 1, 1),
domain: "month",
subDomain: "day",
cellSize: 15,
subDomainTextFormat: "%d",
range: 12,
legend: [20, 40, 60, 80],
});
below is the referencing through separate file, all under visual studio
var cal = new CalHeatMap();
cal.init({
datatype:JSON,
data: "datas-years.json",
itemName: ["volunteer", "volunteers"],
start: new Date(2015, 0, 1, 1),
domain: "month",
subDomain: "day",
cellSize: 15,
subDomainTextFormat: "%d",
range: 12,
//highlight: new Date(2016, 1, 15),
legend: [20, 40, 60, 80],
});
I also would like to know what is the best way to convert the date to seconds format, when i impliment it in live, should i ensure that it is added in seconds format to json file or it can be just like 2/26/2015 ? and then convert it into seconds while borrowing from json file ? Please guide me, thank you
EDIT:
Data in the datas-years.json file in directory
[{
"1420498800":20,
"1420585200":40,
"1420671600":80,
"1421708400": 1,
"1422226800": 1,
"1422313200": 1,
"1422399600": 2,
"1422486000": 1
}]

Google LineChart legend and gridlines

I'd like to get this Google LineChart to look a little more like this chart from GitHub. I'm recreating GitHub's chart because I want a version of it in high quality (vector) and not a highly compressed JPEG (who in their right mind saves charts as JPEGs anyway!).
I'd like to move the legend into the graph and add vertical gridlines (on the horizontal axis "Year"). Does anyone know how to accomplish this?
Here's the code, but please feel free to edit my JSFiddle:
google.load('visualization', '1', {packages: ['corechart', 'line']});
google.setOnLoadCallback(function() {
var data = google.visualization.arrayToDataTable([
['Year', 'JavaScript', 'Java', 'Ruby', 'PHP', 'Python', 'CSS', 'C++', 'Objective C', 'C', 'C#', 'Perl', 'Emacs Lisp', 'VimL', 'Shell','HTML'],
['2008', 2, 7, 1, 4, 3, null, 8, 9, 5, null, 6, null, null, null, null],
['2009', 2, 7, 1, 4, 3, null, 8, 9, 6, null, 5, 10, null, null, null],
['2010', 2, 5, 1, 4, 3, null, 8, 9, 6, null, 7, null, 10, null, null],
['2011', 2, 5, 1, 4, 3, null, 7, 8, 6, 10, 9, null, null, null, null],
['2012', 2, 3, 1, 4, 5, null, 7, 8, 6, 9, null, null, null, 10, null],
['2013', 1, 3, 2, 4, 5, 10, 7, 8, 6, 9, null, null, null, null, null],
['2014', 1, 2, 3, 4, 5, 6, 7, 9, 8, 10, null, null, null, null, null],
['2015', 1, 2, 3, 4, 5, 6, 7, null, 9, 8, null, null, null, null, 10]
]);
var options = {
title: 'Rank of top languages on GitHub over time',
width: '100%',
height: 600,
hAxis: {
title: 'Time',
},
vAxis: {
title: 'Rank',
viewWindow: {
min: 1
},
direction: -1,
gridlines: {
count: 11
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
});
Change data type -> type: 'date'
google.load('visualization', '1.1', {packages: ['corechart', 'line']});
google.setOnLoadCallback(function() {
var data = google.visualization.arrayToDataTable([
[{label: 'Year', type: 'date'}, 'JavaScript', 'Java', 'Ruby', 'PHP', 'Python', 'CSS', 'C++', 'Objective C', 'C', 'C#', 'Perl', 'Emacs Lisp', 'VimL', 'Shell','HTML'],
[new Date(2008, 0, 0), 2, 7, 1, 4, 3, null, 8, 9, 5, null, 6, null, null, null, null],
[new Date(2009, 0, 0), 2, 7, 1, 4, 3, null, 8, 9, 6, null, 5, 10, null, null, null],
[new Date(2010, 0, 0), 2, 5, 1, 4, 3, null, 8, 9, 6, null, 7, null, 10, null, null],
[new Date(2011, 0, 0), 2, 5, 1, 4, 3, null, 7, 8, 6, 10, 9, null, null, null, null],
[new Date(2012, 0, 0), 2, 3, 1, 4, 5, null, 7, 8, 6, 9, null, null, null, 10, null],
[new Date(2013, 0, 0), 1, 3, 2, 4, 5, 10, 7, 8, 6, 9, null, null, null, null, null],
[new Date(2014, 0, 0), 1, 2, 3, 4, 5, 6, 7, 9, 8, 10, null, null, null, null, null],
[new Date(2015, 0, 0), 1, 2, 3, 4, 5, 6, 7, null, 9, 8, null, null, null, null, 10]
]);
var options = {
title: 'Rank of top languages on GitHub over time',
width: '100%',
height: 800,
hAxis: {
title: 'Time',
gridlines: {
count: -1
},
},
vAxis: {
title: 'Rank',
viewWindow: {
min: 1
},
direction: -1,
gridlines: {
count: -1
}
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
});
https://jsfiddle.net/mblenton/x00omtxL/15/

Jquery Flot fill two dataset with DateTime and make a overlap effect

I have a chart (image bellow), where the green line has a reference to the year 2014, and the purple line will be 2013.
var data1 = [
[gd(2014, 1, 1), 4], [gd(2014, 2, 1), 8], [gd(2014, 3, 1), 4], [gd(2014, 4, 1), 10],
[gd(2014, 5, 1), 4], [gd(2014, 6, 1), 16], [gd(2014, 7, 1), 5]];
var data2 = [
[gd(2014, 1, 1), 1], [gd(2014, 2, 1), 0], [gd(2014, 3, 1), 2], [gd(2014, 4, 1), 0],
[gd(2014, 5, 1), 1], [gd(2014, 6, 1), 3], [gd(2014, 7, 1), 1], [gd(2014, 8, 1), 5],
[gd(2014, 9, 1), 2], [gd(2014, 10, 1), 3], [gd(2014, 11, 1), 2], [gd(2014, 12, 1), 1]];
This is the dataset, but look that I put both dataset in 2014 year because if I put 2014 in one dataset and 2013 in another, I miss the overlap effect and I need that effect for comparsion.
This is what happens if a put the 2013 year in one dataset and 2014 in another (image below)
How can I do the same chart, but with this overlap effect, using the 2013 year in one dataset?
This also will fix my hover functionallity.
Code
var data1 = [
[gd(2014, 1, 1), 4], [gd(2014, 2, 1), 8], [gd(2014, 3, 1), 4], [gd(2014, 4, 1), 10],
[gd(2014, 5, 1), 4], [gd(2014, 6, 1), 16], [gd(2014, 7, 1), 5]];
var data2 = [
[gd(2014, 1, 1), 1], [gd(2014, 2, 1), 0], [gd(2014, 3, 1), 2], [gd(2014, 4, 1), 0],
[gd(2014, 5, 1), 1], [gd(2014, 6, 1), 3], [gd(2014, 7, 1), 1], [gd(2014, 8, 1), 5],
[gd(2014, 9, 1), 2], [gd(2014, 10, 1), 3], [gd(2014, 11, 1), 2], [gd(2014, 12, 1), 1]];
$("#flot-dashboard-chart").length && $.plot($("#flot-dashboard-chart"), [
data1, data2
],
{
series: {
lines: {
show: false,
fill: true
},
splines: {
show: true,
tension: 0.4,
lineWidth: 1,
fill: 0.4
},
points: {
radius: 2,
show: true
},
shadowSize: 2
},
grid: {
hoverable: true,
clickable: true,
tickColor: "#d5d5d5",
borderWidth: 1,
color: '#d5d5d5'
},
colors: ["#1ab394", "#464f88"],
xaxis: {
mode: "time",
tickSize: [1, "month"],
tickLength: null,
axisLabel: "Date",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Arial',
axisLabelPadding: 10,
color: "#838383",
timeformat: "%b/%y"
},
yaxis: {
ticks: 4
}
}
);
function gd(year, month, day) {
return new Date(year, month - 1, day).getTime();
}
You use the concept of multiple axes, but just hide the second axis.
To do this you create two data sets, each with their own axis:
var data2014 = {
label: "2014",
data: data1,
xaxis: 1
};
var data2013 = {
label: "2013",
data: data2,
xaxis: 2
};
and then in the axes option setting, set ticks to false to hide one axis.
The max setting for the 2014 data is important, otherwise the data set will scale to fill the whole graph:
xaxes: [{
mode: "time",
tickSize: [1, "month"],
tickLength: null,
color: "#838383",
timeformat: "%b",
max: (new Date("2014/12/1")).getTime()
},{
ticks: false
}],
JS Fiddle here.
Full code below:
var data1 = [
[gd(2014, 1, 1), 4], [gd(2014, 2, 1), 8], [gd(2014, 3, 1), 4], [gd(2014, 4, 1), 10],
[gd(2014, 5, 1), 4], [gd(2014, 6, 1), 16], [gd(2014, 7, 1), 5]];
var data2 = [
[gd(2013, 1, 1), 1], [gd(2013, 2, 1), 0], [gd(2013, 3, 1), 2], [gd(2013, 4, 1), 0],
[gd(2013, 5, 1), 1], [gd(2013, 6, 1), 3], [gd(2013, 7, 1), 1], [gd(2013, 8, 1), 5],
[gd(2013, 9, 1), 2], [gd(2013, 10, 1), 3], [gd(2013, 11, 1), 2], [gd(2013, 12, 1), 1]];
var data2014 = {
label: "2014",
data: data1,
xaxis: 1
};
var data2013 = {
label: "2013",
data: data2,
xaxis: 2
};
$("#flot-dashboard-chart").length && $.plot($("#flot-dashboard-chart"), [
data2014, data2013
],
{
series: {
lines: {
show: false,
fill: true
},
splines: {
show: true,
tension: 0.4,
lineWidth: 1,
fill: 0.4
},
points: {
radius: 2,
show: true
},
shadowSize: 2
},
grid: {
hoverable: true,
clickable: true,
tickColor: "#d5d5d5",
borderWidth: 1,
labelMargin: 17,
margin: {
top: 8,
bottom: 10,
left: 20
},
minBorderMargin: 25,
color: '#d5d5d5'
},
colors: ["#1ab394", "#464f88"],
xaxes: [{
mode: "time",
tickSize: [1, "month"],
color: "#838383",
timeformat: "%b",
max: (new Date("2014/12/1")).getTime()
},{
ticks: false,
}],
yaxis: {
ticks: 5
},
legend: {
backgroundOpacity: 0.5,
noColumns: 0,
position: "ne",
color: "#838383",
}
}
);
function gd(year, month, day) {
return new Date(year, month - 1, day).getTime();
}

Categories