Skip to content

Instantly share code, notes, and snippets.

@mojoaxel
Created July 26, 2018 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojoaxel/827f08eb335f93ba16b46c9daa9d9779 to your computer and use it in GitHub Desktop.
Save mojoaxel/827f08eb335f93ba16b46c9daa9d9779 to your computer and use it in GitHub Desktop.
Plotly.js Timing Diagramm // source https://jsbin.com/fucugep
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Plotly.js Timing Diagramm</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.39.2/plotly.min.js"></script>
<style id="jsbin-css">
body {
font-family: Arial;
}
</style>
</head>
<body>
<h1>Plotly.js Timing Diagram</h1>
<p>This is demonstration how to build a very basic <a href="https://en.wikipedia.org/wiki/Digital_timing_diagram">Timing Diagram</a> using <a href="https://plot.ly/javascript/">plotly.js</a>.</p>
<div id="chart"></div>
<script id="jsbin-javascript">
/**
* MIT License
* Copyright 2018 by Alexander Wunschik (https://github.com/mojoaxel)
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Disable Loop-Protection on jsbin.com
// noprotect
/** ****************************************************************
* This is the definition of timing series.
* Each series must have the following parameters:
* name - A name of this series. Used on the Y-axes and the legend.
* fill - Show area-fill if "true"
* x - th timing series x-axes (numeric or date-time)
* y - digital values "0" or "1" or "true" or "false"
*/
var timing_series = [{
name: "clock",
fill: true,
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
}, {
name: "x",
fill: false,
x: [0, 1, 3, 5, 7, 9, 10],
y: [false, true, false, true, false, true, true]
}, {
name: "y",
fill: true,
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
}];
/** ****************************************************************
* This are general settings on how to render the timing diagram:
* ydelta - the y-axes delta between "0" and "1".
* yspacing - the y-axes spacing between series.
* showStartLine - If "true" a vertical line will be drawn at the beginning of the series.
* showStartLine - If "true" a vertical line will be drawn at the end of the series.
*/
var timing_settings = {
ydelta: 1,
yspacing: 1,
showStartLine: true,
showStopLine: true
}
/** ****************************************************************
* This function takes a set of timing series (as defined above).
*
* @param series - a collection of timing series as specified above.
* @param settings - Settings for the timing diagram as specified above.
*
* @returns an array of plotly trace objects.
*/
function genTracesFromTimingSeries(series, settings) {
var yposition = 0;
var traces = [];
series.forEach(function(serie) {
//TODO: make sure serie.x.length == serie.y.length !
//TODO: make sure all values of series.y are 0 or 1 !
var x = [];
var y = [];
var laststate = serie.y[0];
// make sure the series always starts with a "0" of fill will not work.
if (serie.fill) {
x.push(serie.x[0]);
y.push(yposition);
}
for (var i=0; i<=serie.x.length; i++) {
var ix = serie.x[i];
var iy = serie.y[i];
x.push(ix);
y.push(yposition + (laststate * settings.ydelta));
// if signal state changes we need an additional entry
if (iy !== laststate) {
x.push(ix);
y.push(yposition + (iy * settings.ydelta));
}
laststate = iy;
}
// make sure the series always ends with a "0" of fill will not work.
if (serie.fill) {
x.push(serie.x[serie.x.length-1]);
y.push(yposition);
}
var s = {
x: x,
y: y,
hoverinfo: "text",
connectgaps: true,
fill: 'none',
type: 'scatter',
mode: 'lines',
hoverinfo: 'none',
name: serie.name,
};
if (serie.fill) {
s.fill = 'toself';
}
traces.push(s);
yposition += settings.ydelta + settings.yspacing;
});
return traces;
}
/** ****************************************************************
* This function takes a given layout and extends it with settings
* that are neccesary for the timing diagram.
*
* @param series - a collection of timing series as specified above.
* @param layout - the plotly-layout object that should be extended.
* @param settings - Settings for the timing diagram as specified above.
*
* @returns the extended layout object.
*/
function extendLegendForTimingSeries(series, layout, settings) {
var yposition = 0;
layout = layout || {};
layout.yaxis = layout.yaxis || {};
layout.yaxis.tickvals = layout.yaxis.tickvals || [];
layout.yaxis.ticktext = layout.yaxis.ticktext || [];
series.forEach(function(serie) {
layout.yaxis.tickvals.push(yposition);
layout.yaxis.ticktext.push(serie.name);
yposition += settings.ydelta + settings.yspacing;
});
layout.shapes = layout.shapes || [];
var maxY = (series.length-1)*(settings.ydelta + settings.yspacing) + settings.ydelta;
if (settings.showStartLine) {
layout.shapes.push({
type: 'line',
x0: series[0].x[0],
y0: 0,
x1: series[0].x[0],
y1: maxY
});
}
if (settings.showStopLine) {
layout.shapes.push({
type: 'line',
x0: series[0].x[series[0].x.length-1],
y0: 0,
x1: series[0].x[series[0].x.length-1],
y1: maxY
});
}
return layout;
}
/** **************************************************************** */
var traces = genTracesFromTimingSeries(timing_series, timing_settings);
var layout = extendLegendForTimingSeries(timing_series, {
showlegend: false,
legend: {
traceorder: 'reversed'
},
yaxis: {
fixedrange: true
}
}, timing_settings);
var options = {
scrollZoom: true
}
Plotly.newPlot('chart', traces , layout, options);
</script>
<script id="jsbin-source-css" type="text/css">body {
font-family: Arial;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">/**
* MIT License
* Copyright 2018 by Alexander Wunschik (https://github.com/mojoaxel)
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Disable Loop-Protection on jsbin.com
// noprotect
/** ****************************************************************
* This is the definition of timing series.
* Each series must have the following parameters:
* name - A name of this series. Used on the Y-axes and the legend.
* fill - Show area-fill if "true"
* x - th timing series x-axes (numeric or date-time)
* y - digital values "0" or "1" or "true" or "false"
*/
var timing_series = [{
name: "clock",
fill: true,
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
}, {
name: "x",
fill: false,
x: [0, 1, 3, 5, 7, 9, 10],
y: [false, true, false, true, false, true, true]
}, {
name: "y",
fill: true,
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
}];
/** ****************************************************************
* This are general settings on how to render the timing diagram:
* ydelta - the y-axes delta between "0" and "1".
* yspacing - the y-axes spacing between series.
* showStartLine - If "true" a vertical line will be drawn at the beginning of the series.
* showStartLine - If "true" a vertical line will be drawn at the end of the series.
*/
var timing_settings = {
ydelta: 1,
yspacing: 1,
showStartLine: true,
showStopLine: true
}
/** ****************************************************************
* This function takes a set of timing series (as defined above).
*
* @param series - a collection of timing series as specified above.
* @param settings - Settings for the timing diagram as specified above.
*
* @returns an array of plotly trace objects.
*/
function genTracesFromTimingSeries(series, settings) {
var yposition = 0;
var traces = [];
series.forEach(function(serie) {
//TODO: make sure serie.x.length == serie.y.length !
//TODO: make sure all values of series.y are 0 or 1 !
var x = [];
var y = [];
var laststate = serie.y[0];
// make sure the series always starts with a "0" of fill will not work.
if (serie.fill) {
x.push(serie.x[0]);
y.push(yposition);
}
for (var i=0; i<=serie.x.length; i++) {
var ix = serie.x[i];
var iy = serie.y[i];
x.push(ix);
y.push(yposition + (laststate * settings.ydelta));
// if signal state changes we need an additional entry
if (iy !== laststate) {
x.push(ix);
y.push(yposition + (iy * settings.ydelta));
}
laststate = iy;
}
// make sure the series always ends with a "0" of fill will not work.
if (serie.fill) {
x.push(serie.x[serie.x.length-1]);
y.push(yposition);
}
var s = {
x: x,
y: y,
hoverinfo: "text",
connectgaps: true,
fill: 'none',
type: 'scatter',
mode: 'lines',
hoverinfo: 'none',
name: serie.name,
};
if (serie.fill) {
s.fill = 'toself';
}
traces.push(s);
yposition += settings.ydelta + settings.yspacing;
});
return traces;
}
/** ****************************************************************
* This function takes a given layout and extends it with settings
* that are neccesary for the timing diagram.
*
* @param series - a collection of timing series as specified above.
* @param layout - the plotly-layout object that should be extended.
* @param settings - Settings for the timing diagram as specified above.
*
* @returns the extended layout object.
*/
function extendLegendForTimingSeries(series, layout, settings) {
var yposition = 0;
layout = layout || {};
layout.yaxis = layout.yaxis || {};
layout.yaxis.tickvals = layout.yaxis.tickvals || [];
layout.yaxis.ticktext = layout.yaxis.ticktext || [];
series.forEach(function(serie) {
layout.yaxis.tickvals.push(yposition);
layout.yaxis.ticktext.push(serie.name);
yposition += settings.ydelta + settings.yspacing;
});
layout.shapes = layout.shapes || [];
var maxY = (series.length-1)*(settings.ydelta + settings.yspacing) + settings.ydelta;
if (settings.showStartLine) {
layout.shapes.push({
type: 'line',
x0: series[0].x[0],
y0: 0,
x1: series[0].x[0],
y1: maxY
});
}
if (settings.showStopLine) {
layout.shapes.push({
type: 'line',
x0: series[0].x[series[0].x.length-1],
y0: 0,
x1: series[0].x[series[0].x.length-1],
y1: maxY
});
}
return layout;
}
/** **************************************************************** */
var traces = genTracesFromTimingSeries(timing_series, timing_settings);
var layout = extendLegendForTimingSeries(timing_series, {
showlegend: false,
legend: {
traceorder: 'reversed'
},
yaxis: {
fixedrange: true
}
}, timing_settings);
var options = {
scrollZoom: true
}
Plotly.newPlot('chart', traces , layout, options);</script></body>
</html>
body {
font-family: Arial;
}
/**
* MIT License
* Copyright 2018 by Alexander Wunschik (https://github.com/mojoaxel)
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Disable Loop-Protection on jsbin.com
// noprotect
/** ****************************************************************
* This is the definition of timing series.
* Each series must have the following parameters:
* name - A name of this series. Used on the Y-axes and the legend.
* fill - Show area-fill if "true"
* x - th timing series x-axes (numeric or date-time)
* y - digital values "0" or "1" or "true" or "false"
*/
var timing_series = [{
name: "clock",
fill: true,
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
}, {
name: "x",
fill: false,
x: [0, 1, 3, 5, 7, 9, 10],
y: [false, true, false, true, false, true, true]
}, {
name: "y",
fill: true,
x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
}];
/** ****************************************************************
* This are general settings on how to render the timing diagram:
* ydelta - the y-axes delta between "0" and "1".
* yspacing - the y-axes spacing between series.
* showStartLine - If "true" a vertical line will be drawn at the beginning of the series.
* showStartLine - If "true" a vertical line will be drawn at the end of the series.
*/
var timing_settings = {
ydelta: 1,
yspacing: 1,
showStartLine: true,
showStopLine: true
}
/** ****************************************************************
* This function takes a set of timing series (as defined above).
*
* @param series - a collection of timing series as specified above.
* @param settings - Settings for the timing diagram as specified above.
*
* @returns an array of plotly trace objects.
*/
function genTracesFromTimingSeries(series, settings) {
var yposition = 0;
var traces = [];
series.forEach(function(serie) {
//TODO: make sure serie.x.length == serie.y.length !
//TODO: make sure all values of series.y are 0 or 1 !
var x = [];
var y = [];
var laststate = serie.y[0];
// make sure the series always starts with a "0" of fill will not work.
if (serie.fill) {
x.push(serie.x[0]);
y.push(yposition);
}
for (var i=0; i<=serie.x.length; i++) {
var ix = serie.x[i];
var iy = serie.y[i];
x.push(ix);
y.push(yposition + (laststate * settings.ydelta));
// if signal state changes we need an additional entry
if (iy !== laststate) {
x.push(ix);
y.push(yposition + (iy * settings.ydelta));
}
laststate = iy;
}
// make sure the series always ends with a "0" of fill will not work.
if (serie.fill) {
x.push(serie.x[serie.x.length-1]);
y.push(yposition);
}
var s = {
x: x,
y: y,
hoverinfo: "text",
connectgaps: true,
fill: 'none',
type: 'scatter',
mode: 'lines',
hoverinfo: 'none',
name: serie.name,
};
if (serie.fill) {
s.fill = 'toself';
}
traces.push(s);
yposition += settings.ydelta + settings.yspacing;
});
return traces;
}
/** ****************************************************************
* This function takes a given layout and extends it with settings
* that are neccesary for the timing diagram.
*
* @param series - a collection of timing series as specified above.
* @param layout - the plotly-layout object that should be extended.
* @param settings - Settings for the timing diagram as specified above.
*
* @returns the extended layout object.
*/
function extendLegendForTimingSeries(series, layout, settings) {
var yposition = 0;
layout = layout || {};
layout.yaxis = layout.yaxis || {};
layout.yaxis.tickvals = layout.yaxis.tickvals || [];
layout.yaxis.ticktext = layout.yaxis.ticktext || [];
series.forEach(function(serie) {
layout.yaxis.tickvals.push(yposition);
layout.yaxis.ticktext.push(serie.name);
yposition += settings.ydelta + settings.yspacing;
});
layout.shapes = layout.shapes || [];
var maxY = (series.length-1)*(settings.ydelta + settings.yspacing) + settings.ydelta;
if (settings.showStartLine) {
layout.shapes.push({
type: 'line',
x0: series[0].x[0],
y0: 0,
x1: series[0].x[0],
y1: maxY
});
}
if (settings.showStopLine) {
layout.shapes.push({
type: 'line',
x0: series[0].x[series[0].x.length-1],
y0: 0,
x1: series[0].x[series[0].x.length-1],
y1: maxY
});
}
return layout;
}
/** **************************************************************** */
var traces = genTracesFromTimingSeries(timing_series, timing_settings);
var layout = extendLegendForTimingSeries(timing_series, {
showlegend: false,
legend: {
traceorder: 'reversed'
},
yaxis: {
fixedrange: true
}
}, timing_settings);
var options = {
scrollZoom: true
}
Plotly.newPlot('chart', traces , layout, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment