Skip to content

Commit 2c31d8e

Browse files
committed
Created LogDecadeMinorTickGenerator
1 parent f03abe2 commit 2c31d8e

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using ScottPlot;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace NSD.UI
7+
{
8+
public class LogDecadeMinorTickGenerator : IMinorTickGenerator
9+
{
10+
public int TicksPerDecade { get; set; } = 10;
11+
12+
public IEnumerable<double> GetMinorTicks(double[] majorPositions, CoordinateRange visibleRange)
13+
{
14+
var minDecadeTick = double.Floor(visibleRange.Min);
15+
var maxDecadeTick = double.Ceiling(visibleRange.Max);
16+
17+
if(minDecadeTick == maxDecadeTick)
18+
return [];
19+
20+
// pre-calculate the log-distributed offset positions between major ticks
21+
IEnumerable<double> minorTickOffsets = Enumerable.Range(1, TicksPerDecade - 1)
22+
.Select(x => Math.Log10(x * 10 / TicksPerDecade));
23+
24+
// iterate major ticks and collect minor ticks with offsets
25+
List<double> minorTicks = [];
26+
for (double major = minDecadeTick; major <= maxDecadeTick; major += 1)
27+
{
28+
minorTicks.AddRange(minorTickOffsets.Select(offset => major + offset));
29+
}
30+
31+
return minorTicks;
32+
}
33+
}
34+
}

source/NSD.UI/MainWindow.axaml.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,14 @@ private void CommonChartConfig()
324324
NumericAutomatic xTickGenerator = new()
325325
{
326326
LabelFormatter = logTickLabels,
327-
MinorTickGenerator = new LogMinorTickGenerator() { Divisions = 10 },
327+
MinorTickGenerator = new LogDecadeMinorTickGenerator() { TicksPerDecade = 10 },
328328
IntegerTicksOnly = true,
329329
TargetTickCount = 10
330330
};
331331
NumericAutomatic yTickGenerator = new()
332332
{
333333
LabelFormatter = logTickLabels,
334-
MinorTickGenerator = new LogMinorTickGenerator() { Divisions = 10 },
334+
MinorTickGenerator = new LogDecadeMinorTickGenerator() { TicksPerDecade = 10 },
335335
IntegerTicksOnly = true,
336336
TargetTickCount = 10
337337
};
@@ -352,9 +352,12 @@ private void CommonChartConfig()
352352

353353
private void SetChartLimitsAndRefresh()
354354
{
355-
var top = Math.Log10(viewModel.YMax + (viewModel.YMax * 0.001));
356-
var bottom = Math.Log10(viewModel.YMin - (viewModel.YMin * 0.001));
357-
WpfPlot1.Plot.Axes.SetLimits(Math.Log10(viewModel.XMin), Math.Log10(viewModel.XMax), bottom, top);
355+
double fudgeFactor = 0.001;
356+
var left = Math.Log10(viewModel.XMin - (viewModel.XMin * fudgeFactor));
357+
var right = Math.Log10(viewModel.XMax + (viewModel.XMax * fudgeFactor));
358+
var top = Math.Log10(viewModel.YMax + (viewModel.YMax * fudgeFactor));
359+
var bottom = Math.Log10(viewModel.YMin - (viewModel.YMin * fudgeFactor));
360+
WpfPlot1.Plot.Axes.SetLimits(left, right, bottom, top);
358361
WpfPlot1.Refresh();
359362
}
360363

0 commit comments

Comments
 (0)