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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:16:29+00:00 2026-06-15T19:16:29+00:00

I would like to draw a bar chart in an existing view on the

  • 0

I would like to draw a bar chart in an existing view on the user interface. The single bar highs are calculated after user input and stored in an array. The array should be passed to my custom GraphView class after a button is bushed to draw the bar chart finally. But it doesn’t work. I don’t know if I pass the array right (how can I check it?) and how to access it in my drawrect method to set the single bar highs? As soon I put NSNumber *balkenHoehe = [balkenArray objectAtIndex:i]; in the drawrect method and balkenHoehe as rect-hight I get errors. WHAT DO I HAVE TO DO? Thanks!

here is my code:

my AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *eingabeNennmass;
@property (weak) IBOutlet NSTextField *eingabeToleranzOben;
@property (weak) IBOutlet NSTextField *eingabeToleranzUnten;
@property (weak) IBOutlet NSTextField *eingabeAnzahlWerte;
@property (weak) IBOutlet NSTextField *ausgabeMittelwert;
@property (weak) IBOutlet NSTextField *ausgabeToleranz
@property (weak, nonatomic) IBOutlet GraphView *ausgabeGraph;

- (IBAction)pushSimulation:(NSButton *)sender;

@end

my AppDelegate.m shortened

#import "GraphView.h" 
#import "AppDelegate.h"

implementation AppDelegate
....//couple calculating code and building the array klassenArray//...

    [klassenArray addObject:[NSNumber numberWithDouble: n]];
....//more code//...

[_ausgabeGraph setBalkenArray:klassenArray]; (connected with the GraphView view on the user interface)

....//more code//...

my GraphView.h

#import <Cocoa/Cocoa.h>

@interface GraphView : NSView
{

NSMutableArray *balkenArray;

}

- (NSMutableArray *) balkenArray;
- (void) setBalkenArray:(NSMutableArray *)abalkenArray;

@end

my GraphView.m

#import "GraphView.h"

@implementation GraphView

//*******************************
- (void) setBalkenArray:(NSMutableArray *)abalkenArray
{
    balkenArray = abalkenArray;
    [self setNeedsDisplay:YES];
}

//*******************************
- (NSMutableArray *)balkenArray
{
    return balkenArray;
}

//*******************************
- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }

    return self;
}

//*******************************
- (void)drawRect:(NSRect)dirtyRect
{

    NSRect bounds = [self bounds];
    float fieldWith = bounds.size.width / 12.0;
    float fieldHeight = bounds.size.height / 12.0;

        //background
    NSRect hintergrundRect =
    hintergrundRect = NSMakeRect(bounds.origin.x, bounds.origin.y,
                                  bounds.size.width, bounds.size.height);
    [[NSColor grayColor] set];
    [NSBezierPath fillRect:hintergrundRect];

        //Diagram

    for (int i = 1; i<=10; i++) {
        NSNumber *balkenHoehe = [balkenArray objectAtIndex:i];
//        NSLog(@"rectnumber at index %d is %@", i, balkenHoehe);

        NSRect aRect =
        aRect = NSMakeRect (fieldWith*i, fieldHeight, fieldWith, balkenHoehe); //x, y, width, hight
        // draw rect
    [[NSColor whiteColor] set];
    [NSBezierPath fillRect:aRect];

        // draw border
    [[NSColor blackColor] set];
    NSBezierPath *aPath = [NSBezierPath bezierPathWithRect:aRect];
    [aPath setLineWidth:1];
    [aPath stroke];
    }
}


@end
  • 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-15T19:16:30+00:00Added an answer on June 15, 2026 at 7:16 pm

    Ok, thanks to myself here! Good guy.
    I used the @property and @systnesize thing in GrapView.h

    @interface GraphView : NSView
    {
    NSMutableArray *balkenArray;
    }
    @property(nonatomic, retain)NSMutableArray *balkenArray;
    @property(readwrite, assign)double _maxKlasse;
    @end
    

    and GraphView.m

    @synthesize balkenArray, _maxKlasse;
    

    in AppDelegate.m the array is send to the view:

    _ausgabeGraph.balkenArray=klassenArray;
    _ausgabeGraph._maxKlasse=maxKlasse;
    [_ausgabeGraph setNeedsDisplay:YES];
    

    and finally the array is used in the GraphView.m to draw the bar chart:

    … code …

    for (int i = 1; i<=10; i++) {
      double _x = [(NSNumber*)[balkenArray objectAtIndex:i-1] doubleValue];
    
      //draw rect
      NSRect aRect =
        aRect = NSMakeRect (fieldWith * i, fieldHeight, fieldWith, (6*fieldHeight*_x/_maxKlasse)); //x, y, width, variable hight
        [[NSColor whiteColor] set];
        [NSBezierPath fillRect:aRect];
    

    … more code …

    Thanks to Gabriele Petronella for the hint using property and synthesize. As long as I understood it is much easier to use. I still need to learn the conventions for the naming.

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

Sidebar

Related Questions

I would like to draw a line or bar graph using HTML5 canvas. I
I would like to draw a chart in OpenGL similar to the donut graph
I would like to draw a straight line between where a user touches the
I would like to draw a linear chart in my xhtml page. In the
I have a form that I would like to draw a custom title bar
I would like to draw text on a canvas using the Canvas.drawText call for
I would like to draw some shapes on a JPanel by overriding paintComponent .
I would like to draw a box using SAS/GRAPH with a color gradient (say
in vb.net i would like to draw a regular line on a form. is
We have an array of points through which we would like to draw a

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.