PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.68k stars 9.02k forks source link

MPAndroidChart conditional dataset color in Real time data line chart. #5424

Open mintheinwin opened 1 year ago

mintheinwin commented 1 year ago

I'm using MP Library chart to show data real time data chart with different color depending on Y value condition in run real time every second data changes.

private LineDataSet set;
private List<Integer> colors = new ArrayList<>();
private LineDataSet createSet() {

    set = new LineDataSet(null,"Dynamic Data");
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    // set.setColor(ColorTemplate.getHoloBlue());
    set.setColor(getActivity().getColor(R.color.line_color));
    set.setCircleColor(Color.WHITE);
    // line pointer
    set.setDrawCircles(true);
    set.setLineWidth(3f);
    set.setCircleRadius(3f);
    set.setFillAlpha(5);
    //set.setFillColor(ColorTemplate.getHoloBlue());
    // set.setHighLightColor(Color.rgb(244, 117, 117));
    set.setValueTextColor(Color.WHITE);
    set.setValueTextSize(9f);
    set.setDrawValues(false);

    return set;
}

private void addValueChartEntry(int valueXPosition, float valueY) {
    LineData data = chemicalChart.getData();

    if (data != null) {

        ILineDataSet lineSet = data.getDataSetByIndex(0);
        // set.addEntry(...); // can be called as well

        if (lineSet == null) {
            lineSet = createSet();
            data.addDataSet(lineSet);
        }
        // data.addEntry(new Entry(set.getEntryCount(), valueY), 0);
        data.addEntry(new Entry(valueXPosition, valueY), 0);
        data.notifyDataChanged();
        if (valueY==1.5) {
            colors.add(Color.RED);
        }else {
            colors.add(Color.GREEN);
        }
        // let the chart know it's data has changed
        set.setColors(colors);
        chemicalChart.getData().notifyDataChanged();
        chemicalChart.notifyDataSetChanged();
        // limit the number of visible entries
        chemicalChart.setDragEnabled(false);
        chemicalChart.setTouchEnabled(false);
        chemicalChart.setDragDecelerationEnabled(false);
        chemicalChart.setVisibleXRangeMaximum(6);
        // chart.setVisibleYRange(30, AxisDependency.LEFT);

        // move to the latest entry
        chemicalChart.moveViewToX(data.getEntryCount());
        chemicalChart.invalidate();
        // this automatically refreshes the chart (calls invalidate())
        //binding.lineChart.moveViewTo(data.getEntryCount(), 55f, YAxis.AxisDependency.RIGHT);
    }
}
dishankEbizz commented 1 year ago

Hello, Please help me with this I am getting the same issue.

mintheinwin commented 1 year ago

Hi dishankEbizz, I was created to dynamic dataset for different line (example different line chart color and show gap when Y-axias value is null) Example datalist 2,3,4,null,null,1,23,5

for data position 2 to 4 create one dataset ILineDataSet lineSet = data.getDataSetByIndex(0); for data position null to null next one dataset ILineDataSet lineSet = data.getDataSetByIndex(1); for data position 1...to ...5 ILineDataSet lineSet = data.getDataSetByIndex(2); you can handle in data loop list. You can change to create dataset position when data has changed .

dishankEbizz commented 1 year ago

Hi dishankEbizz, I was created to dynamic dataset for different line (example different line chart color and show gap when Y-axias value is null) Example datalist 2,3,4,null,null,1,23,5

for data position 2 to 4 create one dataset ILineDataSet lineSet = data.getDataSetByIndex(0); for data position null to null next one dataset ILineDataSet lineSet = data.getDataSetByIndex(1); for data position 1...to ...5 ILineDataSet lineSet = data.getDataSetByIndex(2); you can handle in data loop list. You can change to create dataset position when data has changed .

Can you please suggest a solution to this bug? https://github.com/PhilJay/MPAndroidChart/issues/5429#issue-1973858671

GitHubWanglei commented 7 months ago

You can refer to this real-time curve library, https://github.com/GitHubWanglei/RTLineChartView