https://code.google.com/p/afreechart/source/browse/trunk/afreechart_sample/src/org/afree/chart/demo/view/BarChartDemo1View.java?r=41
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
this.chart.draw(canvas, chartArea); | |
} | |
public void setChart(AFreeChart chart) { | |
this.chart = chart; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.afreechart; | |
import org.afree.chart.AFreeChart; | |
import org.afree.chart.ChartFactory; | |
import org.afree.chart.axis.CategoryAxis; | |
import org.afree.chart.axis.CategoryLabelPositions; | |
import org.afree.chart.axis.NumberAxis; | |
import org.afree.chart.plot.CategoryPlot; | |
import org.afree.chart.plot.PlotOrientation; | |
import org.afree.chart.renderer.category.BarRenderer; | |
import org.afree.data.category.CategoryDataset; | |
import org.afree.data.category.DefaultCategoryDataset; | |
import org.afree.graphics.GradientColor; | |
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); | |
GraphView spcv = (GraphView) findViewById(R.id.graphView1); | |
CategoryDataset dataset = createDataset(); | |
AFreeChart chart = createChart(dataset); | |
spcv.setChart(chart); | |
} | |
private static AFreeChart createChart(CategoryDataset dataset) { | |
// create the chart... | |
AFreeChart chart = ChartFactory.createBarChart( | |
"Bar Chart Demo 1", // chart title | |
"Category", // domain axis label | |
"Value", // range axis label | |
dataset, // data | |
PlotOrientation.VERTICAL, // orientation | |
true, // include legend | |
true, // tooltips? | |
false // URLs? | |
); | |
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... | |
// set the background color for the chart... | |
chart.setBackgroundPaintType(new SolidColor(Color.WHITE)); | |
// get a reference to the plot for further customisation... | |
CategoryPlot plot = (CategoryPlot) chart.getPlot(); | |
// set the range axis to display integers only... | |
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); | |
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); | |
// disable bar outlines... | |
BarRenderer renderer = (BarRenderer) plot.getRenderer(); | |
renderer.setDrawBarOutline(false); | |
// set up gradient paints for series... | |
GradientColor gp0 = new GradientColor(Color.BLUE, Color.rgb(0, 0, 64)); | |
GradientColor gp1 = new GradientColor(Color.GREEN, Color.rgb(0, 64, 0)); | |
GradientColor gp2 = new GradientColor(Color.RED, Color.rgb(64, 0, 0)); | |
renderer.setSeriesPaintType(0, gp0); | |
renderer.setSeriesPaintType(1, gp1); | |
renderer.setSeriesPaintType(2, gp2); | |
CategoryAxis domainAxis = plot.getDomainAxis(); | |
domainAxis.setCategoryLabelPositions( | |
CategoryLabelPositions.createUpRotationLabelPositions( | |
Math.PI / 6.0)); | |
// OPTIONAL CUSTOMISATION COMPLETED. | |
return chart; | |
} | |
private static CategoryDataset createDataset() { | |
// row keys... | |
String series1 = "First"; | |
String series2 = "Second"; | |
String series3 = "Third"; | |
// column keys... | |
String category1 = "Category 1"; | |
String category2 = "Category 2"; | |
String category3 = "Category 3"; | |
String category4 = "Category 4"; | |
String category5 = "Category 5"; | |
// create the dataset... | |
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); | |
dataset.addValue(1.0, series1, category1); | |
dataset.addValue(4.0, series1, category2); | |
dataset.addValue(3.0, series1, category3); | |
dataset.addValue(5.0, series1, category4); | |
dataset.addValue(5.0, series1, category5); | |
dataset.addValue(5.0, series2, category1); | |
dataset.addValue(7.0, series2, category2); | |
dataset.addValue(6.0, series2, category3); | |
dataset.addValue(8.0, series2, category4); | |
dataset.addValue(4.0, series2, category5); | |
dataset.addValue(4.0, series3, category1); | |
dataset.addValue(3.0, series3, category2); | |
dataset.addValue(2.0, series3, category3); | |
dataset.addValue(3.0, series3, category4); | |
dataset.addValue(6.0, series3, category5); | |
return dataset; | |
} | |
@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 件のコメント:
コメントを投稿