MpChartlibrary背景颜色变化

时间:2018-08-16 05:39:36

标签: android linechart

是否有任何方法可以更改所选部分的颜色。我正在使用Mp图表库。并尝试以折线图的形式显示一些统计信息。 到目前为止,我已经尝试了许多更改,但是无法更改所选部分https://www.symantec.com/connect/blogs/add-file-msi-using-orca

的颜色
public class Weight extends Fragment  {

private LineChart mChart;
LineChartRenderer lineChartRenderer;

private ILineDataSet boundaryDataSet;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {

    }
}



public Weight() {
    this(null);
}
//Pass the dataset of other line in the Constructor
@SuppressLint("ValidFragment")
public Weight(ILineDataSet boundaryDataSet) {
    this.boundaryDataSet = boundaryDataSet;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_weight, container, false);

    mChart = rootView.findViewById(R.id.chart1);

    mChart.setBackgroundColor(Color.parseColor("#ffffff"));
    // no 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(true);
    mChart.setDrawGridBackground(false);
    mChart.setHighlightPerDragEnabled(true);

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

    // add data
    setData(20, 30);
    mChart.animateX(2500);

    // get the legend (only possible after setting data)
    Legend l = mChart.getLegend();

    // modify the legend ...
    l.setForm(Legend.LegendForm.LINE);
    l.setTextSize(11f);
    l.setTextColor(Color.BLACK);
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);

    //      l.setYOffset(11f);


    XAxis xAxis = mChart.getXAxis();
    xAxis.setTextSize(11f);
    xAxis.setTextColor(Color.BLACK);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTextColor(ColorTemplate.getHoloBlue());
    leftAxis.setAxisMaximum(30);
    leftAxis.setAxisMinimum(0f);
    leftAxis.setDrawGridLines(true);
    leftAxis.setGranularityEnabled(true);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setTextColor(Color.BLACK);
    rightAxis.setAxisMaximum(30);
    rightAxis.setAxisMinimum(0);
    rightAxis.setDrawGridLines(false);
    rightAxis.setDrawZeroLine(false);
    rightAxis.setGranularityEnabled(false);

    return rootView;
}

private void setData(int count, float range) {

    ArrayList<String> first_line =new ArrayList<String>();
    ArrayList<String> second_line =new ArrayList<String>();
    ArrayList<String> theard_line =new ArrayList<String>();

    first_line.add("3");
    first_line.add("6");
    first_line.add("9");
    first_line.add("12");
    first_line.add("15");
    first_line.add("18");
    first_line.add("21");

    second_line.add("2");
    second_line.add("4");
    second_line.add("6");
    second_line.add("8");
    second_line.add("10");
    second_line.add("12");
    second_line.add("14");

    theard_line.add("1");
    theard_line.add("3");
    theard_line.add("3");
    theard_line.add("4");
    theard_line.add("7");
    theard_line.add("10");
    theard_line.add("12");


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

    for (int i = 0; i < first_line.size(); i++) {
        float mult = range / 1;
        float val = Float.parseFloat(first_line.get(i));
        yVals1.add(new Entry(i, val));

    }

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

    for (int i = 0; i < second_line.size(); i++) {
        float mult = range;
        float val = Float.parseFloat(second_line.get(i));
        yVals2.add(new Entry(i, val));



    }

    ArrayList<Entry> yVals3 = new ArrayList<Entry>();
    for (int i = 0; i < theard_line.size(); i++) {
        float mult = range;
        float val = Float.parseFloat(theard_line.get(i));
        yVals3.add(new Entry(i, val));
    }

    LineDataSet set1, set2, set3;
    if (mChart.getData() != null &&
            mChart.getData().getDataSetCount() > 0) {
        set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
        set2 = (LineDataSet) mChart.getData().getDataSetByIndex(1);
        set3 = (LineDataSet) mChart.getData().getDataSetByIndex(2);
        set1.setValues(yVals1);
        set2.setValues(yVals2);
        set3.setValues(yVals3);
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();
    }

    else {

        // create a dataset and give it a type
        set1 = new LineDataSet(yVals1, "DataSet 1");
        set1.setAxisDependency(YAxis.AxisDependency.LEFT);
        set1.setColor(Color.parseColor("#f5f3f3"));
        set1.setCircleColor(R.color.trans);
        set1.setLineWidth(2f);
        set1.setDrawCircles(false);
        set1.setFillColor(R.color.trans);
        set1.setDrawCircleHole(false);
        set1.setFillAlpha(60);
        set1.setHighLightColor(Color.parseColor("#f5f3f3"));
        set1.setHighlightLineWidth(300f);
        set1.setHighlightEnabled(true);
        set1.setDrawFilled(true);


        // create a dataset and give it a type
        set2 = new LineDataSet(yVals2, "DataSet 2");
        set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set2.setColor(Color.parseColor("#f46a99"));
        set2.setCircleColor(Color.BLACK);
        set2.setLineWidth(2f);
        set2.setCircleRadius(3f);
        set2.setFillAlpha(65);
        set2.setFillColor(Color.RED);
        set2.setDrawCircleHole(false);
        //set2.setFillFormatter(new MyFillFormatter(900f));

        set3 = new LineDataSet(yVals3, "DataSet 3");
        set3.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set3.setColor(Color.parseColor("#f5f3f3"));
        set3.setDrawCircles(false);
        set3.setLineWidth(2f);
        set3.setFillColor(Color.parseColor("#000000"));
        Drawable drawable = ContextCompat.getDrawable(getActivity(), 
        R.drawable.fade_red);
        set3.setFillDrawable(drawable);
        set3.setHighlightEnabled(true);
        set3.setHighLightColor(Color.parseColor("#000000"));
        set3.setDrawCircleHole(false);
        set3.setFillAlpha(0);
        set3.setDrawFilled(true);






        // create a data object with the datasets
        LineData data = new LineData(set1, set2, set3);
        data.setValueTextColor(Color.BLACK);
        data.setValueTextSize(9f);

        // set data
        mChart.setData(data);

    }
}

}

我想更改set 3的背景色。是否可以在此库中更改颜色?还是我应该使用其他代码

0 个答案:

没有答案