组合图表实时图表

时间:2017-08-02 16:12:30

标签: android mpandroidchart

我对MPAndroidChart的实时组合图表有一点问题。我有两个不同的图表,它在两个单独的图表中工作,但我想合并为一个。主要问题是当值传入时图中没有任何内容。我明白了,参考资料还可以。

这是我的代码:

private void IntialcombinedChart(){
    mChart = (CombinedChart) findViewById(R.id.chartFetalandToco);

    mChart.setDescription("");
    mChart.setNoDataText("No data yet");
    mChart.setDrawGridBackground(false);

    mChart.setTouchEnabled(true);
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setDrawGridBackground(false);

    mChart.setVisibleXRangeMaximum((float)10);

    XAxis x1 = mChart.getXAxis();
    x1.setDrawGridLines(false);
    x1.setAxisMaxValue(1200);
    x1.setAvoidFirstLastClipping(true);

    YAxis y1 = mChart.getAxisLeft();
    y1.setAxisMaxValue(255);
    y1.setAxisMinValue(0);
    y1.setDrawGridLines(true);


    YAxis y12 = mChart.getAxisRight();
    y12.setEnabled(false);


    cData = new CombinedData();

    cData.setData(new LineData());
    cData.setData(new ScatterData());
    mChart.setData(cData);
    mChart.invalidate();
}



public ScatterDataSet createFetalSet(){
    ScatterDataSet set = new ScatterDataSet(null,"Fetal");
    set.setScatterShapeSize(7.5f);
    set.setDrawValues(false);
    set.setValueTextSize(10f);
    set.setDrawValues(false);
    set.setColor(ColorTemplate.COLORFUL_COLORS[2]);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    return  set;
}
 private LineDataSet createTocoSet(){
    LineDataSet set = new LineDataSet(null,"Toco");
    set.setDrawCircles(false);
    set.setDrawCubic(true);
    set.setDrawValues(false);
    set.setColor(ColorTemplate.COLORFUL_COLORS[3]);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    return  set;


}

 public void AddFetalEntry(float sinValue){
    CombinedData data = mChart.getData();
    if (data != null) {
        LineDataSet dataSetGraphA = (LineDataSet) data.getDataSetByIndex(0);
        ScatterDataSet dataSetGraphB = (ScatterDataSet) data.getDataSetByIndex(1);

        if (dataSetGraphA == null) {
            dataSetGraphA = createTocoSet();
            data.addDataSet(dataSetGraphA);
        }
        if (dataSetGraphB == null) {
            dataSetGraphB = createFetalSet();
            data.addDataSet(dataSetGraphB);
        }

        data.addXValue("");

        data.addEntry(new Entry(sinValue, dataSetGraphA.getEntryCount()), 0);

        data.addEntry(new Entry(sinValue, dataSetGraphB.getEntryCount()), 1);

        data.notifyDataChanged();


        // let the chart know it's data has changed
        mChart.notifyDataSetChanged();
        mChart.invalidate(); // refresh

        mChart.setVisibleXRangeMaximum(2000);

        //int valueCount = data.getXValCount();
        mChart.moveViewToX(data.getXValCount() - 2000);


    }
}

我想我写了一个很好的代码,但它不起作用。我尝试了很多方法来解决这个问题,但我不能。我很高兴何时可以在图表上显示相同的值。

我希望,有人可以帮助我。

2 个答案:

答案 0 :(得分:0)

所以,我花了更多时间来了解mpandroid图表系统。首先,我创建了一个带有数据集的空散点图和一个带有linedataset的空白行。在它之后,我不得不在合并数据中寻找这个chartdatas,我从组合图表中得到了什么。

这是我的代码:

private void IntialcombinedChart(){
    mChart = (CombinedChart) findViewById(R.id.chartFetalandToco);

    mChart.setDescription("");
    mChart.setNoDataText("No data yet");
    mChart.setDrawGridBackground(false);
    mChart.setTouchEnabled(true);
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setDrawGridBackground(false);
    mChart.setVisibleXRangeMaximum((float)10);

    XAxis x1 = mChart.getXAxis();
    x1.setDrawGridLines(false);
    x1.setAxisMaxValue(1200);
    x1.setAvoidFirstLastClipping(true);


    YAxis y1 = mChart.getAxisLeft();
    y1.setAxisMaxValue(255);
    y1.setAxisMinValue(0);
    y1.setDrawGridLines(true);


    YAxis y12 = mChart.getAxisRight();
    y12.setEnabled(false);

    mChart.getLegend().setEnabled(false);

    cData = new CombinedData();

    cData.setData(generateLineData());
    cData.setData(generateScatterData());
    mChart.setData(cData);
    mChart.invalidate();




}
protected ScatterData generateScatterData() {

    ScatterData d = new ScatterData();


    d.addDataSet(createFetalSet());

    return d;
}

