Core Plot: custom x-axis labels for histograms

I use graphical diagrams of Core Plot to calculate the growth rate of the company. I would like the company stock symbols to be marked on an axis centered below their respective bars. Unfortunately, I spent many hours finding a way to correctly set x-tags, but did not manage to use my code. How can the x-axis labels be directed correctly?

My chart setup is as follows:

CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO]; barPlot.baseValue = CPTDecimalFromInt(0); barPlot.barOffset = CPTDecimalFromFloat(0.5f); barPlot.barWidth = CPTDecimalFromFloat(0.5f); double xAxisStart = 0; double xAxisLength = self.companies.count; double yAxisStart = 0; double yAxisLength = 0.5; CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(xAxisStart) length:CPTDecimalFromDouble(xAxisLength + 1.0)] ; plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(yAxisStart) length:CPTDecimalFromDouble(yAxisLength)] ; barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(+0.0) length:CPTDecimalFromDouble(xAxisLength)] ; 

In the following code snippet, I tried using custom shortcuts, but to no avail, as the sample diagram below shows.

 xAxis.labelingPolicy = CPTAxisLabelingPolicyNone; NSMutableArray *customLabels = [[NSMutableArray alloc]init]; [self.companies enumerateObjectsUsingBlock:^(IBCompany *company, NSUInteger idx, BOOL *stop) { NSString *labelText = company.contrSymbol; CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:labelText textStyle:xAxis.labelTextStyle]; label.tickLocation = CPTDecimalFromDouble(idx + 0.5); label.offset = xAxis.labelOffset + xAxis.majorTickLength; label.rotation = M_PI * 2; [customLabels addObject:label]; }]; xAxis.axisLabels = [NSSet setWithArray:customLabels]; 

barcharts with incorrect label positioning

Please note that my bar chart index starts at 1 :

 -(NSArray *)numbersForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange { if ( [plot.identifier isEqual:plotIdentifier] ) {; if ( fieldEnum == CPTScatterPlotFieldX ) { NSMutableArray *indexArray = [[NSMutableArray alloc] init]; [self.companies enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [indexArray addObject:[NSDecimalNumber numberWithInt: idx + 1]]; }]; return [indexArray copy]; } else if ( fieldEnum == CPTScatterPlotFieldY ) { // .. } } else return nil; // should be considered as ERROR 

}

+4
source share
1 answer
  • You should be used to using field identifiers for the right type of graph in your data source. For line art you should use CPTBarPlotFieldBarLocation and CPTBarPlotFieldBarTip . In this case, there is no difference, but this is not always true for other types of plots. For example, the x and y coordinates change for horizontal histograms.

  • A data source always returns the same data set. You need to check the indexRange parameter and only return the requested range.

  • The range of graphs for the x axis runs from 0 to 4. Labels are 0.5, 1.5, and 2.5. plotRange is what messed up the line spacing. Leave it at the default value of nil , and you should get the look you need. From plotRange docs :

If a range of graphs is provided, the bands are evenly distributed over the entire range of the graph. If the plotRange parameter is zero, the location of the bars is provided by Cocoa binding or a bar chart data source. If locations are not provided by either bindings or a data source, the first bar will be placed at zero (0), and subsequent columns will have consecutive positive integer coordinates.

+5
source

Source: https://habr.com/ru/post/1438275/


All Articles