Asrarahmed Kadri wrote: > > > Hi Folks, > > I am constructing a bar-chart using Tkinter. The function takes a list > 'data' and draws horizontal bar for each value. > Now what I want is the the canvas widget should be able to handle > variable number of data-items. I cannot figure out how to do that, > because I have hard coded the lengths of X and Y axes. I want to make > the entire function a lot more flexible so that it determines the > length of both the axes on the basis of data supplied to it. #disclaimer Note: I didn't realize you said horizontal bar charts. This does vertical bar charts. It should be trivial to change this. Sorry for not reading your post more carefully to begin with.
#for width >>> data = [1,5,6,7,8,3,2,9] >>>target_width = 600 >>>padding = 10 >>>num_of_data_items = len(data) >>>individual_width =( target_width-(padding*(num_of_data_items-1)))/num_of_data_items >>>individual_width 66 >>>individual_width*num_of_data_items 528 >>>padding* (len(data)-1) 70 >>>528 + 70 598 #for height >>>target_height = 600 >>> maxval = max(yvals) >>> for item in yvals: print int((float(item)/maxval) * target_height) 66 333 400 466 533 200 133 600 Did you honestly try to think this through before posting? It's a very simple concept. Not trying to be mean, just frank. I think you could've done this on your own if you had tried. Good luck with your bar charts. :) When you ask a question such as this "I cannot figure out how to do that, because I have hard coded the lengths of X and Y axes. I want to make the entire function a lot more flexible so that it determines the length of both the axes on the basis of data supplied to it." The way you should be reasoning is this: I have hardcoded the lengths of the x and y axes. I need to change this for my function to operate how I want it to. How do I change it? 1) I have the data set already, so how do i figure out the width? (or height, if you're doing horizontal bar graphs) Well, I can make the following distinctions: - I have a target width (the width of the canvas) that they must all fit within. - all bars will be the same width - there should be some distance between each bar. - this distance should be the same no matter how many data elements there are, IE fixed. - so if I have a fixed width between variable amounts of data, how would I design an algorithm to perform this for me on any arbitrary data set? 2) How do I figure out the height of the data sets? (or width, if you're doing horizontal bar graphs) The following distinctions can be made: - I have a target height that they all must fit within (the height of the canvas) - Only the maximum value should be the full height of the canvas. - the others should be less than this height, depending NOT on their ratio to the height of the maximum bar, but on their ratio to the data that generated this. -- what do we use for ratios? Fractions! HTH, -Luke _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor