问题
I have created a bar chart using achartengine but the values plotted in the graph is static. Now i have created a database using sqlite and i want to draw the bar chart using the values from the database and refresh it every 1 min so that it plots new values if any. I am posting my code below. Please guide me on how to modify the code so that i can do the above. I tried many tutorials online but it didnt help much.
package flu.solutions.travelsense;
import java.util.ArrayList;
import java.util.List;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
public class ChartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = createIntent();
startActivity(intent);
}
public Intent createIntent()
{
String[] titles = new String[] { "Top 10 Destinations", " " };
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 8, 6, 7, 6, 7, 7, 9, 5, 7, 8, 8 });
values.add(new double[] {});
int[] colors = new int[] { Color.RED, Color.BLACK};
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.HORIZONTAL);
setChartSettings(renderer, "Top 10 Destinations", " ", " ", 0,10, 0,10, Color.GRAY, Color.GREEN);
renderer.setXLabels(1);
renderer.setYLabels(10);
renderer.addXTextLabel(1, "Bangalore");
renderer.addXTextLabel(2, "Mysore");
renderer.addXTextLabel(3, "Chennai");
renderer.addXTextLabel(4, "Delhi");
renderer.addXTextLabel(5, "Kolkatta");
renderer.addXTextLabel(6, "Kashmir");
renderer.addXTextLabel(7, "Hyderabad");
renderer.addXTextLabel(8, "Mumbai");
renderer.addXTextLabel(9, "Kerala");
renderer.addXTextLabel(10, "Gujarat");
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++)
{
SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(false);
}
return ChartFactory.getBarChartIntent(this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors)
{
// creates a SeriesRenderer and initializes it with useful default values as well as colors
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(26);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(25);
int length = colors.length;
for (int i = 0; i < length; i++)
{
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
//r.setChartvalueAngle(-90);
r.setChartValuesSpacing(15);
renderer.addSeriesRenderer(r);
}
return renderer;
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,int labelsColor)
{
// sets lots of default values for this renderer
renderer.setChartTitle(title);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.WHITE);
renderer.setBarSpacing(1);
}
protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values)
{
// adds the axis titles and values into the dataset
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++)
{
CategorySeries series = new CategorySeries(titles[i]);
double[] v = values.get(i);
int seriesLength = v.length;
for (int k = 0; k < seriesLength; k++)
{
series.add(v[k]);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_chart, menu);
return true;
}
}
回答1:
You could use the following: achartengine, AndroidPlot or charts4j. If you want to do it programmatically, then here is a program:
public class BarchartsampleActivity extends Activity {
/** Called when the activity is first created. */
static XYMultipleSeriesDataset dataset;
static XYMultipleSeriesRenderer renderer;
static XYSeriesRenderer rendererSeries;
static CategorySeries series;
static GraphicalView mChartView;
String xvalstring1,xvalstring2,xvalstring3,xvalstring4,xvalstring5;
int xvalint1,xvalint2,xvalint3,xvalint4,xvalint5;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout);
Intent intent=getIntent();
xvalstring1=intent.getStringExtra("xvalstring1").toString().trim();
xvalstring2=intent.getStringExtra("xvalstring2").toString().trim();
xvalstring3=intent.getStringExtra("xvalstring3").toString().trim();
xvalstring4=intent.getStringExtra("xvalstring4").toString().trim();
xvalstring5=intent.getStringExtra("xvalstring5").toString().trim();
xvalint1=Integer.parseInt(xvalstring1);
xvalint2=Integer.parseInt(xvalstring2);
xvalint3=Integer.parseInt(xvalstring3);
xvalint4=Integer.parseInt(xvalstring4);
xvalint5=Integer.parseInt(xvalstring5);
String[] titles = new String[] { "USER INPUTS" };
List<double[]> x = new ArrayList<double[]>();
x.add(new double[] { 1, 2, 3, 4, 5});
List<double[]> values = new ArrayList<double[]>();
values
.add(new double[] { xvalint1,xvalint2,xvalint3,xvalint4,xvalint5 });
dataset = new XYMultipleSeriesDataset();
renderer = new XYMultipleSeriesRenderer();
renderer.setChartTitle("USER INPUTS");
renderer.setXTitle("X");
renderer.setYTitle("Y");
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setXAxisMin(0);
renderer.setXAxisMax(10);
renderer.setYAxisMin(0);
renderer.setYAxisMax(40);
renderer.setClickEnabled(false);
renderer.setExternalZoomEnabled(false);
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { 0,5,0,100 });
renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
renderer.setShowGrid(true);
/*renderer.setPanEnabled(false, false);
renderer.setZoomEnabled(false, false);*/
renderer.setBarSpacing(1.5);
renderer.setLabelsColor(Color.BLUE);
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] {20, 30, 15, 20});
renderer.setBackgroundColor(Color.GREEN);
renderer.setMarginsColor(Color.GREEN);
rendererSeries = new XYSeriesRenderer();
rendererSeries.setColor(Color.GREEN);
rendererSeries.setDisplayChartValues(true);
renderer.addSeriesRenderer(rendererSeries);
if (mChartView == null) {
Log.d("Oncreate ", "if (mChartView == null)");
mChartView = ChartFactory.getBarChartView(this, mDataset(titles,
x, values), renderer, Type.DEFAULT);
layout.addView(mChartView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
} else {
// mChartView.repaint();
Log.d("Oncreate ", "if (mChartView != null)");
}
}
private XYMultipleSeriesDataset mDataset(String[] titles,
List<double[]> xValues, List<double[]> yValues) {
// TODO Auto-generated method stub
XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
addXYSeries(dataset1, titles, xValues, yValues, 0);
return dataset1;
}
private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
List<double[]> xValues, List<double[]> yValues, int scale) {
// TODO Auto-generated method stub
int length = titles.length;
for (int i = 0; i < length; i++) {
XYSeries series = new XYSeries(titles[i], scale);
double[] xV = xValues.get(i);
double[] yV = yValues.get(i);
int seriesLength = xV.length;
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
}
}
来源:https://stackoverflow.com/questions/13795620/how-to-plot-a-bar-chart-in-android