Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

android - Different color bars using multiple series

I am totally new to Android and i am trying to create a bar chart using AChartEngine, it has 3 bars and each bar should be different color. I tried using multiple series but can't make my x-axis values align with bars

protected void onResume() {
    super.onResume();
     layout = (LinearLayout) findViewById(R.id.chart);
     if (mChartView == null) 
     {
             PeakSeries = new XYSeries("Income");
             ForecastPeakSeries = new XYSeries("Expense");
             ForecastSeries = new XYSeries("Expense");

            int[] x = { 1,2,3};
            int income = 2000;
            int expense =  2700;
            System.out.println("Here");
            Series1.add(x[0],income);
            Series2.add(x[1],expense);
            Series3.add(x[2],expense);

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

            dataset.addSeries(Series1);
            dataset.addSeries(Series2);
            dataset.addSeries(Series3);


            XYSeriesRenderer Renderer1 = new XYSeriesRenderer();
            Renderer1 .setColor(Color.RED);
            Renderer1 .setChartValuesTextSize(18);
            Renderer1 .setFillPoints(true);
            Renderer1 .setDisplayChartValues(true);

            // Creating XYSeriesRenderer to customize expenseSeries
            XYSeriesRenderer Renderer2 = new XYSeriesRenderer();
            Renderer2 .setColor(Color.YELLOW);
            Renderer2 .setChartValuesTextSize(18);
            Renderer2 .setDisplayChartValues(true);

            XYSeriesRenderer Renderer3 = new XYSeriesRenderer();
            Renderer3 .setColor(Color.CYAN);
            Renderer3 .setChartValuesTextSize(18);
            Renderer3 .setFillPoints(true);
            Renderer3 .setChartValuesTextAlign(Align.CENTER);
            Renderer3 .setDisplayChartValues(true);

            // Creating a XYMultipleSeriesRenderer to customize the whole chart
                double[] range = {0,5,0,5};
            multiRenderer.setMargins(new int[] {50, 50, 50, 50});
            multiRenderer.setAxisTitleTextSize(16);
            multiRenderer.setChartTitleTextSize(30);
            multiRenderer.setLabelsTextSize(15);
            multiRenderer.setLegendTextSize(20);
            multiRenderer.setLegendHeight(40);
            multiRenderer.setBarSpacing(-0.7);
            multiRenderer.setAxesColor(Color.WHITE);
            multiRenderer.setInitialRange(range, 1);
            multiRenderer.setXAxisMin(0.4);
            multiRenderer.setXAxisMax(4);
            multiRenderer.setYAxisMax(5000);
            multiRenderer.setYAxisMin(0);
            multiRenderer.setXLabelsAlign(Align.RIGHT);
            multiRenderer.setYLabelsAlign(Align.RIGHT);
            //multiRenderer.s(Align.LEFT);
            multiRenderer.setPanEnabled(false, false);
            multiRenderer.setZoomEnabled(true, true);

            multiRenderer.setChartTitle("7 Day Outlook - Maxiumum Demand");
            multiRenderer.setXTitle("Date");
            multiRenderer.setYTitle("MW");

            multiRenderer.addSeriesRenderer(Renderer1 );
            multiRenderer.addSeriesRenderer(Renderer2 );
            multiRenderer.addSeriesRenderer(Renderer3 );

            final GraphicalView mChartView = ChartFactory.getBarChartView(TestSevenDay1.this, dataset, multiRenderer,Type.DEFAULT);
            layout.addView(mChartView);

     }
     else
     {
         mChartView.repaint();
     }
}

Any help is greatly appreciated. enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I handled it by setting the Type from Type.Default to Type.Stacked in the getBarChartView.

ChartFactory.getBarChartView(TestSevenDay1.this, dataset, multiRenderer, Type.STACKED); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...