Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8089523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:25:14+00:00 2026-06-05T19:25:14+00:00

I am making a program that graphs a line based on data inputted by

  • 0

I am making a program that graphs a line based on data inputted by the user. (It’s based on the slope form/equation). I am using the Canvas to graph my equation. I’ve been having a problem graphing the equation in a way that lets it adapt to the scaling (which is based on how large the numbers inputted are.)

How can I make the graphed equation (line) fit the graph as the canvas scales?

Here’s my code:

var c=document.getElementById("graph_");
var ctx=c.getContext("2d");
graph_.style.backgroundColor="white";

// This is used to define the parameters of the canvas. Variables a and b are the x and y intercepts of the linear function.

var z0=Math.max(Math.abs(a),Math.abs(b));
var z=Math.round(z0);           
var z1=Math.round(z);
var z2=z*2
// alert(z1);           
// alert(z2);`

//The code below is used to properly scale the canvas and lines so they can accomodate larger numbers   
var scale = 2*z/360;
var offsetX = 150;
var offsetY = 75


ctx.translate((-c.width /2 * scale) + offsetX,(-c.height / 2 * scale) + offsetY);                   
ctx.scale(scale,scale);    


        var lw = scale/2
        var xnew = 360/2+360/2*a
        var ynew = 360/2-360/2*b
        alert(xnew);    
        alert(ynew);

        //The two lines drawn below are the axises of the graph

                    ctx.lineWidth = 2/lw;
                        ctx.beginPath()
                    ctx.moveTo(150, 40000*-1);
            ctx.lineTo(150, 40000);
        ctx.closePath();

        ctx.lineWidth = 1/lw;
        ctx.moveTo(400000*-1, 75);
        ctx.lineTo(40000, 75);
        ctx.strokeStyle = "#8B8682";
        ctx.stroke();
        ctx.closePath();

        //var xmax = 400000 - b 
        //var xmax1 = xmax/s
        //var ymax =    400000*s
        //var ymax1 = ymax + b

// The code below graphs the equation. 

          ctx.beginPath();
          ctx.lineWidth = 1/lw;
                  ctx.moveTo(xnew, 180);
          ctx.lineTo(180, ynew);
          // ctx.lineTo(xmax, ymax)
          // ctx.lineTo(xmax*-1, ymax*-1)
          ctx.strokeStyle = "red";
          ctx.stroke();                                         

Here is the coding for the whole page:
As you can see the line, if drawn at all, doesn’t become long enough, like it should. (linear lines are always infinite, so the line should be going across the WHOLE graph, not a small portion.)

var canwith=360
var canheight=360

