PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.67k stars 9.02k forks source link

LineChart ANR #2486

Open FtheGFW opened 8 years ago

FtheGFW commented 8 years ago

LineChart drawing too many entries, like 500 caused ANR , version v3.0.0

  public static void setup() {
    int size = 500;
    ArrayList<Entry> values1 = new ArrayList<>(size);
    ArrayList<Entry> values2 = new ArrayList<>(size);
    ArrayList<Entry> values3 = new ArrayList<>(size);
    ArrayList<Entry> values4 = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
      // fill the array list 
      ...
    }

    if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
      LineDataSet set = (LineDataSet) mChart.getData().getDataSetByIndex(0);
      LineDataSet set1 = (LineDataSet) mChart.getData().getDataSetByIndex(1);
      LineDataSet set2 = (LineDataSet) mChart.getData().getDataSetByIndex(2);
      LineDataSet set3 = (LineDataSet) mChart.getData().getDataSetByIndex(3);
      set.setValues(values1);
      set1.setValues(values2);
      set2.setValues(values3);
      set3.setValues(values4);
      mChart.getData().notifyDataChanged();
      mChart.notifyDataSetChanged();
      return;
    }

    mChart.getAxisLeft().setEnabled(false);
    mChart.getAxisRight().setEnabled(false);
    mChart.getXAxis().setEnabled(false);
    mChart.getDescription().setEnabled(false);
    initLegend(mChart.getLegend());
    ArrayList<ILineDataSet> dataSets = new ArrayList<>();
    Context context = mChart.getContext();
    dataSets.add(map(values1, "label1", ContextCompat.getColor(context, R.color.green)));
    int historyLineColor = ContextCompat.getColor(context, R.color.result_history);
    dataSets.add(map(values2, "label2", historyLineColor, historyLineColor, 70));
    int color1 = Color.rgb(251, 152, 0);
    dataSets.add(map(values3, "label3", color1, color1, 66));
    dataSets.add(map(values4, "label4", Color.rgb(116, 214, 1)));
    mChart.setData(new LineData(dataSets));
    mChart.setVisibleXRange(0f, 6f);
    mChart.moveViewToX(size - 7);
    mChart.invalidate();
  }

  @NonNull private static LineDataSet map(ArrayList<Entry> entry, String label, int lineColor) {
    return map(entry, label, lineColor, Color.TRANSPARENT, 0);
  }

  @NonNull
  private static LineDataSet map(ArrayList<Entry> entry, String label, int lineColor, int fillColor,
      int fillAlpha) {
    LineDataSet set1;
    set1 = new LineDataSet(entry, label);
    set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    set1.setCircleColor(lineColor);
    set1.setColor(lineColor);
    set1.setLineWidth(2f);
    set1.setCircleRadius(3f);
    set1.setDrawCircleHole(true);
    set1.setDrawValues(true);
    set1.setValueTextColor(lineColor);
    set1.setDrawFilled(true);
    set1.setFillColor(fillColor);
    set1.setFillAlpha(fillAlpha);
    return set1;
  }
FtheGFW commented 8 years ago

😞