Android上的图表-androidplot和achartengine的问题

Android上的图表-androidplot和achartengine的问题,android,charts,androidplot,Android,Charts,Androidplot,我想在我的活动中集成一个简单的XY折线图。在寻找带有自定义功能(可自定义背景、颜色、轴标签)的免费图表时,我发现了两个候选项:Achartengine和Adnroidplot。还有一些其他的库,但它们不可自定义,或者只能作为单独的意图使用 我还需要支持旧的Android API(至少必须支持1.6) 我尝试了Achartengine,但当我将它集成到ScrollView中时失败了。当我滚动时,图表不知怎么地被破坏了,它被挤压了,一些背景元素似乎消失了 然后我尝试了一个新的情节。起初,由于结对类的

我想在我的活动中集成一个简单的XY折线图。在寻找带有自定义功能(可自定义背景、颜色、轴标签)的免费图表时,我发现了两个候选项:Achartengine和Adnroidplot。还有一些其他的库,但它们不可自定义,或者只能作为单独的意图使用

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

我尝试了Achartengine,但当我将它集成到ScrollView中时失败了。当我滚动时,图表不知怎么地被破坏了,它被挤压了,一些背景元素似乎消失了

然后我尝试了一个新的情节。起初,由于结对类的原因,它没有从1.6开始。但我在Adnroidplot论坛上找到了解决问题的方法。一切似乎都很好,动态更新也很好,尽管定制的观察者工作得很好。自定义X轴标签有点困难(我需要自定义字符串,而不是数字),但通过自定义格式化程序,我终于做到了

但后来我用客户数据库中的真实数据进行了尝试。有一系列具有相等值的点。我震惊地发现Adnroidplot无法画出一条水平线,它挂起或完全弄乱了图表

这是一个测试用例,我从Adnroidplot Quickstart中借用了它,并做了一个小的修改,使一个系列的值相等:

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();
pricesPlot=(XYPlot)findViewById(R.id.mySimpleXYPlot);
//创建要打印的y值数组:
Number[]系列1numbers={7,7};//应为水平线,未得到任何结果或挂起
//将上述阵列转换为XYSeries:
XYSeries系列1=新的SimpleXYSeries系列(
asList(series1Numbers),//SimpleXYSeries获取一个列表,因此将数组转换为一个列表
ArrayFormat.Y\u VALS\u ONLY,//Y\u VALS\u ONLY表示使用元素索引作为x值
“系列1”);//设置系列的显示标题
//创建用于使用LineAndPointRenderer绘制系列的格式化程序:
LineAndPointFormatter系列1格式=新的LineAndPointFormatter(
rgb(0,200,0),//线条颜色
rgb(0,100,0),//点颜色

空);//填充颜色(可选)感谢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

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

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

假设您有一个colors.xml res文件,您可以创建如下清晰系列:

声明:

Number[] yClear = {0, 1, 0};
LineAndPointFormatter clearFormat; 
SimpleXYSeries clear= null;
在onCreate()或onViewCreated()中:

然后,在设置了绘图之后,只需将清晰的绘图添加到图形中

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

我花了几个小时试着调试这个问题,结果就是让生活变得简单,并且这样做。

对于那些使用Acharengine在ScrollView中搜索图表压缩问题的人,这是您问题的解决方案:

renderer.setInScroll(true);

@Coretek-不幸的是,我无法发布完整的代码,但我添加了更多的位,这可能有助于使用一些数据初始化绘图,并测试解决方案是否适合您。啊,关键的变化是您的“seriesNumbers”数组是一个浮点数,而不是像原始帖子中那样的数字[]。请查看我提出的绘制水平线的解决方案,或者我应该说,让你的水平线出现在一块地上。
renderer.setInScroll(true);