Created
July 18, 2017 19:48
-
-
Save uladkasach/b14cea5b6eff9a4df542ad77b7c752a5 to your computer and use it in GitHub Desktop.
Timeline ChartJS working w/ two point datasets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div style = 'width:500px; height:500px;'> | |
<canvas id="canvas" width="400" height="400"></canvas> | |
</div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script> | |
<script> | |
window.chartColors = { | |
red: 'rgb(255, 99, 132)', | |
orange: 'rgb(255, 159, 64)', | |
yellow: 'rgb(255, 205, 86)', | |
green: 'rgb(75, 192, 192)', | |
blue: 'rgb(54, 162, 235)', | |
purple: 'rgb(153, 102, 255)', | |
grey: 'rgb(201, 203, 207)' | |
}; | |
window.randomScalingFactor = function() { | |
return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100); | |
}; | |
var timeFormat = 'MM/DD/YYYY HH:mm'; | |
function newDate(days) { | |
return moment().add(days, 'd').toDate(); | |
} | |
function newDateString(days) { | |
return moment().add(days, 'd').format(timeFormat); | |
} | |
function newTimestamp(days) { | |
return moment().add(days, 'd').unix(); | |
} | |
var color = Chart.helpers.color; | |
var config = { | |
type: 'line', | |
data: { | |
datasets: [{ | |
label: "Dataset with point data", | |
backgroundColor: color(window.chartColors.green).alpha(0.5).rgbString(), | |
borderColor: window.chartColors.green, | |
fill: false, | |
data: [{ | |
x: newDateString(0), | |
y: randomScalingFactor() | |
}, { | |
x: newDateString(5), | |
y: randomScalingFactor() | |
}, { | |
x: newDateString(7), | |
y: randomScalingFactor() | |
}, { | |
x: newDateString(15), | |
y: randomScalingFactor() | |
}], | |
}, | |
{ | |
label: "Dataset with point data 2", | |
backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(), | |
borderColor: window.chartColors.red, | |
fill: false, | |
data: [{ | |
x: newDateString(0), | |
y: randomScalingFactor() | |
}, { | |
x: newDateString(5), | |
y: randomScalingFactor() | |
}, { | |
x: newDateString(7), | |
y: randomScalingFactor() | |
}, { | |
x: newDateString(15), | |
y: randomScalingFactor() | |
}], | |
}] | |
}, | |
options: { | |
scales: { | |
xAxes: [{ | |
type: "time", | |
time: { | |
format: timeFormat, | |
// round: 'day' | |
tooltipFormat: 'll HH:mm' | |
}, | |
scaleLabel: { | |
display: true, | |
labelString: 'Date' | |
} | |
}, ], | |
yAxes: [{ | |
scaleLabel: { | |
display: true, | |
labelString: 'value' | |
} | |
}] | |
}, | |
} | |
}; | |
window.onload = function() { | |
var ctx = document.getElementById("canvas").getContext("2d"); | |
window.myLine = new Chart(ctx, config); | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment