2012-02-03 60 views
3

我想在我的Activity中整合一个简单的XY折线图。在寻找自定义(可定制背景,颜色,轴标签)的免费图表时,我发现了两个候选人:Achartengine和Adnroidplot。还有一些其他库,但它们不是可定制的或仅作为单独的意图提供。Android上的图表 - androidplot和achartengine的问题

我还需要支持较旧的Android API(必须支持至少1.6)。

我试过Achartengine,但是当我将它集成到一个ScrollView中时失败了。当我滚动时,图表变得不知何故被损坏,它被挤压,一些背景元素似乎飘走了。

然后我试了Adnroidplot。起初它并不是从1.6开始的,因为Pair类。但我在Adnroidplot论坛上发现了一个修复问题。一切似乎工作正常,也动态更新,虽然自定义观察员工作正常。自定义X轴标签有点难(我需要自定义字符串而不是数字),但是使用自定义格式化程序我终于做到了。

但后来我试着用客户端数据库中的真实数据进行测试。有一系列具有相同值的点。我很震惊地看到,Adnroidplot无法画出水平线,它挂起或完全弄乱了图表!

下面是测试情况下,我从Adnroidplot快速入门借来的,做了小的修改,使一个系列用相等值:

pricesPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot); 

// Create array of y-values to plot: 
Number[] series1Numbers = {7, 7}; // horizontal line expected, got nothing or hang 

// Turn the above arrays into XYSeries: 
XYSeries series1 = new SimpleXYSeries(
    Arrays.asList(series1Numbers),   // SimpleXYSeries takes a List so turn our array into a List 
    ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
      "Series1");        // Set the display title of the series 

// Create a formatter to use for drawing a series using LineAndPointRenderer: 
LineAndPointFormatter series1Format = new LineAndPointFormatter(
      Color.rgb(0, 200, 0),     // line color 
      Color.rgb(0, 100, 0),     // point color 
      null);    // fill color (optional) <- my app hangs if I add it for a horizontal line 

// Add series1 to the xyplot: 
pricesPlot.addSeries(series1, series1Format); 

// Reduce the number of range labels 
pricesPlot.setTicksPerRangeLabel(3); 

// By default, AndroidPlot displays developer guides to aid in laying out your plot. 
// To get rid of them call disableAllMarkup(): 
pricesPlot.disableAllMarkup(); 

我已经张贴在Adnroidplot论坛,但我不知道有多快他们会回答,问题何时会得到解决。

所以我希望也许有人在StackOverflow可能知道一些解决方法吗?

+0

退房的解决办法,我想出了以绘制一个水平线,或者我应该说,让你的横线显示在AndroidPlot中。 – whyoz 2014-02-06 00:22:34

回答

3

感谢Androidplot论坛上的开发者,我现在得到了解决方案。以下代码是我的Activity.java文件的一个片段。不幸的是,最终的代码后来被重构,一些片断被移动到自定义数据源和自定义XYSeries,我没有权限在这里发布。

此代码适用于Androidplot-core-0.4.4-release.jar,我不确定它是否适用于更高版本。

// for chart 
private XYPlot pricesPlot; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // ... other initialisation code omitted for brevity ... 

    pricesPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot); 

    // the following code was added as a debug code to test that it works. 
    // later many things were changed, so take it "as is" - this was the core of the solution 

    PriceDatesFormat ppf = new PriceDatesFormat(); 
    ppf.Labels = new String[]{ 
       "2011-01", 
       "2011-05",    
       "2011-07", 
       "2011-11", 
       "2011-07", 
       "2011-07"      
     }; 

    pricesPlot.getGraphWidget().setDomainValueFormat(ppf);  

    // Create two arrays of y-values to plot: 
    Float [] seriesNumbers = new Float[]{118f, 185f}; 

    Float min = Collections.min(Arrays.asList(seriesNumbers)); 
    Float max = Collections.max(Arrays.asList(seriesNumbers)); 

    pricesPlot.setRangeBoundaries(min - 0.1*min, max + 0.1*max, BoundaryMode.AUTO);// make them a bit wider out of min/max values 
+0

你可以发布你的全新代码吗? – ctekk 2012-05-10 09:08:30

+1

@Coretek - 不幸的是我不能发布完整的代码,但我添加了一些更多的位,可能有助于初始化一些数据的情节,并测试该解决方案适用于您。 – JustAMartin 2013-08-14 10:17:58

+0

啊,关键的变化是你的“seriesNumbers”数组是一个浮点数,而不是你原来的文章中的数字[]。 – whyoz 2013-08-14 17:51:57

1

我想出了一个简单的解决方法,以确保显示水平线。

基本上,在加载你的第一个真实情节之前,只需绘制一个清晰的Y_VALS_ONLY系列的3点图。

假设你有一个colors.xml RES文件,你可以创建一个明确的一系列这样的:

这样声明:

Number[] yClear = {0, 1, 0}; 
LineAndPointFormatter clearFormat; 
SimpleXYSeries clear= null; 

在你的onCreate()或onViewCreated():

clearFormat = new LineAndPointFormatter(getResources().getColor(R.color.transparent), getResources().getColor(R.color.transparent), 0, null); 

然后,您设置您的情节后,只需将清晰的情节添加到图;

clear = new SimpleXYSeries(Arrays.asList(yClear), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Clear"); 
yourPlot.addSeries(clear, clearFormat); 

我花了几个小时试图调试这个问题,并且归结为使生活变得轻松并且以这种方式进行。

1

对于那些谁用Google搜索排行榜挤压内滚动型问题AChartEngine这是解决问题的方法:

renderer.setInScroll(true);