Perspective Unspoken

My perspective on Git, Docker, Python, Django, PHP and other stuff

Which chart library is best for Django?

I have a project that I’m working on and I want to integrate a chart library to add some color to the data I’m presenting. It’s a budget based app. In the app I’m using charts to show expenses vs income, how much you’ve spent on different categories and various budget visualizations. I had a few requirements that I was looking for.

  • Easy to use – I’ve learnt the hard way it’s way easier to write code than it is to read, especially your own. I wanted a library that’s an absolute pleasure to read (after I’ve written), it should jell well with the rest of my codebase.
  • Customizable – I need a library that will allow me to customize just about all I can… look and feel, the JS options for the chart, configuration of multiple series, chart titles, etc…
  • Extensibility  I’m working on a project that I actually plan to use for some time in the future, so I the library needs to be built in such a way that it lends well to extensions. I would be starting with simple multi-series charts but then I’m going to want to do more complex things like mixing charts.
  • Chart.js – I did my research and I’ve selected Chart.js as my JS library of choice. My library of choice needs to support Chart.js.

I started my search for libraries at django-packages.com. Great place to find libraries. Based on the activity and project status I decided to review Django-chartit, Django-graphos and Django-chartjs.

 

Django Chartit

Django 1.8 – 1.10 | Python 3 | 11 contributors | 6 releases

This was one of the most fully featured libraries of the three. It allows you to configure simple or complex charts. It’s a pretty rigid implementation. It cleanly separates data from presentation, so there’s the notion of a DataPool  that’s passed into a Chart  object. Let’s look at the pros and cons.

Pros

  • Aggregates – There’s builtin support for doing aggregates. They offer a PivotDataPool  which allows you to quickly pull aggregated data from your model.
  • Chart Customization – There’s a great interface for overriding chart options. Chart options are passed into the Chart constructor as part of the configuration of each series. This makes it really easy to perform deep customizations on each chart.
  • Documentation – The documentation that is provided is more than adequate to  show you how to get started and how to use the library well for simple and more complex cases.

Cons

  • Only models – the DataPool  that exists in the library only supports Django models. This may not be a bummer for some but if you ever need to pass in data from elsewhere, say an API, this may not be the library for you.
  • Highcharts only – if you need a library that works with a library other than Highcharts, this may not be the library for you. It’s $50 for a startups that are starting up, and $410 upwards after that.
  • Source code – it will take some reading to get to the point to be able to make extensions, well.. it’ll take me some time. It’s feels a tad bit cryptic. There are tonnes of abbreviations in the codebase that I kept stumbling on. I was trying to get a gist of how the functionality works under the hood and it wasn’t the easiest read.

Feel free to check out the Github repo here.

Django Graphos

Django ? | Python 3 | 21 contributors | 2 releases

The graphos library is another nice library that has a whole slew of supported charting engines and also several data sources. On the list of charts supported are Highcharts, Flot, Google Charts API, YUI Charts, Matplotlib and Morris.js. Let’s look at the pros and cons.

Pros

  • Multiple Datasources – out of the box there is support for the Django ORM, CSV files and plain Python nested lists via derivates of their DataSource  class. It’s really easy to make a DataSource  as well.
  • Chart Libraries – you have a healthy selection of chart libraries to work with, which is great. Due to the fact all the charts share a base class, in theory it wouldn’t take much work to switch between if that becomes a thing that’s needed.
  • Extensible – this library is absolutely beautiful under the hood and would be a joy to extend. There is clear separation of concerns, there’s the concept of a DataSource  and a Chart . Both have very simple hooks that can be overridden to roll your own.
  • Chart Customization – this library doesn’t get in the way of letting you passing options to the underlying JS chart API. It lets you pass in an options  dict to each chart.
  • Documentation – the documentation for this is quite good and there’s also a demo site that the developers have put together. It has examples for each chart type and allows you to get a good idea of what your chart will look like before further configuration.

Cons

  • Aggregates – there’s no inbuilt support for doing aggregates. You’ll have to build in your own implementation of that. The good thing is that the library isn’t very opinionated about data structure or anything. It just requires a list of rows containing your data. That’s all.
  • No domain logic – there’s no chart related logic and concepts in the source. So, no concept of series and axes. It’s just renderers and charts. While this implementation is quite clean, it’s not obvious how you’d build in more chart specific functionality, say, for example a composite chart (line + bar chart).

 

Django ChartJS

Django 1.5+ | Python 3 | 6 contributors | 5 releases

For starters, this library actually supports both ChartJS and HighchartJS. Anyway, this small library takes a completely different perspective on things. This library is designed to deliver data to your chart via AJAX. Let’s look at it more closely.

Pros

  • AJAX ready – it’s nice that your charts are AJAX ready out of the box. It’s something most solutions work towards. If you’re going to create interactive charts, you want persons to interact without reloading the page, so AJAX out of the box is certainly a plus.
  • Intuitive – well, this library uses a very thin layer of abstraction, it’s literally just a View mixin that you can use to serve the data for your chart. It’s too easy to get started with this library.
  • Demo project – in absence of proper documentation, there’s a demo project that provides some guidance on how to use and customize the library.

Cons

  • Chart Customization – there’s no customizing of charts from Django, you have to do that in Javascript. This chart doesn’t participate in customization at all.
  • No template tags –  unlike all the other libraries, this one doesn’t provide any template tags. You will have to devise your own if you want to not violate DRY principles.
  • Documentation – this is a one page on Github. There’s a demo project though.

You can check out the repo here.

 

The Winner

Well… it’s going to depend on what you want. For me, the winners are really between ChartIt and Graphos. If you’re fine working with HighchartJS and ChartIt provides everything you need. Go right ahead with it. Just know that you’re going to need to budget some time if you want to go beyond what exists for ChartIt. Graphos is a great choice for a library where you don’t need anything too complex and you have time to write extensions you need. It feels like a project that you can easily contribute to and extend in your own projects.

Personally, I’m still undecided about what I’m going to use in my project. I might end up extending Graphos to have support for ChartJS. I’d also love to work with devs to see how we could build in more chart related stuff like mixing multiple charts or doing pivots etc…

Thanks for reading! Share your feedback below.

chartitchartjschartscomparisonDjangographosprogrammingPython

jaywhy13 • March 5, 2017


Previous Post

Next Post

000webhost logo