d925ccd6b937a7b5ad37c1a6c977ce80355ff96c
[public/netxms.git] / android / src / console / src / org / netxms / ui / android / main / dashboards / elements / LineChartElement.java
1 /**
2 *
3 */
4 package org.netxms.ui.android.main.dashboards.elements;
5
6 import java.util.Date;
7 import java.util.concurrent.ScheduledExecutorService;
8
9 import org.netxms.client.datacollection.DciData;
10 import org.netxms.client.datacollection.DciDataRow;
11 import org.netxms.ui.android.helpers.Colors;
12 import org.netxms.ui.android.helpers.CustomLabel;
13 import org.netxms.ui.android.main.activities.helpers.ChartDciConfig;
14 import org.netxms.ui.android.main.dashboards.configs.LineChartConfig;
15 import org.netxms.ui.android.service.ClientConnectorService;
16
17 import android.content.Context;
18 import android.content.SharedPreferences;
19 import android.preference.PreferenceManager;
20 import android.util.Log;
21
22 import com.jjoe64.graphview.GraphView;
23 import com.jjoe64.graphview.GraphView.GraphViewData;
24 import com.jjoe64.graphview.GraphView.LegendAlign;
25 import com.jjoe64.graphview.GraphViewSeries;
26 import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
27 import com.jjoe64.graphview.LineGraphView;
28
29 /**
30 * Bar chart element
31 */
32 public class LineChartElement extends AbstractDashboardElement
33 {
34 private static final String TAG = "nxclient/LineChartElement";
35
36 private LineChartConfig config;
37 private GraphView graphView = null;
38 private final SharedPreferences sp;
39
40 /**
41 * @param context
42 * @param xmlConfig
43 */
44 public LineChartElement(Context context, String xmlConfig, ClientConnectorService service, ScheduledExecutorService scheduleTaskExecutor)
45 {
46 super(context, xmlConfig, service, scheduleTaskExecutor);
47 try
48 {
49 config = LineChartConfig.createFromXml(xmlConfig);
50 }
51 catch (Exception e)
52 {
53 Log.e(TAG, "Error parsing element config", e);
54 config = new LineChartConfig();
55 }
56
57 sp = PreferenceManager.getDefaultSharedPreferences(context);
58 graphView = new LineGraphView(context, config.getTitle());
59 graphView.getGraphViewStyle().setTextSize(Integer.parseInt(sp.getString("global.graph.textsize", "10")));
60 graphView.getGraphViewStyle().setLegendWidth(240);
61 graphView.setCustomLabelFormatter(new CustomLabel(Integer.parseInt(sp.getString("global.multipliers", "1"))));
62 // TODO: 2014May25 Find a best way to handle this setting
63 //graphView.setShowLegend(config.isShowLegend());
64 graphView.setShowLegend(sp.getBoolean("global.graph.legend", true));
65 graphView.setScalable(false);
66 graphView.setScrollable(false);
67 graphView.setLegendAlign(LegendAlign.TOP);
68 }
69
70 /* (non-Javadoc)
71 * @see android.view.View#onAttachedToWindow()
72 */
73 @Override
74 protected void onAttachedToWindow()
75 {
76 super.onAttachedToWindow();
77 startRefreshTask(config.getRefreshRate());
78 }
79
80 /* (non-Javadoc)
81 * @see org.netxms.ui.android.main.dashboards.elements.AbstractDashboardElement#refresh()
82 */
83 @Override
84 public void refresh()
85 {
86 final ChartDciConfig[] items = config.getDciList();
87 Log.v(TAG, "refresh(): " + items.length + " items to load");
88 if (items.length == 0)
89 return;
90
91 final long endTime = System.currentTimeMillis();
92 final long startTime = endTime - config.getTimeRangeMillis();
93
94 try
95 {
96 final DciData[] dciData = new DciData[items.length];
97 for (int i = 0; i < dciData.length; i++)
98 {
99 dciData[i] = service.getSession().getCollectedData(items[i].nodeId, items[i].dciId, new Date(startTime), new Date(endTime), 0);
100 }
101 Log.v(TAG, "refresh(): data retrieved from server");
102
103 post(new Runnable()
104 {
105 @Override
106 public void run()
107 {
108 for (int i = 0; i < dciData.length && i < Colors.DEFAULT_ITEM_COLORS.length; i++)
109 {
110 DciDataRow[] dciDataRow = dciData[i].getValues();
111 GraphViewData[] gvData = new GraphViewData[dciDataRow.length];
112 for (int j = dciDataRow.length - 1, k = 0; j >= 0; j--, k++)
113 // dciData are reversed!
114 gvData[k] = new GraphViewData(dciDataRow[j].getTimestamp().getTime(), dciDataRow[j].getValueAsDouble());
115 int color = items[i].getColorAsInt();
116 color = color == -1 ? Colors.DEFAULT_ITEM_COLORS[i] : swapRGB(color);
117 GraphViewSeries series = new GraphViewSeries(items[i].getName(), new GraphViewSeriesStyle(color | 0xFF000000, 3), gvData);
118 graphView.addSeries(series);
119 }
120 graphView.setViewPort(startTime, endTime - startTime + 1);
121 Log.v(TAG, "refresh(): " + dciData.length + " series added; viewport set to " + startTime + "/" + (endTime - startTime + 1));
122
123 if (getChildCount() == 0)
124 addView(graphView);
125 else
126 graphView.redrawAll();
127 }
128 });
129 }
130 catch (Exception e)
131 {
132 Log.e(TAG, "Exception while reading data from server", e);
133 }
134 }
135 }