Code Monkey home page Code Monkey logo

ckanext-basiccharts's Introduction

ckanext-basiccharts

This extension adds Line, Bar and Pie charts to CKAN, using the new Resource View that are still being developed on the master branch (currently unreleased).

It uses Flot Charts, which is compatible with all major browsers (including IE6+).

Installation

Clone this repository and run python setup.py install. Then add which charts you'd like to your ckan.plugins in your CKAN config file.

You can then enable any (or all) of:

  • linechart
  • barchart
  • piechart
  • basicgrid

Finally, restart your webserver. You should see the new chart types as options in the view type's list on any resource that's in the DataStore.

Usage

There are 3 kind of attributes that define what the chart will be: filters, axes, and groups. We'll create charts in the next sections to define them all using the following data:

State Date Population
California 01-01-1990 29,760,021
California 01-01-2000 33,871,648
California 01-01-2010 37,253,956
New York 01-01-1990 17,990,455
New York 01-01-2000 18,976,457
New York 01-01-2010 19,378,102

Filters

If you don't want all data to be plotted in the chart, you can add filters. Here, you define what to include.

For example, with the sample data, if you want to display just for California, you'd create the filter:

State: California

Multiple filters on the same column work as OR. For example, to plot just the data for 01-01-2000 and 01-01-2010, you'd do:

Date: 01-01-2000
Date: 01-01-2010

Multiple filters on different columns work as AND. If we'd add all filters defined in the last paragraphs, we would plot data only for California in 01-01-2000 or 01-01-2010. In techie terms, it'll be State == "California" AND (Date == "01-01-2000" OR Date == "01-01-2010")

Currently you can't exclude data, only include. There's no way to negate a filter (to all states that are not California, for example).

To learn more about filters, check ckanext-viewhelpers.

Axes

This defines what column will be plotted in each axis. Line and bar charts have two axes, Y and X; pie charts only have one, Y.

As long as the DataStore interpreted the column types correctly, the charts will work with any kind of data (numeric, text, or date). To check if this is the case, check the Upload log in the Manage resource's DataStore tab. You should see something like:

Determined headers and types: [{'type': u'text', 'id': u'State'}, {'type':
u'timestamp', 'id': u'Date'}, {'type': u'numeric', 'id': u'Population'}]

Just confirm that the types defined are correct. If not, try to understand why and fix it, as the charts created might behave incorrectly. The DataStore's documentation might help you.

Groups

This allows you to group the data being plotted, based on a certain column. It's optional for bar and line charts, but required for pie charts.

For example, say that you want to create a line chart that shows California and New York's population growth. You define Date to be your X axis, and Population to be your Y axis. Then, when you preview the chart, it'll be like:

Timeline of population growth without groups

Not what you'd want. The problem is that when there's no group, all data is groupped together. You'd like to separate the population from California from the New York's. To do that, you need to group by State.

Timeline of population growth groupped by state

Much better. Notice that there's a group's legend as well. To enable it, you need to check Show groups' legend.

The bar chart works similarly. The pie charts are a bit different. To define them, you're required to set up a column to group by. With the same data, if you'd want to create a chart for, given the sum of California and New York's population, which percentage belongs to one and which to the other, you'd set up group by as State, and Y axis as Population. That'll create:

Timeline of population growth groupped by state

Beware that as we have multiple rows for the same group, what's being plotted on the pie chart is the sum of all values. In this case, the sum of the population in 1990, 2000, and 2010. If you want a single year, add a filter.

As the legends are always embedded in the chart, there's no Show groups' legend option.

Common problems

There are no side-by-side bars

If you'd tried plotting a timeline of the population growth in California and New York as a bar chart, as we did in the last section using a line chart, you'd end up with something like this:

Timeline of population growth groupped by state as bars

