我只使用 CORE GRAPHICS 处理图表。我已经成功地完成了折线图、条形图和单线图的绘制。
现在我的要求是
但是当我在上下文中绘图时,我只得到一个这样的图表
我已经将图表的百分比放在一个数组中并使用 for 循环,我一直在一个数组中绘制所有百分比,但我得到的最终结果只是一个仪表图是数组中的最后一个对象。
我使用这些 link 绘制了这些图表
像这样稍微改变了代码和绘制图表
我用来绘制仪表图的代码是
首先我从我的 Viewcontroller 调用 guagegraph(UIView 的子类)
percentageArray 包含每个量度的百分比
percentageArray = [[NSArray alloc] initWithObjects:@"80", @"76", @"92", @"49", nil];
for (int i = 0; i < percentageArray.count; i++)
{
[guageGraph setPercent:[percentageArray[i] intValue] withIndex:i];
}
然后在 guagegraph 中使用下面的方法
- (void)setPercent:(int)percent withIndex:(int)i
{
CGFloat floatPercent = percent / 100.0;
floatPercent = MIN(1, MAX(0, floatPercent));
percentLayer.percent = floatPercent;
percentLayer.i = i;
[self setNeedsLayout];
[percentLayer setNeedsDisplay];
}
然后调用 percentLayer 类(CALayer 的子类)并绘制上下文
-(void)drawInContext:(CGContextRef)ctx
{
[self DrawRight:ctx];
[self DrawLeft:ctx];
}
-(void)DrawRight:(CGContextRef)ctx
{
CGPoint center = CGPointMake(self.frame.size.width / 2, 160);
CGFloat delta = -toRadians(270 * percent);
CGContextSetFillColorWithColor(ctx, [UIColor orangeColor].CGColor);
CGContextSetLineWidth(ctx, 1);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetAllowsAntialiasing(ctx, YES);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRelativeArc(path, NULL, center.x, center.y, 135 - 5 - (15 * i), (3 * M_PI / 4), -delta);
CGPathAddRelativeArc(path, NULL, center.x, center.y, 150 - 5 - (15 * i), (3 * M_PI / 4) - delta, delta);
CGPathAddLineToPoint(path, NULL, center.x, center.y);
CGContextAddPath(ctx, path);
CGContextFillPath(ctx);
}
-(void)DrawLeft:(CGContextRef)ctx
{
CGPoint center = CGPointMake(self.frame.size.width / 2, 160);
CGFloat delta = toRadians(270 * (1 - percent));
CGContextSetFillColorWithColor(ctx, [UIColor groupTableViewBackgroundColor].CGColor);
CGContextSetLineWidth(ctx, 1);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetAllowsAntialiasing(ctx, YES);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRelativeArc(path, NULL, center.x, center.y, 135 - 5 - (15 * i), (M_PI / 4), -delta);
CGPathAddRelativeArc(path, NULL, center.x, center.y, 150 - 5 - (15 * i), (M_PI / 4) - delta, delta);
CGPathAddLineToPoint(path, NULL, center.x, center.y);
CGContextAddPath(ctx, path);
CGContextFillPath(ctx);
}
drawRight 方法是绘制橙色的,draw left 方法是绘制grouptableviewbackground 颜色的。
当循环开始时,绘制第一个百分比图表,当第二个循环开始时,第一个绘制的图表被当前循环百分比覆盖并绘制第二个图表,这样只显示最后一个图表
感谢任何帮助。
最佳答案
我认为您的问题出在这里 ->
percentageArray = [[NSArray alloc] initWithObjects:@"80", @"76", @"92", @"49", nil];
for (int i = 0; i < percentageArray.count; i++)
{
[guageGraph setPercent:[percentageArray[i] intValue] withIndex:i];
}
我觉得您应该改为将它们添加到数组中。并在绘制完所有对象后显示它们。
编辑
首先
让您的 precentLayer 类显示不同的色标。所以将这段代码添加到 -(void)drawRight
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *randomColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
//update this line
CGContextSetFillColorWithColor(ctx, randomColor.CGColor);
如果不是这样的话,看看它们是否只是相互重叠......
============================================= =============================
THEN 因为只有一个 View ,尝试这样的事情。
percentageArray = [[NSArray alloc] initWithObjects:@"80", @"76", @"92", @"49", nil];
for (int i = 0; i < percentageArray.count; i++) {
UIView *percentGauge =[self setPercent:[percentageArray[i] intValue] withIndex:i];
//make background clear so we can see the guage behind it
percentGauge.backgroundColor = [UIColor clearColor];
}
并使 -(void)setPercent: withIndex: 返回一个 UIView 所以....
- (UIView *)setPercent:(int)percent withIndex:(int)i {
CGFloat floatPercent = percent / 100.0;
floatPercent = MIN(1, MAX(0, floatPercent));
percentLayer.percent = floatPercent;
percentLayer.i = i;
[self setNeedsLayout];
[percentLayer setNeedsDisplay];
return self;
}
如果这没有任何帮助,您可以将您正在使用的文件上传到保管箱,我可以试一试。
关于ios - 使用 Coregraphics 一次绘制多个 Guage 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26629360/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h