java - Android MPChart : drawing lines of different plots on the same activity in different colors

标签 java android plot mpandroidchart real-time-updates

我正在使用 MPAndroidChart 库创建多个绘图并使用不同的数据源实时更新它们。我希望每个图都有一条不同颜色的线。问题是,即使我为不同的图指定了另一种颜色,所有图都显示为相同颜色的线。此外,我需要使用不同的数据源更新每个图表,但我怀疑所有图表都只使用一种数据源,这表明数据也出现了与颜色相同的问题。

这是我在 Activity 文件中指定不同绘图的方式的一部分

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/linearLayout">

        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout"
        android:id="@+id/linearLayout2">

        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    ...

我有 8 个这样的图堆叠在一起,它们一个接一个地在我的应用程序上正确显示。然后在我的 Activity 文件中,我像这样初始化它们中的每一个

OnChartValueSelectedListener ol = new OnChartValueSelectedListener(){

        @Override
        public void onValueSelected(Entry entry, Highlight h) {
            //entry.getData() returns null here
        }

        @Override
        public void onNothingSelected() {

        }
    };

mChart1 = (LineChart) findViewById(R.id.chart1);
mChart1.setOnChartValueSelectedListener(ol);

mChart2 = (LineChart) findViewById(R.id.chart2);
mChart2.setOnChartValueSelectedListener(ol);
...

然后当我收到数据时我会这样做

    LineData data1 = mChart1.getData();

    if (data1 != null) {

        ILineDataSet set1 = data1.getDataSetByIndex(0);

        // set.addEntry(...); // can be called as well

        if (set1 == null) {
            set1 = createSet();
            data1.addDataSet(set1);
        }

        data1.addEntry(new Entry(set1.getEntryCount(), f), 0);
        data1.notifyDataChanged();

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

        // limit the number of visible entries
        mChart1.setVisibleXRangeMaximum(20);

        // move to the latest entry
        mChart1.moveViewToX(data1.getEntryCount());

    }

    LineData data2 = mChart2.getData();


    if (data2 != null) {

        ILineDataSet set2 = data2.getDataSetByIndex(0);

        // set.addEntry(...); // can be called as well

        if (set2 == null) {
            set2 = createSet2();
            data2.addDataSet(set2);
        }

        data2.addEntry(new Entry(set2.getEntryCount(), f), 0);
        data2.notifyDataChanged();

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

        // limit the number of visible entries
        mChart2.setVisibleXRangeMaximum(20);

        // move to the latest entry
        mChart2.moveViewToX(data2.getEntryCount());

    }

然后我有 createSet 函数来分配不同的颜色

private LineDataSet createSet() {

    LineDataSet set1 = new LineDataSet(null, "");
    set1.setAxisDependency(YAxis.AxisDependency.LEFT);
    set1.setColor(ColorTemplate.getHoloBlue());
    set1.setCircleColor(Color.WHITE);
    set1.setLineWidth(1f);
    set1.setCircleRadius(1f);
    set1.setFillAlpha(65);
    set1.setFillColor(ColorTemplate.getHoloBlue());
    set1.setHighLightColor(Color.rgb(244, 117, 117));
    set1.setValueTextColor(Color.WHITE);
    set1.setValueTextSize(0.1f);
    set1.setDrawValues(false);
    return set1;
}

private LineDataSet createSet2() {

    LineDataSet set2 = new LineDataSet(null, "");
    set2.setAxisDependency(YAxis.AxisDependency.LEFT);
    set2.setColor(Color.GREEN);
    set2.setCircleColor(Color.WHITE);
    set2.setLineWidth(1f);
    set2.setCircleRadius(1f);
    set2.setFillAlpha(65);
    set2.setHighLightColor(Color.rgb(44, 117, 117));
    set2.setValueTextColor(Color.WHITE);
    set2.setValueTextSize(0.1f);
    set2.setDrawValues(false);
    return set2;
}

您看到设置的颜色一个设置为 getHoloBlue(),这是默认测试颜色,另一个设置为 Color.GREEN。问题是所有 8 个图都有 getHoloBlue 颜色。如果我在第一个 createSet 函数上放置另一种颜色,它们都会更改为该颜色。

