How can I set background color for the space between two x grid in AchartEngine

Manikandan

I have created a bar chart using AchartEngine. I like to set different color for the space between two x grid, like the image below. And also how to remove "-" symbol in each axis label.( Need to remove "-" present in 4hr-). If setting color is not possible, I like to show only 4 y labels, with highest value at top. And also I like to fix the space between two labels by myself. And I also need to add hr or some other text for the y labels. The below code is what I use.And also in some device for value 0, a thin line get displayed. I don't want that, if value is greater than 0 only bar should be present. And I get like this. I want the chart to be like the one at the end of the question.enter image description here

        int largest = y[0];
        for (int i = 1; i < y.length; i++) {
            if (y[i] > largest) {
                largest = y[i];
            }
        }
        CategorySeries series = new CategorySeries("");
        for (int i = 0; i < y.length; i++) {

            series.add("Bar" + (i + 1), y[i]);
        }

        XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset();
        dataSet.addSeries(series.toXYSeries()); // number of series

        // customization of the chart

        XYSeriesRenderer renderer = new XYSeriesRenderer(); // one renderer for
                                                            // one series
        renderer.setColor(Color.RED);
        renderer.setDisplayChartValues(false);
        renderer.setChartValuesSpacing((float) 5.5d);
        renderer.setLineWidth((float) 1.5d);

        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

        mRenderer.addSeriesRenderer(renderer);
        // mRenderer.setChartTitle("Demo Graph");
        // mRenderer.setXTitle("xValues");

        mRenderer.setYTitle("minutes");

        mRenderer.setLabelsColor(getResources().getColor(R.color.txt_blue));

        mRenderer.setShowGridX(false); // this will show the grid in graph
        mRenderer.setShowGridY(false);
        // mRenderer.setAntialiasing(true);
        mRenderer.setBarSpacing(.5); // adding spacing between the line or
                                        // stacks
        // mRenderer.setApplyBackgroundColor(true);
        // mRenderer.setBackgroundColor(Color.BLACK);
        mRenderer.setXAxisMin(0);
        mRenderer.setYAxisMin(0);
        mRenderer.setXAxisMax(7);
        mRenderer.setZoomInLimitY(10);
        if (largest < 6) {
            mRenderer.setYAxisMax(6);
        } else {
            mRenderer.setYAxisMax(largest);
        }
        mRenderer.setScale(4);

        mRenderer.setXLabels(0);
        mRenderer.addXTextLabel(1, "Sun");
        mRenderer.addXTextLabel(2, "Mon");
        mRenderer.addXTextLabel(3, "Tue");
        mRenderer.addXTextLabel(4, "Wed");
        mRenderer.addXTextLabel(5, "Thu");
        mRenderer.addXTextLabel(6, "Fri");
        mRenderer.addXTextLabel(7, "Sat");
        // mRenderer.setYLabels(0);
        //
        // mRenderer.addYTextLabel(2, "2 min");
        //
        // mRenderer.addYTextLabel(4, "4 min");
        //
        // mRenderer.addYTextLabel(6, "6 min");
        // mRenderer.addYTextLabel(7, "Sat");
        mRenderer.setXLabelsColor(getResources().getColor(R.color.txt_blue));
        mRenderer.setYLabelsColor(0, getResources().getColor(R.color.txt_blue));
        mRenderer.setLabelsColor(getResources().getColor(R.color.txt_blue));
        // mRenderer.setYLabelsAlign(Align.RIGHT);
        mRenderer.setYLabelsPadding(20);
        mRenderer.setPanEnabled(false, false);
        mRenderer.setZoomEnabled(false, false);
        mRenderer.setPanEnabled(false, false); // will fix the chart position
        // Intent intent = ChartFactory.getBarChartIntent(context, dataSet,
        // mRenderer,Type.DEFAULT);
        mRenderer.setBarSpacing(1);
        // mRenderer.setYLabelsPadding(5);
        mRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
        mRenderer.setShowAxes(false);
        // mRenderer.setXAxisMin(-0.5);
        // 1080 x 1920
        if (screen_width == 480 && screen_height == 800) {

            mRenderer.setAxisTitleTextSize(15);
            mRenderer.setLabelsTextSize(15);
        } else if (screen_width > 480 && screen_width < 1000
                && screen_height > 800 && screen_height < 1700) {
            mRenderer.setAxisTitleTextSize(20);
            mRenderer.setLabelsTextSize(20);
        }

        else if (screen_width > 1000 && screen_height > 1700) {
            mRenderer.setAxisTitleTextSize(30);
            mRenderer.setLabelsTextSize(30);
        } else if (screen_width < 480 && screen_height < 800) {
            mRenderer.setAxisTitleTextSize(10);
            mRenderer.setLabelsTextSize(10);
        }

        mRenderer.setShowLegend(false);

        mChart = (GraphicalView) ChartFactory.getBarChartView(getBaseContext(),
                dataSet, mRenderer, Type.DEFAULT);

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

enter image description here

jboi

Two things:

  • The way I get this kind of backgrounds is to add stacked bar series with one value on the x-axis and as much values on the y-axis as you need. Than render this stacked bar first and let the real bar chart render on top of it. The other way, to set the background color transparent and have a background drawable behind the chart, is difficult. I tried that but I didn't find a way to synchronize the background with the value in the chart (basically I gave up on this and was succesful with the `Background-series").

  • Eliminate the dash: My idea was, to set the dash to an transparent color. But the color depends in AChartEngine on the color of the labels, not - as one might expect - on the color of the axis. So, without rewriting parts of the Renderer it's not possible in this version (currently 1.1) to eliminate the dash on a value.

