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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:07:48+00:00 2026-05-30T19:07:48+00:00

Am new to coreplot and I want to display NSdate (year or moth or

  • 0

Am new to coreplot and I want to display NSdate (year or moth or day) along the x-axis of a bargraph which is drawn using coreplot.I have gone through some samples to plot NSDate along x-axis.but am unable to plot..

Please can any one provide me the code for plotting NSDate along x-axis…
Thanks

I tried the below code from stackoverflow but am unable to display the labels on x-axis

// If you make sure your dates are calculated at noon, you shouldn't have to 
// worry about daylight savings. If you use midnight, you will have to adjust
// for daylight savings time.
NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926 * 10];
NSTimeInterval oneDay = 24 * 60 * 60;

// Invert graph view to compensate for iOS coordinates
//CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1);
// self.view.transform = verticalFlip;

 // allocate the graph within the current view bounds
graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds];

// assign theme to graph
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];

// Setting the graph as our hosting layer
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds];

[self.view addSubview:hostingView];

hostingView.hostedGraph = graph;

graph.paddingLeft = 20.0;
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingBottom = 150.0;

// setup a plot space for the plot to live in
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
// sets the range of x values
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow)
                                               length:CPTDecimalFromFloat(oneDay*5.0f)];
// sets the range of y values
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) 
                                               length:CPTDecimalFromFloat(5)];

// plotting style is set to line plots
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;

// X-axis parameters setting
CPTXYAxisSet *axisSet = (id)graph.axisSet;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay);
axisSet.xAxis.minorTicksPerInterval = 0;
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1"); //added for date, adjust x line
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelOffset = 3.0f;

// added for date
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;

// Y-axis parameters setting    
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"0.5");
axisSet.yAxis.minorTicksPerInterval = 2;
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneDay); // added for date, adjusts y line
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
axisSet.yAxis.labelOffset = 3.0f;


// This actually performs the plotting
CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease];

CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
//xSquaredPlot.identifier = @"X Squared Plot";
xSquaredPlot.identifier = @"Date Plot";

dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
xSquaredPlot.dataLineStyle = dataLineStyle;
xSquaredPlot.dataSource = self;

CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
xSquaredPlot.plotSymbol = greenCirclePlotSymbol;  

// add plot to graph
[graph addPlot:xSquaredPlot];

// Add some data
NSMutableArray *newData = [NSMutableArray array];
NSUInteger i;
for ( i = 0; i < 5; i++ ) {
    NSTimeInterval x = oneDay*i;
    id y = [NSDecimalNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2];
    [newData addObject:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX], 
      y, [NSNumber numberWithInt:CPTScatterPlotFieldY], 
      nil]];
    NSLog(@"%@",newData);
}
plotData = [newData retain];

}

#pragma mark - Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return plotData.count;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:     (NSUInteger)index
 {
NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
  • 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-05-30T19:07:49+00:00Added an answer on May 30, 2026 at 7:07 pm

    If you are looking to do something like this… (I apologize for the size of the image, I am not familiar with how to size the image for SO)

    Key Indicators

    You will need to define custom labels for your X Axis.

    Consider the following code snippet (taken from the code that generates the image above):

    /* --Define some custom labels for the data elements-- */
    // Note that x is defined as: CPTXYAxisSet *axisSet = (CPTXYAxisSet*)self.graph.axisSet;
    //                                      CPTXYAxis *x = axisSet.xAxis;
    x.labelingPolicy = CPTAxisLabelingPolicyNone; // This allows us to create custom axis labels for x axis
    NSMutableArray *ticks = [NSMutableArray arrayWithCapacity:1];
    for(unsigned int counter = 0; counter < [_axisLabelStrings count];counter++) {
        // Here the instance variable _axisLabelStrings is a list of custom labels
        [ticks addObject:[NSNumber numberWithInt:counter]];
    }
    NSUInteger labelLocation = 0;
    NSMutableArray* customLabels = [NSMutableArray arrayWithCapacity:[_axisLabelStrings count]];
    @try {
        for (NSNumber *tickLocation in ticks) {
            CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [_axisLabelStrings objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
            newLabel.tickLocation = [tickLocation decimalValue];
            newLabel.offset = 3.0f;//x.labelOffset + x.majorTickLength could be useful here.
            newLabel.rotation = M_PI/3.5f;
            [customLabels addObject:newLabel];
            [newLabel release];
        }
    }
    @catch (NSException * e) {
        NSLog(@"An exception occurred while creating date labels for x-axis");
    }
    @finally {
        x.axisLabels =  [NSSet setWithArray:customLabels];  
    }
    x.majorTickLocations = [NSSet setWithArray:ticks];
    

    Note that the _axisLabelStrings will need to be processed and sorted how you want it. You will need to take NSDates, sort them, and generate NSStrings for your labels.

    But that, in a nutshell, is how you create custom labels.

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

Sidebar

Related Questions

New to DDD here and have a architecture question which should be a typical
New to Drupal and want to do the following: Display a list of the
New to javascript/jquery and having a hard time with using this or $(this) to
New to both Ruby and Rails but I'm book educated by now (which apparently
New day, new problem :-) Code: Client Side: void abw_Closed(object sender, EventArgs e) {
I'm quite new to iphone programming and I want to do the following stuff:
NEW EDIT: I have narrowed my problem to this - i have a view
new to java and brand new to the site. I have a JLabel added
New to jquery question... Using jQuery, depending on the radio button clicked I would
I m new in iphone..I create Application in which I maintain data About Income

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.