Android achartengine, Change Point Value Color

632 views Asked by At

Is there any way to change the color of achartengine line graph's line point Value color from white to black?

In the below image I want to change colors 2700, 2500 etc from white to black.

Here is my code:

private void openChart() {

    int count = 5;
    Date[] dt = new Date[5];
    for (int i = 0; i < count; i++) {
        GregorianCalendar gc = new GregorianCalendar(2014, 10, i + 1);
        dt[i] = gc.getTime();
    }

    int[] visits = {2000, 2500, 2700, 2100, 2800};

    // Creating TimeSeries for Visits
    TimeSeries visitsSeries = new TimeSeries("BMR Rate");

    // Adding data to Visits Series
    for (int i = 0; i < dt.length; i++)
        visitsSeries.add(dt[i], visits[i]);

    // Creating a dataset to hold each series
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

    // Adding Visits Series to the dataset
    dataset.addSeries(visitsSeries);

    // Creating XYSeriesRenderer to customize visitsSeries
    XYSeriesRenderer visitsRenderer = new XYSeriesRenderer();
    visitsRenderer.setColor(Color.WHITE);
    visitsRenderer.setPointStyle(PointStyle.SQUARE);

    visitsRenderer.setFillPoints(true);
    visitsRenderer.setLineWidth(3);
    visitsRenderer.setDisplayChartValues(true);


    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();

    multiRenderer.setChartTitle("Body Mass Index");
    // multiRenderer.setMargins(new int[]{0, 0, 0, 10});
    multiRenderer.setApplyBackgroundColor(true);
    // multiRenderer.setPointSize(15);
    multiRenderer.setLabelsColor(Color.WHITE);
    multiRenderer.setGridColor(Color.RED);
    multiRenderer.setMarginsColor(Color.parseColor("#54D66A"));
    multiRenderer.setYLabelsColor(0,Color.WHITE);
    multiRenderer.setXLabelsColor(Color.WHITE);

    multiRenderer.setXTitle("Days");
    multiRenderer.setYTitle("Count");
    multiRenderer.setZoomButtonsVisible(true);

    // Adding visitsRenderer and viewsRenderer to multipleRenderer
    // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
    // should be same
    multiRenderer.addSeriesRenderer(visitsRenderer);
    //  multiRenderer.addSeriesRenderer(viewsRenderer);

    // Getting a reference to LinearLayout of the MainActivity Layout
    LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);

    // Creating a Time Chart
    mChart = (GraphicalView) ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer, "dd-MMM-yyyy");

    multiRenderer.setClickEnabled(true);
    multiRenderer.setSelectableBuffer(10);

    // Setting a click event listener for the graph
    mChart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Format formatter = new SimpleDateFormat("dd-MM");

            SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();

            if (seriesSelection != null) {
                int seriesIndex = seriesSelection.getSeriesIndex();
                String selectedSeries = "Visits";
                if (seriesIndex == 0)
                    selectedSeries = "Visits";
                else
                    selectedSeries = "Views";

                // Getting the clicked Date ( x value )
                long clickedDateSeconds = (long) seriesSelection.getXValue();
                Date clickedDate = new Date(clickedDateSeconds);
                String strDate = formatter.format(clickedDate);

                // Getting the y value
                int amount = (int) seriesSelection.getValue();

                // Displaying Toast Message
                Toast.makeText(
                        getBaseContext(),
                        selectedSeries + " on "  + strDate + " : " + amount ,
                        Toast.LENGTH_SHORT).show();
            }
        }

    });

    // Adding the Line Chart to the LinearLayout
    chartContainer.addView(mChart);
}

2

There are 2 answers

3
GreyBeardedGeek On

If you want to change the color of just one point, you'll probably have to subclass the Renderer and set the color yourself when rendering that point.

1
Kirti On

Add colors to labels of render:

 multiRenderer.setYLabelsColor(0, Color.BLACK);  
 multiRenderer.setXLabelsColor(Color.BLACK);