achart engine annotation raising exception errors - android

I am trying to develop an app that will display a graph.On the y-axis there will be data and on the x-axis months(e.g jan,feb,march,april ...).As far as i know am trying to use annotations to add the months but am having errors.I cant seem to find a good enough example on how to use annotations.Please help.Heres is my code.
public class LineGraph {
public Intent getIntent(Context context){
int x[]={1,2,3,4,5,6};
int y[]={1,4,9,16,25,36};
String xaxis[]={"jan","feb","march","april","june","july","august"};
TimeSeries series=new TimeSeries("line1");
for(int i=0;i<x.length;i++){
series.add(x[i],y[i]);
series.addAnnotation(xaxis[i], x[i], y[i]);
}
XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYSeriesRenderer renderer=new XYSeriesRenderer();
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
XYMultipleSeriesRenderer mRenderer=new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
mRenderer.setChartTitle("haha");
Intent intent = ChartFactory.getLineChartIntent(context,dataset,mRenderer,"Line graph title");
return intent;
}
}
I am trying to display text lables for the x-axis instead of integer values.A better aproach is also welcome.

Annotations are a good choice for displaying random text wherever you need on the graph. In order to display custom labels, you will have to add custom labels:
renderer.addXTextLabel(x, text);
You will also need to disable the default numerical labels:
renderer.setXLabels(0);

Related

android achartengine : show axes even if there is no plot data

am trying to draw a line chart with achartengine in an Android App. The graph auto-refreshes every few seconds. The problem is : the axes become invisible if there is no data to plot. How do I make the axes appear even if there is nothing to plot?
Kindly help.
private XYMultipleSeriesDataset graphDataset = null;
private XYMultipleSeriesRenderer graphRenderer = null;
private GraphicalView graphView;
.....
.....
this.graphDataset = new XYMultipleSeriesDataset();
this.graphRenderer = new XYMultipleSeriesRenderer();
this.graphRenderer.setXLabels(0);
...
/// other initialization code, like labels & fonts
...
// then i add the data to the series
XYSeries series = new XYSeries("Simple graph");
for (int i=0; i<valueArray.size();i++) {
series.add(valueArray.get(i), yValue.get(i));
}
this.graphDataset.addSeries(series);
....
// then I do renderer initialization
XYSeriesRenderer xyRenderer = this.setChartLineProperties(index);
...
// then finally initializing the graphview
this.graphView = ChartFactory.getLineChartView(this, this.graphDataset,
this.graphRenderer);
In order for the axes to be displayed, the chart needs to know what values range it has to display. If there is no value added to the dataset, the range can be set this way:
renderer.setXAxisMin(minX);
renderer.setXAxisMax(maxX);
renderer.setYAxisMin(minY);
renderer.setYAxisMax(maxY);

Formatting achartengine line graph

I am using achartengine, to plot a line graph. I am trying to customize it to have Y axis lines which pass through the values, and show the values on top. Does any one have any idea, how can I achieve an effect like this one :
Here is what I currently have :
And here is the code Snippet :
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(true);
renderer.setColor(Color.WHITE);
renderer.setLineWidth(2f);
XYMultipleSeriesRenderer seriesRenderer = new XYMultipleSeriesRenderer();
seriesRenderer.addSeriesRenderer(renderer);
seriesRenderer.setLabelsTextSize(20); // Text size
seriesRenderer.setShowAxes(true); // show both axes
seriesRenderer.setShowLegend(false);
seriesRenderer.setShowGridX(true); // X grid helps identify values
seriesRenderer.setShowLabels(true); // See the values
seriesRenderer.setYLabelsAlign(Align.RIGHT);
seriesRenderer.setZoomButtonsVisible(false); // bye bye zoom
seriesRenderer.setXAxisMin(0);
seriesRenderer.setXAxisMax(10);
seriesRenderer.setTextTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Lifestyle Rounded M54.ttf"));
seriesRenderer.setPointSize(10f);
seriesRenderer.setPanEnabled(true, false);
View view = ChartFactory.getLineChartView(context, dataset, seriesRenderer);
return view;
Any input will be highly appreciated.
You can add annotation that can be displayed wherever you need on the graph:
series.addAnnotation(text, x, y);

Android AChartEngine graphicalView share events