private LineData generateLineData() {
    LineData d = new LineData();
    d.addDataSet(createTocoSet());
    return d;
}
public ScatterDataSet createFetalSet(){
    ScatterDataSet set = new ScatterDataSet(null,"Fetal");
    set.setScatterShapeSize(10f);
    set.setDrawValues(true);
    set.setValueTextSize(10f);
    set.setDrawValues(false);
    set.setColor(ColorTemplate.COLORFUL_COLORS[2]);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    return  set;
}

private LineDataSet createTocoSet(){
    LineDataSet set = new LineDataSet(null,"Toco");
    set.setDrawCircles(false);
    set.setDrawCubic(true);
    set.setDrawValues(false);
    set.setColor(ColorTemplate.COLORFUL_COLORS[3]);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    return  set;


}
public void AddFetalEntry(float sinValue){
    fmDetected ++;
    CombinedData data = mChart.getData();
    if (data != null) {


        ScatterData scatterDataData = data.getScatterData();
        if(scatterDataData != null){

            ScatterDataSet set = (ScatterDataSet) data.getDataSetByIndex(1);
            if (set == null) {

                set = createFetalSet();
                data.addDataSet(set);
            }
            scatterDataData.addXValue("");
            LineData tocoData = data.getLineData();
            if(tocoData != null) {

                LineDataSet pakk = (LineDataSet) data.getDataSetByIndex(0);

                scatterDataData.addEntry(new Entry(sinValue,pakk.getEntryCount()-1), 0);
            }
            mChart.notifyDataSetChanged();
            mChart.invalidate();
        }
        data.addXValue("");
        data.notifyDataChanged();


        // let the chart know it's data has changed
        mChart.notifyDataSetChanged();
        mChart.invalidate();
        mChart.setVisibleXRangeMaximum(1200);




        //int valueCount = data.getXValCount();
        mChart.moveViewToX(data.getXValCount() - 7);
    }
}
public void AddTocoEntry(float sinValue){
    CombinedData data = mChart.getData();
    if (data != null) {


        LineData tocoData = data.getLineData();
        if(tocoData != null){

            LineDataSet set = (LineDataSet) data.getDataSetByIndex(0);
            if (set == null) {

                set = createTocoSet();
                data.addDataSet(set);
            }
            tocoData.addXValue("");
            tocoData.addEntry(new Entry(sinValue,set.getEntryCount()),0);

            mChart.notifyDataSetChanged();
            mChart.invalidate();
        }
        data.addXValue("");
        data.notifyDataChanged();


        // let the chart know it's data has changed
        mChart.notifyDataSetChanged();
        mChart.invalidate();
        mChart.setVisibleXRangeMaximum(1200);




        //int valueCount = data.getXValCount();
        mChart.moveViewToX(data.getXValCount() - 7);

    }
}

如果有人可以帮助我,我的代码看起来更好,我将非常感激。 :)

答案 1 :(得分:0)

尝试以下方法:

<0K,0%,2.45M/s,remain:11m0s>,
<3072K,0%,9.13M/s,remain:6m58s>,
<6144K,0%,9.35M/s,remain:5m35s>,
<9216K,0%,9.37M/s,remain:4m54s>,
<12288K,0%,9.52M/s,remain:4m28s>,
<15360K,1%,9.42M/s,remain:4m11s>,

并使用实际数据初始化图形:

<0K,0%,2.45M/s,remain:11m0s>, <3072K,0%,9.13M/s,remain:6m58s>, <6144K,0%,9.35M/s,remain:5m35s>, <9216K,0%,9.37M/s,remain:4m54s>, <12288K,0%,9.52M/s,remain:4m28s>, <15360K,1%,9.42M/s,remain:4m11s>,

其余代码应该没问题。