areski / django-nvd3

Django wrapper for nvd3 - It's time for beautiful charts
Other
416 stars 124 forks source link

y-series not sorted alphabetically #18

Closed miquelcamprodon closed 10 years ago

miquelcamprodon commented 10 years ago

Hi areski. Great work! There is an small issue that can be solved with a line of code: The y series, named y1, y2, y3, ... are located in a dictionary and when data is read sometimes it is not sorted alphabetically.

I suggest to add a sorting after the reading of the y series:

After line 36: y_axis_list = [d for d in series.keys() if 'y' in d]

Add y_axis_list.sort()

areski commented 10 years ago

Thanks!

Won't it be better to sort your data before calling django-nvd3 ? I would imagine some peoples could be against a forced sort.

On Mon, Dec 2, 2013 at 11:34 PM, miquelcamprodon notifications@github.comwrote:

Hi areski. Great work! There is an small issue that can be solved with a line of code: The y series, named y1, y2, y3, ... are located in a dictionary and when data is read sometimes it is not sorted alphabetically.

I suggest to add a sorting after the reading of the y series:

After line 36 y_axis_list = [d for d in series.keys() if 'y' in d]

Add y_axis_list.sort()

— Reply to this email directly or view it on GitHubhttps://github.com/areski/django-nvd3/issues/18 .

Kind regards, /Areski


Arezqui Belaid, areski@gmail.com Founder at Star2Billing (www.star2billing.com)

Tel: +34650784355 Twitter: http://twitter.com/areskib LinkedIn: http://www.linkedin.com/in/areski

miquelcamprodon commented 10 years ago

Hi areski, Maybe I am not explaining the issue well (or maybe I am missing something), but my problem is that the set of series

y1: the first serie of y points y2: the second serie of y points y3: the third serie of y points

is a dictionary and I don't know the way to specify the order of series, as when the keys y1, y2, y3 are readen from the dict not necessarily y1 will appear before y2 and y2 before y3. For that reason I suggest to sort the keys of the dictionary before using them.

For example:

chartdata = {
    'x': xdata,
    'name1': 'Acepted', 'y1': ydata1, 'kwargs1': kwargs1, 'extra1': extra_serie1,                
    'name2': 'Not accepted', 'y2': ydata2, 'kwargs2': kwargs2, 'extra2': extra_serie2,
    'name3': 'Unknown', 'y3': ydata3, 'kwargs3': kwargs3, 'extra3': extra_serie3,        
}

I want to show the series in the specified order, and to ensure this I performed this small change.

areski commented 10 years ago

Thanks for making this clear, I just pushed your fix 44297a1

zebulon2 commented 10 years ago

To order the keys, use for chartdata a SortedDict() that Django provides. Then create it as explained here: https://code.djangoproject.com/wiki/SortedDict. Note the syntax you need to use to order the keys, it is different from a traditional Python dictionary.