public void Generate(double low, double high, SKRect dataRect) { if (low == high) { ticks.Clear(); return; } if (lockTickDensity && ts != null) { GenerateTickList(low, high, ts.spacing); return; } else if (fixedSpacing != null) { if (fixedSpacing <= 0) { throw new ArgumentException("fixedSpacing must be >0"); } GenerateTickList(low, high, (double)fixedSpacing); return; } else { // Start by using a too-high tick density (tick labels will overlap) if (side == Side.left || side == Side.right) { ts = new TickSpacing(low, high, (int)(dataRect.Height / 8)); } else { ts = new TickSpacing(low, high, (int)(dataRect.Width / 24)); } // then decrease density until tick labels no longer overlap for (int i = 0; i < 10; i++) { GenerateTickList(ts.low, ts.high, ts.spacing); if (!TicksOverlap(dataRect)) { break; } else { ts.DecreaseDensity(); } } } }
private void Recalculate(TickSpacing ts, double low, double high, Graphics gfx) { ticks.Clear(); int maxCharCount = 0; for (double value = ts.firstTick; value < high; value += ts.spacing) { string label = Math.Round(value, 10).ToString(); ticks.Add(new Tick(value, label)); maxCharCount = Math.Max(maxCharCount, label.Length); } // slight performance enhancement by only measuring the longest string string maxString = new string('8', maxCharCount); biggestTickLabelSize = gfx.MeasureString(maxString, font); // add extra padding to the label to make spacing more comfortable biggestTickLabelSize.Width += 5; biggestTickLabelSize.Height += 5; }
public void FindBestTickDensity(double low, double high, RectangleF dataRect, Graphics gfx) { // Start by using a too-high tick density (tick labels will overlap) // then decrease density until tick labels no longer overlap int verticalTickCount = (int)(dataRect.Height / 8); int horizontalTickCount = (int)(dataRect.Width / 8 * 3); int startingTickCount = (side == Side.left || side == Side.right) ? verticalTickCount : horizontalTickCount; TickSpacing ts = new TickSpacing(low, high, startingTickCount); for (int i = 0; i < 10; i++) { Recalculate(ts, low, high, gfx); if (!TicksOverlap(dataRect)) { break; } else { ts.DecreaseDensity(low, high); } } }