Production chart partially added and hidden

This commit is contained in:
Danielv123 committed 2016-11-07 09:26:58 +01:00
1 parent b66df4d233
commit afb00ab3fb
4 files changed
+127 -95

No files matched your search

+1
View File
@@ -13,6 +13,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"body-parser": "^1.15.2", "body-parser": "^1.15.2",
"canvasjs": "^1.8.1",
"child_process": "^1.0.2", "child_process": "^1.0.2",
"express": "^4.14.0", "express": "^4.14.0",
"fs": "0.0.2", "fs": "0.0.2",
+4 -4
View File
@@ -2,7 +2,7 @@
<head> <head>
<title>Clusterio</title> <title>Clusterio</title>
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/0.2.0/Chart.min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.js" type="text/javascript"></script>
<script src="main.js"></script> <script src="main.js"></script>
</head> </head>
@@ -31,13 +31,13 @@
</div> </div>
<canvas id="contentGraph" width="200" height="200"></canvas> <canvas id="contentGraph" width="200" height="200"></canvas>
</div> </div>
<div id="production"> <!--<div id="production">
<h2>Production</h2> <h2>Production</h2>
<div id="legend"> <div id="legend">
</div> </div>
<canvas id="productionChart" width="400" height="150"></canvas> <div id="productionChart" style="height: 300px; width: 100%;"></div>
</div> </div>-->
<div id="slaves"> <div id="slaves">
<h2>Slaves</h2> <h2>Slaves</h2>
<div id="display"> <div id="display">
+119 -91
View File
@@ -4,6 +4,7 @@ contents = {
"copper-plate":7312, "copper-plate":7312,
} }
// nice functions // nice functions
// hash a string to a hash
function djb2(str){ function djb2(str){
var hash = 5381; var hash = 5381;
for (var i = 0; i < str.length; i++) { for (var i = 0; i < str.length; i++) {
@@ -11,7 +12,7 @@ function djb2(str){
} }
return hash; return hash;
} }
// hash a string to a color
function hashColor(str) { function hashColor(str) {
var hash = djb2(str); var hash = djb2(str);
var r = (hash & 0xFF0000) >> 16; var r = (hash & 0xFF0000) >> 16;
@@ -19,21 +20,23 @@ function hashColor(str) {
var b = hash & 0x0000FF; var b = hash & 0x0000FF;
return "#" + ("0" + r.toString(16)).substr(-2) + ("0" + g.toString(16)).substr(-2) + ("0" + b.toString(16)).substr(-2); return "#" + ("0" + r.toString(16)).substr(-2) + ("0" + g.toString(16)).substr(-2) + ("0" + b.toString(16)).substr(-2);
} }
// exactly what you would expect it to, returns String
function capitalizeFirstLetter(string) { function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1); return string.charAt(0).toUpperCase() + string.slice(1);
} }
// function to draw data we recieve from ajax requests // hide the HTML object passed as argument #1
function hideThis(object) { function hideThis(object) {
object.style.visibility = "hidden"; object.style.visibility = "hidden";
} }
// function to sort arrays of objects after a keys value
function sortByKey(array, key) { function sortByKey(array, key) {
return array.sort(function(a, b) { return array.sort(function(a, b) {
var x = Number(a[key]); var y = Number(b[key]); var x = Number(a[key]); var y = Number(b[key]);
return ((x < y) ? 1 : ((x > y) ? -1 : 0)); return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}); });
} }
// function to draw data we recieve from ajax requests
function drawcontents(data) { function drawcontents(data) {
var data = sortByKey(data, "count"); var data = sortByKey(data, "count");
result = "<table>"; result = "<table>";
@@ -65,89 +68,126 @@ function display(page) {
} }
} }
// Function to redraw charts in case they bug out // setTimeout is used to put this processing in the back of the queue, after the HTML canvas is done
function drawcharts() { window.onload = function () {
// create chart of items in master storage // dataPoints
ctx = document.querySelector("#contentGraph").getContext('2d'); var dataPoints1 = [];
PieChart = new Chart(ctx); var dataPoints2 = [];
// production chart var chart = new CanvasJS.Chart("productionChart",{
// https://codepen.io/statuswoe/pen/hyldD zoomEnabled: true,
var count = 20; title: {
var data = { text: "Production"
labels : ["1","2","3","4","5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"], },
datasets : [ toolTip: {
{ shared: true
fillColor : "rgba(220,220,220,0.5)", },
strokeColor : "rgba(220,220,220,1)", legend: {
pointColor : "rgba(220,220,220,1)", verticalAlign: "top",
pointStrokeColor : "#fff", horizontalAlign: "center",
title:"one", fontSize: 14,
data : [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] fontWeight: "bold",
}, fontFamily: "calibri",
{ fontColor: "dimGrey"
fillColor : "rgba(151,187,205,0.5)", },
strokeColor : "rgba(151,187,205,1)", axisX: {
pointColor : "rgba(151,187,205,1)", title: "Shows items in network"
pointStrokeColor : "#fff", },
title:"two", axisY:{
data : [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] prefix: '',
} includeZero: true
] },
} data: [{
// this is ugly, don't judge me // dataSeries1
var updateData = function(oldData){ type: "line",
if(piedataOld[0]){ xValueType: "dateTime",
//console.log(piedata) showInLegend: true,
var labels = oldData["labels"]; name: "Company A",
var dataSetA = oldData["datasets"][0]["data"]; dataPoints: dataPoints1
var dataSetB = oldData["datasets"][1]["data"]; },
{
labels.shift(); // dataSeries2
count++; type: "line",
labels.push(count.toString()); xValueType: "dateTime",
// this is where we generate new data showInLegend: true,
var newDataA = piedata[0].value - piedataOld[0].value; name: "Company B" ,
var newDataB = piedata[1].value - piedataOld[1].value; dataPoints: dataPoints2
// not quite sure how this works, where is dataSetA placed in data/the graph? }],
dataSetA.push(newDataA); legend:{
dataSetB.push(newDataB); cursor:"pointer",
dataSetA.shift(); itemclick : function(e) {
dataSetB.shift(); if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else {
e.dataSeries.visible = true;
}
chart.render();
} }
}
});
var updateInterval = 10000;
// initial value
var yValue1 = 640;
var yValue2 = 604;
var time = new Date;
time.setHours(9);
time.setMinutes(30);
time.setSeconds(00);
time.setMilliseconds(00);
// starting at 9.30 am
var updateChart = function (count) {
count = count || 1;
// count is number of times loop runs to generate random dataPoints.
for (var i = 0; i < count; i++) {
// add interval duration to time
time.setTime(time.getTime()+ updateInterval);
// generating random values
var deltaY1 = .5 + Math.random() *(-.5-.5);
var deltaY2 = .5 + Math.random() *(-.5-.5);
// adding random value and rounding it to two digits.
yValue1 = Math.round((yValue1 + deltaY1)*100)/100;
yValue2 = Math.round((yValue2 + deltaY2)*100)/100;
// pushing the new values
dataPoints1.push({
x: time.getTime(),
y: yValue1
});
dataPoints2.push({
x: time.getTime(),
y: yValue2
});
};
// updating legend text with updated with y Value
chart.options.data[0].legendText = " Company A $" + yValue1;
chart.options.data[1].legendText = " Company B $" + yValue2;
chart.render();
}; };
// Not sure why the scaleOverride isn't working... // generates first set of dataPoints
var optionsNoAnimation = { updateChart(3000);
animation : false,
//Boolean - If we want to override with a hard coded scale
scaleOverride : true,
//** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : 20,
//Number - The value jump in the hard coded scale
scaleStepWidth : 10,
//Number - The scale starting value
scaleStartValue : 0
}
//Get the context of the canvas element we want to select // update chart after specified interval
var ctx = document.getElementById("productionChart").getContext("2d"); setInterval(function(){updateChart()}, updateInterval);
var optionsNoAnimation = {animation : false}
productionChart = new Chart(ctx);
productionChart.Line(data, optionsNoAnimation);
setInterval(function(){
updateData(data);
productionChart.Line(data, optionsNoAnimation);
},1000);
} }
// setTimeout is used to put this processing in the back of the queue, after the HTML canvas is done
setTimeout(function(){
drawcharts();
},10)
var piedata = {}; var piedata = {};
var piedataOld = {}; var piedataOld = {};
// get cluster inventory from master // get cluster inventory from master
@@ -172,18 +212,6 @@ setInterval(function() {
label: data[i].name, label: data[i].name,
} }
} }
PieChart.Pie(piedata, {
animation: false,
legend: {
display: true,
labels: {
fontColor: 'rgb(255, 99, 132)'
}
},
hover: {
mode: "label",
}
});
} }
} }
xmlhttp.open("GET", "inventory", true); xmlhttp.open("GET", "inventory", true);
+3
View File
@@ -19,6 +19,9 @@
#body > div { #body > div {
display:none; display:none;
} }
#body > #production {
display:block;
}
#contents { #contents {
width:300px; width:300px;
} }