Don't let this fool you. It looks like a stacked bar chart, but it's not (there's no stacked bars chart as well). The bars aren't stacked, they're drawn over each other. This almost always isn't what you want. To fix it, we need to support side-by-side bars, but we don't yet. If you'd like to help, check issue #8.

License

Copyright (C) 2014 Open Knowledge Foundation

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/.

ckanext-basiccharts's People

Contributors

amercader avatar aqu1ntero avatar joetsoi avatar jqnatividad avatar kindly avatar tino097 avatar tomecirun avatar vitorbaptista avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ckanext-basiccharts's Issues

Line charts join points in order of y-axis not x-axis

instead of joining points in a line graph according to the x-axis, for instance date, the points are joined in by the y axis. This can be seen in the following image.

screenshot-residential electricity usage by precinct 2006-2013 - csv-geo-au - the city of sydney open data - google chrome
It might not be noticed if all the test include on monotonically increasing or decreasing data points.

This is possibly related to the column chart order issue too #32

Only works with Basic Grid

After a fresh install in 2.8.2 with this plugins:

ckan.plugins = datastore datapusher stats text_view image_view recline_view linechart barchart piechart basicgrid

Only works basicgrid. Barchart, piechar and linechart give me this error:

After a deep search, the error is: " ckan.views.filters is undefined" in javascript

In CKAN 2.7.4 works perfect (with the same resource).

TypeError: 'NoneType' object is not iterable

I had to change IResourceView to IResourcePreview in plugin.py
Now I get this error: โ‡ TypeError: 'NoneType' object is not iterable

This traceback might help:
Module /home/XXX/ckan/lib/default/src/ckan/ckan/templates/package/resource_read.html:74 in block "data_preview" view
<<
{% block data_preview %}
{{ h.resource_preview(c.resource, c.package) }}
{% endblock %}

Module ckan.lib.helpers:1596 in resource_preview view
<< data_dict = {'resource': resource, 'package': package}

       if datapreview.get_preview_plugin(data_dict, return_first=True):
           url = url_for(controller='package', action='resource_datapreview',
                         resource_id=resource['id'], id=package['id'], qualified=True)

Module ckan.lib.datapreview:98 in get_preview_plugin view
<< else:
# new school provide a dict
p_info.update(data)
# if we can preview
if p_info['can_preview']:
TypeError: 'NoneType' object is not iterable

Problem with tooltips

I'm not sure what's going on, but the tooltip's value for the X axis is wrong. Here's an example:

tooltips-problem

The data is:

time,speed
0,0
1,5
2,10
3,13
4,17
5,21
6,25

Can not use basic chart with private dataset

@vitorbaptista

Can you look at fixing this. This is because you can you use datastore_search_sql with private and this is causing a lot of confusion.

We should probably just use the patched datastore_search for this. I am happy to squeeze this into the views branch.

Load scripts after fanstatic's scripts

If we don't do that, we're unable to use jQuery or any other resource that's loaded with fanstatic.

We can'ts imply create a new .js and add it to fanstatic as well because we need stuff from the jinja2 templates (i.e. filter templates, the resource, etc.).

The current workaround is to use window.onload, but that doesn't scale (there can only be a single window.onload on a page).

Charts not showing in preview

I installed basic charts and I have my data in datastore. I was able to select bar chart or pie chart from the views option but the charts are not loaded in the preview. When I checked the browser console, I got the following error lines

jQuery.Deferred exception: self.el.context is undefined initialize@http://172.30.3.49:5000/fanstatic/basiccharts/:version:2018-11-28T08:31:38.55/basiccharts_view.js:26:9
module.createInstance@http://172.30.3.49:5000/fanstatic/base/:version:2018-06-05T08:57:29.15/module.js:338:7
module.initializeElement/<@http://172.30.3.49:5000/fanstatic/base/:version:2018-06-05T08:57:29.15/module.js:307:9
each@http://172.30.3.49:5000/fanstatic/vendor/:version:2018-06-05T08:57:28.68/jquery.js:362:11
module.initializeElement@http://172.30.3.49:5000/fanstatic/base/:version:2018-06-05T08:57:29.15/module.js:303:5
module.initialize/<@http://172.30.3.49:5000/fanstatic/base/:version:2018-06-05T08:57:29.15/module.js:280:7
each@http://172.30.3.49:5000/fanstatic/vendor/:version:2018-06-05T08:57:28.68/jquery.js:362:11
each@http://172.30.3.49:5000/fanstatic/vendor/:version:2018-06-05T08:57:28.68/jquery.js:157:11
module.initialize@http://172.30.3.49:5000/fanstatic/base/:version:2018-06-05T08:57:29.15/module.js:279:44
ckan.initialize/<@http://172.30.3.49:5000/fanstatic/base/:version:2018-06-05T08:57:29.15/main.js:46:7
fire@http://172.30.3.49:5000/fanstatic/vendor/:version:2018-06-05T08:57:28.68/jquery.js:3317:12
fireWith@http://172.30.3.49:5000/fanstatic/vendor/:version:2018-06-05T08:57:28.68/jquery.js:3447:8
mightThrow@http://172.30.3.49:5000/fanstatic/vendor/:version:2018-06-05T08:57:28.68/jquery.js:3642:14
resolve/</process<@http://172.30.3.49:5000/fanstatic/vendor/:version:2018-06-05T08:57:28.68/jquery.js:3651:13
undefined

Please help!!!

Remove code related to filters' UI

As per CKAN's #1792, this will be done on core. The only thing we need to do is set ourselves as filterable, and support the filters.

Series should be optional

The attributes' for a bar chart, for example, are:

  • X Axis
  • Y Axis
  • Series

The axes are quite self-explanatory. The series define a kind of "group by".
For example, say that you want to plot the population of every city in the US.
You'll set up the attributes as:

  • X Axis == "city"
  • Y Axis == "population"
  • Series == ???

There's no need for series in this case. As we required them, you'll end up
setting Series == "city" just to create the chart. This creates another
problem. Now, each bar will be in a different colour, for no reason at all.
Just a lot of chart junk. What you really want is to not set Series.

Series are only useful, in that case, if you'd like to group the bars by state. So you'll use:

  • X Axis == "city"
  • Y Axis == "population"
  • Series == "state"

Now the bars will be coloured depending on their state, and you'd add a legend
to map which color represent which state.

There's a problem with pie charts, though. If Series isn't required
anymore, and you create a pie chart with:

  • Y Axis == "population"

It'll simply be a pie with 100%, and no labels at all. Not very useful. In that
case, having Series should be required.

Add option to suppress labels in very thin pie slices

Flot has the ability to suppress labels when a pie slice goes below a given threshold. Perhaps, if a slice is below 5%, the label should be hidden?

If tooltips are on (#29) and legends are an available option (#28), then suppressing labels shouldn't be a problem.

Resizing the pie charts when adding to a dashboard is broken

To reproduce, create a pie chart, add it to a dashboard and play a bit resizing it. There're some sizes where it goes crazy, with the div's scrollbars flickering.

It might be a bug in jquery.resize, or in flot's resize. I did a workaround a while ago by using width/height to 90%, which diminishes the issue, but it would be great to fix it for good.

Sorting numeric fields isn't working

The problem is that we sort the data before parsing it, so we're sorting numeric fields as strings.

function initPlot(elementId, sortData, resource, params) {
  // ...
  if (sortData) {
    hits = sortData(hits);
  }

  data = prepareDataForPlot(fields, hits, config.xaxis, config.yaxis, params);
  // ...

The sortData() needs to happen after prepareDataForPlot(), or at least we need to do the parsing before.

Add ability to disable legend

Sometimes they don't add anything useful, specially in the barchart. Specially
if you're filtering a single year, and the date is in the series as well. In
that case, there'll be a single legend (which might be also in the chart title
or something).

Maybe, instead of adding the option, we can disable automatically given some
constraints.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.