First i give my View show below. and give some explanation:
In my ViewGroup i have two GraphicalView, each of them share the same size space. Let's just call the chart above chartA and below one chartB.
Then i have some questions:
If i make a move a movement on chartA ,how could i make the chartB move just as my finger moving on chartB?
If i pinch on chartA can also chartB change auto?
I want to add some callback function to some points? Does ACE supports this?
I do not want to show numbers negative while user pinching.How can I make this point?
Last is my chart code:
mDataset.addSeries(series);
PointStyle style = PointStyle.CIRCLE;
renderer = buildRenderer(lineColor, style, true);
setChartSettings(renderer, "X", "Y", 0, 50, yMin, yMax, Color.WHITE,
Color.WHITE, title,chartColor);
GraphicalView chart = ChartFactory.getLineChartView(context, mDataset, renderer);
layout.addView(chart, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
HashMap map = new HashMap();
and the renderer:
protected XYMultipleSeriesRenderer buildRenderer(int color,
PointStyle style,
boolean fill) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(color);
r.setPointStyle(style);
r.setFillPoints(fill);
r.setLineWidth(3);
renderer.addSeriesRenderer(r);
return renderer;
}
I random all the data i want.
You have to register a PanListener on your chartA and on every panApplied event, just do: rendererB.setXAxisMin(rendererA.getXAxisMin()); and the same for max X and for the Y axis and then call chartB.repaint();
Same as the above, just that you need to add a ZoomListener.
You can set a click listener: chartA.setOnClickListener();
Please reformulate into a separate question.
Also, see this code showing you how to use the above APIs.

Multiple Color bars for single series and single renderer

I am generating a bar chart graph for the Single series and single renderer. I need the bars to be generated with different colors according to my given data condition.
Following is my code for Renderer,
private XYMultipleSeriesRenderer getDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setBarSpacing(1.0);
renderer.setAxisTitleTextSize(12);
renderer.setChartTitleTextSize(12);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
renderer.setXLabels(0);
XYSeriesRenderer r = new XYSeriesRenderer();
SQLiteDatabase DB = Context
.openOrCreateDatabase("WaterElectricityReadingDataBase.db",
MODE_WORLD_READABLE, null);
Cursor c = DB
.rawQuery(
"select ReadingValue,ReadingMode, from WaterElectricity ",
null);
if (c.moveToFirst()) {
do {
String ReadingMode = c.getString(c
.getColumnIndex("ReadingMode"));
if (ReadingMode.contains("Water")){
r.setColor(Color.GREEN);
}else if(ReadingMode.contains("Electricity")){
r.setColor(Color.WHITE);
}
}while (c.moveToNext());
}
r.setPointStyle(PointStyle.DIAMOND);
r.setFillBelowLine(false);
r.setFillPoints(true);
renderer.addSeriesRenderer(r);
setChartSettings(renderer);
return renderer;
}
In the above code if the ReadingMode is "Water" I need the bar to generated in Green color and for the other, in white color. If I run the above code, I did observe that all bars are generated in green color. I am having records with ReadingMode as "Water" and "Electricity" in my database. But why is this happeing? Do I need to take multiple renderers but I am having single series?
Can any one suggest me the good solution? My head is about to break.
Any help will be appreciated!! Thanks in advance!!
Bar chart using achartengine. Check the accepted answer in the link.
OR
Use a Different series for each bar.
http://code.google.com/p/achartengine/issues/detail?id=76. Some one reported an issue regarding the same.

Bar chart not working in AchartEngine

I am trying to create Bar Chart using AchartEngine but various things doesn't work for me..
1) Unable to show Grid.
2) Unable to remove category series title i.e "Bar Graph".
3) It's not showing bars as it should show.
4) By Default white color background is visible.
is it possible to provide space between each bars??
Bar Chart Code
CategorySeries series = new CategorySeries("Bar Graph");
for (int i = 0; i < availCatList.size(); i++)
series.add(availCatList.get(i), mTotal.get(i));
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series.toXYSeries());
XYSeriesRenderer seriesRenderer = new XYSeriesRenderer();
seriesRenderer.setChartValuesSpacing(0.5f);
seriesRenderer.setDisplayChartValues(true);
seriesRenderer.setColor(Color.GREEN);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setChartTitle("Bar Chart");
mRenderer.setShowGrid(true);
mRenderer.setAxisTitleTextSize(15);
mRenderer.setXLabelsAlign(Align.CENTER);
mRenderer.setXTitle("Categories");
mRenderer.setYTitle("Amount");
mRenderer.setScale(1.0f);
for (int i = 0; i < availCatList.size(); i++) {
mRenderer.addXTextLabel(i + 1, availCatList.get(i));
}
mRenderer.setXLabels(0);
mRenderer.addSeriesRenderer(seriesRenderer);
view = ChartFactory.getBarChartView(this, dataset, mRenderer,
Type.DEFAULT);
// layout.removeAllViews();
layout.addView(view);
Any Help would be highly appreciated..
Thanks
mRenderer.setShowGrid(true) works fine in all the examples in the demo code. Try to set another color using mRenderer.setGridColor(color);
You mean the legend: mRenderer.setShowLegend(false);
series.add(mTotal.get(i)); is the correct usage for bar charts.
That's probably from the profile. Just set your own background color.
Space between bars: read the APIs for mRenderer.setBarSpacing();

Categories

Resources