Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Another jjs demo - use javafx libraries to display a line graph of values read from stdin
/*
stdinplot.js
demo of jss usage - reads numbers from stdin and displays a dynamically
scaling line graph. ignores non-numerical values.
example:
$ jjs -fx stdinplot.js < sin_values.txt
*/
var Application = javafx.application.Application;
var Group = javafx.scene.Group;
var Scene = javafx.scene.Scene;
var Canvas = javafx.scene.canvas.Canvas;
var GraphicsContext = javafx.scene.canvas.GraphicsContext;
var PixelWriter = javafx.scene.image.PixelWriter;
var Color = javafx.scene.paint.Color;
var AbstractAnimationTimer = javafx.animation.AnimationTimer;
var AnimationTimer = Java.extend(AbstractAnimationTimer);
var LinkedList = java.util.LinkedList;
function CircularBuffer(size) {
this.size = size;
this.queue = new LinkedList();
}
CircularBuffer.prototype.add = function(e) {
if (this.size == this.queue.size()) {
this.queue.remove();
}
this.queue.add(e);
}
CircularBuffer.prototype.any = function() {
return this.queue.size() > 0;
}
CircularBuffer.prototype.toArray = function() {
return this.queue.toArray();
}
function plot(gc, height, values) {
var pixelWriter = gc.getPixelWriter();
var max = Math.max.apply(null, values);
var min = Math.min.apply(null, values);
var range = Math.abs(max) + Math.abs(min);
for (var x = 0; x < values.length; x++) {
var y_pre = values[x];
if (min < 0) {
y_pre -= min;
}
var y = ((y_pre / range) * height);
pixelWriter.setColor(x, y, Color.BLACK);
}
}
function start(primaryStage) {
primaryStage.title = "stdinplot";
var width = 300;
var height = 100;
var root = new Group();
var canvas = new Canvas(width, height);
var graphicsContext = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
primaryStage.scene = new Scene(root, width, height);
primaryStage.show();
var graphData = new CircularBuffer(width);
var stdinReader = new java.io.BufferedReader(
new java.io.InputStreamReader(java.lang.System.in)
);
new AnimationTimer() {
handle: function handle(now) {
var l = stdinReader.readLine();
var n = parseFloat(l);
if (! isNaN(n)) {
graphData.add(n);
}
graphicsContext.clearRect(0, 0, width, height);
plot(graphicsContext, height, graphData.toArray());
}
}.start();
}
generated by the following python:
import math
def dumpsin(lim):
for r in [ math.radians(x) for x in range(lim) ]:
print math.sin(r)
dumpsin(1000)
0.0
0.0174524064373
0.0348994967025
0.0523359562429
0.0697564737441
0.0871557427477
0.104528463268
0.121869343405
0.13917310096
0.15643446504
0.173648177667
0.190808995377
0.207911690818
0.224951054344
0.2419218956
0.258819045103
0.275637355817
0.292371704723
0.309016994375
0.325568154457
0.342020143326
0.358367949545
0.374606593416
0.390731128489
0.406736643076
0.422618261741
0.438371146789
0.45399049974
0.469471562786
0.484809620246
0.5
0.51503807491
0.529919264233
0.544639035015
0.559192903471
0.573576436351
0.587785252292
0.601815023152
0.615661475326
0.62932039105
0.642787609687
0.656059028991
0.669130606359
0.681998360062
0.694658370459
0.707106781187
0.719339800339
0.731353701619
0.743144825477
0.754709580223
0.766044443119
0.777145961457
0.788010753607
0.798635510047
0.809016994375
0.819152044289
0.829037572555
0.838670567945
0.848048096156
0.857167300702
0.866025403784
0.874619707139
0.882947592859
0.891006524188
0.898794046299
0.906307787037
0.913545457643
0.920504853452
0.927183854567
0.933580426497
0.939692620786
0.945518575599
0.951056516295
0.956304755963
0.961261695938
0.965925826289
0.970295726276
0.974370064785
0.978147600734
0.981627183448
0.984807753012
0.987688340595
0.990268068742
0.992546151641
0.994521895368
0.996194698092
0.99756405026
0.998629534755
0.999390827019
0.999847695156
1.0
0.999847695156
0.999390827019
0.998629534755
0.99756405026
0.996194698092
0.994521895368
0.992546151641
0.990268068742
0.987688340595
0.984807753012
0.981627183448
0.978147600734
0.974370064785
0.970295726276
0.965925826289
0.961261695938
0.956304755963
0.951056516295
0.945518575599
0.939692620786
0.933580426497
0.927183854567
0.920504853452
0.913545457643
0.906307787037
0.898794046299
0.891006524188
0.882947592859
0.874619707139
0.866025403784
0.857167300702
0.848048096156
0.838670567945
0.829037572555
0.819152044289
0.809016994375
0.798635510047
0.788010753607
0.777145961457
0.766044443119
0.754709580223
0.743144825477
0.731353701619
0.719339800339
0.707106781187
0.694658370459
0.681998360062
0.669130606359
0.656059028991
0.642787609687
0.62932039105
0.615661475326
0.601815023152
0.587785252292
0.573576436351
0.559192903471
0.544639035015
0.529919264233
0.51503807491
0.5
0.484809620246
0.469471562786
0.45399049974
0.438371146789
0.422618261741
0.406736643076
0.390731128489
0.374606593416
0.358367949545
0.342020143326
0.325568154457
0.309016994375
0.292371704723
0.275637355817
0.258819045103
0.2419218956
0.224951054344
0.207911690818
0.190808995377
0.173648177667
0.15643446504
0.13917310096
0.121869343405
0.104528463268
0.0871557427477
0.0697564737441
0.0523359562429
0.0348994967025
0.0174524064373
1.22464679915e-16
-0.0174524064373
-0.0348994967025
-0.0523359562429
-0.0697564737441
-0.0871557427477
-0.104528463268
-0.121869343405
-0.13917310096
-0.15643446504
-0.173648177667
-0.190808995377
-0.207911690818
-0.224951054344
-0.2419218956
-0.258819045103
-0.275637355817
-0.292371704723
-0.309016994375
-0.325568154457
-0.342020143326
-0.358367949545
-0.374606593416
-0.390731128489
-0.406736643076
-0.422618261741
-0.438371146789
-0.45399049974
-0.469471562786
-0.484809620246
-0.5
-0.51503807491
-0.529919264233
-0.544639035015
-0.559192903471
-0.573576436351
-0.587785252292
-0.601815023152
-0.615661475326
-0.62932039105
-0.642787609687
-0.656059028991
-0.669130606359
-0.681998360062
-0.694658370459
-0.707106781187
-0.719339800339
-0.731353701619
-0.743144825477
-0.754709580223
-0.766044443119
-0.777145961457
-0.788010753607
-0.798635510047
-0.809016994375
-0.819152044289
-0.829037572555
-0.838670567945
-0.848048096156
-0.857167300702
-0.866025403784
-0.874619707139
-0.882947592859
-0.891006524188
-0.898794046299
-0.906307787037
-0.913545457643
-0.920504853452
-0.927183854567
-0.933580426497
-0.939692620786
-0.945518575599
-0.951056516295
-0.956304755963
-0.961261695938
-0.965925826289
-0.970295726276
-0.974370064785
-0.978147600734
-0.981627183448
-0.984807753012
-0.987688340595
-0.990268068742
-0.992546151641
-0.994521895368
-0.996194698092
-0.99756405026
-0.998629534755
-0.999390827019
-0.999847695156
-1.0
-0.999847695156
-0.999390827019
-0.998629534755
-0.99756405026
-0.996194698092
-0.994521895368
-0.992546151641
-0.990268068742
-0.987688340595
-0.984807753012
-0.981627183448
-0.978147600734
-0.974370064785
-0.970295726276
-0.965925826289
-0.961261695938
-0.956304755963
-0.951056516295
-0.945518575599
-0.939692620786
-0.933580426497
-0.927183854567
-0.920504853452
-0.913545457643
-0.906307787037
-0.898794046299
-0.891006524188
-0.882947592859
-0.874619707139
-0.866025403784
-0.857167300702
-0.848048096156
-0.838670567945
-0.829037572555
-0.819152044289
-0.809016994375
-0.798635510047
-0.788010753607
-0.777145961457
-0.766044443119
-0.754709580223
-0.743144825477
-0.731353701619
-0.719339800339
-0.707106781187
-0.694658370459
-0.681998360062
-0.669130606359
-0.656059028991
-0.642787609687
-0.62932039105
-0.615661475326
-0.601815023152
-0.587785252292
-0.573576436351
-0.559192903471
-0.544639035015
-0.529919264233
-0.51503807491
-0.5
-0.484809620246
-0.469471562786
-0.45399049974
-0.438371146789
-0.422618261741
-0.406736643076
-0.390731128489
-0.374606593416
-0.358367949545
-0.342020143326
-0.325568154457
-0.309016994375
-0.292371704723
-0.275637355817
-0.258819045103
-0.2419218956
-0.224951054344
-0.207911690818
-0.190808995377
-0.173648177667
-0.15643446504
-0.13917310096
-0.121869343405
-0.104528463268
-0.0871557427477
-0.0697564737441
-0.0523359562429
-0.0348994967025
-0.0174524064373
-2.44929359829e-16
0.0174524064373
0.0348994967025
0.0523359562429
0.0697564737441
0.0871557427477
0.104528463268
0.121869343405
0.13917310096
0.15643446504
0.173648177667
0.190808995377
0.207911690818
0.224951054344
0.2419218956
0.258819045103
0.275637355817
0.292371704723
0.309016994375
0.325568154457
0.342020143326
0.358367949545
0.374606593416
0.390731128489
0.406736643076
0.422618261741
0.438371146789
0.45399049974
0.469471562786
0.484809620246
0.5
0.51503807491
0.529919264233
0.544639035015
0.559192903471
0.573576436351
0.587785252292
0.601815023152
0.615661475326
0.62932039105
0.642787609687
0.656059028991
0.669130606359
0.681998360062
0.694658370459
0.707106781187
0.719339800339
0.731353701619
0.743144825477
0.754709580223
0.766044443119
0.777145961457
0.788010753607
0.798635510047
0.809016994375
0.819152044289
0.829037572555
0.838670567945
0.848048096156
0.857167300702
0.866025403784
0.874619707139
0.882947592859
0.891006524188
0.898794046299
0.906307787037
0.913545457643
0.920504853452
0.927183854567
0.933580426497
0.939692620786
0.945518575599
0.951056516295
0.956304755963
0.961261695938
0.965925826289
0.970295726276
0.974370064785
0.978147600734
0.981627183448
0.984807753012
0.987688340595
0.990268068742
0.992546151641
0.994521895368
0.996194698092
0.99756405026
0.998629534755
0.999390827019
0.999847695156
1.0
0.999847695156
0.999390827019
0.998629534755
0.99756405026
0.996194698092
0.994521895368
0.992546151641
0.990268068742
0.987688340595
0.984807753012
0.981627183448
0.978147600734
0.974370064785
0.970295726276
0.965925826289
0.961261695938
0.956304755963
0.951056516295
0.945518575599
0.939692620786
0.933580426497
0.927183854567
0.920504853452
0.913545457643
0.906307787037
0.898794046299
0.891006524188
0.882947592859
0.874619707139
0.866025403784
0.857167300702
0.848048096156
0.838670567945
0.829037572555
0.819152044289
0.809016994375
0.798635510047
0.788010753607
0.777145961457
0.766044443119
0.754709580223
0.743144825477
0.731353701619
0.719339800339
0.707106781187
0.694658370459
0.681998360062
0.669130606359
0.656059028991
0.642787609687
0.62932039105
0.615661475326
0.601815023152
0.587785252292
0.573576436351
0.559192903471
0.544639035015
0.529919264233
0.51503807491
0.5
0.484809620246
0.469471562786
0.45399049974
0.438371146789
0.422618261741
0.406736643076
0.390731128489
0.374606593416
0.358367949545
0.342020143326
0.325568154457
0.309016994375
0.292371704723
0.275637355817
0.258819045103
0.2419218956
0.224951054344
0.207911690818
0.190808995377
0.173648177667
0.15643446504
0.13917310096
0.121869343405
0.104528463268
0.0871557427477
0.0697564737441
0.0523359562429
0.0348994967025
0.0174524064373
3.67394039744e-16
-0.0174524064373
-0.0348994967025
-0.0523359562429
-0.0697564737441
-0.0871557427477
-0.104528463268
-0.121869343405
-0.13917310096
-0.15643446504
-0.173648177667
-0.190808995377
-0.207911690818
-0.224951054344
-0.2419218956
-0.258819045103
-0.275637355817
-0.292371704723
-0.309016994375
-0.325568154457
-0.342020143326
-0.358367949545
-0.374606593416
-0.390731128489
-0.406736643076
-0.422618261741
-0.438371146789
-0.45399049974
-0.469471562786
-0.484809620246
-0.5
-0.51503807491
-0.529919264233
-0.544639035015
-0.559192903471
-0.573576436351
-0.587785252292
-0.601815023152
-0.615661475326
-0.62932039105
-0.642787609687
-0.656059028991
-0.669130606359
-0.681998360062
-0.694658370459
-0.707106781187
-0.719339800339
-0.731353701619
-0.743144825477
-0.754709580223
-0.766044443119
-0.777145961457
-0.788010753607
-0.798635510047
-0.809016994375
-0.819152044289
-0.829037572555
-0.838670567945
-0.848048096156
-0.857167300702
-0.866025403784
-0.874619707139
-0.882947592859
-0.891006524188
-0.898794046299
-0.906307787037
-0.913545457643
-0.920504853452
-0.927183854567
-0.933580426497
-0.939692620786
-0.945518575599
-0.951056516295
-0.956304755963
-0.961261695938
-0.965925826289
-0.970295726276
-0.974370064785
-0.978147600734
-0.981627183448
-0.984807753012
-0.987688340595
-0.990268068742
-0.992546151641
-0.994521895368
-0.996194698092
-0.99756405026
-0.998629534755
-0.999390827019
-0.999847695156
-1.0
-0.999847695156
-0.999390827019
-0.998629534755
-0.99756405026
-0.996194698092
-0.994521895368
-0.992546151641
-0.990268068742
-0.987688340595
-0.984807753012
-0.981627183448
-0.978147600734
-0.974370064785
-0.970295726276
-0.965925826289
-0.961261695938
-0.956304755963
-0.951056516295
-0.945518575599
-0.939692620786
-0.933580426497
-0.927183854567
-0.920504853452
-0.913545457643
-0.906307787037
-0.898794046299
-0.891006524188
-0.882947592859
-0.874619707139
-0.866025403784
-0.857167300702
-0.848048096156
-0.838670567945
-0.829037572555
-0.819152044289
-0.809016994375
-0.798635510047
-0.788010753607
-0.777145961457
-0.766044443119
-0.754709580223
-0.743144825477
-0.731353701619
-0.719339800339
-0.707106781187
-0.694658370459
-0.681998360062
-0.669130606359
-0.656059028991
-0.642787609687
-0.62932039105
-0.615661475326
-0.601815023152
-0.587785252292
-0.573576436351
-0.559192903471
-0.544639035015
-0.529919264233
-0.51503807491
-0.5
-0.484809620246
-0.469471562786
-0.45399049974
-0.438371146789
-0.422618261741
-0.406736643076
-0.390731128489
-0.374606593416
-0.358367949545
-0.342020143326
-0.325568154457
-0.309016994375
-0.292371704723
-0.275637355817
-0.258819045103
-0.2419218956
-0.224951054344
-0.207911690818
-0.190808995377
-0.173648177667
-0.15643446504
-0.13917310096
-0.121869343405
-0.104528463268
-0.0871557427477
-0.0697564737441
-0.0523359562429
-0.0348994967025
-0.0174524064373
-4.89858719659e-16
0.0174524064373
0.0348994967025
0.0523359562429
0.0697564737441
0.0871557427477
0.104528463268
0.121869343405
0.13917310096
0.15643446504
0.173648177667
0.190808995377
0.207911690818
0.224951054344
0.2419218956
0.258819045103
0.275637355817
0.292371704723
0.309016994375
0.325568154457
0.342020143326
0.358367949545
0.374606593416
0.390731128489
0.406736643076
0.422618261741
0.438371146789
0.45399049974
0.469471562786
0.484809620246
0.5
0.51503807491
0.529919264233
0.544639035015
0.559192903471
0.573576436351
0.587785252292
0.601815023152
0.615661475326
0.62932039105
0.642787609687
0.656059028991
0.669130606359
0.681998360062
0.694658370459
0.707106781187
0.719339800339
0.731353701619
0.743144825477
0.754709580223
0.766044443119
0.777145961457
0.788010753607
0.798635510047
0.809016994375
0.819152044289
0.829037572555
0.838670567945
0.848048096156
0.857167300702
0.866025403784
0.874619707139
0.882947592859
0.891006524188
0.898794046299
0.906307787037
0.913545457643
0.920504853452
0.927183854567
0.933580426497
0.939692620786
0.945518575599
0.951056516295
0.956304755963
0.961261695938
0.965925826289
0.970295726276
0.974370064785
0.978147600734
0.981627183448
0.984807753012
0.987688340595
0.990268068742
0.992546151641
0.994521895368
0.996194698092
0.99756405026
0.998629534755
0.999390827019
0.999847695156
1.0
0.999847695156
0.999390827019
0.998629534755
0.99756405026
0.996194698092
0.994521895368
0.992546151641
0.990268068742
0.987688340595
0.984807753012
0.981627183448
0.978147600734
0.974370064785
0.970295726276
0.965925826289
0.961261695938
0.956304755963
0.951056516295
0.945518575599
0.939692620786
0.933580426497
0.927183854567
0.920504853452
0.913545457643
0.906307787037
0.898794046299
0.891006524188
0.882947592859
0.874619707139
0.866025403784
0.857167300702
0.848048096156
0.838670567945
0.829037572555
0.819152044289
0.809016994375
0.798635510047
0.788010753607
0.777145961457
0.766044443119
0.754709580223
0.743144825477
0.731353701619
0.719339800339
0.707106781187
0.694658370459
0.681998360062
0.669130606359
0.656059028991
0.642787609687
0.62932039105
0.615661475326
0.601815023152
0.587785252292
0.573576436351
0.559192903471
0.544639035015
0.529919264233
0.51503807491
0.5
0.484809620246
0.469471562786
0.45399049974
0.438371146789
0.422618261741
0.406736643076
0.390731128489
0.374606593416
0.358367949545
0.342020143326
0.325568154457
0.309016994375
0.292371704723
0.275637355817
0.258819045103
0.2419218956
0.224951054344
0.207911690818
0.190808995377
0.173648177667
0.15643446504
0.13917310096
0.121869343405
0.104528463268
0.0871557427477
0.0697564737441
0.0523359562429
0.0348994967025
0.0174524064373
6.12323399574e-16
-0.0174524064373
-0.0348994967025
-0.0523359562429
-0.0697564737441
-0.0871557427477
-0.104528463268
-0.121869343405
-0.13917310096
-0.15643446504
-0.173648177667
-0.190808995377
-0.207911690818
-0.224951054344
-0.2419218956
-0.258819045103
-0.275637355817
-0.292371704723
-0.309016994375
-0.325568154457
-0.342020143326
-0.358367949545
-0.374606593416
-0.390731128489
-0.406736643076
-0.422618261741
-0.438371146789
-0.45399049974
-0.469471562786
-0.484809620246
-0.5
-0.51503807491
-0.529919264233
-0.544639035015
-0.559192903471
-0.573576436351
-0.587785252292
-0.601815023152
-0.615661475326
-0.62932039105
-0.642787609687
-0.656059028991
-0.669130606359
-0.681998360062
-0.694658370459
-0.707106781187
-0.719339800339
-0.731353701619
-0.743144825477
-0.754709580223
-0.766044443119
-0.777145961457
-0.788010753607
-0.798635510047
-0.809016994375
-0.819152044289
-0.829037572555
-0.838670567945
-0.848048096156
-0.857167300702
-0.866025403784
-0.874619707139
-0.882947592859
-0.891006524188
-0.898794046299
-0.906307787037
-0.913545457643
-0.920504853452
-0.927183854567
-0.933580426497
-0.939692620786
-0.945518575599
-0.951056516295
-0.956304755963
-0.961261695938
-0.965925826289
-0.970295726276
-0.974370064785
-0.978147600734
-0.981627183448
-0.984807753012
-0.987688340595
-0.990268068742
-0.992546151641
-0.994521895368
-0.996194698092
-0.99756405026
-0.998629534755
-0.999390827019
-0.999847695156
-1.0
-0.999847695156
-0.999390827019
-0.998629534755
-0.99756405026
-0.996194698092
-0.994521895368
-0.992546151641
-0.990268068742
-0.987688340595
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment