Coreplot on iOS - How to remove the default padding so the chart fills the view completely without axis labels - objective-c

I am trying to put 6 Coreplot charts on an iPad in landscape mode so each chart is 128pixels in height (ie. 768/6)
The issue I am having is that if I set all the padding to 0 the chart still has lots of padding around the chart being plotted.
Is there a way to remove this default padding from the chart?
Thanks in advance
Damien
The pink marks in the image are the padding I want to remove
http://i.stack.imgur.com/nXzFv.png
or
http://s10.postimage.org/d2rl0ajsn/Image13.png
Here is the chart without the theme applied (The padding is still present) I added a pink border so you can see the actual size
I also tried setting the padding to -20.0 and the same padding was present but the visible chart area was cutting the chart off.
There seems to be an outer frame which is setting this padding and holding the chart area..

The default padding on the graph itself (not the plot area frame) is 20 pixels on each side. You can change that, too.
graph.paddingLeft = 0.0;
graph.paddingTop = 0.0;
graph.paddingRight = 0.0;
graph.paddingBottom = 0.0;

HINT:
Temporarily set the background color of the different views to wild colors so that you can see the affect of various padding values.
self.graph.plotAreaFrame.backgroundColor = [[UIColor yellowColor] CGColor];
self.graph.plotAreaFrame.paddingTop = 2.0f;
self.graph.plotAreaFrame.paddingRight = 8.0f;
self.graph.plotAreaFrame.paddingBottom = 30.0f;
self.graph.plotAreaFrame.paddingLeft = 42.0f;
self.graph.backgroundColor = [[UIColor greenColor] CGColor];
self.graph.paddingTop = 2.0f;
self.graph.paddingRight = 2.0f;
self.graph.paddingBottom = 2.0f;
self.graph.paddingLeft = 2.0f;
// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;
self.hostingView.backgroundColor = [UIColor redColor];

Related

Adjust UILabel to align to right of screen

