如何动态增加MPAndroidChart折线图宽度

时间:2019-05-29 07:43:15

标签: width mpandroidchart

如何在android中动态增加MPAndroidChart折线图的宽度。我正在使用MPAndroidChart:v3.1.0版本。

我已经尝试过         mChart.setViewPortOffsets(0f,10f,10f,0f); 结果没有得到。图表未花费在右侧。当我提供少量数据时,它会正确显示。但是随着数据折线图数量的紧缩。我有3种方法 (1)read_Data(从数据库读取数据) (2)initialize_chart_1 (3)displayLineChart_1

private void read_Data() {
    Random random1 = new Random(); Random random2 = new Random();        

    // ----------- SCORE -----------
    for (int j=1;j<=200;++j) {
        IDList1.add("Chapter Name " + j);
        // random.nextInt((max - min) + 1) + min
        int data1 = random1.nextInt((100 - (-10)) + 1) + (-10);
        ScoreList.add((long) data1);
        int data2 = random2.nextInt((100 - (-10)) + 1) + (-10);
        DOKList.add((long) data2);
    }

    // ----------- SCORE -----------
    Long score_min = Collections.min(ScoreList);
    Long score_max = Collections.max(ScoreList);

    Long DOK_min= Collections.min(DOKList);
    Long DOK_max = Collections.max(DOKList);

    yMin = score_min>DOK_min?score_min:DOK_min;
    yMax = score_max>DOK_max?score_max:DOK_max;

    yMin = yMin - 20;
    yMax = yMax + 20;


    // ----------- SCORE -----------
    for (int i=0;i<IDList1.size();++i){
        if (IDList1.get(i).length()>3) {
            assessmentID_For_DB.add(IDList1.get(i).substring(0, 3).concat("..."));
        }else {
            assessmentID_For_DB.add(IDList1.get(i));
        }
        if (IDList1.get(i).length()>13) {
            assessmentID_For_Table.add(IDList1.get(i).substring(0, 14).concat("..."));
        }else {
            assessmentID_For_Table.add(IDList1.get(i).concat("..."));
        }
    }        

    Object[] objNames = assessmentID_For_DB.toArray();
    assessmentID_For_Chart = Arrays.copyOf(objNames, Objects.requireNonNull(objNames).length, String[].class);


}

    private void initialize_chart_1(){
    // background color
                    mChart.setBackgroundColor(getResources().getColor(android.R.color.transparent));
    // disable description text
    mChart.getDescription().setEnabled(false);
    // enable touch gestures
    mChart.setTouchEnabled(true);

    mChart.setDragDecelerationFrictionCoef(0.9f);

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

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

    // y Axix - hide the grid lines
    mChart.getAxisLeft().setDrawGridLines(false);
    mChart.getAxisLeft().setDrawAxisLine(false);
    mChart.getAxisRight().setDrawAxisLine(false);
    mChart.getAxisRight().setDrawGridLines(false);
    // y Axis value
    mChart.getAxisLeft().setDrawLabels(false);
    mChart.getAxisRight().setDrawLabels(false);

    // X-Axis
    mChart.getXAxis().setDrawGridLines(false);
    mChart.getXAxis().setEnabled(true);
    mChart.getXAxis().setDrawLabels(true);

    // Word wrapping for the Legend
    mChart.getLegend().setWordWrapEnabled(true);

    // Hide the legend
    mChart.getLegend().setEnabled(false);
}

    private void displayLineChart_1(){
    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setAxisMinimum(0f);
    xAxis.setGranularity(1f);

    xAxis.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            return assessmentID_For_Chart[(int) value % assessmentID_For_Chart.length];
        }
    });


    YAxis leftAxis = mChart.getAxisLeft();

    leftAxis.setAxisMinimum(yMin);
    leftAxis.setAxisMaximum(yMax);

    ArrayList<Entry> value1 = new ArrayList<>();

    for (int i=0;i<ScoreList.size();++i)
        value1.add(new Entry(i, ScoreList.get(i)));

    LineDataSet set1;

    set1 = new LineDataSet(value1, "Score");
    set1.setColor(Color.BLACK);
    set1.setCircleColor(Color.BLACK);
    set1.setLineWidth(3f);
    set1.setCircleRadius(10f);
    set1.setDrawCircleHole(true);
    set1.setValueTextSize(0f);
    set1.setDrawFilled(false);
    set1.setCircleHoleRadius(7f);

    ArrayList<Entry>value2 = new ArrayList<>();
    for (int i = 0; i< DOKList.size(); ++i)
        value2.add(new Entry(i, DOKList.get(i)));

    LineDataSet set2;

    set2 = new LineDataSet(value2, "Target");

    set2.setColor(Color.GREEN);
    set2.setCircleColor(Color.GREEN);
    set2.setLineWidth(3f);
    set2.setCircleRadius(10f);
    set2.setDrawCircleHole(true);
    set2.setValueTextSize(0f);
    set2.setDrawFilled(false);
    set2.setCircleHoleRadius(7f);


    LineData data = new LineData(set1, set2);

    mChart.setData(data);
    //mChart.setVisibleXRangeMaximum(2);
    //mChart.invalidate();

    mChart.setViewPortOffsets(0f, 10f, 10f, 0f);

    mChart.zoomToCenter(2,0);
    //mChart.zoom(5,0, 70, 80);

}

0 个答案:

没有答案