Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
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

1 solution

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
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900