2014年2月20日木曜日

AFreeChartでグラフ作成 :AFreeChart

1.ライブラリ:afreechart-0.0.4.jar をダウンロードして、libs フォルダに入れる。
2.view を作成する。
3.データを入れる。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.afreechart.GraphView
android:id="@+id/graphView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.afreechart;
import org.afree.chart.AFreeChart;
import org.afree.graphics.geom.RectShape;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class GraphView extends View {
private AFreeChart chart;
private RectShape chartArea;
public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
chartArea = new RectShape();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
chartArea.setWidth(w);
chartArea.setHeight(h/2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.chart.draw(canvas, chartArea);
}
public void setChart(AFreeChart chart) {
this.chart = chart;
}
}
view raw GraphView.java hosted with ❤ by GitHub
package com.example.afreechart;
import org.afree.chart.AFreeChart;
import org.afree.chart.ChartFactory;
import org.afree.chart.plot.PlotOrientation;
import org.afree.data.xy.XYSeries;
import org.afree.data.xy.XYSeriesCollection;
import org.afree.graphics.SolidColor;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("XYSeries");
series.add(1, 1);
series.add(2, 2);
series.add(3, 3);
series.add(4, 4);
series.add(5, 5);
series.add(6, 6);
series.add(7, 7);
dataset.addSeries(series);
AFreeChart chart = ChartFactory.createXYLineChart("タイトル", "X軸ラベル",
"y軸ラベル", dataset, PlotOrientation.VERTICAL, false, true, false);
chart.setBackgroundPaintType(new SolidColor(Color.GRAY));//背景の色
chart.setBorderPaintType(new SolidColor(Color.BLACK));//枠線の色
GraphView spcv = (GraphView) findViewById(R.id.graphView1);
spcv.setChart(chart);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

関連記事

0 件のコメント:

コメントを投稿