// alert(window.innerWidth)

 function doSolve() {
var s=''
var x1 = document.getElementById('x1').value
var y1 = document.getElementById('y1').value
var x2 = document.getElementById('x2').value 
var y2 = document.getElementById('y2').value 
var m 
var b
var a 

    try {
        if ((x2 - x1)==0) {
            m='Undefined'
            b='Undefined'
            a=x1 
        } else {
            m = (y2 - y1)/(x2 - x1)
            b = (y2-x2*m)
            a = (-b/m)
        }


        s += 'Coordinates are: ('
        s += x1
        s += ','
        s += y1
        s += '),('
        s += x2
        s += ','
        s += y2
        s += ')'
        s += '<br>Slope:'
        s += m
        s +='<br>y intercept:'
        s += b
        s += '<br>x intercept:' 
        s += a

        if (m=='undefined') {
            s += '<br>Equation: x = ' + x1      
        } else {
            s += '<br>Equation: y = '
            if (m!=0) {
                if (m!=1) {
                    s += m + 'x' 
                } else {
                    s += 'x' 
                }
            }
            if (b!=0) {
                if (b>0) {
                    s += ' + ' + b
                } else {
                    s += ' - ' + b*-1
                }
            }
        }

        document.getElementById('outputx').innerHTML=s

    } catch (e) {alert(e.message)}  

    try {

        var c=document.getElementById("graph_");
        var ctx=c.getContext("2d");
        graph_.style.backgroundColor="white";
        var z0=Math.max(Math.abs(a),Math.abs(b));
        var z=Math.round(z0);           
        var z1=Math.round(z);
        var z2=z*2
        // alert(z1);   
        // alert(z2);

           var scale = 2*z/360;
           var offsetX = 150;
           var offsetY = 75


    ctx.translate((-c.width /2 * scale) + offsetX,(-c.height / 2 * scale) + offsetY);                   
    ctx.scale(scale,scale);    


        var lw = scale/2
        var xnew = 360/2+360/2*a
        var ynew = 360/2-360/2*b
        alert(xnew);    
        alert(ynew);


        ctx.lineWidth = 2/lw;
        ctx.beginPath()
        ctx.moveTo(150, 40000*-1);
        ctx.lineTo(150, 40000);
        ctx.closePath();

        ctx.lineWidth = 1/lw;
        ctx.moveTo(400000*-1, 75);
        ctx.lineTo(40000, 75);
        ctx.strokeStyle = "#8B8682";
        ctx.stroke();
        ctx.closePath();

        var xmax = 400000 - b 
        var xmax1 = xmax/s
        var ymax =  400000*s
        var ymax1 = ymax + b

          ctx.beginPath();
          ctx.lineWidth = 1/lw;
          ctx.moveTo(xnew, 180);
          ctx.lineTo(180, ynew);
          ctx.lineTo(xmax, ymax)
          ctx.lineTo(xmax*-1, ymax*-1)
          ctx.strokeStyle = "red";
          ctx.stroke();                                         

    } catch (e) {alert(e.message)}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:25:15+00:00Added an answer on June 5, 2026 at 7:25 pm

    I couln’t cope with your code, so I made my own implementation adjusting to your visual requirements, hope this fix the problem:

    try {
        var c = document.getElementById("graph_");
        var ctx = c.getContext("2d");
        graph_.style.backgroundColor="white";
    
        var w = c.width;
        var h = c.height;
    
        var xAxisSize = 40;
        var yAxisSize = 40;    
    
        var scaleFactorX = w / xAxisSize;
        var scaleFactorY = -(h / yAxisSize);        
    
        var offsetX = -10;
        var offsetY = -10;
    
        ctx.scale(scaleFactorX, scaleFactorY);
        ctx.translate((xAxisSize / 2) + offsetX, -((yAxisSize / 2) + offsetY));
    
        ctx.lineWidth = 3 / scaleFactorX;
        ctx.beginPath();
        ctx.moveTo(-xAxisSize, 0);
        ctx.lineTo( xAxisSize, 0);
        ctx.strokeStyle = "#8B8682";        
        ctx.stroke();
        ctx.closePath();
    
        ctx.lineWidth = 3 / scaleFactorY;
        ctx.beginPath();
        ctx.moveTo(0, -yAxisSize);
        ctx.lineTo(0,  yAxisSize);
        ctx.strokeStyle = "#8B8682";
        ctx.stroke();
        ctx.closePath();
    
        ctx.lineWidth = 3 / scaleFactorY;
        ctx.beginPath();
        var xx1 = -xAxisSize - offsetX;
        var yy1 = m * xx1 + b;
        var xx2 =  xAxisSize + offsetX;
        var yy2 = m * xx2 + b;        
        ctx.moveTo(xx1, yy1);
        ctx.lineTo(xx2,yy2);
        ctx.strokeStyle = "red";
        ctx.stroke();
        ctx.closePath();  
    } catch (e) { 
        alert(e.message) 
    }   
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been making a program that uses core data. It has two entities, Medicine
I'm making a program that communicate with certain patient monitor using C sockets. I'm
I'm currently making a PHP-program that solves equations. I've divided the input equation into
Every program that I'm making using pygtk is right-alignment by default (maybe because my
I'm making a program that asks for an integer from the user, it then
I am making a program that solves an equation. I have a variable that
I'm making a program that has menus and I'm using switch to navigate between
I'm making a program that takes names from the user seperated by commas. The
I'm making a program that makes a fractal, and the user is suppose to
I'm making a program that is using Windows Shell Integration, and the registry changes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.