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 8370691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:58:26+00:00 2026-06-09T13:58:26+00:00

I understand how to draw lines using a canvas, but how can I use

  • 0

I understand how to draw lines using a canvas, but how can I use the same lines using a canvas to draw a graph?

The problem is with the coordinates. (0,0) starts right at the top left corner of the device. How can I set (0,0) as the margin and draw the particular line with respect to the margin?

  • 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-09T13:58:27+00:00Added an answer on June 9, 2026 at 1:58 pm

    1)Create an activity.

    public class GraphView1 extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        float[] values = new float[] { "your values"};
        String[] verlabels = new String[] { "your values" };
        String[] horlabels = new String[] { "your values"
        GraphView graphView = new GraphView(this, values,"GraphView",horlabels, verlabels, GraphView.BAR);
        setContentView(graphView);
    }
    } 
    

    2)Then create another class extends View:

    public class GraphView2 extends View{
    
    
    public static boolean LINE = true;
    
    private Paint paint;
    private float[] values;
    private String[] str;
    private String[] verlabels;
    private String title;
    private boolean type;
    Context context;
    
    
    public GraphView(Context context, float[] values, String title, String[] str,String[] verlabels, boolean type) {
    
        super(context);
        if (values == null)
            values = new float[0];
        else
            this.values = values;
        if (title == null)
            title = "";
        else
            this.title = title;
        if (str == null)
            this.str = new String[0];
        else
            this.str = str;
        if (verlabels == null)
            this.verlabels = new String[0];
        else
            this.verlabels = verlabels;
        this.type = type;
        paint = new Paint();
    }
    
    @Override
    protected void onDraw(final Canvas canvas) {
        context=getContext();
        float border = 15;
        float horstart = border * 2;
        float height = getHeight();
        float width = getWidth();
        float max = getMax();
        Log.w("max", ""+max);
        float min = getMin();
        Log.w("min", ""+min);
        float diff = max - min;
        float graphheight = height - (2 * border);
        float graphwidth = width - (2 * border);
    
        paint.setTextAlign(Align.LEFT);
        int vers = verlabels.length;
        for (int i = 0; i < verlabels.length; i++) {
            paint.setColor(Color.DKGRAY);
            float y = ((graphheight / vers) * i) + border;
            canvas.drawLine(horstart, y, width, y, paint);
            paint.setColor(Color.WHITE);
            paint.setTextSize(10);
            canvas.drawText(verlabels[i], 0, y, paint);
        }
        int hors = values.length;
        for (int i = 0; i < str.length; i++) {
            paint.setColor(Color.DKGRAY);
            float x = ((graphwidth / hors) * i) + horstart;
            canvas.drawLine(x, height - border, x, border, paint);
            paint.setTextAlign(Align.LEFT);
            if (i==str.length)
                paint.setTextAlign(Align.RIGHT);
            if (i==0)
                paint.setTextAlign(Align.LEFT);
            paint.setColor(Color.WHITE);
            paint.setTextSize(9);
            canvas.drawText( str[i], x, height - 4, paint);
        }
    
        paint.setTextAlign(Align.CENTER);
        canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
    
    
    
        if (max != min) {
            paint.setColor(Color.BLUE);
            paint.setStyle(Paint.Style.FILL);
    
            if (type == BAR) {
                float datalength = values.length;
                float colwidth = (width - (2 * border)) / datalength;
                for (int i = 0; i < values.length; i++) {
                //  float val = values[i] - min;
    
                //  float rat = val / diff;
                //  float h = graphheight * rat;
                //  canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);                     
                    float graph_h = getHeight()-(border*2); 
                     // Log.e("", "graph_h > "+graph_h);
    
                    float ind_h = graph_h/7; 
                      //Log.e("", "ind_h > "+ind_h);
    
                    float t = values[i]/5;
    
                    float top = (graph_h - ind_h*(t)); 
                     // Log.e("", " > "+i+1);
                     // Log.e("", "top > "+top);
    
                    //for values between 0 and 5 ,vice versa
                    //Log.e("", " values[i] > "+values[i]);
                    float acc = ind_h/5;
                    acc = acc * (values[i]%5);
    
                  //  Log.e("", " acc > "+acc);
    
                    canvas.drawRect((i * colwidth) + horstart, top+border-acc , ((i * colwidth) + horstart) + (colwidth - 1), graph_h+border, paint);                       
                }
            } else {
                float datalength = values.length;
                float colwidth = (width - (2 * border)) / datalength;
                float halfcol = colwidth / 2;
                float lasth = 0;
                for (int i = 0; i < values.length; i++) {
                    float val = values[i] - min;
                    float rat = val / diff;
                    float h = graphheight * rat;
                    if (i > 0)
                        canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
                    lasth = h;
                }
            }
        }
    }
    
    
    
    private float getMax() {
        float largest = Integer.MIN_VALUE;
        for (int i = 0; i < values.length; i++)
            if (values[i] > largest)
                largest = values[i];
        return largest;
    }
    
    private float getMin() {
        float smallest = Integer.MAX_VALUE;
        for (int i = 0; i < values.length; i++)
            if (values[i] < smallest)
                smallest = values[i];
        return smallest;
    }
    
    
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using OpenGL on Android to draw set width lines, which work great
I have been using RenderTargetBitmap to draw lines for my application as shown here
I am facing a problem in using JUNG. I want to draw a network
I know how to draw paths on a canvas and understand how to undo/redo.
I made a simple drawing app with which I can draw lines on a
I am trying to use Canvas.drawLine method to draw a polygon Here's the code
I am writing a program in C, using OpenGL, to draw some primitives (lines,
I don't understand what I'm doing wrong here: public void Draw(GameTime gameTime) // in
I understand this is an easy question but for some reason this just isn't
I understand I can create an enum like this: public enum MyEnum { ONE(1),

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.