Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mintheinwin opened this issue Oct 19, 2023 · 4 comments
Open

Comments

@mintheinwin
Copy link

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
Copy link

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

@mintheinwin
Copy link
Author

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
Copy link

dishankEbizz commented Nov 3, 2023

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?
#5429 (comment)

@GitHubWanglei
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants