tags:

views:

61

answers:

1

I have the datalist = 62, 76, 80, 72, 71, 75, 77 that i want to present as a 2D line drawed point to point. In my Wpf application i have a canvas with the height: 173 and width: 455.

How do i go about it and draw a line by my datalist and use almost the whole height of the canvas? as it is now the data is just represented on a line at the bottom at the canvas, not using (and therefor giving a bad representation of the data) whole heigh of the canvas. It would also be nice to place it in the center of the box too...

+3  A: 

You'll need to transform your datalist to represent the whole height of the canvas. There are two obvious ways of doing this:

1) Divide the height of the canvas by the largest value in your datalist. Multiply all the values in the datalist by this and bind to these new datapoints.

2) Add the line as you are now and then apply a scale transform of the multiplier from option 1.

Either way you shouldn't need to centre now since the whole canvas will be used up.


I see on review you actually say "almost the whole height of the canvas" and the Adrian's comment below is correct in that I didn't think through the lower bounds. Here's some better pseudo-code

int border = 20; //How much of the canvas you *don't* want to use
int graphHeight = Canvas.Height - border;
int maxValue = DataList.GetMaxValue();
int minValue = DataList.GetMinValue();

double multiplier = graphHeight / (maxValue - minValue);

foreach(int value in DataList)
{
    int distanceFromBottom = value - minValue;
    double proportionalValue = distanceFromBottom * multiplier;
    double newValue = proportionalValue  + (border/2) // move it up to the middle of the canvas
}

Store the newValues in a new DataList and bind to that.

Martin Harris
Either of these should only use the canvas from 62*173/77 to 173, leaving everything below blank.
Adrian Archer
@Adrian Good catch, I've updated my answer
Martin Harris
you fixed it I think
Adrian Archer
works great, thanks. but the data is presented in the opposite. max = min and so on, how do i flip it?
Jason94
@Jason94 The only way I can see that happening is if you've set your border to be bigger than the canvas height (so graphHeight becomes negative) or you got (maxValue - minValue) bacwards (so multiplier becomes negative).
Martin Harris