Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix https://github.com/peopledoc/django-chartjs/issues/56 #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions chartjs/views/columns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Tools to build Columns HighCharts parameters."""
from .base import JSONView

from ..colors import next_color

class BaseColumnsHighChartsView(JSONView):
"""Base Class to generate Column HighCharts configuration.
Expand All @@ -12,19 +12,23 @@ class BaseColumnsHighChartsView(JSONView):
providers = {}
credits = {"enabled": True}

def get_colors(self):
return next_color()

def get_context_data(self, **kwargs):
"""Return graph configuration."""
context = super(BaseColumnsHighChartsView, self).get_context_data(**kwargs)
context.update(
{
'labels': self.get_labels(),
"chart": self.get_type(),
"title": self.get_title(),
"subtitle": self.get_subtitle(),
"xAxis": self.get_xAxis(),
"yAxis": self.get_yAxis(),
"tooltip": self.get_tooltip(),
"plotOptions": self.get_plotOptions(),
"series": self.get_series(),
"datasets": self.get_series(),
"credits": self.credits,
}
)
Expand Down Expand Up @@ -100,13 +104,26 @@ def get_plotOptions(self):
return {"column": {"pointPadding": 0.2, "borderWidth": 0}}

def get_series(self):
"""Generate HighCharts series from providers and data."""
series = []
datasets = []
color_generator = self.get_colors()
data = self.get_data()
providers = self.get_providers()
for i, d in enumerate(data):
series.append({"name": providers[i], "data": d})
return series
for i, entry in enumerate(data):
color = tuple(next(color_generator))
dataset = {"data": entry,
"label": providers[i],}
dataset.update(self.get_dataset_options(i, color))
datasets.append(dataset)
return datasets

def get_dataset_options(self, index, color):
default_opt = {
"backgroundColor": "rgba(%d, %d, %d, 0.5)" % color,
"borderColor": "rgba(%d, %d, %d, 1)" % color,
"pointBackgroundColor": "rgba(%d, %d, %d, 1)" % color,
"pointBorderColor": "#fff",
}
return default_opt

def get_data(self):
"""Return a list of series [[25, 34, 0, 1, 50], ...]).
Expand Down