kaiserfarrell / core-plot

Automatically exported from code.google.com/p/core-plot
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

All graph points are overlapping #518

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I am creating a scattergraph.But my points are overlapping.I am using the 
following code.

-(void)configureGraph {
    // 1 - Create the graph
    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
    [graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
    self.hostView.hostedGraph = graph;
    // 2 - Set graph title
    NSString *title = @"License Graph";
    graph.title = title;
    // 3 - Create and set text style
    CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
    titleStyle.color = [CPTColor whiteColor];
    titleStyle.fontName = @"Helvetica-Bold";
    titleStyle.fontSize = 16.0f;
    graph.titleTextStyle = titleStyle;
    graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
    graph.titleDisplacement = CGPointMake(0.0f, 10.0f);
    // 4 - Set padding for plot area
    [graph.plotAreaFrame setPaddingLeft:30.0f];
    [graph.plotAreaFrame setPaddingBottom:30.0f];
    // 5 - Enable user interactions for plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
}

-(void)configurePlots {
    // 1 - Get graph and plot space
    CPTGraph *graph = self.hostView.hostedGraph;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    // 2 - Create the three plots
    CPTScatterPlot *avgPlot = [[CPTScatterPlot alloc] init];
    avgPlot.dataSource = self;
    avgPlot.identifier = CPDAvgFixed;
    CPTColor *aaplColor = [CPTColor redColor];
    [graph addPlot:avgPlot toPlotSpace:plotSpace];
    CPTScatterPlot *fixedPlot = [[CPTScatterPlot alloc] init];
    fixedPlot.dataSource = self;
    fixedPlot.identifier = CPDMaxFixed;
    CPTColor *googColor = [CPTColor greenColor];
    [graph addPlot:fixedPlot toPlotSpace:plotSpace];
    /*CPTScatterPlot *msftPlot = [[CPTScatterPlot alloc] init];
    msftPlot.dataSource = self;
    msftPlot.identifier = CPDTickerSymbolMSFT;
    CPTColor *msftColor = [CPTColor blueColor];
    [graph addPlot:msftPlot toPlotSpace:plotSpace];*/
    // 3 - Set up plot space
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:avgPlot, fixedPlot, nil]];
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
    plotSpace.xRange = xRange;
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
    plotSpace.yRange = yRange;
    // 4 - Create styles and symbols
    CPTMutableLineStyle *avgLineStyle = [avgPlot.dataLineStyle mutableCopy];
    avgLineStyle.lineWidth = 2.5;
    avgLineStyle.lineColor = aaplColor;
    avgPlot.dataLineStyle = avgLineStyle;
    CPTMutableLineStyle *avgSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    avgSymbolLineStyle.lineColor = aaplColor;
    CPTPlotSymbol *avgSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    avgSymbol.fill = [CPTFill fillWithColor:aaplColor];
    avgSymbol.lineStyle = avgSymbolLineStyle;
    avgSymbol.size = CGSizeMake(6.0f, 6.0f);
    avgPlot.plotSymbol = avgSymbol;
    CPTMutableLineStyle *fixedLineStyle = [fixedPlot.dataLineStyle mutableCopy];
    fixedLineStyle.lineWidth = 1.0;
    fixedLineStyle.lineColor = googColor;
    fixedPlot.dataLineStyle = fixedLineStyle;
    CPTMutableLineStyle *fixedSymbolLineStyle = [CPTMutableLineStyle lineStyle];
    fixedSymbolLineStyle.lineColor = googColor;
    CPTPlotSymbol *fixedSymbol = [CPTPlotSymbol starPlotSymbol];
    fixedSymbol.fill = [CPTFill fillWithColor:googColor];
    fixedSymbol.lineStyle = fixedSymbolLineStyle;
    fixedSymbol.size = CGSizeMake(6.0f, 6.0f);
    fixedPlot.plotSymbol = fixedSymbol;

}

