I am asking this question because, this is quiet no clear to me why my graph does not get update each time the user select to parse data for 1day or 1week or 1month basis. A similar How to update/remove/insert new series/data in Shinobi Line Chart in android?
I attempted the answer provided by a member called Kai, apparently he works for shinobicontrols.
You may also also note that I have implemented the shinobichart library inside a GraphFragment that seats in a view pager, which imports android.android.support.v4.app.Fragment;
ViewPagerAdapter class imports import android.support.v4.app.FragmentPagerAdapter; and the fragment transaction that calls the GraphFragment class budles an array of graph data from another activity that parses the JSON graphdata. I am trying to make this question clear so that when you read my code atleast you get an idea that the problem is not the JSON data because is pulled accordingly based on 1week, 1day or 1month. The issues is that Shinobichart does remove the series and its data but does not plot new parsed data. I read shinobichart user-guide how to handle chart-life cycle but was unable to find the solution I want. I also read ChartFragment handle onPause and onResume for the developer and I wonder If the same applies to SupportChartFragment.
Here is my GraphFragment that integrates shinobichart.Hope someone can help. Thank you in advance.
public class GraphFragment extends Fragment implements OnClickListener,
ShinobiChart.OnCrosshairActivationStateChangedListener{
private static final int CROSSHAIR_INACTIVE_COLOR = Color.argb(255, 240, 240, 240);
private static final int CROSSHAIR_ACTIVE_COLOR = Color.argb(150, 0, 0, 0);
Context context;
String label_x[];
ArrayList<DataAssetGraph> alAssetGraph = new ArrayList<DataAssetGraph>();
Button btnOneDayG, btnOneWeekG, btnOneMonthG;
String endDate;
String assetId;
ProgressDialog dialog;
ShinobiChart shinobiChart;
LineSeries series;
SupportChartFragment chartFragment;
String startDate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = null;
view = inflater.inflate(R.layout.fragment_chart, null);
initView(view);
if (getArguments() != null) {
alAssetGraph = (ArrayList<DataAssetGraph>) getArguments()
.getSerializable(WebElement.RECORDS);
if (alAssetGraph != null && alAssetGraph.size() > 0) {
// DrawGraph(alAssetGraph);
// Only setup the chart the first time the activity is created
if (savedInstanceState == null) {
// Log.d("Init Graph", "Retrieve"+ alAssetGraph);
chartFragment =
(SupportChartFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.chart);
shinobiChart = chartFragment.getShinobiChart();
// TODO: replace <license_key_here> with you trial license key
shinobiChart.setLicenseKey("sCVfKPnWajLtffqMjAxNTA0MThzdGVybmx5QHJpZ2h0Y2xpY2t" +
"tZWRpYS5jby56YQ==rveipQf9y4819/K4wLwWKR86Q1RIViUBTLEhBXAwh6q5zW53TgYi" +
"JcIUvc3S7DhTfH4KzUNeol9Rc5rXrzLOBnzP0TStc8n+eytCBhUFEgR21Cv7gq1dLEvOu" +
"tLENUwUtZ6Crk+Z8syIKEuyfZ8/1gtPvHIc=BQxSUisl3BaWf/7myRmmlIjRnMU2cA7q+" +
"/03ZX9wdj30RzapYANf51ee3Pi8m2rVW6aD7t6Hi4Qy5vv9xpaQYXF5T7XzsafhzS3hbBo" +
"kp36BoJZg8IrceBj742nQajYyV7trx5GIw9jy/V6r0bvctKYwTim7Kzq+YPWGMtqtQoU=" +
"PFJTQUtleVZhbHVlPjxNb2R1bHVzPnh6YlRrc2dYWWJvQUh5VGR6dkNzQXUrUVAxQnM5b2" +
"VrZUxxZVdacnRFbUx3OHZlWStBK3pteXg4NGpJbFkzT2hGdlNYbHZDSjlKVGZQTTF4S2Zwe" +
"WZBVXBGeXgxRnVBMThOcDNETUxXR1JJbTJ6WXA3a1YyMEdYZGU3RnJyTHZjdGhIbW1BZ21PTT" +
"dwMFBsNWlSKzNVMDg5M1N4b2hCZlJ5RHdEeE9vdDNlMD08L01vZHVsdXM+PEV4cG9uZW50Pk" +
"FRQUI8L0V4cG9uZW50PjwvUlNBS2V5VmFsdWU+");
// Create the series
createLineSeries(alAssetGraph);
// Add this Activity as a listener for any crosshair changes
shinobiChart.setOnCrosshairActivationStateChangedListener(this);
}
} else {
}
}
return view;
}
private void initView(View view)
{
btnOneDayG=(Button)view.findViewById(R.id.btnOneDayG);
btnOneWeekG=(Button)view.findViewById(R.id.btnOneWeekG);
btnOneMonthG=(Button)view.findViewById(R.id.btnOneMonthG);
btnOneDayG.setSelected(true);
btnOneDayG.setOnClickListener(this);
btnOneMonthG.setOnClickListener(this);
btnOneWeekG.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnOneDayG:
btnOneWeekG.setSelected(false);
btnOneMonthG.setSelected(false);
if(!btnOneDayG.isSelected())
{
btnOneDayG.setSelected(true);
startDate=GlobalData.getDateBeforeOneDay();
getGraphHistory(startDate);
System.out.println("Btn Date 1 day: %tc"+startDate);
}
break;
case R.id.btnOneWeekG:
btnOneDayG.setSelected(false);
btnOneMonthG.setSelected(false);
if(!btnOneWeekG.isSelected())
{
btnOneWeekG.setSelected(true);
startDate=GlobalData.getDateBeforeOneWeek();
getGraphHistory(startDate);
System.out.println("Btn Date 1 week: %tc"+startDate);
}
break;
case R.id.btnOneMonthG:
btnOneWeekG.setSelected(false);
btnOneDayG.setSelected(false);
if(!btnOneMonthG.isSelected())
{
btnOneMonthG.setSelected(true);
startDate=GlobalData.getDateBeforeOneMonth();
getGraphHistory(startDate);
System.out.println("Btn Date 1 Month: %tc"+startDate);
}
break;
default:
break;
}
}
private void createLineSeries(ArrayList<DataAssetGraph> alGraph) {
// TODO Auto-generated method stub
shinobiChart.getSeries();
// remove Series
while (shinobiChart.getSeries().size() > 0) {
shinobiChart.removeSeries(shinobiChart.getSeries().get(0));
}
// remove Axis
while (shinobiChart.getAllXAxes().size() > 0) {
shinobiChart.removeXAxis(shinobiChart.getAllXAxes().get(0));
}
while (shinobiChart.getAllYAxes().size() > 0) {
shinobiChart.removeYAxis(shinobiChart.getAllYAxes().get(0));
}
// Create the X-axis, showing ticks daily with a custom format and
// clipping the tick at the far right
DateTimeAxis xAxis = new DateTimeAxis();
// xAxis.setTitle("Date/Time");
xAxis.enableGesturePanning(true);
xAxis.enableGestureZooming(true);
xAxis.getDoubleTapBehavior();
// Create the Y-axis, clipping the tick at the top
NumberAxis yAxis = new NumberAxis();
// yAxis.setTitle("Temperature");
yAxis.enableGesturePanning(true);
yAxis.enableGestureZooming(true);
// Declare length of graph array
int length=alGraph.size();
LineSeries series = new LineSeries();
series.getStyle().getPointStyle().setPointsShown(false);
DataAdapter<Date, Double> data = new SimpleDataAdapter<Date, Double>();
for(int i=0;i<length;i++)
{
String dateString=alGraph.get(i).x_cord;
double y_cord= alGraph.get(i).y_cord;
Date x_cord=convertToDate(dateString);
data.add(new DataPoint<Date, Double>(x_cord, y_cord));
}
// reload and redraw the graph
series.setDataAdapter(data);
series.setCrosshairEnabled(true);
shinobiChart.addSeries(series, xAxis, yAxis);
series.getStyle().setLineColor(Color.WHITE);
System.out.println("Add Series");
// Style the chart and the crosshair
shinobiChart.getStyle().setPlotAreaBackgroundColor(
GraphFragment.CROSSHAIR_ACTIVE_COLOR);
shinobiChart.getCrosshair().getStyle().setLineColor(Color.BLUE);
shinobiChart.getStyle().setBackgroundColor(Color.WHITE);
// shinobiChart.getStyle().setPlotAreaBackgroundColor(Color.BLACK);
shinobiChart.getStyle().getBackgroundColor();
shinobiChart.getXAxis().getStyle().getGridlineStyle().setGridlinesShown(true);
shinobiChart.getYAxis().getStyle().getGridlineStyle().setGridlinesShown(true);
// Remember to redraw the chart to make the changes visible
shinobiChart.redrawChart();
}
private Date convertToDate(String dateString)
{
Date convertedDate= new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try
{
convertedDate = dateFormat.parse(dateString);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return convertedDate;
}
private void getGraphHistory(String start_date)
{
System.out.println("Get graph History: %tc"+ start_date);
dialog= new ProgressDialog(getActivity());
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Retrieving graph...");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
endDate=GlobalData.getCurrentDate();
assetId=ComponentActivity.assetId;
new LoadAssetGraphTask(getActivity(),assetId, start_date,endDate)
{
@Override
protected void onPostExecute(ArrayList<DataAssetGraph> result)
{
super.onPostExecute(result);
if(dialog.isShowing())
dialog.dismiss();
if(result!=null && result.size()>0)
{
createLineSeries(result);
System.out.println("onPostExecute Called");
}
};
}.execute();
//}
}
@Override
public void onCrosshairActivationStateChanged(ShinobiChart chart) {
// Set the plot area background color depending on the crosshair's
// activation state
if (chart.getCrosshair().isActive()) {
chart.getStyle().setPlotAreaBackgroundColor(GraphFragment.CROSSHAIR_ACTIVE_COLOR);
chart.getLegend().getStyle().setTextColor(Color.WHITE);
}
else {
chart.getStyle().setPlotAreaBackgroundColor(GraphFragment.CROSSHAIR_INACTIVE_COLOR);
chart.getLegend().getStyle().setTextColor(Color.BLACK);
}
// Remember to redraw the chart to make the color change visible
chart.redrawChart();
}