Currently I am trying to create a messaging feature in an application.
I am trying to align the UILabel with a message sent from the User to the right side of the screen. As the text width contents increased, I wanted it to stretch to the left side of the screen, then line break once it achieves a max width.
As you can see I have the text line breaking correctly but I cannot get the text to sit on the screen correctly.
My solution to the problem was to adjust the width of the UILabel programmatically, however I have been unable to get it to change from the fixed size I have in the xib file of the custom outgoing message cell
Could someone please advise on how to do this?
EDIT:
I drew boxes over the current bounds of the UILabel to show what it's currently doing
This is the text moved and the UILabel bound redrawn to explain what I am trying to do.
Screenshot of app
Here, you are supposed to calculate the width of the label based on text length.
Try this API to get size of label based on text input.
CGSize yourLabelSize = [yourLabel.text sizeWithAttributes:#{NSFontAttributeName : [UIFont fontWithName:yourLabel.font size:yourLabel.fontSize]}];
or
I am not below would work in your case. but you can give a try.
Create a label in Xcode, use Auto layout to set padding (mostly 4 points on trailing & flexible leading space > 4 points). make sure you set number of lines to "0" and have text aligned to right. Do not set fixed size for width here
You can achieve the desired effect by measuring the length of the text and and calling size to fit on the label. Then you offset the frame of the label (or the surrounding superView if desired) to either the left or right side of the screen.
Here's a quick method (probably best to declare variables maxWidth and padding elsewhere). Note that 'w' is a variable for the screen's width.
-(UIView *)bubbleWithText:(NSString *)text atOffset:(float)yOff isLeftAligned:(bool)leftAligned {
float maxWidth = 200.0f;
float intraCellPadding = 5.0f;
UILabel * label = [UILabel new];
label.frame = CGRectMake(intraCellPadding, intraCellPadding, maxWidth, 0);
label.textColor = (leftAligned) ? [UIColor blackColor] : [UIColor whiteColor];
label.text = text;
label.numberOfLines = 0;
[label sizeToFit];
float width = label.frame.size.width;
float height = label.frame.size.height;
float xOff = (leftAligned) ? intraCellPadding : w-3*intraCellPadding-width;
UIView * bubble = [UIView new];
bubble.frame = CGRectMake(xOff, yOff, width + 2 * intraCellPadding, height + 2 * intraCellPadding);
bubble.backgroundColor = (leftAligned) ? [UIColor greenColor] : [UIColor blueColor];
bubble.layer.cornerRadius = intraCellPadding;
[bubble addSubview:label];
return bubble;
}
And you would call the above with something like this:
float interCellPadding = 10.0f;
float yOff = 20.0f;
bool previousWasLeft = false;
NSArray * texts = #[#"Bacon ipsum dolor amet kevin swine ham hock, leberkas porchetta salami bacon picanha shoulder pig strip steak sirloin.",
#"Short loin hamburger meatloaf jowl, sirloin swine pork belly tail t-bone venison.",
#"Flank meatloaf prosciutto ball tip strip steak rump.",
#"Short piggy.",
#"Tri-tip ribeye drumstick andouille leberkas bacon pork chop meatball pork spare ribs chicken.",
#"Strip steak filet mignon tri-tip sausage, cow frankfurter bacon ribeye cupim burgdoggen shank pork belly fatback ham hock."];
for (NSString * text in texts){
//random assignment left / right
//by creating a random number, either one or zero
//this doubles up as a boolean
bool alignLeft = arc4random_uniform(2);
//here we increment the offset
//depending on whether the previous bubble
//was on the same side or not, giving a
//bit more space for alternating bubbles.
yOff += (previousWasLeft != alignLeft) ? interCellPadding : interCellPadding/2.0f;
previousWasLeft = alignLeft;
//create the bubble and add
UIView * bubble = [self bubbleWithText:text atOffset:yOff isLeftAligned:alignLeft];
[self.view addSubview:bubble]; //or scrollView whatever
//incrmeent the offset
yOff += bubble.frame.size.height;
}
This is quick and dirty, but shows how to implement what you need. For a lot of cells (which is probably what you want), if you were going the custom route, you'd have to keep an eye on releasing them from memory. It might be easier to adapt a UICollectionView cell and simply align the content within that.
I know this is an old post, but I had this issue and came across this post. How I solved it was just with a little math
//Calculations for how far you want the UILabel from the edge of the screen
var xScreenVal = UIScreen.MainScreen.Bounds.Size.Width - 40;
var screenWidth = UIScreen.MainScreen.Bounds.Size.Width;
var textWidth = body.Text.StringSize(body.Font).Width;
var offset = screenWidth - 50 - (xScreenVal + textWidth);
var distanceFromScreenEdge = offset + xScreenVal;
//Setting the Frame
myUILabel.Frame = new CGRect(distanceFromScreenEdge, ContentView.Bounds.Top, textWidth + 25, ContentView.Bounds.Height);
This was in Xamarin.ios, but it would translate easily.

CALayer: Linear Gradient Issue

I am drawing a Linear Gradient on one of my CALayers with some colors, but randomly, with the same input, the colors are drawn on the screen as a pink color.
The code is as follows:
bottomComponents = bottomColor.colorComponents;
topComponents = topColor.colorComponents;
middleComponents = middleColor.colorComponents;
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t numberOfLocations = 3;
CGFloat locations[3] = {0.0f, MIDDLE_POINT, 1.0f};
CGFloat components[12] =
{
topComponents.red, topComponents.green, topComponents.blue, topComponents.alpha,
middleComponents.red, middleComponents.green, middleComponents.blue, middleComponents.alpha,
bottomComponents.red, bottomComponents.green, bottomComponents.blue, bottomComponents.alpha
};
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, numberOfLocations);
CGRect currentBounds = self.bounds;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetHeight(currentBounds));
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, bottomCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
Where colorComponents is just returning a struct with the color components.
When I output the color in a log, it is the proper color, but when it shows up on screen, regardless of the start colors, it is a pink-ish color.
Is there anything that I have done wrong that could cause the random pink to show up?
The pink shows up completely sporadically. I will load from the exact same values and rarely, but surely, it will show up pink. Loading from different values yields the same results. Its happening around 1-2% of the time.
Any chance some of the times you use UIColor predefined colors? (For example greenColor)
I found those don't generate proper components.
Liviu

Changing the layer shape for CPTPlotSpaceAnnotation?

