Implementing a chart with AFreeChart into a View

2019-04-16 00:41发布

Here is my problem: I am using AFreeChart to display a chart in my activity. The reason why I used AFreeChart was because I first finished this chart with JFreeChart, and, realized after that, it wasn't compatible with Android.

So, with AFreeChart, I could create the same chart with exactly the same code, but I don't know how to display it on a View.

Here I am creating the chart:

private void creerGraphique(){

    //Here I have the creation of the DateSet

    AFreeChart chart = ChartFactory.createXYLineChart(
                "Mois", // Title
                "Jours", // x-axis Label
                "Repetitions", // y-axis Label
                graph, // Dataset
                PlotOrientation.VERTICAL, // Plot Orientation
                true, // Show Legend
                true, // Use tooltips
                false // Configure chart to generate URLs?
            );
}

Here I want to use it:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.graphique);

    this.stockTableau();
    this.creerGraphique();

            //HERE: How can I display it since it's already created
}

I downloaded the AFreeChart demo code, but a function which wasn't on the package was used, and so, I couldn't use it too.

I thank you for your help.

PS: I'm not an english, so I hope my problem is clear, do not hesitate to ask me more details.

2条回答
神经病院院长
2楼-- · 2019-04-16 01:15

Have you looked at the sample in AFreeChart? It's quite straight forward, look at what they did for this chart for instance :

http://code.google.com/p/afreechart/source/browse/trunk/afreechart_sample/src/org/afree/chart/demo/view/PieChartDemo01View.java

They extend a DemoView which is basically an Android View with a setChart method, and pass the chart to the View.
So either extend DemoView or create you own equivalent if you don't need everything that's in it and follow the sample !

Good luck.

查看更多
趁早两清
3楼-- · 2019-04-16 01:15

It is also worth noting that using the code you've pasted above, it would be helpful to call the chart's draw() method when you want it to draw.

As a simple example, if you were using a SurfaceView, you could create a method something like the following:

private void drawChart(AFreeChart chart, ChartRenderingInfo info) {
    getHolder.lockCanvas();
    chart.draw(canvas, area, info);
    getHolder().unlockCanvasAndPost(canvas);
}

Where 'canvas' and 'area' have been set.

This is useful if you are looking to do a very simple implementation where you don't want to use the DemoView discussed above.

查看更多
登录 后发表回答