I am trying to add graph to my Android application using AChartEngine. The only thing different from examples is that I want to make from graph bitmap and then insert into ImageView
.
I am able to add graph without any problems to the LinearLayout, but after using method toBitmap()
on graph and inserting it into ImageView
I don't have anything and get NullPointerException
. The possible problem that I've found is ID of generated graph which is -1.
This is the code:
public class Chart extends Activity{
/** The main dataset that includes all the series that go into a chart. */
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
/** The main renderer that includes all the renderers customizing a chart. */
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
/** The most recently added series. */
private XYSeries mCurrentSeries;
/** The most recently created renderer, customizing the current series. */
private XYSeriesRenderer mCurrentRenderer;
/** The chart view that displays the data. */
private GraphicalView mChartView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_layout);
// set some properties on the main renderer
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setPointSize(5);
}
@Override
protected void onResume() {
super.onResume();
if (mChartView == null) {
initChart();
addSampleData();
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
Bitmap img = mChartView.toBitmap();
ImageView imgView = (ImageView)findViewById(R.id.graphView);
imgView.setImageBitmap(img);
} else {
mChartView.repaint();
}
}
}
Can someone tell me what should I do to generate image and insert it normally?
Use Following code for converting your Layout into Bitmap. I hope it works for You.
This is not necessarily a solution to your problem exactly, but is there a reason you need it to be a bitmap? If you just need to display the graph on your screen consider having a linear layout where you want the graph(If you don't have one already) to be placed in your xml file. Then all you need to do is add mChartView to that layout with: