How to use Chart.js in Nodejs? - node.js

How to create a pie chart using NodeJs with chartjs?
want to create different types of charts using chartjs but when I tried to run the code shows "cannot set the property of width"
TypeError: Cannot set property 'width' of undefined
at ni.resize (node_modules\chart.js\dist\Chart.min.js:7:93312)
at ni.initialize (node_modules\chart.js\dist\Chart.min.js:7:92818)
at ni.construct (node_modules\chart.js\dist\Chart.min.js:7:92559)
at new ni (node_modules\chart.js\dist\Chart.min.js:7:91964)
at Object.<anonymous> (chartjs\chart.js:7:15)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
I tried to run this code to produce a pie chart image but found this error.
let Canvas = require("canvas"),
canvas = Canvas.createCanvas(400, 400),
ctx = canvas.getContext("2d"),
Chart = require('chart.js'),
fs = require("fs");
var myChart = new Chart(ctx, {
type: "pie",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "# of Votes",
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)"
],
borderColor: [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)"
],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
]
}
}
});
canvas.toBuffer(function(err, buf) {
if (err) throw err;
fs.writeFileSync("chart.png", buf);
});
Should be an image in the current folder

I suggest using ChartJS for Node: https://www.npmjs.com/package/chartjs-node
It draws a chart which you can then save to the file system and insert accordingly.
Here's a snippet of code as an example:
import * as ChartjsNode from 'chartjs-node'; // Import the library
const chartNode = new ChartjsNode(600, 600); // Create an instance with dimensions
const barGraphOptions = {
type: 'bar',
data: []
};
// Draw the chart and write the file to the file system
await new Promise(resolve => {
chartNode
.drawChart(barGraphOptions)
.then(() => {
chartNode.getImageBuffer('image/png');
})
.then(() => {
chartNode.writeImageToFile('image/png', 'some_file.png').then(() => {
resolve();
});
})
.catch(e => {
console.log('Caught', e);
});
});

ChartJS is an HTML5 based file which is used in front-end development only.
For more information visit this site.
You can refer to this answer if you want to use nodejs to create Charts.
P.S. This question is duplicate to this

I had the same issue as OP. I couldn't figure out a way to do it using the standard approach of combining Node Canvas and Chart.JS, but I found using ChartjsNodeCanvas works.
import { CanvasRenderService } from 'chartjs-node-canvas';
canvasRenderService: CanvasRenderService;
async createGraph(data: any) {
const configuration = {
type: 'bar',
data: {
labels: // Your labels,
datasets: [
data
]
},
options: {
// Your options
}
};
const canvasRenderService = new CanvasRenderService(300, 500);
return await canvasRenderService.renderToDataURL(configuration);
// or return canvasRenderService.renderToBuffer(configuration);
// or return canvasRenderService.renderToStream(configuration);
}
await this.createGraph(data);
Note, ChartjsNodeCanvas requires ChartJS as a dependency.

Related

Issue in implementing ChartJS in my new assignment of ReactJS App, please let me know where I am wrong

