gpt4 book ai didi

ios - 尝试使用 QuartzCore 在饼图上实现偏移半径

转载 作者:行者123 更新时间:2023-11-29 02:40:23 27 4
gpt4 key购买 nike

我正在使用XYPieChart library绘制我的饼图。我不需要切片选择,所以我手动禁用了它,而且我将始终有 3 个切片。

这就是我的图表现在的样子:

enter image description here

它工作正常,但我希望它看起来像这样:

enter image description here

这是我当前的代码,大多数事情发生在其中:

- (void)reloadData
{
if (_dataSource)
{
CALayer *parentLayer = [_pieView layer];
NSArray *slicelayers = [parentLayer sublayers];

double startToAngle = 0.0;
double endToAngle = startToAngle;

NSUInteger sliceCount = [_dataSource numberOfSlicesInPieChart:self];

double sum = 0.0;
double values[sliceCount];
for (int index = 0; index < sliceCount; index++) {
values[index] = [_dataSource pieChart:self valueForSliceAtIndex:index];
sum += values[index];
}

NSLog(@"%f", values[0]);
NSLog(@"%f", values[1]);
NSLog(@"%f", values[2]);


double angles[sliceCount];
for (int index = 0; index < sliceCount; index++) {
double div;
if (sum == 0)
div = 0;
else
div = values[index] / sum ;
angles[index] = M_PI * 2 * div;

NSLog(@"sum %f", sum);
NSLog(@"pi %f", M_PI);
}
NSLog(@"%f", angles[0]);
NSLog(@"%f", angles[1]);
NSLog(@"%f", angles[2]);


[CATransaction begin];
[CATransaction setAnimationDuration:_animationSpeed];

[_pieView setUserInteractionEnabled:NO];

__block NSMutableArray *layersToRemove = nil;

BOOL isOnStart = ([slicelayers count] == 0 && sliceCount);
NSInteger diff = sliceCount - [slicelayers count];
layersToRemove = [NSMutableArray arrayWithArray:slicelayers];

BOOL isOnEnd = ([slicelayers count] && (sliceCount == 0 || sum <= 0));
if(isOnEnd)
{
for(SliceLayer *layer in _pieView.layer.sublayers){
[layer createArcAnimationForKey:@"startAngle"
fromValue:[NSNumber numberWithDouble:_startPieAngle]
toValue:[NSNumber numberWithDouble:_startPieAngle]
Delegate:self];
[layer createArcAnimationForKey:@"endAngle"
fromValue:[NSNumber numberWithDouble:_startPieAngle]
toValue:[NSNumber numberWithDouble:_startPieAngle]
Delegate:self];
}
[CATransaction commit];
return;
}

for(int index = 0; index < sliceCount; index ++)
{
SliceLayer *layer;
double angle = angles[index];
endToAngle += angle;
double startFromAngle = _startPieAngle + startToAngle;
double endFromAngle = _startPieAngle + endToAngle;

NSLog(@"slice layers %d", slicelayers.count);


layer = [self createSliceLayer];
if (isOnStart)
startFromAngle = endFromAngle = _startPieAngle;
[parentLayer addSublayer:layer];
diff--;


if( index >= [slicelayers count] )
{
layer = [self createSliceLayer];
if (isOnStart)
startFromAngle = endFromAngle = _startPieAngle;
[parentLayer addSublayer:layer];
diff--;
}
else
{
SliceLayer *onelayer = [slicelayers objectAtIndex:index];
if(diff == 0 || onelayer.value == (CGFloat)values[index])
{
layer = onelayer;
[layersToRemove removeObject:layer];
}
else if(diff > 0)
{
layer = [self createSliceLayer];
[parentLayer insertSublayer:layer atIndex:index];
diff--;
}
else if(diff < 0)
{
while(diff < 0)
{
[onelayer removeFromSuperlayer];
[parentLayer addSublayer:onelayer];
diff++;
onelayer = [slicelayers objectAtIndex:index];
if(onelayer.value == (CGFloat)values[index] || diff == 0)
{
layer = onelayer;
[layersToRemove removeObject:layer];
break;
}
}
}
}

layer.value = values[index];
layer.percentage = (sum)?layer.value/sum:0;
UIColor *color = nil;
if([_dataSource respondsToSelector:@selector(pieChart:colorForSliceAtIndex:)])
{
color = [_dataSource pieChart:self colorForSliceAtIndex:index];
}

if(!color)
{
color = [UIColor colorWithHue:((index/8)%20)/20.0+0.02 saturation:(index%8+3)/10.0 brightness:91/100.0 alpha:1];
}

[layer setFillColor:color.CGColor];

[layer createArcAnimationForKey:@"startAngle"
fromValue:[NSNumber numberWithDouble:startFromAngle]
toValue:[NSNumber numberWithDouble:startToAngle+_startPieAngle]
Delegate:self];
[layer createArcAnimationForKey:@"endAngle"
fromValue:[NSNumber numberWithDouble:endFromAngle]
toValue:[NSNumber numberWithDouble:endToAngle+_startPieAngle]
Delegate:self];
startToAngle = endToAngle;
}
[CATransaction setDisableActions:YES];
for(SliceLayer *layer in layersToRemove)
{
[layer setFillColor:[self backgroundColor].CGColor];
[layer setDelegate:nil];
[layer setZPosition:0];
CATextLayer *textLayer = [[layer sublayers] objectAtIndex:0];
[textLayer setHidden:YES];
}

[layersToRemove enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperlayer];
}];