我是 Android 开发新手,所以我确信我在这里遗漏了一些东西。

最佳答案

而不是这个:

ILineDataSet set1 = data1.getDataSetByIndex(0);
ILineDataSet set2 = data2.getDataSetByIndex(0);

为每个图 TableView 创建单独的数据集:

LineDataSet lineDataSet1 = new LineDataSet(lineEntries1, "legend");
LineDataSet lineDataSet2 = new LineDataSet(lineEntries2, "legend");
// line entries are arraylist of strings
enter code here

之后:

LineData lineData1 = new LineData(lineDataSet1);
LineData lineData2 = new LineData(lineDataSet2);

然后:

mChart1.setData(lineData1);
mChart2.setData(lineData2);

按照该流程,我认为您正在制作不需要的数据集数组,而且我认为您在定义数据集和数据时做错了一些事情。在正常流程中,我们向数据提供数据集,但在您的解决方案中,情况恰恰相反。

对于单个图 TableView 中单行的正常流动,请按照下面的示例,并以这种方式添加不同的图 TableView 和数据集。

示例:

    ArrayList<Entry> lineEntries = new ArrayList<Entry>();
    lineEntries.add(new Entry(0, 1));
    lineEntries.add(new Entry(1, 2));
    lineEntries.add(new Entry(2, 3));
    lineEntries.add(new Entry(3, 4));
    lineEntries.add(new Entry(4, 2));
    lineEntries.add(new Entry(5, 3));
    lineEntries.add(new Entry(6, 1));
    lineEntries.add(new Entry(7, 5));
    lineEntries.add(new Entry(8, 7));
    lineEntries.add(new Entry(9, 6));
    lineEntries.add(new Entry(10, 4));
    lineEntries.add(new Entry(11, 5));

    LineDataSet lineDataSet = new LineDataSet(lineEntries, "Oil Price");
    lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
    lineDataSet.setHighlightEnabled(true);
    lineDataSet.setLineWidth(2);
    lineDataSet.setColor(getColor("defaultBlue"));
    lineDataSet.setCircleColor(getColor("defaultOrange"));
    lineDataSet.setCircleRadius(6);
    lineDataSet.setCircleHoleRadius(3);
    lineDataSet.setDrawHighlightIndicators(true);
    lineDataSet.setHighLightColor(Color.RED);
    lineDataSet.setValueTextSize(12);
    lineDataSet.setValueTextColor(getColor("primaryDark"));

    LineData lineData = new LineData(lineDataSet);

    lineChart.getDescription().setText("Price in last 12 days");
    lineChart.getDescription().setTextSize(12);
    lineChart.setDrawMarkers(true);
    lineChart.setMarker(markerView(context));
    lineChart.getAxisLeft().addLimitLine(lowerLimitLine(2,"Lower Limit",2,12,getColor("defaultOrange"),getColor("defaultOrange")));
    lineChart.getAxisLeft().addLimitLine(upperLimitLine(5,"Upper Limit",2,12,getColor("defaultGreen"),getColor("defaultGreen")));
    lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
    lineChart.animateY(1000);
    lineChart.getXAxis().setGranularityEnabled(true);
    lineChart.getXAxis().setGranularity(1.0f);
    lineChart.getXAxis().setLabelCount(lineDataSet.getEntryCount());
    lineChart.setData(lineData);

关于java - Android MPChart : drawing lines of different plots on the same activity in different colors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49054473/

相关文章:

java - 用什么替换 com.sun.image.codec.jpeg 类?

Java-Fake 系统时钟启动

带有 Facebook 登录的 Android WebView

java - 如何为我的 ClusterManager 使用 onClusterItemRendered 等函数?

android - 当编辑文本获得焦点时滚动页面

java int字段让空

java - "Fix"Java 应用程序窗口在辅助监视器分离时隐藏

r - 在 ggplot2 中使用填充值绘制经度纬度时出错

python - 在 Pandas 中绘制二进制矩阵

python - Seaborn JointGrid 六边形图 : aspect ratio issue