MV Can I use javascript to draw a graph?

For a mini-game I want to make a bunch of variables update every second. The idea is for the player to bet if the value goes up or down.

The base is easy to make. I don't have a problem updating the variable, or having the player guess which number will go up and which number will go down.

I save all the last 20 values of each variable in an array (which is stored in a variable too).

To make this game look more slick, and add some more tactical insight for the player, I would like to show a graph with the last 20 values, like this:

1740610542971.png

(Gimp made example of what I try to do.)

But even with all my javascript knowledge (which is less than it sounds), I have no idea how to pull this off.

So, I already have the values in an array, in the right order. So element 0 would be on the left (20 turns ago) and element 20 on the right (current turn)

It would be extra swell if I can show the values of multiple arrays in the same graph. Like above, I used 3 lines for 3 different arrays. (not really, though. I just made this in gimp)
Is there an easy way to do this, either with a script-call or a piece of code I can run with a plugin command?

I'm afraid this is going to be very complicated to pull off with a simple script call. I could do it with bars, if I make every bar its own picture picture... But it would be much prettier if I could use lines.

So, is there an easy way to do this, or should I just go for bars?

Edit: Actually the only thing I don't know how to do is how to draw a line from X,Y coordinates of the first value to X,Y coordinates of the second value. And how to draw a white background, unless I can just use show-picture.
Yeah it is easier to use script.
You can use pictures to make lines, for example use a 1 pixel tall picture, but you still need to rotate it using a script call and do lots of calculation.
CT_Bolt had some draw line between events plugin so you could theoretically just have a bunch of events with lines between them and move them to coordinates
Yeah it is easier to use script.
You can use pictures to make lines, for example use a 1 pixel tall picture, but you still need to rotate it using a script call and do lots of calculation.
Yeah, but I don't know enough javascript to make that script call. 😛

Even if I would be able to rotate images, which I don't know how to do, it would be a pain in the rare to calculate the angle and length of each line, specially since the length would be in 100% 😛

This is pretty much how I would do it with bars, which would be a lot easier, since they are always perfectly vertical.

CT_Bolt had some draw line between events plugin so you could theoretically just have a bunch of events with lines between them and move them to coordinates
As in a plugin? I could take a look, but I don't think a plugin would give me the freedom I need. If I can use javascript to draw the graph, I can use javascript to update the graph. Also, I need it to be pixel perfect, since the values go up to the thousands, but the difference between the values can be as small as 0.1%, so I can't bind them to events.

Thanks for your input, but if I would need to use a plugin, I would rather step down to bars, simply because I can do that myself, with absolutely all the freedom I desire. 😛 Don't get me wrong, I don't dislike plugins, not at all. But when creating a game-system, I rather do it without. That would make it easier to share the system later. Or maybe sell it, if the system is good enough.
Javascript has an in-built function to draw lines: see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineTo.

Depending on how much coding you want to do, you can take a look at the drawCircle function in rpg_core.js for example, and adapt it to draw lines based on points in an array using a for loop.

If you're drawing these lines in a custom window, the white background can be drawn as a white rectangle (do a search for fillRect in rpg_windows.js to get an idea of how to do that) if you don't want to use a picture.

Finally, you may also find this thread useful if you want to implement it in a script call on a map: https://forums.rpgmakerweb.com/inde...to-another-and-toggle-them-on-and-off.138630/
If you are interested in an event solution, you probably need tons of pictures.
Actually, some game actually does that. For example, some meters HUD in red alert 3. The game's user interface is actually a swf (like adobe flash). Said meter or bar uses 100 pictures to represent 1% to 100% of the fill state.

So instead of rotating the picture, you create multiple pictures representing different rotation angles of each step and assemble them.
1740616355461.png

It can be done using pure event command, unless you hit 100 pictures limit of MV.:hswt:
As in a plugin? I could take a look, but I don't think a plugin would give me the freedom I need. If I can use javascript to draw the graph, I can use javascript to update the graph. Also, I need it to be pixel perfect, since the values go up to the thousands, but the difference between the values can be as small as 0.1%, so I can't bind them to events.
ctBolt's original plugin was event to event. He made several enhancements to it, most at user's requests, and one of those enhancements was to enable it to connect from x1,y1 to x2,y2. That was a request I made and he quickly fulfilled that wish. I can vouch for it working really well. I really miss ctbolt. I believe it will do everything you want. You can adjust the line thickness and color. You do have to give an id number to each segment, though, so a line that goes from 1, 99 up to 73, 12, across to 118, 24 would have to have an id's of 1, 2, 3 (or 11,12,13) or whatever you want to number your id's. If there were fewer than 10 segments per line, it would be easier to keep track of the id's. I really think it's worth a look.

Edit- I just reread your post and it looks like you want 20 segments per line. Still doable, just do 1-20 id's for line 1, 21-40 for line 2, 41-60 for line 3, etc.
I'll take a look, but I would rather do it without the use of a plugin, if possible. I want to make a full system which I can share and maybe sell. I can't do that if a 3rd party plugin is used to create this. 😛
I would open up the dev tools and start drawing with canvas/pixi so you can test your chart and refer to the tutorials online or on YouTube for more guidance.