[layersToRemove removeAllObjects];
for(SliceLayer *layer in _pieView.layer.sublayers)
{
[layer setZPosition:kDefaultSliceZOrder];

CGPoint currPos = layer.position;
double middleAngle = (layer.startAngle + layer.endAngle)/2;
CGPoint newPos = CGPointMake(currPos.x, currPos.y);
layer.position = newPos;


NSLog(@"seno %f coseno %f", _selectedSliceOffsetRadius*cos(middleAngle), _selectedSliceOffsetRadius*sin(middleAngle));

NSLog(@"value: %f", layer.value);
NSLog(@"asa %f %f middle: %f",layer.startAngle,layer.endAngle, middleAngle);
NSLog(@"position x: %f", layer.position.x);
NSLog(@"postion y: %f", layer.position.y);
}

[_pieView setUserInteractionEnabled:NO];

[CATransaction setDisableActions:NO];
[CATransaction commit];
}
}

现在,当我更改以下行时:

CGPoint newPos = CGPointMake(currPos.x, currPos.y);

到:

CGPoint newPos = CGPointMake(currPos.x + _selectedSliceOffsetRadius*cos(middleAngle), currPos.y + _selectedSliceOffsetRadius*sin(middleAngle));

如果切片相等,我会得到预期的结果,如果它们不相等,我会得到与此类似的图像: enter image description here

顺便说一句,_selectedSliceOffsetRadius = 10;。过去一天我一直在研究这个问题,已经接近完成,但还没有准备好。

如有任何建议,我们将不胜感激。

更新:

在得到下面的精彩回答后,我得到了以下结论:

enter image description here

enter image description here

边框上还有一条细线。我不确定为什么我会得到这种效果。

更新 2:

刚刚修正了在行中添加一个:

float radiusOfPieChart = self.pieChartRight.frame.size.width/2+1;

我猜这与图表的边距有关,因此稍微增加半径可以使问题消失。

最佳答案

我使用 XYPieChart 附带的示例项目来创建它。我以前从未使用过 XYPieChart 库/框架,所以下面的代码可能有点丰富,但应该可以。

// Some variables we're going to use
CGFloat totalValueOfSlices;
CGFloat offset = -90.0f /*YOUR_CHART_OFFSET (default should be -90)*/;
CGFloat percentage = 0;
CGPoint originOfPieChart = CGPointMake(self.pieChart.frame.size.width/2, self.pieChart.frame.size.height/2);
CGFloat radiusOfPieChart = self.pieChart.frame.size.width/2;
UIBezierPath *path = [UIBezierPath bezierPath];

// The first thing we want to know is what our total value of the piechart is
for(NSNumber *number in self.slices)
{
totalValueOfSlices += [number floatValue];
}

// Now for each slice, we're going to draw a line
for (NSNumber *number in _slices)
{
// move point to the origin of the pie chart
[path moveToPoint:originOfPieChart];

// Get the value of the current slice
CGFloat sliceValueAsFloat = [number floatValue];

// Calculate how much percentage this is based on the totalValueOfSlices and the previous percentages
percentage += sliceValueAsFloat/totalValueOfSlices;

// We multiply the percentage by 360, so we get the degrees
CGFloat degrees = percentage * 360 + offset;

// Now we turn degrees to radians, we use them in the next formula
CGFloat radians = degrees * 0.0174532925;

// Now Based on the radians, radius and origin, we're going to calculate where to draw to
CGFloat x = originOfPieChart.x + (radiusOfPieChart * cos(radians));
CGFloat y = originOfPieChart.y + (radiusOfPieChart * sin(radians));

// Time to draw our line
[path addLineToPoint:CGPointMake(x, y)];

}
// Now we're going to add a sublayer to the piechart, that adds our drawn lines
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor blueColor] CGColor]; /*YOUR_BACKGROUND_COLOR*/;
shapeLayer.lineWidth = 16.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[self.pieChart.layer addSublayer:shapeLayer];

关于ios - 尝试使用 QuartzCore 在饼图上实现偏移半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25869070/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com