4 package org
.netxms
.ui
.android
.main
.dashboards
.elements
;
7 import java
.util
.concurrent
.ScheduledExecutorService
;
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
;
17 import android
.content
.Context
;
18 import android
.content
.SharedPreferences
;
19 import android
.preference
.PreferenceManager
;
20 import android
.util
.Log
;
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
;
32 public class LineChartElement
extends AbstractDashboardElement
34 private static final String TAG
= "nxclient/LineChartElement";
36 private LineChartConfig config
;
37 private GraphView graphView
= null;
38 private final SharedPreferences sp
;
44 public LineChartElement(Context context
, String xmlConfig
, ClientConnectorService service
, ScheduledExecutorService scheduleTaskExecutor
)
46 super(context
, xmlConfig
, service
, scheduleTaskExecutor
);
49 config
= LineChartConfig
.createFromXml(xmlConfig
);
53 Log
.e(TAG
, "Error parsing element config", e
);
54 config
= new LineChartConfig();
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
);
71 * @see android.view.View#onAttachedToWindow()
74 protected void onAttachedToWindow()
76 super.onAttachedToWindow();
77 startRefreshTask(config
.getRefreshRate());
81 * @see org.netxms.ui.android.main.dashboards.elements.AbstractDashboardElement#refresh()
86 final ChartDciConfig
[] items
= config
.getDciList();
87 Log
.v(TAG
, "refresh(): " + items
.length
+ " items to load");
88 if (items
.length
== 0)
91 final long endTime
= System
.currentTimeMillis();
92 final long startTime
= endTime
- config
.getTimeRangeMillis();
96 final DciData
[] dciData
= new DciData
[items
.length
];
97 for (int i
= 0; i
< dciData
.length
; i
++)
99 dciData
[i
] = service
.getSession().getCollectedData(items
[i
].nodeId
, items
[i
].dciId
, new Date(startTime
), new Date(endTime
), 0);
101 Log
.v(TAG
, "refresh(): data retrieved from server");
108 for (int i
= 0; i
< dciData
.length
&& i
< Colors
.DEFAULT_ITEM_COLORS
.length
; i
++)
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
);
120 graphView
.setViewPort(startTime
, endTime
- startTime
+ 1);
121 Log
.v(TAG
, "refresh(): " + dciData
.length
+ " series added; viewport set to " + startTime
+ "/" + (endTime
- startTime
+ 1));
123 if (getChildCount() == 0)
126 graphView
.redrawAll();
132 Log
.e(TAG
, "Exception while reading data from server", e
);