ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

在Android上绘制图表 – androidplot和achartengine的问题

2019-09-02 14:26:03  阅读:200  来源: 互联网

标签:android charts androidplot


我想在我的Activity中集成一个简单的XY线图.在寻找带有自定义(自定义背景,颜色,轴标签)的免费图表时,我找到了两个候选人:Achartengine和Adnroidplot.还有一些其他库,但它们不可自定义或仅作为单独的Intent提供.

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

我尝试过Achartengine,但是当我将它集成到ScrollView中时失败了.当我滚动时,图表变得有点腐败,它被挤压,一些背景元素似乎消失了.

然后我尝试了Adnroidplot.起初它没有从1.6开始因为Pair类.但我在Adnroidplot论坛上找到了解决问题的方法.一切似乎工作正常,虽然自定义Observers工作正常,也动态更新.自定义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();

我已经在Adnroidplot论坛上发布了,但我不确定他们会有多快回答以及何时解决问题.

所以我希望StackOverflow上的某个人可能知道一些解决方法吗?

解决方法:

感谢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

标签:android,charts,androidplot
来源: https://codeday.me/bug/20190902/1791457.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有