我创建了一个图表,其中篡改显示在 timeChart 中。我需要用不同的颜色标记几个温度,所以我使用了一个XYCombinedChart,并添加了一个带有我想要标记的温度的散点字符。一切都很顺利。唯一的问题是,为了使它工作,我在散点图的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();
}
}
显然,图表以毫秒为单位显示时间。如何使用时间表中的 X 轴并显示实际日期?
我刚刚找到了这个问题的解决方案。它是一个自定义库,类似于原始AChartEngine的扩展。它包括一个名为 CombinedTimeChart 的类。它与组合XYChart完全相同,但您可以使用正确的日期设置x轴,而无需手动设置其标签。我提供了一个链接来获取库:
https://github.com/hdodenhof/achartengine
感谢创作者的出色工作。我测试了它,效果很好。您唯一需要做的就是将整个项目导入到您的工作区中(如果使用 Eclipse(,然后将其作为库导入到您的项目中:
- 在包资源管理器中右键单击项目名称。
- 性能
- 人造人
- 加。。。
- 选择具有导入项目名称的文件夹(图表引擎主(
- 应用
- 还行
现在,您已准备好使用库,就像通常使用原始AChartEngine库一样。请记住使用方法ChartFactory.getCombinedTimeChartView(context, dataset, renderer, type)
获取带有所选图形的图表。
我在论坛上看到过很多相关的问题,答案是"将CombinedXYChart与折线图和散点图一起使用",这不是一个糟糕的解决方案,但在我看来,以上是创建这种图表的更好,更简单的方法。
希望对您有所帮助。