我创建了一个图表,其中在timeChart中显示了篡改。我需要用不同的颜色标记一些温度,因此我使用了XYCombinedChart并在要标记的温度上添加了ScatterChar。一切都很好。唯一的问题是,为了使其工作,我在ScatterCharts的XYSeries中使用了毫秒,如下所示:
XYSeries pointSeries = new XYSeries("NotFromSensor");
dataset.addSeries(pointSeries);
int t = 0;
for (t = 0; t < MainActivity.temps.size(); t++){
pointSeries.add(dt[t].getTime(), MainActivity.temps.get(t));
}
创建图形的整个代码如下所示:
public class DeviceGraphActivity extends Activity {
private XYMultipleSeriesDataset dataset, pointDataset ;
private XYMultipleSeriesRenderer mRenderer;
private XYSeriesRenderer renderer1, pointRenderer;
public GraphicalView getDeviceView(Context context, String title){
dataset = new XYMultipleSeriesDataset();
clearDataset();// a set with all the data that will be displayed on the graph
mRenderer = new XYMultipleSeriesRenderer();//determines the variables of the overall graph
int i;
System.out.println(ShowGraphActivity.flag);
if(DeviceFilterActivity.checkedDevices.isEmpty()){
if(ShowGraphActivity.flag == true){
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE Time BETWEEN " +
"'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
"AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
setData(false);
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0 AND Time BETWEEN " +
"'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
"AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
setData(true);
}
else{
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures");
setData(false);
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0");
setData(true);
}
renderer1 = new XYSeriesRenderer();
renderer1.setLineWidth(3);//sets graph line's width
renderer1.setChartValuesTextSize(20);//sets the size of the text on the char
renderer1.setColor(Color.BLUE);//sets the color of the graph
renderer1.setDisplayBoundingPoints(true);
mRenderer.addSeriesRenderer(renderer1);//adds the renderer(1 graph) to the overall graph
pointRenderer = new XYSeriesRenderer();
pointRenderer.setLineWidth(3);//sets graph line's width
pointRenderer.setChartValuesTextSize(20);//sets the size of the text on the char
pointRenderer.setPointStyle(PointStyle.CIRCLE);
pointRenderer.setColor(Color.RED);//sets the color of the graph
pointRenderer.setDisplayBoundingPoints(true);
mRenderer.addSeriesRenderer(pointRenderer);
}
else{
for(i = 0; i < DeviceFilterActivity.checkedDevices.size(); i++){
if(ShowGraphActivity.flag){
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId()+" AND Time BETWEEN " +
"'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
"AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
setData(i, false);
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0 AND DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId()+" AND Time BETWEEN " +
"'"+(MainActivity.yy1)+"-"+((MainActivity.mm1))+"-"+(MainActivity.dd1)+"' " +
"AND '"+(MainActivity.yy2)+"-"+((MainActivity.mm2))+"-"+((MainActivity.dd2))+"'");
setData(i,true);
}
else{
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId());
setData(i, false);
MainActivity.setXYValues("SELECT Temperature, Time FROM Temperatures WHERE FromSensor = 0 AND DeviceID = "+DeviceFilterActivity.checkedDevices.get(i).getId());
setData(i, true);
}
renderer1 = new XYSeriesRenderer();
renderer1.setLineWidth(3);//sets graph line's width
renderer1.setChartValuesTextSize(20);//sets the size of the text on the chart
renderer1.setColor(DeviceFilterActivity.checkedColors.get(i));//sets the color of the graph
renderer1.setDisplayBoundingPoints(true);
mRenderer.addSeriesRenderer(renderer1);//adds the renderer(1 graph) to the overall graph
pointRenderer = new XYSeriesRenderer();
pointRenderer.setLineWidth(3);//sets graph line's width
pointRenderer.setChartValuesTextSize(20);//sets the size of the text on the chart
pointRenderer.setColor(Color.RED);//sets the color of the graph
pointRenderer.setDisplayBoundingPoints(true);
mRenderer.addSeriesRenderer(pointRenderer);
}
}
mRenderer.setLabelsTextSize(20);
mRenderer.setPanEnabled(true);
mRenderer.setZoomEnabled(true);
mRenderer.setZoomButtonsVisible(true);
mRenderer.setYAxisMax(50);
mRenderer.setYAxisMin(0);
mRenderer.setShowGrid(true);
mRenderer.setMargins(new int[] { 50, 50, 25, 22 });
mRenderer.setLegendTextSize(25);
// we show the grid
mRenderer.setInScroll(true);
mRenderer.setChartTitle(title);
mRenderer.setChartTitleTextSize(30);
//------------------------
mRenderer.setClickEnabled(false);//the graph can't be clicked so that it can be refreshed as well as be zoomed in or out
mRenderer.setSelectableBuffer(10);
//---------------------------
String[] types = new String[]{ new String(TimeChart.TYPE), new String(ScatterChart.TYPE)};
//return ChartFactory.getTimeChartView(context, dataset, mRenderer, "YYYY-MM-DD");
return ChartFactory.getCombinedXYChartView(context, dataset, mRenderer, types);//return a graph depending on the above declarations, that will be used as a view in the previous activity
}
public void setData(boolean arePoints){//sets the temperatures and dates data that will be used on the graph
Date[] dt = new Date[MainActivity.dates.size()];
for (int k = 0; k< MainActivity.dates.size(); k++){
GregorianCalendar gc = new GregorianCalendar(MainActivity.dates.get(k).getYy(),
MainActivity.dates.get(k).getMM()-1,MainActivity.dates.get(k).getDd(),
MainActivity.dates.get(k).getHh(),MainActivity.dates.get(k).getMm(),
MainActivity.dates.get(k).getSs());
dt[k] = gc.getTime();
}
if(!arePoints){
TimeSeries series = new TimeSeries("Temperature");
dataset.addSeries(series);
int t = 0;
for (t = 0; t < MainActivity.temps.size(); t++){
series.add(dt[t] , MainActivity.temps.get(t));
}
}
else{
XYSeries pointSeries = new XYSeries("NotFromSensor");
dataset.addSeries(pointSeries);
int t = 0;
for (t = 0; t < MainActivity.temps.size(); t++){
pointSeries.add(dt[t].getTime(), MainActivity.temps.get(t));
}
}
}
public void setData(int i,boolean arePoints){//sets the temperatures and dates data that will be used on the graph
Date[] dt = new Date[MainActivity.dates.size()];
for (int k = 0; k< MainActivity.dates.size(); k++){
GregorianCalendar gc = new GregorianCalendar(MainActivity.dates.get(k).getYy(),
MainActivity.dates.get(k).getMM()-1,MainActivity.dates.get(k).getDd(),
MainActivity.dates.get(k).getHh(),MainActivity.dates.get(k).getMm(),
MainActivity.dates.get(k).getSs());
dt[k] = gc.getTime();
}
if(!arePoints){
TimeSeries series = new TimeSeries("device "+DeviceFilterActivity.checkedDevices.get(i).getName());
dataset.addSeries(series);
int t = 0;
for (t = 0; t < MainActivity.temps.size(); t++){
series.add(dt[t] , MainActivity.temps.get(t));
}
}
else{
XYSeries pointSeries = new XYSeries("NotFromSensor");
dataset.addSeries(pointSeries);
int t = 0;
for (t = 0; t < MainActivity.temps.size(); t++){
pointSeries.add(dt[t].getTime() , MainActivity.temps.get(t));
}
}
}
public void clearDataset(){
this.dataset.clear();
}
}
显然,该图表以毫秒为单位显示时间。如何使用TimeChart中的X轴显示真实日期?
我刚刚找到了解决这个问题的方法。这是一个自定义库,类似于对原始AChartEngine的扩展。它包括一个名为CombinedTimeChart的类。它与CombinedXYChart完全相同,但是您可以使用适当的日期设置x轴,而不必手动设置其标签。我提供了获取该库的链接:
https://github.com/hdodenhof/achartengine
归功于创作者的出色表现。我测试了它,效果很好。唯一要做的就是将整个项目导入到您的工作空间中(如果使用Eclipse),并将其作为库导入到您的项目中:
现在,您可以像通常使用原始AChartEngine库那样使用该库了。记住要使用该方法ChartFactory.getCombinedTimeChartView(context, dataset, renderer, type)
来获得带有所选图形的图表。
我在论坛上看到了许多相关问题,答案为“使用CombinedXYChart与LineChart和ScatterChart结合使用”,这不是一个不好的解决方案,但在我看来,以上是创建此类图表的更好,更轻松的方法。
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句