Support a limited set of filter to ease writing SVG template
|Reported by:||Sylvain Leroux||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The Django template system is able to process any kind of text document, including SVG.
But the standard filter provided by Django are mostly "text" oriented, and does not provide basic "mathematical" operations often required by SVG template authors.
Specifically, it appears that when producing SVG chart for example, one require often to scale from model data range to chart axis. With the added complexity that SVG y-axis grows downward -- whereas many charts y-axis grow upward. For now, the only solution is (a) to perform the scaling in the view, which throws out the separation of concern or (b) preferably, provide a custom filter.
Since I think this is a common pattern while producing SVG, it could be interesting to provide a standard "scaling" filter. Here is what I propose (a patch is attached):
@register.filter("map") def map_filter(value, src_min, src_max, dst_min, dst_max): """map the value from [src_min,src_max] range to [dst_min, dst_max] range. Modeled after the corresponding map() function available (for example) in Processing http://processing.org/reference/map_.html """ return dst_min + ( value - src_min ) * ( dst_max - dst_min) / ( src_max - src_min)
As stated in the comment, this is modeled after the corresponding Processing map() function, since our designer was more familiar with that language. Maybe, "scale()" could be less confusing for Python-aware people.