Quick example of what the base graph can look like - modify, add, and edit as needed - this is using PIXI:
JavaScript:
// spriteset shorthand as variable s
// create new pixi graphic as variable g
// we will add it to the spriteset scene when complete
const s = SceneManager._scene._spriteset,
      g = new PIXI.Graphics()
  .beginFill(0xffffff)  // Start the fill color with white
  .drawRect(0, 0, 360, 360)  // Draw the rectangle for the background
  .endFill()  // stop the fill color
  .lineStyle(2, 0x000000)  // Set pen thickness to 2 and use color black (hexadecimal)
  .moveTo(40, 40)  // move pen to this xy point
  .lineTo(40, 320)  // from where the pen is, draw a line to this point - this is the X-axis line
  .lineTo(320, 320);  // from where we stopped, draw a line to this point - this is the Y-Axis line
// name the child for easy finding
g.name = 'graph';
// Add the graphic to the scene
s.addChild(g);
To do your other lines, use moveTo() for the starting point and lineTo() to draw the line to the new point, etc. Or, you can create a new graphic for the individual lines and add them to the scene separately/later.

When using a new script call, you will have to find the child (by its name since we named it) to edit the graph and then run the rest of your code from there:
JavaScript:
const g = SceneManager._scene._spriteset.getChildByName('graph');
g.lineStyle(2, 0x0000ff)  // thickness 2 with blue color
  .moveTo(40, 100)
  .lineTo(60, 120)
...
...
@bass2yang Awesome. I am going to play around with that!

I have been playing around with the idea of bars, and with the use of a single bar, I can make this:
1740660129928.png

This is 52 entrees. because with 20 entrees it would not look like a graph.

To understand what I did here, I'll explain, for those interested:

I made a loop that runs once for each element in the array. In there, I show a picture, which is the red bar. Each pictures position, is depending on the element id. So element #0 is on the left, element #51 on the right. The height of each bar, is depending on how large the value is, compared to the highest value in the array. So the highest value will always reach the top.

Like this:
JavaScript:
var array       = $gameVariables.value(1);
var mValue      = Math.max(...array);

for (let n = 0; n < array.length; n++) {
    var p = n+1;
    var x = 48 + 12 * n;                    // x position
    var y = 576;                            // y position
    var w = 25;                             // width in %
    var h = -100 * array[n] / mValue;       // height in %
    $gameScreen.showPicture(p, 'Bar', 0, x, y, w, h, 255, 0);
};

The background is just a parallax.

Edit: I did give the bar a gradient, which makes it look pretty slick, actually:
1740663009186.png


Still lines would be better, so I am def. going to look into that piece of code. See if I can understand what it does. 😛
Still lines would be better, so I am def. going to look into that piece of code. See if I can understand what it does. 😛
Sounds cool - yeah, you would use a thicker line and draw it by changing the y values. Move the x value when you want to start a new bar line.

Here is an example (you can tie it to your variables instead as needed):
JavaScript:
const s = SceneManager._scene._spriteset;
const g = new PIXI.Graphics()
 .beginFill(0xffffff)
 .drawRect(0,0,240,240)
 .endFill()
 .lineStyle(2, 0x000000)
 .moveTo(40,40)
 .lineTo(40,200)
 .lineTo(200,200);
g.name = 'graph';
s.addChild(g);
// Add bars to child g and to separate the layers
const b = new PIXI.Graphics()
 .lineStyle(20, 0x0000ff)
 .moveTo(50,200)
 .lineTo(50,150)
 .moveTo(70,200)
 .lineTo(70,75)
 .moveTo(90,200)
 .lineTo(90,90);
b.name = 'bars';
g.addChild(b);
Here is the result:
s1.png


To clear the bars only (and assuming you are now using a new script call command), you would have to find the child and use the clear() function:
JavaScript:
const b = SceneManager._scene._spriteset.getChildByName('graph').getChildByName('bars');
b.clear();

Edit: you do not have to chain the code like the way I did (example: new PIXI Graphics().lineStyle().moveTo...)

