有时行不会在图表上显示 - 图表引擎

时间:2014-04-23 12:30:42

标签: android view achartengine

我使用图表引擎开发了一个应用程序。并使用achartengine绘制图表。 我用时间图绘制了一个图表。这里的日期在x轴上,而在y轴上的值。当输入日期太长时,即如果一个日期是jan-10而第二个日期是apr 12那么两个点之间的线没有显示。 ![在此处输入图像说明] [日期还有另一个点jan 10在我们滚动的时候我们可以看到但是点之间的线没有显示] 1

这是代码

public View getChartView(long startDate,long currentDate,int visibility)
    {
         String titles = "New tickets";
           Date[]dateList = new Date[dates.length];
           for(int i=0;i<dates.length;i++)
           {
               dateList[i]=convertDateFromString(dates[i]);
           }
            int[] colors = new int[] { Color.GREEN};
            PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE};
            XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
            setChartSettings(renderer, "Project work status", "Date", "Tickets",startDate,
                    currentDate,yMin,yMax, Color.GRAY, Color.LTGRAY);
            float textSize=context.getResources().getDimension(R.dimen.text_title_medium);
            CustomTimeChart chart2 = new CustomTimeChart(
                    buildDateDataset(titles, dateList, values), renderer,context,visibility,textSize);
//          TimeChart chart = new TimeChart(buildDateDataset(titles, dateList, values), renderer);
            chart2.setDateFormat("dd,MMM");
            final GraphicalView graphView = new GraphicalView(context, chart2);
               graphView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    SeriesSelection ss = graphView.getCurrentSeriesAndPoint();
                    if(ss!=null)
                    {
                        long clickedDateSeconds = (long) ss.getXValue();
                        Date clickedDate = new Date(clickedDateSeconds);
                        WeightTrackingActivity.dateCal.setTime(clickedDate);
                        WeightTrackingActivity.weight = ss.getValue();
                        try {
                            clickedDate=formatter.parse(clickedDate.toString());
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        if(clickedDate.before(Funcs.getCurrentDateInDate()))
                            WeightTrackingActivity.btnRecordWeight.setText("Update weight");
                        else
                            WeightTrackingActivity.btnRecordWeight.setText(WeightTrackingActivity.weightBtnText);
                    }
                    else
                    {
                        WeightTrackingActivity.btnRecordWeight.setText(WeightTrackingActivity.weightBtnText);
                        WeightTrackingActivity.dateCal.setTime(new Date());
                    }
                }
            });
        return graphView;
    }
    protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
              String yTitle, double startDate, double currentDate, double yMin, double yMax, int axesColor,
              int labelsColor) {
//      renderer.setAxesColor(axesColor);
        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.WHITE);
        renderer.setShowGrid(true);
        renderer.setGridColor(Color.LTGRAY);
        renderer.setShowLegend(false);
        renderer.setMarginsColor(Color.WHITE);
//      renderer.setPanEnabled(true, true);
        renderer.setClickEnabled(true);
//      renderer.setPanLimits(new double[]{yMin,yMax});
        renderer.setZoomEnabled(true, false);
        renderer.setLabelsTextSize(context.getResources().getDimension(R.dimen.text_title_medium));
        renderer.setPointSize(5f);

            renderer.setXAxisMin(startDate);
            renderer.setXAxisMax(currentDate);
            renderer.setXLabelsColor(Color.BLACK);
            renderer.setXLabels(5);
            renderer.setXLabelsPadding(2);
            renderer.setYAxisMin(yMin);
            renderer.setYAxisMax(yMax);
            renderer.setYLabels(6);
            renderer.setYLabelsColor(0, Color.WHITE);
            renderer.setYLabelsAlign(Align.RIGHT);
            renderer.setYLabelsPadding(2);
          }

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

     private void sortingValues(Date[] xValues, double[] yValues) {
         for(int i=0;i<xValues.length;i++)
         {
             for(int j=0;j<xValues.length-1;j++)
             {
                 if(xValues[i].before(xValues[j]))
                 {
                     Date d = xValues[i];
                     xValues[i]=xValues[j];
                     xValues[j]=d;
                     Double x=yValues[i];
                     yValues[i]=yValues[j];
                     yValues[j]=x;
                 }
             }
         }
    }

    protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
            XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
            setRenderer(renderer, colors, styles);
            return renderer;
          }

          protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {

            renderer.setMargins(new int[] { 20, 40, 15, 20 });
            int length = colors.length;
            for (int i = 0; i < length; i++) {
              XYSeriesRenderer r = new XYSeriesRenderer();
              r.setColor(colors[i]);
              r.setPointStyle(styles[i]);
              r.setFillPoints(true);
              r.setLineWidth(3);
              r.setDisplayChartValues(true);
              r.setChartValuesTextSize(context.getResources().getDimension(R.dimen.text_title_small));
              r.setDisplayChartValuesDistance(2);
              FillOutsideLine fillOutsideLine=new FillOutsideLine(FillOutsideLine.Type.BELOW);
              fillOutsideLine.setColor(Color.parseColor("#A3E0FF"));
              r.addFillOutsideLine(fillOutsideLine);
              renderer.addSeriesRenderer(r);
            }
          }

          private Date convertDateFromString(String date) {
                Date dateConverted = new Date();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    dateConverted = df.parse(date);
                } catch ( ParseException e ) {

                }
                return dateConverted;
            }

1 个答案:

答案 0 :(得分:1)

您可以关闭硬件加速

 android:hardwareAccelerated="false"

为特定视图禁用它(这对我有用)

尝试:

  View.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

我在渲染图表后添加了这个,但它应该可以在任何地方使用。