а

dhtmlxChart with Django dataset


#1

Hi,
I’m trying to create a chart line (or scatter) with 2 series from Django data.
I don’t understand how I can implement a data object from Django to ithe dhtmlxChart in HTML file ?
For exemple, I would like to use this kind of dataset in Django side :
y1_series= [0, 1, 1, 2, 3, 5, 8, 13, 21]
x1_series= [0, 1, 2, 3, 4, 5, 6, 7, 8]
y2_series= [10, 20, 30, 40, 50, 60, 70, 80, 90]
x2_series= [0, 1, 2, 3, 4, 5, 6, 7, 8]

Today I use this code to send dataset (django side) to html code:

return render(request, '.../MyPage.html', {'y1' : y1_series, 'x1' : x1_series, 'y2' : y2_series, 'x2' : x2_series})

In html code I get my dataset like this (and it works) :
var y1series = {{ y1 }}
var x1series = {{ x1 }}
var y2series = {{ y2 }}
var x2series = {{ x2 }}

But, in Html side, how can I implement it in dhtmlXChart (value and/or parse)
I study and try to modify code in these 2 links without success :
dhtmlx.com/docs/products/dhtmlx … _init.html
dhtmlx.com/docs/products/dhtmlx … eries.html

Have you any idea why I can’t do it (formatted issue data from Django) ?
Thanks for your reply


#2

Hi, you can use something like next

snippet.dhtmlx.com/9358261fa

Format of data

[ { x:1, y1:0, y2:10}, { x:2, y1:1, y2:20}, { x:3, y1:1, y2:30}, { x:4, y1:2, y2:40},


#3

Thanks Stanislav, it’s work great !
To transfert my series from Django I used :
in Django, Views.py:

[code]myseries= [
{ ‘x’:1, ‘y1’:0, ‘y2’:10},
{ ‘x’:2, ‘y1’:1, ‘y2’:20},
{ ‘x’:3, ‘y1’:1, ‘y2’:30},
{ ‘x’:4, ‘y1’:2, ‘y2’:40},
{ ‘x’:5, ‘y1’:3, ‘y2’:50}
]
// here dictionary Keys between quote

return render(request, ‘…/MyPage.html’, { ‘my_series’ : myseries})[/code]

In HTML page :

[code]var cvar box = new dhtmlXLayoutObject(document.body, “3E”);

var chart = box.cells(“a”).attachChart({
view:“line”,
container:“chartBox”,
xAxis:{ template:"’#x#"}
});
chart.addSeries({ value:"#y1#", line:{color:"#a7ee70" }});
chart.addSeries({ value:"#y2#", line:{color:"#58dccd" }});
chart.parse({{ my_series|safe }},“json”);[/code]

Last (but not least) question. In my Django file Views.py, how can I iterate properly my list “myseries” with dictionnariy list ? (create a new class?)
Thanks for your reply