-(void)configureAxes {
    // 1 - Create styles
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = [CPTColor whiteColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor whiteColor];
    axisTextStyle.fontName = @"Helvetica-Bold";
    axisTextStyle.fontSize = 11.0f;
    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor whiteColor];
    tickLineStyle.lineWidth = 2.0f;
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;
    // 2 - Get axis set
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
    // 3 - Configure x-axis
    CPTAxis *x = axisSet.xAxis;
    x.title = @"Date";
    x.titleTextStyle = axisTitleStyle;
    x.titleOffset = 20.0f;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 4.0f;
    x.minorTicksPerInterval = 0;
    x.tickDirection = CPTSignNegative;
    int dateCount = [dates count];
    NSMutableArray *xLabels = [[NSMutableArray alloc] initWithCapacity:dateCount];
    NSMutableArray *xLocations = [[NSMutableArray alloc] initWithCapacity:dateCount];
    NSInteger i = 0;
    for (NSString *date in dates) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date  textStyle:x.labelTextStyle];
        CGFloat location = i+20;
        i=i+20;
        label.tickLocation = CPTDecimalFromCGFloat(location);
        label.offset = x.majorTickLength;
        label.rotation = M_PI * 0.25;

        if (label) {
            [xLabels addObject:label];
            [xLocations addObject:[NSNumber numberWithFloat:location]];
        }
    }
    x.axisLabels = [NSSet setWithArray:xLabels];
    x.majorTickLocations = [NSSet setWithArray:xLocations];

// 4 - Configure y-axis
    CPTAxis *y = axisSet.yAxis;
    y.title = @"Number of License";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = -40.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyNone;
    y.labelTextStyle = axisTextStyle;
    y.labelOffset = 16.0f;
    y.majorTickLineStyle = axisLineStyle;
    y.majorTickLength = 4.0f;
    y.minorTickLength = 2.0f;
    y.tickDirection = CPTSignPositive;
    NSInteger majorIncrement = 2;
    NSInteger minorIncrement = 1;
    CGFloat yMax = 14.0f;  // should determine dynamically based on max price
    NSMutableSet *yLabels = [NSMutableSet set];
    NSMutableSet *yMajorLocations = [NSMutableSet set];
    NSMutableSet *yMinorLocations = [NSMutableSet set];
    for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
        NSUInteger mod = j % majorIncrement;
        if (mod == 0) {
            CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%i", j] textStyle:y.labelTextStyle];
            NSDecimal location = CPTDecimalFromInteger(j);
            label.tickLocation = location;
            label.offset = -y.majorTickLength - y.labelOffset;
            if (label) {
                [yLabels addObject:label];
            }
            [yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
        } else {
            [yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
        }
    }
    y.axisLabels = yLabels;
    y.majorTickLocations = yMajorLocations;
    y.minorTickLocations = yMinorLocations;

    }

Original issue reported on code.google.com by kandpa...@gmail.com on 29 Mar 2013 at 6:27

Attachments:

GoogleCodeExporter commented 8 years ago
All three plots are getting the same data from the datasource. Use the plot 
parameter to the datasource methods to determine which plot is requesting 
information. Plots have an identifier property that you can use to distinguish 
them easily.

Original comment by eskr...@mac.com on 29 Mar 2013 at 11:08

GoogleCodeExporter commented 8 years ago
Please tell me how to make some space between the different points so that they 
exactly match the x-label(which are dates).Currently there is no space between 
diferent data points.

Original comment by kandpa...@gmail.com on 1 Apr 2013 at 10:21

GoogleCodeExporter commented 8 years ago
Lets take this conversation to the Core Plot discussion board where more people 
are likely to find it.

https://groups.google.com/forum/?fromgroups#!forum/coreplot-discuss

Original comment by eskr...@mac.com on 1 Apr 2013 at 11:11

GoogleCodeExporter commented 8 years ago
Ok

Original comment by kandpa...@gmail.com on 2 Apr 2013 at 7:15