如何解决这个错误 - 我想创建MPAndroidChart动态多行

时间:2016-08-08 11:07:40

标签: java android arrays arraylist mpandroidchart

我尝试了MPAndroidChart动态多行

private LineChart mChart;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mChart = (LineChart) findViewById(R.id.chart1);
    mChart.setDrawGridBackground(false);

    mChart.setDescription("");
    mChart.setNoDataTextDescription("You need to provide data for the chart.");

    mChart.setTouchEnabled(true);

    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);

    mChart.setPinchZoom(true);

    LimitLine llXAxis = new LimitLine(10f, "Index 10");
    llXAxis.setLineWidth(4f);
    llXAxis.enableDashedLine(10f, 10f, 0f);
    llXAxis.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
    llXAxis.setTextSize(10f);

    XAxis xAxis = mChart.getXAxis();
    xAxis.enableGridDashedLine(10f, 10f, 0f);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.removeAllLimitLines();
    leftAxis.enableGridDashedLine(10f, 10f, 0f);
    leftAxis.setDrawZeroLine(false);

    leftAxis.setDrawLimitLinesBehindData(true);

    mChart.getAxisRight().setEnabled(false);
    float[] lineData_0 = {37500, 38000, 38500, 39000, 39500, 40000, 40500};
    float[] lineData_1 = {3000, 3500, 3800, 3600, 3400, 2000, 2800};
    float[] lineData_2 = {0, 0, 6000, 25000, 24800, 24750, 5000};
    float[] lineData_3 = {0, 6500, 11000, 12500, 13000, 14000, 2000};
    float[] lineData_4 = {0, 9000, 12400, 12450, 9300, 8000, 6000};

    float[][] all_line_data = {lineData_0, lineData_1, lineData_2, lineData_3, lineData_4};

    ArrayList<ILineDataSet> dataSets = null;

    ArrayList<Entry>[] values = (ArrayList<Entry>[]) new ArrayList[all_line_data.length];

    for (int j = 0; j < all_line_data.length; j++) {
        for (int i = 0; i < all_line_data[j].length; i++) {
            values[j].add(new Entry(all_line_data[j][i], i));
        }

        LineDataSet[] set0 = new LineDataSet[j];
        set0[j].enableDashedLine(10f, 5f, 0f);
        set0[j].enableDashedHighlightLine(10f, 5f, 0f);
        set0[j].setColor(Color.BLACK);
        set0[j].setCircleColor(Color.BLACK);
        set0[j].setLineWidth(1f);
        set0[j].setCircleRadius(3f);
        set0[j].setDrawCircleHole(false);
        set0[j].setValueTextSize(9f);
        set0[j].setDrawFilled(true);

        dataSets = new ArrayList<ILineDataSet>();
        dataSets.add(set0[j]);
    }
    LineData data = new LineData(dataSets);
    mChart.setData(data);
}

我收到以下错误....

  • 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.util.ArrayList.add(java.lang.Object)'

1 个答案:

答案 0 :(得分:0)

我解决了这个问题

    LineChart mChart = (LineChart) findViewById(R.id.chartLine);
    mChart.setDrawGridBackground(true);

    // no description text
    mChart.setDescription("");
    mChart.setNoDataTextDescription("You need to provide data for the chart.");

    // enable touch gestures
    mChart.setTouchEnabled(true);

    // enable scaling and dragging
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);

    // if disabled, scaling can be done on x- and y-axis separately
    mChart.setPinchZoom(true);

    CustomMarkerView mv = new CustomMarkerView (MainActivity.this, R.layout.line_graph_marker_view, colors);
    mChart.setMarkerView(mv);

    // x-axis limit line
    LimitLine llXAxis = new LimitLine(10f, "Index 10");
    llXAxis.setLineWidth(4f);
    llXAxis.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
    llXAxis.setTextSize(10f);

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
    leftAxis.setAxisMinValue(0);
    leftAxis.setDrawZeroLine(true);

    // limit lines are drawn behind data (and not on top)
    leftAxis.setDrawLimitLinesBehindData(true);

    mChart.getAxisRight().setEnabled(false);

    float[] lineData_0 = {37500, 38000, 38500, 39000, 39500, 40000, 40500};
    float[] lineData_1 = {3000, 3500, 3800, 3600, 3400, 2000, 2800};
    float[] lineData_2 = {0, 0, 6000, 25000, 24800, 24750, 5000};
    float[] lineData_3 = {0, 6500, 11000, 12500, 13000, 14000, 2000};
    float[] lineData_4 = {0, 9000, 12400, 12450, 9300, 8000, 6000};

    float[][] all_line_data = {lineData_0, lineData_1, lineData_2, lineData_3, lineData_4};


    int[] graph_linear_background = {R.drawable.graph_background_0, R.drawable.graph_background_1, R.drawable.graph_background_2, R.drawable.graph_background_3, R.drawable.graph_background_4};
    int[] graph_linear_point_color = {Color.rgb(100, 199, 255), Color.rgb(109, 220, 124), Color.rgb(236, 91, 102), Color.rgb(184, 85, 240), Color.rgb(70, 119, 191)};

    List<ArrayList<Entry>> values_all = new ArrayList<ArrayList<Entry>>();
    for (int k = 0; k < all_line_data.length; k++) {
        ArrayList<Entry> list = new ArrayList<>();
        for (int i = 0; i < all_line_data[k].length; i++) {
            list.add(new Entry(i, all_line_data[k][i]));
        }
        values_all.add(list);
    }

    ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();

    String[] arr = new String[]{"a", "b", "c", "d", "e", "f"};
    Map<String, LineDataSet> map = new HashMap<>();
    for(int i = 0; i < all_line_data.length; i++) {

        map.put("set0" + i, new LineDataSet(values_all.get(i), arr[i]));

        // set the line to be drawn like this "- - - - - -"
        map.get("set0" + i).setColor(graph_linear_point_color[i]);
        map.get("set0" + i).setCircleColor(graph_linear_point_color[i]);
        map.get("set0" + i).setLineWidth(3f);
        map.get("set0" + i).setDrawValues(false);
        map.get("set0" + i).setCircleRadius(4f);
        map.get("set0" + i).setDrawCircleHole(false);
        map.get("set0" + i).setDrawFilled(true);

        dataSets.add(map.get("set0" + i));

        if (Utils.getSDKInt() >= 18) {
            // fill drawable only supported on api level 18 and above
            Drawable drawable = ContextCompat.getDrawable(this, graph_linear_background[i]);
            map.get("set0" + i).setFillDrawable(drawable);
        } else {
            map.get("set0" + i).setFillColor(Color.TRANSPARENT);
        }
    }
    // create a data object with the datasets
    LineData line_data = new LineData(dataSets);
    // set data
    mChart.setData(line_data);

输出是......

enter image description here