Reputation: 531
Hello i have the following class to show the graph , as it can be seen in the code i am using some random value for it , how can we refresh this activity in every second to get the moving graphs. Thanks
public class SalesGrowthChart extends AbstractDemoChart {
public String getName() {
return "Sales growth";
}
public String getDesc() {
return "The sales growth across several years (time chart)";
}
public Intent execute(Context context) {
String[] titles = new String[] { "Sales growth January 1995 to December 2000" };
List<Date[]> dates = new ArrayList<Date[]>();
List<double[]> values = new ArrayList<double[]>();
Date[] dateValues = new Date[] { new Date(95, 0, 1), new Date(95, 3, 1), new Date(95, 6, 1),
new Date(95, 9, 1), new Date(96, 0, 1), new Date(96, 3, 1), new Date(96, 6, 1),
new Date(96, 9, 1), new Date(97, 0, 1), new Date(97, 3, 1), new Date(97, 6, 1),
new Date(97, 9, 1), new Date(98, 0, 1), new Date(98, 3, 1), new Date(98, 6, 1),
new Date(98, 9, 1), new Date(99, 0, 1), new Date(99, 3, 1), new Date(99, 6, 1),
new Date(99, 9, 1), new Date(100, 0, 1), new Date(100, 3, 1), new Date(100, 6, 1),
new Date(100, 9, 1), new Date(100, 11, 1) };
dates.add(dateValues);
values.add(new double[] { (int) (Math.random() * 4), (int) (Math.random() * 4),(int) (Math.random() * 4), (int) (Math.random() * 4), (int) (Math.random() * 4), (int) (Math.random() * 4) });
int[] colors = new int[] { Color.BLUE };
PointStyle[] styles = new PointStyle[] { PointStyle.POINT };
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
setChartSettings(renderer, "Sales growth", "Date", "%", dateValues[0].getTime(),
dateValues[dateValues.length - 1].getTime(), -4, 11, Color.GRAY, Color.LTGRAY);
renderer.setYLabels(10);
return ChartFactory.getTimeChartIntent(context, buildDateDataset(titles, dates, values),
renderer, "MMM yyyy");
}
}
Upvotes: 0
Views: 243
Reputation: 8615
You're going to have to create a new thread in your main activity like so:
public class myActivity extends Activity {
boolean graphActive = true;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
new Thread(new Runnable() {
public void run() { // Process to keep running
while (graphActive) {
SalesGrowthChart.execute();
try {
Thread.sleep(1000) // sleeps for 1 second
} catch (Error e) {
}
}
}}).start();
}
}
You can kill the thread anywhere by setting graphActive = false Hope this helps. :)
Upvotes: 1