在aChartEngine中平移时,条形图重叠到Y轴

时间:2014-01-14 14:20:58

标签: android graph bar-chart achartengine pan

我正在处理显示条形图的应用程序。条形图是根据动态值绘制的,条形图数据以MB / KB为单位。

Code Snippet : -

private void graphinit(ArrayList<Integer> graphInDataList, ArrayList<Integer> graphOutDataList)
{
    mRenderer.setXAxisMin(0);
    mRenderer.setXAxisMax(graphXMaxLimit);
    mRenderer.setYAxisMin(0);
    // mRenderer.setYAxisMax(100);

    int max = getMaximumNumber(graphInDataList, graphOutDataList);
    mRenderer.setYAxisMax(max);

    double[] limits = new double[] {0, graphXMaxLimit, 0, max};
    mRenderer.setPanLimits(limits);

    mRenderer.setBarSpacing(0.3f);

    mRenderer.setDisplayChartValues(false);
    mRenderer.setShowAxes(true);
    mRenderer.setShowLegend(false);
    mRenderer.setShowLabels(true);
    mRenderer.setPanEnabled(true, false);
    mRenderer.setClickEnabled(false);
    mRenderer.setZoomEnabled(true, false);

    mRenderer.setZoomButtonsVisible(false);
    mRenderer.setShowGrid(false);

    mRenderer.setYLabelsPadding(3);
    mRenderer.setXTitle("Analysis Duration in mins");
    mRenderer.setYTitle("Data in KB");

    // mRenderer.setXAxisMin(-0.5);
    mRenderer.setYLabelsAlign(Align.RIGHT);

    // set up the margin to the graph
    mRenderer.setMargins(new int[] { 5, 40, 0, 5});

    // mRenderer.setXAxisMin(1);
    mRenderer.setXAxisMin(-0.5);
    // mRenderer.setXAxisMax(60);

    inDataSeries = new XYSeries("InData");      
    for (int i = 0; i < graphInDataList.size(); i++) {      
        inDataSeries.add(i, graphInDataList.get(i));
    }

    outDataSeries = new XYSeries("OutData");        
    for (int i = 0; i < graphOutDataList.size(); i++) {
        outDataSeries.add(i, graphOutDataList.get(i));
    }

    mDataset.addSeries(inDataSeries);
    mDataset.addSeries(outDataSeries);

    if(graphInDataList.size()>0){                   
        right_left=(int) (graphInDataList.size()/60);
    }

    XYSeriesRenderer in_data_renderer = new XYSeriesRenderer();     
    in_data_renderer.setColor(Color.GREEN);

    XYSeriesRenderer out_data_renderer = new XYSeriesRenderer();        
    out_data_renderer.setColor(Color.RED);

    mRenderer.addSeriesRenderer(in_data_renderer);
    mRenderer.addSeriesRenderer(out_data_renderer); 

    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(Color.TRANSPARENT);
    mRenderer.setMarginsColor(getResources().getColor(R.color.transparent_background));
}

EDIT :

在onResume()

中创建
     if (mChartView == null) {

        LinearLayout layout = (LinearLayout) findViewById(R.id.linear_chart);           
        mChartView = ChartFactory.getBarChartView(this, mDataset,
                mRenderer, Type.STACKED);

        Logger.vLog("HistoryDetailScreen", "mChartView : "+mChartView);

        layout.addView(mChartView);

        @SuppressWarnings("unused")
        boolean enabled = mDataset.getSeriesCount() > 0;

    } else {
        mChartView.repaint();
    }

我在onCreate()中使用了graphinit(arrlist1,arrlist2)方法。

PROBLEM :

当我们在条形图上进行平移时,条形图与Y轴重叠。

SCREENSHOT Default Screen :

enter image description here

SCREENSHOT Panned Screen :

enter image description here

我也设置了pan限制。有什么问题? 提前谢谢..

2 个答案:

答案 0 :(得分:0)

这对我有用......

mRenderer.setXAxisMin(-0.5); //for first bar overlaps y-axis

答案 1 :(得分:-1)

您正在正确设置平移限制,但请注意,标尺本身的宽度不是零。鉴于此,您必须将X轴最小值设置为低于最小X pan限制的值,例如-0.5

您甚至可以通过侦听缩放事件并相应地更改限制来在运行时更改平移限制值。

mRenderer.setPanLimits(new double[] {-0.5, graphXMaxLimit, 0, max});

mChartView.addZoomListener(new ZoomListener() {
  public void zoomApplied(ZoomEvent e) {
    double currentMinX = mRenderer.getXAxisMin();
    double currentMaxX = mRenderer.getXAxisMax();
    double coef = (graphXMaxLimit + 0.5) / (currentMaxX - currentMinX);
    mRenderer.setPanLimits(new double[] {-0.5 / coef, graphXMaxLimit, 0, max});
  }

  public void zoomReset() {
    Log.i("Zoom", "Reset");
  }
}, true, true);