Hi i am using achartengine's pie-chart to represent my actual sales, now as my app starts one of the chart element in the piechart should get highlighted, like when we do the onclick on pie chart.
Below is the code for my piechart
final DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setPanEnabled(false);// Disable User Interaction
renderer.setLabelsColor(Color.BLACK);
renderer.setShowLegend(true);
renderer.setLegendTextSize(20);
renderer.setInScroll(true);
renderer.setStartAngle(180);
renderer.setChartTitle("Sales By Market Segment- Month");
renderer.setLabelsTextSize(22);
final CategorySeries categorySeries = new CategorySeries("Sales");
categorySeries.add("Craft1", cursor.getInt(1));
categorySeries.add("product1", cursor.getInt(2) );
categorySeries.add("product2", cursor.getInt(3) );
categorySeries.add("product", cursor.getInt(4));
categorySeries.add("product1", cursor.getInt(5));
mChartView2=ChartFactory.getPieChartView(context, categorySeries,renderer);
parent.addView(mChartView2);
renderer.setClickEnabled(true);
mChartView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView2.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(getActivity(), "No chart element selected", Toast.LENGTH_SHORT)
.show();
}else {
for (int i = 0; i < categorySeries.getItemCount(); i++) {
renderer.getSeriesRendererAt(i).setHighlighted(i == seriesSelection.getPointIndex());
}
mChartView2.repaint();
Toast.makeText(
getActivity(),
"Chart data point index " + seriesSelection.getPointIndex() + " selected"
+ " point value=" + seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});
return ChartFactory.getPieChartIntent(context, categorySeries, renderer,null);
}
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
products are getting highlighted when clicking but I want to highlight one product, when app starts.
Found the solution:
mChartView2=ChartFactory.getPieChartView(context, categorySeries,renderer);
parent.addView(mChartView2);
renderer.getSeriesRendererAt(0).setHighlighted(true);
mChartView2.repaint();
Related
I want to click each slices in the chart and then I have to show a popup based on clicking in each slices. I have implemented onclicklistener, but it is not working.
Code :
Clicking part:
gv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = gv.getCurrentSeriesAndPoint();
if (seriesSelection != null) {
int seriesIndex = seriesSelection.getSeriesIndex();
String selectedSeries = "Income";
if (seriesIndex == 0)
selectedSeries = "Income";
else
selectedSeries = "Expense";
int amount = (int) seriesSelection.getValue();
Toast.makeText(
getActivity(),
selectedSeries + " in " + "" + " : " + amount,
Toast.LENGTH_SHORT).show();
}
}
});
Rendering part:
public GraphicalView createIntent(List<double[]> values1, int[] colors, List<String[]> titles) {
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setApplyBackgroundColor(false);
renderer.setShowLegend(false);
renderer.setShowLabels(false);
renderer.setStartAngle(chartStartAngle); // 0 is 90 degree
renderer.setLabelsColor(Color.BLACK);
//renderer.setBackgroundColor(ContextCompat.getColor(get));
renderer.setScale((float) 1.40);
renderer.setPanEnabled(false);
renderer.setZoomEnabled(false);
renderer.setClickEnabled(true);//
renderer.setSelectableBuffer(100);
return ChartFactory.getDoughnutChartView(getActivity().getApplicationContext(),
buildMultipleCategoryDataset("Project", titles, values1),
renderer);
}
We are using AChart engine to draw bar graph. As per design requirement have to place an image on top of the tallest bar in the graph. We know the index of largest value in the XY series. Is there any method to find the coordinates of the tallest bar to position the image? Below is the helper class we are using to generate the bar graph. When user taps on the bar we have to place an image on top of the bar.
Issues :
Not able to get the click event correctly
How to position the image on top of the bar
public class BarGraphHelper {
private Context mContext;
private BarGraph mBarGraph;
private DisplayMetrics mMetrics;
private XYMultipleSeriesDataset mDataset;
private XYMultipleSeriesRenderer renderer;
private View barChartView = null;
private BarChart chart;
public BarGraphHelper(Context context, BarGraph barGraph){
mContext = context;
mBarGraph = barGraph;
init();
}
private void init() {
renderer = new XYMultipleSeriesRenderer();
mDataset = new XYMultipleSeriesDataset();
mMetrics = mContext.getResources().getDisplayMetrics();
}
public View getBarGraphView(){
if(renderer == null){
return null;
}
getBarGraphDataset();
renderer = getBarRenderer();
setBarGraphSettings();
chart = new BarChart(mDataset, renderer, Type.DEFAULT);
barChartView = new GraphicalView(mContext, chart);
barChartView.setOnClickListener(onBarClick);
return barChartView;
}
private void setBarGraphSettings() {
if(renderer != null){
renderer.setXAxisMin(0.5);
renderer.setYAxisMin(0);
renderer.setYAxisMax(110);
renderer.setXLabelsColor(Color.WHITE);
renderer.setYLabelsColor(0, Color.WHITE);
renderer.setXTitle("mins");
renderer.setYTitle("Tweets");
renderer.setShowAxes(true);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setBarSpacing(0.2);
renderer.setBarWidth(15);
renderer.setShowGrid(false);
renderer.setYLabels(0);
renderer.setXLabels(0); // sets the number of integer labels to appear
int margin[] = renderer.getMargins();
renderer.setMargins(new int[] { (int)convertDiptoPixel(margin[0]), (int)convertDiptoPixel(margin[1]), (int)convertDiptoPixel(margin[2]), (int)convertDiptoPixel(margin[3]) });
renderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
renderer.setLabelsColor(Color.WHITE);
renderer.setAxisTitleTextSize(convertDiptoPixel(renderer.getAxisTitleTextSize()));
renderer.setShowLegend(false);
renderer.setLabelsTextSize(convertDiptoPixel(renderer.getLabelsTextSize()));
renderer.setZoomButtonsVisible(false);
renderer.setPanEnabled(true, true);
renderer.setZoomEnabled(false, false);
renderer.setClickEnabled(true);
renderer.setSelectableBuffer(10);
}
}
private void getBarGraphDataset() {
if(mBarGraph != null){
CategorySeries positiveSeries = new CategorySeries("Positive");
ArrayList<Sentiment> sentimentList = mBarGraph.getSentimentList();
if(sentimentList != null && !sentimentList.isEmpty()){
Sentiment sentiment = null;
for(int i=0; i< sentimentList.size(); i++){
sentiment = sentimentList.get(i);
positiveSeries.add(sentiment.getTotalTweetCount());
}
mDataset.addSeries(positiveSeries.toXYSeries());
}
}
}
public XYMultipleSeriesRenderer getBarRenderer() {
XYSeriesRenderer seriesRenderer = new XYSeriesRenderer();
seriesRenderer.setColor(Color.parseColor("#5FB356"));
seriesRenderer.setDisplayChartValues(true);
renderer.addSeriesRenderer(seriesRenderer);
return renderer;
}
private float convertDiptoPixel(float pixel){
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, pixel, mMetrics);
}
OnClickListener onBarClick = new OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = ((GraphicalView) barChartView).getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(mContext, "No chart element was clicked", Toast.LENGTH_SHORT)
.show();
} else {
double[] screenPoint = chart.toScreenPoint(new double[] {
seriesSelection.getXValue(),seriesSelection.getValue() },0);
Toast toast = Toast.makeText(
mContext,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
+ " clicked point value X=" + (int)screenPoint[0] + ", Y=" + (int)screenPoint[1], Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, (int)screenPoint[0],(int)screenPoint[1]);
toast.show();
}
}
};
}
I am creating Stacked bar chart using Achartengine library. The values doesn't show properly. I want want to show the chart value like this {20,20,20} at top of the bar and align to center.
Here is my code
dataset.addSeries(JumlahSeries);
dataset.addSeries(JumlahSeries1);
dataset.addSeries(FirstSeries);
final XYSeriesRenderer FirstRenderer = new XYSeriesRenderer();
Log.d("Error Here","29");
FirstRenderer.setColor(Color.BLUE);
FirstRenderer.setFillPoints(false);
FirstRenderer.setLineWidth(5);
FirstRenderer.setChartValuesTextSize(15);
FirstRenderer.setDisplayChartValues(false);
// Creating XYSeriesRenderer to customize expenseSeries
Log.d("Error Here","30");
final XYSeriesRenderer SecondRenderer = new XYSeriesRenderer();
SecondRenderer.setColor(Color.RED);
SecondRenderer.setFillPoints(false);
SecondRenderer.setLineWidth(5);
SecondRenderer.setDisplayChartValues(false);
SecondRenderer.setChartValuesTextSize(15);
Log.d("Error Here","31");
final XYSeriesRenderer ThirdRenderer = new XYSeriesRenderer();
ThirdRenderer.setFillPoints(false);
ThirdRenderer.setColor(Color.GREEN);
ThirdRenderer.setLineWidth(5);
ThirdRenderer.setDisplayChartValues(false);
ThirdRenderer.setChartValuesTextSize(15);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
final XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
//zoomIn = new Zoom((GraphicalView) mChart, true, multiRenderer.getZoomRate());
//multiRenderer.setChartTitle("\n"+"\n"+ title);
multiRenderer.setChartTitleTextSize(20);
//multiRenderer.setXTitle("\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"\n"+"Laporan Pada :"+ DateToDisplay);
multiRenderer.setShowGridX(false);
multiRenderer.setShowLegend(true);
multiRenderer.setShowLabels(true);
multiRenderer.setAxisTitleTextSize(15);
multiRenderer.setMargins(new int[] { 70,70,25,25});
multiRenderer.setYTitle("Bilangan\n\n"+"\n"+"\n"+"\n");
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setBarSpacing(spacing);
multiRenderer.setYAxisMax(max);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setXLabels(0);
multiRenderer.setPanEnabled(true, false);
double Xmax = 2;
if(StacktotalX.getTotal().size()<5){
Log.d("Error Here","32");
Xmax=StacktotalX.getTotal().size()*1.3;
}
multiRenderer.setXAxisMax(Xmax) ;
multiRenderer.setXAxisMin(-1) ;
multiRenderer.setLabelsTextSize(13);
multiRenderer.setZoomEnabled(true,false);
multiRenderer.setClickEnabled(true);
for (int i = 0; i < StacktotalX.getTotal().size(); i++)
{
Log.d("Error Here","33");
multiRenderer.addXTextLabel(i,StacktotalX.getTotal().get(i).toString());
Log.d("texl",StacktotalX.getTotal().get(i).toString());
}
multiRenderer.addSeriesRenderer(ThirdRenderer);
multiRenderer.addSeriesRenderer(SecondRenderer);
multiRenderer.addSeriesRenderer(FirstRenderer);
Log.d("Error Here","34");
mChart = ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, Pattern);
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.TRANSPARENT);
multiRenderer.setMarginsColor(Color.TRANSPARENT);
multiRenderer.setLabelsColor(Color.BLACK);
mChart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();
if (seriesSelection != null) {
int seriesIndex = seriesSelection.getSeriesIndex();
String selectedSeries="Bilangan";
Log.d("seriesIndex",String.valueOf(seriesIndex));
// String yobject = [(int)seriesSelection.getXValue()-1];
// Getting the clicked Month
//String month = x[(int)seriesSelection.getXValue()-1];
// Getting the y value
int amount = (int) seriesSelection.getValue();
Log.d("amout",String.valueOf(amount));
Toast.makeText(
getBaseContext(),
selectedSeries + " in " + " : " + amount ,
Toast.LENGTH_SHORT).show();
}
}
});
// Start Activity*/
chartContainer.addView(mChart);
new Thread(new Runnable() {
public void run() {
long time = 0;
time = 9000/max;
Log.d("Error Here","35");
if(time>1.5){
time = (long) 1.5;
}if(time<0.5){
time = (long) 0.5;
}
int animation = 1;
Log.d("Error Here","36");
while(true){
boolean stop = true;
for(int k=0; k< StacktotalX.getTotal().size();k++){
// for(int k=0;k<jum1.size();k++)
// {
// JumlahSeries.add(k,jum1.get(tims));
// tims++;
// }
if(animation<= jum3.get(k)){
Log.d("Error Here","37");
FirstSeries.add(k,animation);
stop=false;
}
if(animation<= jum.get(k)){
Log.d("Error Here","38");
JumlahSeries1.add(k,animation);
stop=false;
}
if(animation<= jum1.get(k)){
Log.d("Error Here","39");
JumlahSeries.add(k,animation);
stop=false;
}
}
try {
Log.d("Error Here","40");
Thread.sleep(time);
Log.d("Error Here","4");
mChart.repaint();
}catch (InterruptedException e) {
e.printStackTrace();
}
animation++;
if(stop){
FirstSeries.clear();
JumlahSeries1.clear();
JumlahSeries.clear();
for(int y=0;y<StacktotalX.getTotal().size();y++)
{
FirstSeries.add(y,jum3.get(y));
JumlahSeries1.add(y, jum.get(y));
JumlahSeries.add(y, jum1.get(y));
}
FirstRenderer.setChartValuesSpacing(10);
FirstRenderer.setChartValuesTextAlign(Align.LEFT);
FirstRenderer.setDisplayChartValues(true);
FirstRenderer.getChartValuesTextAlign();
SecondRenderer.setChartValuesSpacing(10);
SecondRenderer.setChartValuesTextAlign(Align.CENTER);
SecondRenderer.setDisplayChartValues(true);
SecondRenderer.getChartValuesTextAlign();
ThirdRenderer.setChartValuesSpacing(10);
ThirdRenderer.setChartValuesTextAlign(Align.RIGHT);
ThirdRenderer.setDisplayChartValues(true);
ThirdRenderer.getChartValuesTextAlign();
mChart.repaint();
break;
}
}
}
}).start();*
This is my image that now is showing:-
i am drawing a bar chart and pie chart to represent the various application names in it using the achartengine library.
When i instantiate CategorySeries object it requires a series title as :
CategorySeries series = new CategorySeries("title") //IN BAR CHART
CategorySeries series = new CategorySeries("title") //IN PIE CHART
Unlike Pie chart the applications name does not get inflated at the bottom of the bar chart
say, in case of pie chart its as follows :
(1) application 1
(2) application 2
(3) application ... up to
(10) application 10.
But its not showing in case of bar char but instead its showing as :
(1) NUMBER OF APPLICATIONS ---->>> (This is the series title i had
passed in as the parameter while
instantiating the object of CategorySeries)
PLEASE SUGGEST ME TO SHOW THE KEY OF BAR CHART ASWELL,I HAVE ATTACHED A CODE SNIPPET BELOW
if (c.getCount() > 0) {
if (! mPieChart) {
series = new CategorySeries("NUMBER OF APPLICATIONS");
getActivity().startManagingCursor(c);
c.moveToPosition(-1);
//series.clear();
while (c.moveToNext()) {
recievedBytes = c.getLong(ApplicationDataCounterDao.CONTENT_RECEIVED_COLUMN);
sentBytes = c.getLong(ApplicationDataCounterDao.CONTENT_SENT_COLUMN);
totalBytes = (recievedBytes + sentBytes) / (1024 * 1024);
if (totalBytes > 0) {
applicationInfo
.add(new ApplicationDataUsage(
c.getString(ApplicationDataCounterDao.CONTENT_APPLICATION_NAME_COLUMN),
Double.toString(totalBytes)));
}
}
int size = applicationInfo.size();
for (int i = 0; i < size; ++i) {
Log.v("BAR CHART", "data added are " + applicationInfo.get(i).mApplicationName);
double value = Double.parseDouble(applicationInfo
.get(i).mDataUsage);
series.add("BAR", value);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series.toXYSeries());
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setDisplayChartValues(true);
renderer.setChartValuesSpacing((float) 0.5);
renderer.setColor(Color.CYAN);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setBarSpacing(0.5);
mRenderer.getBarSpacing();
mRenderer.setXTitle("Application Number");
mRenderer.setYTitle("Data Usage");
mRenderer.setAxesColor(Color.GREEN);
mRenderer.setZoomButtonsVisible(true);
mRenderer.setInScroll(true);
mRenderer.setLabelsColor(Color.RED);
mRenderer.addSeriesRenderer(renderer);
if (mChartViewBar == null) {
Log.v(LOG_TAG, "chart view is null");
mChartViewBar = ChartFactory
.getBarChartView(getActivity(), dataset,
mRenderer, Type.DEFAULT);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
mChartViewBar
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartViewBar
.getCurrentSeriesAndPoint();
CategorySeries series22 =new CategorySeries("333");
mChartViewBar.repaint();
if (seriesSelection == null) {
Toast.makeText(
getActivity(),
"No chart element was clicked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
getActivity(),
"APPLICATION NAME : "
+ series22.getTitle()
+ 1
+ " was clicked"
+ " DATA USAGE : "
+ seriesSelection
.getXValue(),
Toast.LENGTH_SHORT).show();
}
}
});
mChartLayout.removeAllViews();
mChartLayout.addView(mChartViewBar, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
} else {
mChartLayout.removeAllViews();
mChartLayout.addView(mChartViewBar, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mChartViewBar.repaint();
}
} else if (mPieChart) {
CategorySeries seriesPie = new CategorySeries("Pie Graph");
getActivity().startManagingCursor(c);
c.moveToPosition(-1);
while (c.moveToNext()) {
long recievedBytes = c .getLong(ApplicationDataCounterDao.CONTENT_RECEIVED_COLUMN);
Long sentBytes = c.getLong(ApplicationDataCounterDao.CONTENT_SENT_COLUMN);
totalBytes = (recievedBytes + sentBytes)
/ (1024 * 1024);
if (totalBytes > 0) {
applicationInfo
.add(new ApplicationDataUsage(
c.getString(ApplicationDataCounterDao.CONTENT_APPLICATION_NAME_COLUMN),
Double.toString(totalBytes)));
}
}
int size = applicationInfo.size();
for (ApplicationDataUsage data_value : applicationInfo) {
Log.v("PIE CHART", "data added are " + data_value.mApplicationName);
double dataUsage = Double
.parseDouble(data_value.mDataUsage);
seriesPie.add(data_value.mApplicationName, dataUsage);
}
for (int i = 0; i < size; ++i) {
prepareColors();
}
DefaultRenderer renderer = new DefaultRenderer();
for (int fillColor : colorList) {
// Log.d("1111111111",
// "FILL COLOUR TILL SIZE OF COLORLIST");
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(fillColor);
renderer.addSeriesRenderer(r);
}
renderer.setInScroll(true);
renderer.setChartTitle("APPLICATION WISE PIE CHART");
renderer.setChartTitleTextSize(20);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.BLACK);
renderer.setZoomButtonsVisible(true);
if (mChartViewPie == null) {
Log.v(LOG_TAG, "chat view is null");
// series.clear();
mChartViewPie = ChartFactory.getPieChartView(
getActivity(), seriesPie, renderer);
renderer.setClickEnabled(true);
renderer.setSelectableBuffer(10);
mChartViewPie
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartViewPie
.getCurrentSeriesAndPoint();
mChartViewPie.repaint();
if (seriesSelection == null) {
ToastHelper.showToastMessage(
"No item was Clicked",
getActivity());
} else {
ToastHelper.showToastMessage(
"Application number "
+ seriesSelection
.getPointIndex()
+ " was clicked and it's Data Usage is "
+ seriesSelection
.getValue(),
getActivity());
}
}
});
/*if (mChartViewBar != null) {
mChartLayout.removeView(mChartViewBar);
}
if (mChartViewPie != null) {
mChartLayout.removeView(mChartViewPie);
}*/
mChartLayout.addView(mChartViewPie, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
} else {
/* if (mChartViewBar != null) {
mChartLayout.removeView(mChartViewBar);
}
if (mChartViewPie != null) {
mChartLayout.removeView(mChartViewPie);
}*/
mChartLayout.removeAllViews();
mChartLayout.addView(mChartViewPie, new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mChartViewPie.repaint();
}
}
You are confusing the legend with the X axis labels. The BarChart is an XYChart which means it is getting both legend and x axis labels. The PieChart is not an XYChart, so it is not getting X axis and Y axis.
The series titles go to the legend, as you mentioned.
In the X axis the series values indexes go by default. However, you can hide the default labels and add your own custom ones:
renderer.setXLabels(0);
renderer.addXTextLabel(1, "label);
renderer.addXTextLabel(2, "label);
...
I'm using AChartEngine to display averages across several days (2 months if possible). I'd like to provide a comparison between one individual and a group. At this point, I'm working on how to provide dates across the x-axis (ie, 12/15/2011 or Dec. 11, 2011 or even 12/15, Dec. 15).
I'm not sure how to do this in AchartEngine; the documentation is a little hard for me to follow without pictures (sorry). I'd like something like this: Using AChartEngine library for graphs, not able to get value for diffrent x-axis value
Here's my code:
private void setupHistoryChart() {
XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
setChartSettings(renderer);
Intent intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
startActivity(intent);
}
public XYMultipleSeriesRenderer getBarDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(15);
renderer.setChartTitleTextSize(18);
renderer.setLabelsTextSize(12);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] {20, 30, 15, 0});
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(Color.BLUE);
renderer.addSeriesRenderer(r);
return renderer;
}
private void setChartSettings(XYMultipleSeriesRenderer renderer) {
renderer.setChartTitle("History");
renderer.setChartTitleTextSize((float) 14.0);
renderer.setXTitle("Day");
renderer.setXLabelsAlign(Align.LEFT);
renderer.setYLabelsAlign(Align.LEFT);
renderer.setBarSpacing(0.5);
renderer.setZoomEnabled(true);
renderer.setZoomRate(0.2f);
renderer.setYTitle("Unit)");
renderer.setXAxisMin(1.0);
if(AppStatus.mDayMap.size() == 0){
renderer.setXAxisMax(3.0);
renderer.setYAxisMax(10.0);
}else{
renderer.setXAxisMax(AppStatus.mDayMap.size());
// Get the max Integer out of myMap
Map.Entry<String, Integer> maxEntry = null;
for (Map.Entry<String, Integer> entry : AppStatus.mDayMap.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}
renderer.setYAxisMax(maxEntry.getValue()/1000 + maxEntry.getValue()/1000);
}
renderer.setYAxisMin(0);
}
private XYMultipleSeriesDataset getBarDemoDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
HashMap<String, Integer> myMap = AppStatus.mDayMap;
CategorySeries series = new CategorySeries ("Your History " + 1);
try{
if(AppStatus.mDayMap.size() == 0){
series.add(3.0);
series.add(10.0);
dataset.addSeries(series.toXYSeries());
}else{
Log.v(LOG_TAG, "Size of Map = " + myMap.size());
float sum = (float) 0.0;
if(myMap.size() > 0){
Iterator it = myMap.entrySet().iterator();
float j= (float)0.0;
for (int i = 0; i < 1 /*2*/; i++) {
//CategorySeries series = new CategorySeries("Your History " + (i + 1));
while(it.hasNext()){
Map.Entry pairs = (Map.Entry)it.next();
sum = sum + (float) (1.0 + j);
series.add((((Integer) pairs.getValue()).doubleValue())/1000);
}
dataset.addSeries(series.toXYSeries());
}
}
}
}catch(IllegalArgumentException e){
Log.e(LOG_TAG, "IllegalArgumentException " + e.toString());
series.add(3.0);
series.add(10.0);
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
You can set custom text for the X axis labels:
renderer.addXTextLabel(x, text);
renderer.setXLabels(0);
In order to have multiple color bars in the bar chart, you will have to add multiple series.
There are some instructions here on how to download and run the demo application which includes a lot of good code to demonstrate the AChartEngine usage.