Open joeDiHare opened 6 years ago
That's a good question, and one that I struggled with Meta for awhile. I think the easiest way to just modify the init function so that you can provide a self.data and a self.labels. I do love this project, and use it often; I also understand why it was done this way, but the default behavior should allow for explicit attachment of data and labels for those of us getting both in a single api call.
I define the below class and then initialise it with the data
class TimeSeriesChart(Chart):
chart_type = 'line'
scales = {
'xAxes': [Axes(type='time', position='bottom')],
}
# make bezier curves pleasant but not too curvy - value found by experimentation
options = {'elements': {'line': {'tension': 0.15}}}
def __init__(self, data_labels, datasets, title=None, *args, **kwargs):
"""
:param data_labels: A list of labels for each dataset
:param datasets: A list of dicts in the format `{'x': <x-value>, 'y': <y-value>}` where the `x-value` is a date
and the `y-value` is numeric
"""
super(TimeSeriesChart, self).__init__(*args, **kwargs)
self.data_labels = data_labels if data_labels else [None] * len(datasets)
self.datasets = datasets
if not data_labels: # hide the legend
self.legend = Legend(display=False)
if title:
self.title = Title(text=title, display=True)
def get_datasets(self, **kwargs):
res = []
for i, (label, data) in enumerate(zip(self.data_labels, self.datasets)):
res.append(DataSet(type='line', label=label, data=data, borderColor=colours[i % len(colours)], fill=False))
return res
Is there a way to pass custom data to the views instead of hard-coding it? The examples in this repo have hardcoded data like:
But I can't seem to overwrite get_datasets() to accept data from other scripts, eg. this won't work and will load empty data:
Similarly, adding a init() will mess things up.
What is the
django-jchart
way to pass data do a view?This gets more confusing when trying to implement the async loading. As per the doc, we should add in the
url.py
:but how to pass the data to
LineChart()
?