I want to upload a chart in my react app but when I use this Barchar.jsx component (have used js nothing changed) it is showing following errors in console(are in image.)
import React from "react";
import { Bar, Chart } from "react-chartjs-2";
import {Chart as ChartJS, BarElement } from 'chart.js';
ChartJS.register(BarElement)
const BarChart = () => {
var data = {
labels: ['RCB', 'MI', 'RR', 'SRH', 'CSK', 'KXIP', 'DD', 'DCH', 'GL', 'RPS', 'KKR'],
datasets: [{
label: '# of toss wins',
data: [70, 85, 63, 35, 66, 68, 72, 43, 15, 6, 78],
backgroungColor: ['red', 'lightblue', 'pink', 'orange', 'yellow', 'gold', 'blue', 'black', 'gold', 'voilet', 'purple' ],
borderWidth: 1
}]
};
var options = {
maintainAspectRatio: false,
scales: {
y:{
beginAtZero: true
}
},
};
return (
<div>
<Bar
data= {data}
height={400}
width={600}
options={options}
/>
</div>
)
};
export default BarChart ;
Its because you try to use treeshaking but import and register only the element of the bar while chart.js needs a lot more.
For ease of use you are best of to change
import {Chart as ChartJS, BarElement } from 'chart.js';
ChartJS.register(BarElement)
into:
import 'chart.js/auto'
If you really want to use treeshaking you should look at the docs and import and register everything you are using:
https://www.chartjs.org/docs/3.7.1/getting-started/integration.html#bundlers-webpack-rollup-etc
SO BASSICALLY MISTAKE WHICH I DID WAS I DIDN'T IMPORT THIS BARCHART IN APPJS.CORRECT CCODE WILL BE LIKE THIS (FOR BARCHART.JSX) YOU CAN CHANGE IN IT MANY THINGS.
import React from "react";
import { Bar } from "react-chartjs-2";
import 'chart.js/auto'
const BarChart =() => {
return <div>
<Bar
options={{
scales: {
y: {
beginAtZero: true
}
}
}}
data ={{
labels: ['RCB', 'MI', 'RR', 'SRH', 'CSK', 'KXIP', 'DD', 'DCH', 'GL', 'RPS', 'KKR'],
datasets: [{
label: '# of toss wins',
data: [70, 85, 63, 35, 66, 68, 72, 43, 15, 6, 78],
backgroundColor: ['red', 'Blue', '#ff64dc', '#fc7404', 'yellow', 'red', '#000080', 'silver', 'gold', '#9400d3', 'purple' ],
borderColor:['black'],
borderWidth: 1
},
{
label: '# of match wins',
data: [73, 92, 63, 42, 79, 70, 62, 29, 13, 10, 77],
backgroundColor: ['#C9920E', 'Blue', '#ff64dc', '#fc7404', 'yellow', 'lightgrey', '#000080', 'silver', 'gold', '#9400d3', 'purple' ],
borderColor:['black'],
borderWidth: 1
}
]
}}
/></div>
};
export default BarChart ;

Using Datalabels and ChartJS with Chartjs-node-canvas

