Android:数据集和渲染器不应为空,并且应具有相同的系列数

时间:2012-01-20 09:18:10

标签: android

`* //我正在实施一个折线图。我收到如下错误:
//错误:数据集和渲染器不应为空,并且应具有相同数量的系列
//我无法理解这个?谁能帮我?

Intent intent = ChartFactory.getLineChartIntent(context, buildDateDataset
(titles, x, values),renderer, ""); 
return intent;

protected XYMultipleSeriesDataset buildDateDataset(String[] titles, List<Date[]> xValues,
List<double[]> yValues) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++) {
TimeSeries series = new TimeSeries(titles[i]);
Date[] xV = xValues.get(i);
double[] yV = yValues.get(i);
int seriesLength = xV.length;
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
  dataset.addSeries(series);
}
return dataset;
}

In the linechart line i am sending the param values. On that my app is crashing.   
I also added XYMultipleSeriesDataset code.

My execute function. In this function i am getting x and y values.

List<Date[]> x = new ArrayList<Date[]>();
List<double[]> values = new ArrayList<double[]>(); 

double price[] = new double[PriceData.size()];
System.out.println("Print :" + price);
Date priceDt[] = new Date[PriceData.size()];
System.out.println("Print :" + priceDt);

for(int i = 0; i <titles.length; i++ )
{
for (int j = 0; j < PriceData.size(); j++) 
{
date = PriceData.get(j).PriceDt;
System.out.println(date);
Date d = new Date(date); //check it  
System.out.println(d); 
priceDt[j] = d; 
x.add(priceDt);    
}
}
for (int i = 0; i < PriceData.size(); i++) 
{
//date = date.replace("/", "");
System.out.println(PriceData.get(i).Price);
System.out.println(PriceData.get(i).PriceDt); 
price[i] = Double.valueOf(PriceData.get(i).Price);
values.add(price);
}
int[] colors = new int[] { Color.CYAN,Color.GREEN};
// PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE};
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,PointStyle.POINT};
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles); 
int length = renderer.getSeriesRendererCount(); 
for (int i = 0; i < length; i++) 
{  
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true); 
}
setChartSettings(renderer, "", "Month", "price", 0, 12, 0, 190,
Color.GREEN, Color.GREEN);
renderer.setXLabels(PriceData.size());
renderer.setYLabels(PriceData.size());     
renderer.setShowGrid(false);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.WHITE);
renderer.setMarginsColor(Color.WHITE);
//renderer.setAxisTitleTextSize(15);
renderer.setMargins(new int[] {10, 40, 10, 10});
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
Intent intent = ChartFactory.getLineChartIntent(context, 
buildDateDataset(titles, x, values), renderer, ""); 
return intent;
}
}

1 个答案:

答案 0 :(得分:1)

通过这种方式,您可以绘制折线图,​​在任何线程或循环中给出x,y的值 不需要其他活动。试试吧

public class Test extends Activity {
    private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
    private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
    private XYSeries mCurrentSeries;
    private XYSeriesRenderer mCurrentRenderer;
    private GraphicalView mChartView;
    int x,y;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mRenderer.setPanLimits(new double[] { 00,600 , 00, 300 });     //xmin,xmax,ymin,ymax  bars/grids limit
        mRenderer.setZoomLimits(new double[] { 00, 200, 00, 30 });     //xmin,xmax,ymin,ymax  zoom limit
        String seriesTitle = "Series " + (mDataset.getSeriesCount() + 1);
        XYSeries series = new XYSeries(seriesTitle);
        mDataset.addSeries(series);
        mCurrentSeries = series;
        mRenderer.setYLabelsAlign(Align.RIGHT);
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        mRenderer.addSeriesRenderer(renderer);
        mCurrentRenderer = renderer;


        if(mChartView== null){

            //ADD VALUE OF X,Y HERE to the series
            mCurrentSeries.add(x,y); 

            if (mChartView != null) {
                mChartView.repaint();
            }
             mChartView = ChartFactory.getLineChartView(Test.this, mDataset, mRenderer);
             mChartView.addPanListener(new PanListener() {
                    public void panApplied() {
                      System.out.println("New X range=[" + mRenderer.getXAxisMin() + ", " + mRenderer.getXAxisMax()
                          + "], Y range=[" + mRenderer.getYAxisMax() + ", " + mRenderer.getYAxisMax() + "]");
                    }
                  });
             //ADDING YOUR VIEW TO ANY LAYOUT   adding in xml
             layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,400));  

        }



        // TODO Auto-generated method stub
    }

}

不要忘记添加manifest.xml文件

  <activity android:name="org.achartengine.GraphicalActivity" />