d4rxh4wx/AChartEngine — pull request #1 by yuriykulikov (2012-04-17 23:00:05+00:00)
Title: GraphicalView: constructors for XML inflation

In some cases it is preferrable to inflate chart views from an XML layout.
This approach has numerous benifits:
- easier integration with other views (e.g. in a ListView)
- faster design of the chart using Graphical Layout preview in Eclipse

Views which extend GraphicalView may have additinal methods, interacting
with the chart - like adding values to series.

Example layout:

Example Activity:
public class BarChartActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BarChartView chartView = (BarChartView) findViewById(R.id.chart);
chartView.addValue(3f, 4f);
}
}

Example class for the view:
public class BarChartView extends GraphicalView {
BarChart barChart;

public BarChartView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public BarChartView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public BarChartView(Context context) {
super(context);
}

@Override
protected AbstractChart createInitialChart() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYSeries series = new XYSeries("Example series");
series.add(1, 1);
series.add(2, 2);
dataset.addSeries(series);
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.addSeriesRenderer(new SimpleSeriesRenderer());
barChart = new BarChart(dataset, renderer , BarChart.Type.DEFAULT);
return barChart;
}

public void addValue(double x, double y) {
barChart.getDataset().getSeriesAt(0).add(x, y);
repaint();
}
}

modified: achartengine/src/org/achartengine/GraphicalView.java