You can put it in a variable and call separate lines:
JavaScript:
const g = new PIXI.Graphics();
g.lineStyle(2,0x000000);
g.moveTo(20,20);
...
That way you can use loops our add other commands in between.
Thanks! 🙂 But if I stick to bars, then my option would be easier (since I don't have to put in all the coordinates manually for each of the 52 bars.)

However, for lines, which should ne much prettier, I will look into your previous code. It seems simple enough, but now I have to modify it, so the height will be adjusted to the value compared to the highest value. 😛

Thanks for your help, this will be a puzzle, but from here on I am sure I can make it work.
Thanks! 🙂 But if I stick to bars, then my option would be easier (since I don't have to put in all the coordinates manually for each of the 52 bars.)

However, for lines, which should ne much prettier, I will look into your previous code. It seems simple enough, but now I have to modify it, so the height will be adjusted to the value compared to the highest value. 😛

Thanks for your help, this will be a puzzle, but from here on I am sure I can make it work.
Well, easy is relative when you think about it, but you are correct as well. You should use the workflow you feel works for you.

If I am looking to draw something without using any premade images, I would draw the full graphic with a loop as well (continuing with previous code for bars):
JavaScript:
const b = new PIXI.Graphics();
b.lineStyle(20, 0x0000ff);
for (let i = 50; i <= 190; i += 20) {
  b.moveTo(i, 200);
  b.lineTo(i, 50 + Math.randomInt(101));
}
b.name = 'bars';
g.addChild(b);

The cool thing about the pixi graphic is that you can even add it to the child of a picture. Meaning, do a show picture with None for the picture. For the code above, use this for the value of s:
JavaScript:
const s = SceneManager._scene._spriteset._pictureContainer.children[0]; // use the picture ID - 1; children 0 is picture #1
s.addChild(g);
Now, anything that you already do with the game screen pictures will affect your new graphic and you can use the picture commands instead for most of the things.
I'd love to learn more about that, but for this little project, that would be too much at once. I'm really a noob with javascript, and in order to use it, I try to understand every line I use. (Otherwise I can't modify and debug either) so this seems like something I need a bit more time for. 😛

I'll start practicing with the lines, though. Once I understand that well enough, I'll look for the next step. 😛 Although a picture in the background might be nice... It's just a little early for that.

And I saw there is a gamejam coming up, so if I can figure how the javascript part works before then, I might be able to make the mini-game during the jam.
Although a picture in the background might be nice... It's just a little early for that.
What I mean is that your graph that you create with PIXI is attached to a picture ID (with None selected for the picture) so you would be able to use it with the show picture and move picture commands.
Hello, I started a thread with a similar theme

And with the bass2yang's code I find a way to resolve my problem
Code:
const s = SceneManager._scene._spriteset;
const g = new PIXI.Graphics()
 .beginFill(0xffffff)
 .drawRect(0,0,240,240)
 .endFill()
 .lineStyle(2, 0x000000)
 .moveTo(40,40)
 .lineTo(40,200)
 .lineTo(200,200);
g.name = 'graph';
s.addChild(g);
// Add bars to child g and to separate the layers
const b = new PIXI.Graphics()
 .lineStyle(20, 0x0000ff)
 .moveTo(50,200)
 .lineTo(50,150)
 .moveTo(70,200)
 .lineTo(70,75)
 .moveTo(90,200)
 .lineTo(90,90);
b.name = 'bars';
g.addChild(b);

But I'm wonder if there is a way to clear the graph in function of a coordinate xy and a way to clear all graphs in one go.
This script work only for the first graph I drawed
Code:
const b = SceneManager._scene._spriteset.getChildByName('graph').getChildByName('bars');
b.clear();

(Also it is possible to apply to line style css?)
(Also it is possible to apply to line style css?)
no, as CSS are permantly, and cannot simply turn on/off in anyway without coding.
no, as CSS are permantly, and cannot simply turn on/off in anyway without coding.
It would be possible to have a line style using array in the same way as if will use css?
My problem it's just to be able to modify all line styles at once.
css = permanent, scriptcall is more control and variable to do what you want.
add, modifiy, erase, change, etc.

css is not possible for doing so.

you can make custom function with the settings so you can do a simple:
JavaScript:
function squareGraph() {
    const s = SceneManager._scene._spriteset;
    const g = new PIXI.Graphics()
         .beginFill(0xffffff)
         .drawRect(0,0,240,240)
         .endFill()
         .lineStyle(2, 0x000000)
         .moveTo(40,40)
         .lineTo(40,200)
         .lineTo(200,200);
        g.name = 'graph';
        s.addChild(g);
}
and call it with that squareGraph() if you premade them.
but inside event, you can use the same call to change them on the name.

premade and fix to add and remove is this easy to use, but I would omit css as they
are fixed, not something you want (this is mostly screen, not map based.
css = permanent, scriptcall is more control and variable to do what you want.
add, modifiy, erase, change, etc.

css is not possible for doing so.

you can make custom function with the settings so you can do a simple:
JavaScript:
function squareGraph() {
    const s = SceneManager._scene._spriteset;
    const g = new PIXI.Graphics()
         .beginFill(0xffffff)
         .drawRect(0,0,240,240)
         .endFill()
         .lineStyle(2, 0x000000)
         .moveTo(40,40)
         .lineTo(40,200)
         .lineTo(200,200);
        g.name = 'graph';
        s.addChild(g);
}
and call it with that squareGraph() if you premade them.
but inside event, you can use the same call to change them on the name.

premade and fix to add and remove is this easy to use, but I would omit css as they
are fixed, not something you want (this is mostly screen, not map based.
This wasn't that I asked, I asked if I can using array to mimic what a css could do

Then this line is not possible because I need to manage too many elements which will use 1 line style, so if I need to modify it that will be just a nightmare

Original thread: https://forums.rpgmakerweb.com/threads/can-i-use-javascript-to-draw-a-graph.175848/