Skip to content

Commit 4fd55ba

Browse files
authored
Merge pull request #978 from hargata/Hargata/updated.layout
mpg chart grace revisited.
2 parents 8daa4d7 + 340b9c9 commit 4fd55ba

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

Helper/StaticHelper.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,5 +855,19 @@ public static byte[] RemindersToCalendar(List<ReminderRecordViewModel> reminders
855855
string calendarContent = sb.ToString();
856856
return Encoding.UTF8.GetBytes(calendarContent);
857857
}
858+
public static decimal CalculateNiceStepSize(decimal min, decimal max, int desiredTicks)
859+
{
860+
double range = Convert.ToDouble(max - min);
861+
double roughStep = range / desiredTicks;
862+
double exponent = Math.Floor(Math.Log10(roughStep));
863+
double stepPower = Math.Pow(10, exponent);
864+
double normalizedStep = roughStep / stepPower;
865+
866+
// Choose the closest nice interval (1, 2, or 5)
867+
double[] niceSteps = { 1, 2, 5 };
868+
double goodNormalizedStep = niceSteps.OrderBy(s => Math.Abs(s - normalizedStep)).First();
869+
870+
return Convert.ToDecimal(goodNormalizedStep * stepPower);
871+
}
858872
}
859873
}

Views/Vehicle/_MPGByMonthReport.cshtml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,30 @@
66
var barGraphColors = StaticHelper.GetBarChartColors();
77
var userConfig = config.GetUserConfig(User);
88
var userLanguage = userConfig.UserLanguage;
9-
9+
}
10+
@if (Model.CostData.Any(x => x.Cost > 0))
11+
{
12+
var chartMax = Math.Ceiling(Model.CostData.Max(x => x.Cost));
1013
var graphGrace = Decimal.ToInt32(Model.CostData.Max(x => x.Cost) - Model.CostData.Min(x => x.Cost));
11-
if (graphGrace < 0 || Model.CostData.Min(x=>x.Cost) - graphGrace < 0)
14+
var chartMin = Math.Floor(Model.CostData.Min(x => x.Cost) - graphGrace);
15+
if (graphGrace < 0 || chartMin < 0)
1216
{
1317
graphGrace = 0;
18+
chartMin = 0;
19+
}
20+
var stepSize = StaticHelper.CalculateNiceStepSize(chartMin, chartMax, 8);
21+
var remMin = stepSize > 0 ? chartMin % stepSize : 0;
22+
var cleanedMin = chartMin - remMin;
23+
if (remMin >= (stepSize / 2))
24+
{
25+
cleanedMin += stepSize;
26+
}
27+
var remMax = stepSize > 0 ? chartMax % stepSize : 0;
28+
var cleanedMax = chartMax - remMax;
29+
if (remMax >= (stepSize / 2))
30+
{
31+
cleanedMax += stepSize;
1432
}
15-
}
16-
@if (Model.CostData.Any(x => x.Cost > 0))
17-
{
1833
<canvas id="bar-chart-mpg"></canvas>
1934
<script>
2035
renderChart();
@@ -60,9 +75,11 @@
6075
scales: {
6176
y: {
6277
beginAtZero: false,
63-
grace: decodeHTMLEntities('@(graphGrace)'),
78+
max: Math.ceil(decodeHTMLEntities('@(cleanedMax)')),
79+
min: Math.floor(decodeHTMLEntities('@(cleanedMin)')),
6480
ticks: {
65-
color: useDarkMode ? "#fff" : "#000"
81+
color: useDarkMode ? "#fff" : "#000",
82+
stepSize: Math.ceil(decodeHTMLEntities('@(stepSize)'))
6683
}
6784
},
6885
x: {

0 commit comments

Comments
 (0)