GWT - native js chart

Grzegorz S

I'm trying to create a PieChart widget for use in GWT project using external javascript file. How I get class instance from external js library in native function GWT ?

    public class PieChart implements IsWidget{

    Canvas c;

    public PieChart() {
        c = Canvas.createIfSupported();

        init(c.getElement());

    }

    private native void init(Element e) /*-{
            var data = [];
            var ctx = e.getContext("2d");
            var myNewChart = new Chart(ctx);
            myNewChart.Doughnut(data,{animateScale: true});
    }-*/;

    private native void add(PieChartData p) /*-{
    var data = [{
        value: p.value,
        color: p.color,
        hightlight: p.hightlight,
        label: p.label
    }];

    myNewChart.addData(data); // <-- null
}-*/;

    @Override
    public Widget asWidget() {
        return c;
    }

}

I have a problem with adding value to the chart. Please help me. Thank you.


I want to create a class PieChart as a widget using the Canvas in GWT and to can use the chart. Using the class PieChart :

    AbsolutePanel panel = new AbsolutePanel();

    PieChart p = new PieChart();
    panel.add(p); 

Compiling a project - chart is displayed. I want to dynamically add data to the chart. I don't know what to do to initialize the object with native js and use it in other methods. A reference to the variable myNewChart displays error - NullPointer, because it myNewChart variable is not initialized.

I would in this way add data :

    AbsolutePanel panel = new AbsolutePanel();

    PieChart p = new PieChart();
    panel.add(p); 
    p.add(data);

I don't know how to do it ;/

Exactly want in GWT to create a class that I could use the charts, using external js file from this page: http://www.chartjs.org/docs/

tshani

I guess the "// <-- null" is where you have the problem?!

Try keeping a reference to the created chart object.

Create a member in your class

JavaScriptObject myNewChart;

and init it in your init method by replacing

var myNewChart = new Chart(ctx);

with

[email protected]::myNewChart = new Chart(ctx);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章