Hi

Many thanks for Your prompt solutions .
I have found an example using Highcharts and modified it to be generic .  
The view is always simple and constant .


def chart_bars():
    graphname="Grade By Person"
    x_chart=[]
    y_chart=[]
    rows=db.executesql("select * from Grades order by grade 
desc",as_dict=True)
    for r in rows:
        x_chart.append(str(r["name"]))
        y_chart.append(r["grade"])
    title="Grades charts"
    stitle="web2py - Graph based on query"
    return Generic_bar(graphname,x_chart,y_chart,title,stitle)

def Generic_bar(graphname ,x_chart , y_chart , title ,stitle  ):
    data_map={}
    data_map["graphname"]=graphname
    data_map["dados"]=y_chart
    data_map["x_values"]=x_chart
    data_map['titulo']=title
    data_map['subtitulo']=stitle
    chart="""
    <script type="text/javascript">
    Highcharts.setOptions({
        lang:{
        downloadJPEG: "Download as JPG",
        downloadPDF: "Download as PDF",
        downloadPNG: "Download as PNG",
        downloadSVG: "Download as Vector SVG",
        loading: "Loading...",
        noData: "No Data to Display",
        printChart: "Print Graph ",
        }
        });
            // Build the chart
            $('#chart').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: '%(titulo)s'
        },
        subtitle: {
            text: '%(subtitulo)s'
        },
        xAxis: {
            categories: %(x_values)s,
            crosshair: true
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Grade (1-100)'
            }
        },
        tooltip: {
            headerFormat: '<span 
style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td 
style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b> {point.y:.1f} </b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        credits:{enabled:false},
        series: [{
            name: '%(graphname)s',
            data: %(dados)s
        }]
    });
    </script>
    """ %data_map
    return dict(chart=XML(chart))

View :-
----------

{{extend 'layout.html'}}
<script src="{{=URL("static", "js", args="highcharts.js")}}" 
type="text/javascript"></script>
<script src="{{=URL("static", "js", args="exporting.js")}}" 
type="text/javascript"></script>
<div id="chart"></div>
{{=chart}}
 



On Tuesday, October 31, 2017 at 10:00:58 AM UTC+2, rafi farchi wrote:

> Hi
>
> I am looking for a simple working example (controler function + view) that 
> uses d3.js to plot graph (lets say bar or pie) 
> but not based on hard coded values instead based on a database query .
> if possiple also example of plotting 2 graphs on the same page (both based 
> on the same data)
>
> Many thanks
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to