I use NodeJS with ChartJS to render graph directly to a file and it works fine.
I do it by ChartjsNodeCanvas npm module installed.
In order to display labels on the graph I use official ChartJS plugin ChartJS-plugin-datalabels
But it seems like Canvas module has a different scheme of activating ChartJS plugins and most probably I activate datalabels plugin incorrectly. So I can’t get the labels displayed on the graph.
const Chart = require('chart.js');
const datalabels = require('chartjs-plugin-datalabels')
const { CanvasRenderService } = require('chartjs-node-canvas');
const width = 600;
const height = 400;
const chartCallback = (ChartJS) => {
ChartJS.defaults.global.elements.rectangle.borderWidth = 2;
ChartJS.plugins.register({
datalabels
});
};
const canvasRenderService = new CanvasRenderService(width, height, chartCallback);
(async () => {
const configuration = {
type: 'bar',
data: {
datasets: [
{
type:"bar",
barThickness: 24,
label: 'My dataset',
data: ydata['data'],
backgroundColor: 'rgba(75,192, 192, 0.2)',
borderColor: 'rgba(54, 162, 135, 0.2)',
borderWidth: 1,
datalabels: {
align: 'start',
anchor: 'start'
}
},
labels: xdata,
}
options: {
scales: {
yAxes: [
{
id: 'left-y-axis',
position: 'left',
stacked: false,
ticks: {
beginAtZero: true,
callback: (value) => value + "R"
},
},
{
id: 'right-y-axis',
position: 'right'
}
],
xAxes: [{
stacked: true,
}]
},
plugins: {
datalabels: {
backgroundColor: function(context) {
return context.dataset.backgroundColor;
},
borderRadius: 4,
color: 'red',
font: {
weight: 'bold'
},
formatter: Math.round
}
}
}
};
const image = await canvasRenderService.renderToBuffer(configuration);
fs.writeFileSync(tgMsgPath+"test.png", image)
})();
The chart is shown but no labels on the graph.
This is how to activate plugins with ChartJs-niode-canvas:
const chartJsFactory = () => {
const Chart = require('chart.js');
require('chartjs-plugin-datalabels');
delete require.cache[require.resolve('chart.js')];
delete require.cache[require.resolve('chartjs-plugin-datalabels')];
return Chart;
}
// ...
const canvasRenderService = new CanvasRenderService(
chartWidth,
chartHeight,
chartCallback,
undefined,
chartJsFactory
);

var collider= this.physics.add.overlap(player, pipe, perder, false, this);

Im making this game and I got some problems with an overlap function.
So, the main problem is, when the player touch 2 pipes at same time, the player loses 2 lives instead of 1. This occurs because player touches 2 pixels at same time, 1 from one pipe and 1 from another. So I create this collider flag "collider" to just call one time the function "perder" and not 2 like previously.
...
addOnePipe: function(x, y) {
pipe = this.physics.add.image(x, y, 'br').setScale(1.1);
pipe.setVelocityX(-150);
this.pipes.addMultiple(pipe);
pipe.checkWorldBounds = true;
pipe.outOfBoundsKill = true;
***var collider = this.physics.add.overlap(player, pipe, perder, false, this);***
},
addRowOfPipes: function() {
var hole = Phaser.Math.Between(1, 6);
//var hole=6;
enter code here
for (var i = 0; i < 8; i++)
if (i != hole && i != hole + 1){
this.addOnePipe(760, i *54 + 40);
}
},
});
***function perder (player,pipe) {
vida--;
if (vida==0){
this.scene.start('gameover');
}
else(this.scene.start('game2'));
collider.active = false;
}***
The full game is here:
var Preloader = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function Preloader ()
{
Phaser.Scene.call(this, { key: 'preloader' });
},
preload: function ()
{
this.load.image('imgfinal', 'https://cdn.shopify.com/s/files/1/0769/4313/products/GAMEOVER_copy_1024x1024.jpg?v=1508717540');
this.load.image('br', 'assets/sprites/crate32.png');
this.load.image('ground2', 'http://images6.fanpop.com/image/photos/40700000/Backround-geometry-dash-40776456-250-250.jpg');
this.load.image('sky', 'https://st2.depositphotos.com/7267546/10689/v/950/depositphotos_106891270-stock-illustration-cartoon-nature-landscape-for-game.jpg');
this.load.image('final2', 'http://3.bp.blogspot.com/--aDP7X-70Yo/T0epwcsA0EI/AAAAAAAAAjU/D6b1H0gs8_4/s1600/fixe+5+estrelas.jpg');
this.load.image('final', 'https://images-na.ssl-images-amazon.com/images/I/81MPs6C9M8L._SY355_.png');
this.load.image('ground', 'https://lh3.googleusercontent.com/-8_3XLw9ontM/VsmYVcGUStI/AAAAAAAAAgE/ZGFffMQDNFw/w318-h318-n-rw/bg_03.png');
this.load.image('bomb', 'src/games/firstgame/assets/bomb.png');
this.load.image('sky2', 'https://st2.depositphotos.com/5479200/11693/v/950/depositphotos_116931742-stock-illustration-game-background-2d-application-vector.jpg');
this.load.image('triangle', 'assets/sprites/triangle.png');
this.load.image('branco', 'https://madeirasgasometro.vteximg.com.br/arquivos/ids/170727-490-490/pp40-branco-neve-square.jpg?v=636801256712100000');
this.load.image('back','https://http2.mlstatic.com/piso-de-granito-verde-ubatuba-D_NQ_NP_798225-MLB25401365082_022017-F.jpg');
this.load.image('play', 'https://http2.mlstatic.com/revestimento-adesivo-decofix-marmore-cinza-2-metros-D_NQ_NP_421311-MLB20503488532_112015-F.jpg');
this.load.spritesheet('dude', 'src/games/firstgame/assets/dude.png', { frameWidth: 32, frameHeight: 48 });
this.load.image('tubo', 'assets/sprites/block.png');
this.load.audio('drums', ['assets/audio/tech/drums.ogg', 'assets/audio/tech/drums.mp3']);
this.load.audio('synth2', ['assets/audio/tech/synth2.ogg', 'assets/audio/tech/synth2.mp3']);
this.load.audio('boden', ['assets/audio/bodenstaendig_2000_in_rock_4bit.mp3', 'assets/audio/bodenstaendig_2000_in_rock_4bit.ogg']);
this.load.image('reverse','http://www.euvosescrevi.com.br/wp-content/uploads/2016/04/earth.jpg');
this.load.image('star', 'src/games/firstgame/assets/star.png');
this.load.setPath('assets/audio/kyobi/');
this.load.audioSprite('kyobi', 'kyobi.json');
},
create: function ()
{
this.scene.start('mainmenu');
}
});
var MainMenu = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function MainMenu ()
{
Phaser.Scene.call(this, { key: 'mainmenu' });
window.MENU = this;
},
create: function ()
{
vida=3;
var back= this.add.image(400,300,'back');
var play= this.add.image(400,250,'play').setScale(0.2);
var play1;
play1= this.add.image(400,400,'play').setScale(0.2);
this.add.text(240, 70, '"The Runner" ', { font: '50px Arial', fill: '#ffffff' });
this.add.text(337.5, 230, 'PLAY', { font: '50px Courier', fill: '#00000' });
this.add.text(335, 360, 'HOW', { font: '50px Courier', fill: '#00000' });
this.add.text(400, 400, 'TO', { font: '50px Courier', fill: '#00000' });
play1.setInteractive();
play1.once('pointerup', function () {
this.scene.start('rules');
}, this);
play.setInteractive();
play.once('pointerup', function () {
this.scene.start('game2');
}, this);
}
});
var Rules = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function Rules ()
{
Phaser.Scene.call(this, { key: 'rules' });
window.MENU = this;
},
create: function ()
{
var back= this.add.image(400,300,'back');
var play3= this.add.image(650,500,'play').setScale(0.2);
this.add.text(590, 480, 'BACK', { font: '50px Courier', fill: '#000000' });
this.add.text(20, 40, 'Welcome to "The Runner" ', { font: '50px Times', fill: '#ffffff' });
this.add.text(60, 115, 'Rules: ', { font: '40px Times', fill: '#ffffff' });
this.add.text(65, 180, 'The aren´t any rules for this game. Just give your best to', { font: '30px Times', fill: '#ffffff' });
this.add.text(65, 220, 'complete the maps and DONT USE CHEATS!! ', { font: '30px Times', fill: '#ffffff' });
this.add.text(70, 300, 'Controls: Up Arrow ', { font: '30px Times', fill: '#ffffff' });
this.add.text(190, 350, 'Left Arrow ', { font: '30px Times', fill: '#ffffff' });
this.add.text(190, 400, 'Right Arrow ', { font: '30px Times', fill: '#ffffff' });
this.add.text(328, 304.5, '|', { font: '20px Times', fill: '#ffffff' });
this.add.text(320, 290, '^', { font: '40px Times', fill: '#ffffff' });
this.add.text(332, 353, '<', { font: '30px Times', fill: '#ffffff' });
this.add.text(346, 327, '_', { font: '40px Times', fill: '#ffffff' });
this.add.text(370, 401, '>', { font: '30px Times', fill: '#ffffff' });
this.add.text(350, 375, '_', { font: '40px Times', fill: '#ffffff' });
play3.setInteractive();
play3.once('pointerup', function () {
this.scene.start('mainmenu');
}, this);
}
});
var Game = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function Game ()
{
Phaser.Scene.call(this, { key: 'game' });
window.GAME = this;
var player;
var platforms;
var cursors;
var tubos;
var timer;
var star;
},
create: function ()
{
drums = this.sound.add('drums');
synth2 = this.sound.add('synth2');
boden = this.sound.add('boden');
var loopMarker = {
name: 'loop',
start: 0,
duration: 7.68,
config: {
loop: true
}
}
drums.addMarker(loopMarker);
drums.play('loop', {
delay: 0
});
synth2.addMarker(loopMarker);
synth2.play('loop', {
delay: 0
});
this.cameras.main.setBounds(0, 0, 5500, 600);
this.physics.world.setBounds(0, 0, 5500, 600);
this.add.image(0, 0, 'sky').setOrigin(0).setScrollFactor(1);
this.add.image(1000, 0, 'sky').setOrigin(0).setScrollFactor(1);
this.add.image(3899.5, 0, 'sky').setOrigin(0).setScrollFactor(1);
this.add.image(2000, 0, 'reverse').setOrigin(0).setScrollFactor(1);
cursors = this.input.keyboard.createCursorKeys();
player = this.physics.add.sprite(100, 300, 'dude').setGravity(0, 300);
this.cameras.main.startFollow(player, true, 0.08, 0.08);
this.cameras.main.setZoom(1);
tubos= this.physics.add.staticGroup();
tubos.create(300, 20, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 62, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 177, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 219, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 261, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 303, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 345, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 387, 'tubo').setScale(0.45).refreshBody();
tubos.create(300, 429, 'tubo').setScale(0.45).refreshBody();
//------------------------------------------------------------
tubos.create(500, 20, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 62, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 104, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 146, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 188, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 230, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 345, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 387, 'tubo').setScale(0.45).refreshBody();
tubos.create(500, 429, 'tubo').setScale(0.45).refreshBody();
//------------------------------------------------------------
tubos.create(700, 70, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 110, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 150, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 190, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 230, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 270, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 310, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 350, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 390, 'tubo').setScale(0.44).refreshBody();
tubos.create(700, 430, 'tubo').setScale(0.44).refreshBody();
//------------------------------------------------------------
tubos.create(900, 20, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 60, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 150, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 190, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 230, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 270, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 310, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 350, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 390, 'tubo').setScale(0.44).refreshBody();
tubos.create(900, 430, 'tubo').setScale(0.44).refreshBody();
//------------------------------------------------------------
tubos.create(1100, 0, 'tubo').setScale(0.44).refreshBody();
tubos.create(1100, 100, 'tubo').setScale(0.44).refreshBody();
tubos.create(1100, 140, 'tubo').setScale(0.44).refreshBody();
tubos.create(1100, 180, 'tubo').setScale(0.44).refreshBody();
tubos.create(1100, 220, 'tubo').setScale(0.44).refreshBody();
tubos.create(1101, 260, 'tubo').setScale(0.44).refreshBody();
tubos.create(1141, 304, 'tubo').setScale(0.50).refreshBody();
tubos.create(1100, 350, 'tubo').setScale(0.44).refreshBody();
tubos.create(1100, 390, 'tubo').setScale(0.44).refreshBody();
tubos.create(1100, 430, 'tubo').setScale(0.44).refreshBody();
//------------------------------------------------------------
tubos.create(1300, 65, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 105, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 145, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 185, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 225, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 265, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 306, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 347, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 388, 'tubo').setScale(0.43).refreshBody();
tubos.create(1300, 429, 'tubo').setScale(0.43).refreshBody();
//------------------------------------------------------------
tubos.create(1400, 260,'tubo').setScale(0.43).refreshBody();
tubos.create(1450, 95, 'tubo').setScale(0.43).refreshBody();
tubos.create(1570, 95, 'tubo').setScale(0.43).refreshBody();
tubos.create(1690, 95, 'tubo').setScale(0.43).refreshBody();
tubos.create(1810, 95, 'tubo').setScale(0.43).refreshBody();
tubos.create(1930, 95, 'tubo').setScale(0.43).refreshBody();
//---------------------------------------------------
tubos.create(1977, 132, 'tubo').setScale(0.46).refreshBody();
tubos.create(1977, 174, 'tubo').setScale(0.46).refreshBody();
tubos.create(1977, 216, 'tubo').setScale(0.46).refreshBody();
tubos.create(1977, 258, 'tubo').setScale(0.46).refreshBody();
tubos.create(1977, 300, 'tubo').setScale(0.46).refreshBody();
tubos.create(1977, 342, 'tubo').setScale(0.46).refreshBody();
tubos.create(1977, 384, 'tubo').setScale(0.46).refreshBody();
tubos.create(1977, 427, 'tubo').setScale(0.46).refreshBody();
//------------------------------------------------------------
tubos.create(2350, 100, 'tubo').setScale(0.43).refreshBody();
tubos.create(2600, 160, 'tubo').setScale(0.43).refreshBody();
tubos.create(2850, 230, 'tubo').setScale(0.43).refreshBody();
tubos.create(3100, 290, 'tubo').setScale(0.43).refreshBody();
tubos.create(3350, 360, 'tubo').setScale(0.43).refreshBody();
tubos.create(3600, 360, 'tubo').setScale(0.43).refreshBody();
tubos.create(3850, 455, 'tubo').setScale(0.43).refreshBody();
tubos.create(3850, 395, 'tubo').setScale(0.43).refreshBody();
tubos.create(3850, 335, 'tubo').setScale(0.43).refreshBody();
tubos.create(3850, 275, 'tubo').setScale(0.43).refreshBody();
tubos.create(3850, 215, 'tubo').setScale(0.43).refreshBody();
tubos.create(3850, 155, 'tubo').setScale(0.43).refreshBody();
tubos.create(3850, 90, 'tubo').setScale(0.43).refreshBody();
//------------------------------------------------------------
tubos.create(4300, 130, 'tubo').setScale(0.43).refreshBody();
tubos.create(4300, 180, 'tubo').setScale(0.43).refreshBody();
tubos.create(4300, 230, 'tubo').setScale(0.43).refreshBody();
tubos.create(4300, 280, 'tubo').setScale(0.43).refreshBody();
tubos.create(4300, 330, 'tubo').setScale(0.43).refreshBody();
tubos.create(4300, 380, 'tubo').setScale(0.43).refreshBody();
tubos.create(4300, 430, 'tubo').setScale(0.43).refreshBody();
star = this.physics.add.staticGroup();
star.create(4300,90, 'star');
platforms = this.physics.add.staticGroup();
platforms.create(-225, 768, 'ground').setScale(2).refreshBody();
platforms.create(411, 768, 'ground').setScale(2).refreshBody();
platforms.create(1047, 768, 'ground').setScale(2).refreshBody();
platforms.create(1682, 768, 'ground').setScale(2).refreshBody();
platforms.create(4238, 768, 'ground').setScale(2).refreshBody();
platforms.create(4605, 768, 'ground').setScale(2).refreshBody();
//-----------------------------------------------------------------
platforms.create(2047.7,0,'ground').setScale(0.3).refreshBody();
platforms.create(2140, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2233, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2326, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2419, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2512, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2605, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2698, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2791, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2884, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(2977, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3070, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3163, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3256, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3349, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3442, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3535, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3628, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3721, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3814, 0, 'ground').setScale(0.3).refreshBody();
platforms.create(3872, 0, 'ground').setScale(0.3).refreshBody();
player.setBounce(0.1);
player.setCollideWorldBounds(true);
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [ { key: 'dude', frame: 4 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
this.physics.add.collider(player, platforms);
this.physics.add.collider(player, tubos);
this.physics.add.overlap(player, star, collectStar, null, this);
info = this.add.text(10, 7, '', { font: '48px Arial', fill: '#ff0000' });
info2 = this.add.text(19, 50, '', { font: '48px Arial', fill: '#ff0000' });
timer = this.time.addEvent({ delay: 46000, callback: Gameover,callbackScope: this, });
info.setScrollFactor(0);
info2.setScrollFactor(0);
},
update: function ()
{
if ( player.x >= 2000 && player.x <=3915 ) {
player.angle = -180;
player.setGravity(0, -100);
if (cursors.left.isDown)
{
player.setVelocityX(-90);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.setVelocityX(90);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown && player.body.touching.up)
{
player.setVelocityY(-1500);
}
}
else if (player.x <2000 || player.x>3915){
player.angle=0;
player.setGravity(0, 300);
}
if (cursors.left.isDown)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.setVelocityX(160);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown && player.body.touching.down)
{
player.setVelocityY(-470);
}
info.setText('Time: ' + Math.floor(46000 - timer.getElapsed()));
info2.setText('Lifes: ' + vida);
}
});
var Game2 = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function Game2 ()
{
Phaser.Scene.call(this, { key: 'game2' });
window.GAME = this;
var timer1;
var platforms;
var player;
var pipe;
var timer2;
pipes=null;
},
create: function ()
{
this.add.image(350, 280, 'sky2').setScale(1.1);
this.pipes = this.add.group();
this.physics.world.enable(this.pipes);
platforms = this.physics.add.staticGroup();
platforms.create(0, 535, 'ground2').setScale(0.6).refreshBody();
platforms.create(150, 535, 'ground2').setScale(0.6).refreshBody();
platforms.create(300, 535, 'ground2').setScale(0.6).refreshBody();
platforms.create(450, 535, 'ground2').setScale(0.6).refreshBody();
platforms.create(600, 535, 'ground2').setScale(0.6).refreshBody();
platforms.create(750, 535, 'ground2').setScale(0.6).refreshBody();
cursors = this.input.keyboard.createCursorKeys();
player = this.physics.add.sprite(100, 300, 'dude').setGravity(0, 300);
player.setCollideWorldBounds(true);
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [ { key: 'dude', frame: 4 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
this.physics.add.collider(player, platforms);
info = this.add.text(10, 10, '', { font: '48px Arial', fill: '#ff0000' });
info3 = this.add.text(19, 50, '', { font: '48px Arial', fill: '#ff0000' });
timer2 = this.time.addEvent({ delay: 90000, callback: ganhar,callbackScope: this, });
timer1 = this.time.addEvent({ delay: 2000, callback: this.addRowOfPipes,callbackScope: this, loop: true });
},
update: function ()
{
if (cursors.left.isDown)
{
player.setVelocityX(-130);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.setVelocityX(130);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown)
{
player.setVelocityY(-250);
}
if (cursors.down.isDown)
{
player.setVelocityY(300);
}
info3.setText('Lifes: ' + vida);
info.setText('Time: ' + Math.floor(90000 - timer2.getElapsed()));
},
addOnePipe: function(x, y) {
pipe = this.physics.add.image(x, y, 'br').setScale(1.1);
pipe.setVelocityX(-150);
this.pipes.addMultiple(pipe);
pipe.checkWorldBounds = true;
pipe.outOfBoundsKill = true;
var collider= this.physics.add.overlap(player, pipe, perder, false, this);
},
addRowOfPipes: function() {
var hole = Phaser.Math.Between(1, 6);
//var hole=6;
for (var i = 0; i < 8; i++)
if (i != hole && i != hole + 1){
this.addOnePipe(760, i *54 + 40);
}
},
});
function perder(player,pipe)
{
vida--;
if (vida==0){
this.scene.start('gameover');
}
else(this.scene.start('game2'));
collider.active = false;
}
function ganhar ()
{
this.scene.start('gamewon2');
}
function collectStar (player, star)
{
star.disableBody(true, true);
drums.stop();
synth2.stop();
this.scene.start('gamewon');
}
function Gameover ()
{
drums.stop();
synth2.stop();
vida--;
if (vida==0){
this.scene.start('gameover');
}
else(this.scene.start('game'));
}
var GameWon = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function GameWon ()
{
Phaser.Scene.call(this, { key: 'gamewon' });
window.OVER = this;
},
create: function ()
{
music = this.sound.addAudioSprite('kyobi');
music.play('title');
var map2= this.add.image(400,300,'back');
this.add.sprite(580, 380, 'final').setScale(0.8);
var play4= this.add.image(151,481,'play').setScale(0.2);
this.add.text(96, 460, 'Map2', { font: '50px Courier', fill: '#000000' });
this.add.text(40, 30, 'Congratulations!!!', { font: '45px Arial', fill: '#ffffff' });
this.add.text(40, 100, 'You won Map 1.', { font: '39px Courier', fill: '#ffffff' });
this.add.text(40, 150, 'Try to beat the 2 Map.', { font: '39px Courier', fill: '#ffffff' });
play4.setInteractive();
play4.once('pointerup', function () {
music.stop();
this.scene.start('game2');
}, this);
}
});
var GameWon2 = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function GameWon2 ()
{
Phaser.Scene.call(this, { key: 'gamewon2' });
window.OVER = this;
},
create: function ()
{
var map3= this.add.image(400,300,'back');
this.add.sprite(580, 380, 'final').setScale(0.8);
var play6= this.add.image(151,481,'play').setScale(0.2);
this.add.text(96, 460, 'Menu', { font: '50px Courier', fill: '#000000' });
this.add.text(40, 30, 'Congratulations!!!', { font: '45px Arial', fill: '#ffffff' });
this.add.text(40, 100, 'You won Map 2.', { font: '39px Courier', fill: '#ffffff' });
this.add.text(40, 150, 'More maps coming soon.Be Aware', { font: '39px Courier', fill: '#ffffff' });
play6.setInteractive();
play6.once('pointerup', function () {
this.scene.start('mainmenu');
}, this);
}
});
var GameOver = new Phaser.Class({
Extends: Phaser.Scene,
initialize:
function GameOver ()
{
Phaser.Scene.call(this, { key: 'gameover' });
window.OVER = this;
},
create: function ()
{
var back= this.add.image(400,300,'back');
this.add.sprite(397.5, 230, 'imgfinal').setScale(0.75);
var play5= this.add.image(395,508,'play').setScale(0.22);
this.add.text(311, 490, 'Restart', { font: '40px Courier', fill: '#000000' });
play5.setInteractive();
play5.once('pointerup', function () {
this.scene.start('mainmenu');
}, this);
}
});
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'phaser-example',
physics: {
default: 'arcade',
arcade: {
debug: false
}
},
scene: [ Preloader, MainMenu, Game, Game2, Rules, GameOver,GameWon, GameWon2 ]
};
var synth2;
var drums;
var music;
var vida=3;
var game = new Phaser.Game(config);
var blocked = false;