EDIT: Example for first point.

Here's some short code to produce the background for itself.

public Intent execute(Context context) {
    // The demo source counts series by number of titles here
    String[] titles = new String[] { "The Background", "", "" };

    // "stacked" values. The highest first. It's printed first.
    List<double[]> values = new ArrayList<double[]>();
    values.add(new double[] { 6d, 6d, 6d, 6d, 6d, 6d, 6d });
    values.add(new double[] { 4d, 4d, 4d, 4d, 4d, 4d, 4d });
    values.add(new double[] { 2d, 2d, 2d, 2d, 2d, 2d, 2d });

    // Background colors. Here blue and white to actually differentiate it
    int[] colors = new int[] { Color.BLUE, Color.WHITE, Color.BLUE};

   // Some settings for the renderer. Change as necessary
    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
    setChartSettings(
        renderer, "", "", "", 1d, 7d, 0d, 6d,
        Color.TRANSPARENT, Color.TRANSPARENT);
    renderer.setXLabels(0);
    renderer.setYLabels(0);
    renderer.setPanEnabled(false, false);
    renderer.setZoomEnabled(false);
    renderer.setBarSpacing(0d);
    renderer.setShowLegend(false);

    // done. start activity with given intent
    return ChartFactory.getBarChartIntent(
        context, buildBarDataset(titles, values), renderer, Type.STACKED);
}
  • I've used the demo source to have the code here as short as possible. The methods I use are buildBarRenderer, buildBarDataset and setChartSettings. You can find them all in the AbstractDemoChart class.

    • The min and max values are hardcoded to 1-7 (for weekdays) and 0-6 for y-values. Also, the settings are just the keep away all decorations. In your case you set them as you've shown in your example.

    • As aChartEngine has no real concept of stacked bars - it just draws the bars behind each other, so that the one with the highest value is seen above the others - you just need to add your values now at the end of the values list as new series.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I set background color for the space between two x grid in AchartEngine

From Dev

How can I set Background Color for a Scene?

From Dev

How can I set Background Color for a Scene?

From Dev

How can I reduce the space between two discrete values in the x-axis?

From Dev

How can i remove space between layout and background?

From Dev

How can I set the background color for a StringTree with VCL Styles?

From Dev

how can i set the background color of the TextBox in an editable ComboBox?

From Dev

how can I set static controls background color programmatically

From Dev

How can I add a space in between two outputs?

From Dev

How can I create a space between two components in a JPanel

From Dev

How can I remove the white space between two tables?

From Dev

How can I add space in PHP between two outputs

From Dev

How can I create a space between two components in a JPanel

From Dev

How can I remove the white space between two tables?

From Dev

How can I reduce the space between two elements in CSS?

From Dev

how can i make space between two fixed navbar with the container

From Dev

How can I remove a space only if it occurs in between two numbers?

From Dev

How can I set a minimum amount of space between flexbox items?

From Dev

Can I set the background color of the ABAP editor?

From Dev

How can I set title color for a disabled NSButton?(OS X)

From Dev

How to set space between two cells

From Dev

How to set space between two flexbox

From Dev

Space between two background images

From Dev

How to set different background-color to rows dinamically in Gijgo Grid?

From Dev

How can I share my clipboard between two X servers?

From Dev

How can I share my clipboard between two X servers?

From Dev

How can i get x or y between two points?

From Dev

How can I change background color for the desktop?

From Dev

How can I change background color for the desktop?

Related Related

  1. 1

    How can I set background color for the space between two x grid in AchartEngine

  2. 2

    How can I set Background Color for a Scene?

  3. 3

    How can I set Background Color for a Scene?

  4. 4

    How can I reduce the space between two discrete values in the x-axis?

  5. 5

    How can i remove space between layout and background?

  6. 6

    How can I set the background color for a StringTree with VCL Styles?

  7. 7

    how can i set the background color of the TextBox in an editable ComboBox?

  8. 8

    how can I set static controls background color programmatically

  9. 9

    How can I add a space in between two outputs?

  10. 10

    How can I create a space between two components in a JPanel

  11. 11

    How can I remove the white space between two tables?

  12. 12

    How can I add space in PHP between two outputs

  13. 13

    How can I create a space between two components in a JPanel

  14. 14

    How can I remove the white space between two tables?

  15. 15

    How can I reduce the space between two elements in CSS?

  16. 16

    how can i make space between two fixed navbar with the container

  17. 17

    How can I remove a space only if it occurs in between two numbers?

  18. 18

    How can I set a minimum amount of space between flexbox items?

  19. 19

    Can I set the background color of the ABAP editor?

  20. 20

    How can I set title color for a disabled NSButton?(OS X)

  21. 21

    How to set space between two cells

  22. 22

    How to set space between two flexbox

  23. 23

    Space between two background images

  24. 24

    How to set different background-color to rows dinamically in Gijgo Grid?

  25. 25

    How can I share my clipboard between two X servers?

  26. 26

    How can I share my clipboard between two X servers?

  27. 27

    How can i get x or y between two points?

  28. 28

    How can I change background color for the desktop?

  29. 29

    How can I change background color for the desktop?

HotTag

Archive