shannah / CN1aChartEngine

A port of Android aChartEngine Library for Codename One
7 stars 6 forks source link

single value #6

Closed ivanilavsky closed 10 years ago

ivanilavsky commented 10 years ago

Hi Steve, I'm using CN1aChartEngine and everything works OK until I need to display bar chart with single value in set(s). Chart (bars) is incorrectly scaled (see attached screenshots) How to scale the chart in this case please ? chart1 chart2

Thanks bye Ivan

shannah commented 10 years ago

Can you post some sample code so I can take a look?

ivanilavsky commented 10 years ago

I cannot attach a file here so I copy/paste the content of sample class (just copy it into BChart.java in package "userclasses" in your project and just let show Form generated by BChart.create() method).

package userclasses;

import java.util.Vector;

import org.achartengine.ChartFactory;
import org.achartengine.compat.Color;
import org.achartengine.compat.Context;
import org.achartengine.compat.Paint;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import com.codename1.ui.Component;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;

public class BChart {

    public Form create() {

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        Vector<Vector<Double>> seriesVector = new Vector<Vector<Double>>();//getChartData().getSeries();
        Vector<Double> serie1 = new Vector<Double>();
        serie1.add(7142.0);
        Vector<Double> serie2 = new Vector<Double>();
        serie2.add(7856.0);
        seriesVector.add(serie1);
        seriesVector.add(serie2);

        Vector<String> xAxis = new Vector<String>();
        xAxis.add("2012.01");

        double ymax = 0.0;
        double ymin = 0.0;

        for (Vector<Double> serie : seriesVector){

            String title = "Title";
            CategorySeries series = new CategorySeries(title);

            int length = serie.size();
            for (int k = 0; k < length; k++) {

                series.add(serie.get(k));
                if (serie.get(k)>ymax){
                    ymax = serie.get(k);
                }
                if (serie.get(k)<ymin){
                    ymin = serie.get(k);
                }

            }
            dataset.addSeries(series.toXYSeries());

        }

        int[] colors = new int[]{-8937781, -11812};

        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
        setChartSettings(renderer, "", "", "", 0.5, xAxis.size()+0.5,
                (ymin<0?ymin*1.25:ymin), ymax*1.05, Color.GRAY, Color.LTGRAY);
        renderer.setBarSpacing(0.5);
        renderer.setXLabels(0);
        renderer.setYLabels(0);
        renderer.setShowLegend(true);

        renderer.setMargins(new int[]{10, 10, 10, 0});
        renderer.setFitLegend(true);                

        renderer.setYLabelsAlign(Paint.Align.LEFT);

        // x labels
        for (int i=0;i<xAxis.size();i++){
             renderer.addXTextLabel(i+1, xAxis.get(i));
        }
        renderer.setLabelsTextSize(12);

        for (int i=0;i<seriesVector.size();i++){
            XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
            r.setDisplayChartValues(true);
            r.setChartValuesTextSize(12);
            r.setChartValuesSpacing(3);
            r.setGradientEnabled(false);
            r.setChartValuesTextAlign(Paint.Align.CENTER);
        }

        Context context = null;
        Component c = ChartFactory.getBarChartView(context, dataset, renderer, org.achartengine.chart.BarChart.Type.DEFAULT).getPeer();

        Form f = new Form();

        f.setLayout(new BorderLayout());
        f.addComponent(BorderLayout.CENTER, c);

        return f;
    }

    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        int length = colors.length;
        for (int i = 0; i < length; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }

    protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
            String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
            int labelsColor) {
        renderer.setChartTitle(title);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }

}

I cannot attach a file here so I copy/paste the content of sample class (just copy it into BChart.java in package "userclasses" in your project and just let show Form generated by BChart.create() method).

package userclasses;

import java.util.Vector;

import org.achartengine.ChartFactory;
import org.achartengine.compat.Color;
import org.achartengine.compat.Context;
import org.achartengine.compat.Paint;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import com.codename1.ui.Component;
import com.codename1.ui.Form;
import com.codename1.ui.layouts.BorderLayout;

public class BChart {

    public Form create() {

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        Vector<Vector<Double>> seriesVector = new Vector<Vector<Double>>();//getChartData().getSeries();
        Vector<Double> serie1 = new Vector<Double>();
        serie1.add(7142.0);
        Vector<Double> serie2 = new Vector<Double>();
        serie2.add(7856.0);
        seriesVector.add(serie1);
        seriesVector.add(serie2);

        Vector<String> xAxis = new Vector<String>();
        xAxis.add("2012.01");

        double ymax = 0.0;
        double ymin = 0.0;

        for (Vector<Double> serie : seriesVector){

            String title = "Title";
            CategorySeries series = new CategorySeries(title);

            int length = serie.size();
            for (int k = 0; k < length; k++) {

                series.add(serie.get(k));
                if (serie.get(k)>ymax){
                    ymax = serie.get(k);
                }
                if (serie.get(k)<ymin){
                    ymin = serie.get(k);
                }

            }
            dataset.addSeries(series.toXYSeries());

        }

        int[] colors = new int[]{-8937781, -11812};

        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
        setChartSettings(renderer, "", "", "", 0.5, xAxis.size()+0.5,
                (ymin<0?ymin*1.25:ymin), ymax*1.05, Color.GRAY, Color.LTGRAY);
        renderer.setBarSpacing(0.5);
        renderer.setXLabels(0);
        renderer.setYLabels(0);
        renderer.setShowLegend(true);

        renderer.setMargins(new int[]{10, 10, 10, 0});
        renderer.setFitLegend(true);                

        renderer.setYLabelsAlign(Paint.Align.LEFT);

        // x labels
        for (int i=0;i<xAxis.size();i++){
             renderer.addXTextLabel(i+1, xAxis.get(i));
        }
        renderer.setLabelsTextSize(12);

        for (int i=0;i<seriesVector.size();i++){
            XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
            r.setDisplayChartValues(true);
            r.setChartValuesTextSize(12);
            r.setChartValuesSpacing(3);
            r.setGradientEnabled(false);
            r.setChartValuesTextAlign(Paint.Align.CENTER);
        }

        Context context = null;
        Component c = ChartFactory.getBarChartView(context, dataset, renderer, org.achartengine.chart.BarChart.Type.DEFAULT).getPeer();

        Form f = new Form();

        f.setLayout(new BorderLayout());
        f.addComponent(BorderLayout.CENTER, c);

        return f;
    }

    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        int length = colors.length;
        for (int i = 0; i < length; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }

    protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
            String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
            int labelsColor) {
        renderer.setChartTitle(title);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }

}
shannah commented 10 years ago

Try explicitly setting the bar width. E.g. renderer.setBarWidth(100);

protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); renderer.setAxisTitleTextSize(16); renderer.setChartTitleTextSize(20); renderer.setLabelsTextSize(15); renderer.setLegendTextSize(15); renderer.setBarWidth(100); int length = colors.length; for (int i = 0; i < length; i++) { XYSeriesRenderer r = new XYSeriesRenderer(); r.setColor(colors[i]); renderer.addSeriesRenderer(r); } return renderer; }

ivanilavsky commented 10 years ago

thank you