syntax to loop through docs send from express router and use it to plot chart js

I am starting to learn node js (express js). I am trying to plot a chart using data sent from the mongo db using mongoose....I would want to loop the docs sent from the database to be used in ploting the data. I had tried using fetch api but it failed due to Passport req.user (it didn't recognize user )
here is my code. I will appreciate for any help and leads.
router.get('/mychart', loggedInYes, function (req, res, next) {
Products.find({user:req.user._id},function (err,docs) {
if (err)
return err;
res.render('user/mychart',{docs:docs});
});
});
and here is the chart js where I would want to use Docs
<canvas id="myChart" width="400" height="400"></canvas>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [{
{#
each docs
}
} {
{
this.docs.yearofbirth
}
} {
{
/each}}],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
</script>
The problem is how to loop through the data....data: [{
{#
each docs
}
} {
{
this.docs.yearofbirth
}
} {
{
/each}}],
I solved it by including the credentials objects in the client hbs file. It worked
fetch('/api/foo', {credentials: 'include'})

ng2-chartjs2 ignores type and always shows a bar chart

I am using angular2 with ng2-chartjs2. I have the following component and template which I took directly from https://www.npmjs.com/package/ng2-chartjs2. The chart displays fine, but when I change the type in the template from bar to line I still see the same bar chart with no errors.
import { Component } from '#angular/core';
import { Router, RouterLink, CanActivate } from '#angular/router';
import { CORE_DIRECTIVES } from '#angular/common';
import { DashboardLayoutComponent } from '../dashboard_layout/dashboard_layout.component';
import {ChartComponent, Chart} from 'ng2-chartjs2';
#Component({
selector: 'home',
templateUrl: 'client/components/home/home.component.html',
directives: [DashboardLayoutComponent, CORE_DIRECTIVES, ChartComponent]
})
export class HomeComponent {
constructor(private _router: Router) {
}
labels: string[] = ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"];
data: Chart.Dataset[] = [
{
label: '# of Votes',
data: [12, 19, 3, 5, 25, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}
];
}
Template:
<!-- template -->
<dashboard-layout pageTitle="Statistical Overview">
<div class="home">
<chart [labels]="labels" [data]="data" type="line"></chart>
</div>
</dashboard-layout>
It seems like I am following the documentation correctly. Is this a bug? Is there a workaround if so?
Look at the source code
if(!this.options){
this.options = {
type: 'bar', <== this line
data: {
labels: this.labels,
datasets: this.data
}
}
}
This way if you don't provide options it always will be as bar chart
You can leverage the following workaround:
import { ChartComponent } from 'ng2-chartjs2';
#Component({
selector: 'my-app',
template: `
<chart [options]="options"></chart>
`,
directives: [ChartComponent]
})
export class AppComponent {
options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 25, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}
]
}
};
}
Demo Plunker

Resources