I want to add a widget to my graph that a user can click and drag. I've managed to do this using CPTPlotSpaceAnnonation (because I also want it to also scroll with the data) and CPTBorderedLayer. The basic functionality works and now I want to improve the look of the widget. So far it's just a green circle.
The follow code generates the circle below, notice how the shadow is clipped to the rect of the layer and the border follows the layer and not the circle,
How can I stroke the circle and not the layer? I guess I can avoid clipping by making the frame of the layer larger. Is possible to use CAShapeLayer class with the Core Plot annotations? This would be an easy way of drawing the widgets.
// Add a circular annotation to graph with fill and shadow
annotation = [[CPTPlotSpaceAnnotation alloc]
initWithPlotSpace:graph.defaultPlotSpace
anchorPlotPoint:anchorPoint];
// Choosing line styles and fill for the widget
NSRect frame = NSMakeRect(0, 0, 50.0, 50.0);
CPTBorderedLayer *borderedLayer = [[CPTBorderedLayer alloc] initWithFrame:frame];
// Circular shape with green fill
CGPathRef outerPath = CGPathCreateWithEllipseInRect(frame, NULL);
borderedLayer.outerBorderPath = outerPath;
CPTFill *fill = [CPTFill fillWithColor:[CPTColor greenColor]];
borderedLayer.fill = fill;
// Basic shadow
CPTMutableShadow *shadow = [CPTMutableShadow shadow];
shadow.shadowOffset = CGSizeMake(0.0, 0.0);
shadow.shadowBlurRadius = 6.0;
shadow.shadowColor = [[CPTColor blackColor] colorWithAlphaComponent:1.0];
borderedLayer.shadow = shadow;
// Add to graph
annotation.contentLayer = borderedLayer;
annotation.contentLayer.opacity = 1.0;
[graph addAnnotation:annotation];
Don't set the border paths of the layer. Instead, just set the cornerRadius to half the width of the frame.
borderedLayer.cornerRadius = frame.size.width * 0.5;

How to draw a multiple point line using UIBezierPath with gradient

I need to draw a lot of lines.
I'm using UIBezierPath for drawing lines and small 5x10 pattern image for gradient.
Here's the part of code below:
l1 = [UIBezierPath bezierPath];
[l1 moveToPoint:CGPointMake(76, 373)];
[l1 addLineToPoint:CGPointMake(940, 373)];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
CGRect pathRect = self.view.frame;
path = l1;
pathLayer.frame = self.view.bounds;
pathLayer.bounds = pathRect;
pathLayer.geometryFlipped = NO;
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"line1Pattern.png"]];
pathLayer.fillColor = nil;
pathLayer.lineWidth = 8.0f;
pathLayer.opacity = 1;
[pathLayer setShadowOffset:CGSizeMake(0, 5)];
[pathLayer setShadowOpacity:0.5];
pathLayer.lineCap = kCALineCapRound;
pathLayer.lineJoin = kCALineJoinRound;
[pathArray addObject:pathLayer];
self.pathLayer = pathLayer;
If the line is straight horizontal (like blue and red) - everything is ok and the result is great.
But if I draw tilted line, or straight and then tiled - the result is not ok.
Here's the link to image: left side - what I need, right side - what mu result: example
So, as I understand - I need to rotate the gradient to match the line. But I cant imagine how to realize that.
Also some lines could have zig-zag form like on this graph: example
Could anyone help me with this problem? Or suggest another way of drawing lines.
Thanks!
You can draw only horizontal lines and rotate them with transform. But if you'll need a curve, then everything will be much more complicated.
Also you can create a gradient and draw it instead of tiled image.

How To set Plotarea of Core Plot Graph with Custom View

I am plotting graph using core plot. I am trying to plot my graph on plot area, but it contains white background. But i want to plot graph with my own background. This is my code.
CGRect frame = [self.hostingView bounds];
self.graph = [[[CPTXYGraph alloc] initWithFrame:frame] autorelease];
// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 10.0f;
self.graph.plotAreaFrame.paddingRight = 10.0f;
self.graph.plotAreaFrame.paddingBottom = 25.0f;
self.graph.plotAreaFrame.paddingLeft = 30.0f;
self.graph.plotAreaFrame.plotArea.fill=[(CPTFill *)[CPTFill alloc] initWithColor: [CPTColor colorWithComponentRed:0.8 green:0.8 blue:0.8 alpha:1.0]];
// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;
}
In above code i tried to change color but i want to draw horizontal dotted lines for each ticks on y axis.
To draw horizontal lines, set the majorGridLineStyle and/or minorGridLineStyle for the y-axis to draw lines from the major ticks and minor ticks, respectively. Use the dashPattern and patternPhase properties of the line style to control the dotting pattern. See Apple's Quartz 2D docs for details on how to use those line style properties.
try this:-
//configure plot method
//0 - Create Theme
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
// 1 - Create the graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.GraphView.frame];
graph = (CPTXYGraph *)[theme newGraph];
graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];