matthisk / django-jchart

📈 A Django package for plotting charts using the excellent Chart.JS library.
BSD 3-Clause "New" or "Revised" License
121 stars 23 forks source link

Chart values duplicating in template #14

Open slammer23 opened 7 years ago

slammer23 commented 7 years ago

Hi, Have a problem with data duplication. No idea if that is Django related or jchart. Everytime I refresh the page, my bar chart is keep getting duplicated. It grows with the new data I add to the chart.

views.py: I create a new chart object from my and pass it to my template. This adds my new data to my old data and the chart just grows. Am I missing some init somewhere?

live_chart = LiveChart()
live_chart.setData(totals_overview_db)
context = {  'live_chart' : live_chart  }
return render(request, 'overview_dashboard.html',  context)

charts.py

class LiveChart(Chart):
chart_type = 'bar'
title = {'display': True, 'text': '' }
data = []

def setData(self, totals):
        for t in totals:
            self.data.append(t['total_live'])
            self.label.append(t['country'])

        self.title['text'] = 'Totals with Live'

def get_datasets(self, **kwargs):
return [DataSet(backgroundColor=('#a2c9ff', '#e8641b', '#9370db', '#16cac3', '#dcedc1'), data=self.data)]

Sorry for the reopening, I am new to GitHub :)

BR

matthisk commented 7 years ago

Why are you calling setData from the view function, a get_datasets method within the Chart class should be enough. Please consult this page of the documentation.