简体   繁体   English

iOS核心图:将y轴填充调整为标签大小

[英]iOS Core Plot: Adjust y axis padding to label size

I have spent the last couple of days to use Core Plot for the first time. 我花了最近几天才第一次使用Core Plot。 It took me some some figure out how it works but I could realize almost all features I was looking for. 我花了一些时间弄清楚它是如何工作的,但我几乎可以实现我所寻找的所有功能。 But for one thing I did not find a solution: 但有一件事我没有找到解决方案:

I am using a XY-Plot with labels on both axes. 我正在使用两个轴上带有标签的XY图。 The plotAreaFrame has a left padding to shift the plotArea to the right and create some free space for the y axis labels. plotAreaFrame有一个左边的填充,可以将plotArea向右移动,并为y轴标签创建一些空闲空间。 This works fine as long as the labels are not to big, eg for values up to 100. But if the y values become bigger, eg. 只要标签不大,这样就可以正常工作,例如,对于高达100的值,但是如果y值变大,例如。 10.0000, the padding is not enough to show the complete label. 10.0000,填充不足以显示完整的标签。

Of course I could just use a higher padding but this would waste space if I have only small y values. 当然我可以使用更高的填充但是如果我只有很小的y值,这会浪费空间。

Is there any way to autosize the padding according to the labels? 有没有办法根据标签自动调整填充?

Andrei, 安德烈,

I realize this is an old question but perhaps this is a useful thought to you or others. 我意识到这是一个古老的问题,但也许这对你或其他人来说是一个有用的想法。 I wonder if you can do a bit better than just guessing the padding size you need. 我想知道你是否可以做得更好,而不仅仅是猜测你需要的填充大小。 How about this code snippet: 这个代码片段怎么样:

// set up the Y axis
float minY = 0;
float maxY = 100;      // just some max value
float tickLength = 10; // just a pleasing value
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(minY) length:CPTDecimalFromFloat(maxY)];

// set up the line style for y axis
CPTMutableLineStyle * lineStyle = [CPTLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;  // just picked a pleasing value

// set up the y axis
axisSet = [[CPTXYAxisSet alloc]init];
axisSet.yAxis.majorTickLength = tickLength;

// Now you can determine the size of the rectangle needed to display axis and labels by getting
// the axis font and measureing how many pixels were needed to draw the maximum Y axis label.

UIFont * font = [UIFont fontWithName:axisSet.yAxis.labelTextStyle.fontName size:axisSet.yAxis.labelTextStyle.fontSize];
NSString * maxYLabel = [axisSet.xAxis.labelFormatter stringFromNumber:[NSNumber numberWithDouble:plotSpace.yRange.maxLimitDouble] ];
CGSize textSize = [maxYLabel sizeWithFont:font];

// The offset you are looking for can now be computed by:
textSize.width + axisSet.yAxis.majorTickLength + axisSet.yAxis.labelOffset;

Anyway, I haven't testing the code above but the concept should apply I think... 无论如何,我没有测试上面的代码,但我认为这个概念应该适用...

After getting no answer here I looked all over the web but found no answer there either. 在这里得不到答案之后我在网上看了一遍,但也没有找到答案。 The solution which now works for me is just checking the current range of the Y axis, look how many digits the value have and calculate the necessary width manually: number_of_digits * width_per_digit + fixed_padding 现在适用于我的解决方案只是检查Y轴的当前范围,查看该值的位数并手动计算所需的宽度:number_of_digits * width_per_digit + fixed_pa​​dding

Of course this is only an estimation. 当然这只是一个估计。 But it is better that a fixed padding wich is too big for small numbers and too small for large number... 但是对于小数字而言固定填充更大,而对于大数字而言太小则更好......

I've used the proposed solution by Yohst first. 我首先使用了Yohst提出的解决方案。 But then I realized that sometimes bigger number for the maximum range won't result in a bigger label, for example when the Y value is time (as on the screenshot below). 但后来我意识到,有时最大范围的较大数字不会导致更大的标签,例如当Y值是时间时(如下面的屏幕截图所示)。

So I iterated over the labels of the axis to find the maximum width of the label and to set it as graph's left padding: 所以我遍历轴的标签以找到标签的最大宽度并将其设置为图形的左边距:

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)_graph.axisSet;
CPTXYAxis *yAxis = axisSet.yAxis;
[yAxis relabel];
CGFloat maxLabelWidth = 0;
for (CPTAxisLabel *label in yAxis.axisLabels) {
  maxLabelWidth = MAX(maxLabelWidth, label.contentLayer.bounds.size.width);
}
_graph.plotAreaFrame.paddingLeft = /* extra padding + */ maxLabelWidth;
// force update axes because of the new padding
[axisSet relabelAxes];
[axisSet setNeedsLayout];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM