patterncsharpMinor
Rebase values in an array to match the chart scale
Viewed 0 times
rebasethearraychartmatchvaluesscale
Problem
I have 2
Does doing it like this make sense? How is this problem usually solved?
long[24] arrays. Both of them contain values for each hour of a day. One of them has hit count and other queue times. Now queue times in ms are 5 digit numbers, counts are 2 digit numbers. I want to show them in one chart, because queue times are to some degree corresponding with hits, but the scale is totally different. So what I came up with is that I take the max value from one list and recalculate all the values from second array using that maximum.var count = new long[24]; // values similar to [0, 9, 25, 65 ..]
var waitings = new long[24]; // values similar to [34111, 65321, 5003 ..]
count = NormalizeDataset(count, waitings.Max());
// ... skipped
@functions {
private long[] NormalizeDataset(long[] count, long max)
{
long countMax = count.Max();
var returnable = new long[24];
for (int i = 0; i < count.Length; i++)
{
var l = (count[i] * max) / countMax;
returnable[i] = l;
}
return returnable;
}
}Does doing it like this make sense? How is this problem usually solved?
Solution
Even though there is not much code to review a few remarks:
-
would eliminate any need to worry about this ever again.
-
Given the current data constraints it's not really a problem but multiplying by
-
Using LINQ would make the code a bit bit more concise.
Refactored method:
Given that
-
returnable has a hard coded length. Should your input array ever need to be larger you'd have to remember to make this larger as well. A simple var returnable = new long[count.Length];would eliminate any need to worry about this ever again.
-
Given the current data constraints it's not really a problem but multiplying by
max first could yield in an overflow. It might pay off to store the scaling factor as double instead (in which case it can also be pre-calculated).-
Using LINQ would make the code a bit bit more concise.
Refactored method:
private long[] NormalizeDataset(long[] count, long max)
{
double scale = max / (double)count.Max();
return count.Select(x => x * scale).ToArray();
}Given that
scale is probably around the order of 100 and that the scaling is more or less an arbitrary choice you could just not use double for the scale.Code Snippets
var returnable = new long[count.Length];private long[] NormalizeDataset(long[] count, long max)
{
double scale = max / (double)count.Max();
return count.Select(x => x * scale).ToArray();
}Context
StackExchange Code Review Q#79609, answer score: 4
Revisions (0)
No revisions yet.