15,999,626 members
1.00/5 (1 vote)
See more: (untagged)
The nature of the environment I am using means I need to create functionality that draws a line chart and I am having trouble working out the mathematics for a generic solution based on the highest value of any line (I know the solution I am using works fine with hard coded scale values and factors).

So onto the question:

1. The height of the container for the line chart is 2.8513in
2. The Y scale currently adapts itself to the maximum value of any line, so for example a maximum value of 1428 would mean the scale would start at 0 and end at 1450 in 10 sections.
3. The lines are drawn by a starting height and and ending height, the starting height is the previous value and the ending height is the current line value

What I need to work out is some kind of generic formula (I am good at maths but not to that level) that will work out the correct height for the lines based on the values within the scale for that container height.

To reiterate the scale is worked out dynamically based on the maximum value found but the container height is set.

If anyone can provide any help that would be most welcome.
Posted

## Solution 1

You need to multiply the maximum line height by the current items value as a percentage of maximum value.

Height = MaxHeight * (Value / MaxValue)

If you want to draw the horizonal lines across the chart to show 10ths of the maximum value then you don't have to involve the values (unless you want to assign values to the lines), you only need to know the height.

NoLine = 10

LineY = (LineNumber / NoLines) * Height
LineValue = (LineNumber / NoLines) * MaxValue

This is all based around decimal percentages, where 100% = 1.

SegmentValue / TotalValue = Decimal Percentage

v4