com.joker.graphs.JokerStatisticsUserRegist.java Source code

Java tutorial

Introduction

Here is the source code for com.joker.graphs.JokerStatisticsUserRegist.java

Source

package com.joker.graphs;

/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.gen.joker.R;
import com.joker.net.ThreadPoolUtils;
import com.joker.thread.HttpGetThread;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 *  
 * 
 * @author Administrator
 * 
 */
public class JokerStatisticsUserRegist extends Activity {

    // 
    String[] titles;
    List<double[]> x;
    List<double[]> y;
    private static int years = 1;
    private int year;// 
    private int max_year = 2014;// 
    private int min_year = 2011;// 
    private int max_value;// 12y

    // 
    private GraphicalView mChartView;
    private Button next;
    private Button last;
    private TextView tv_year;
    LinearLayout layout;

    // 
    private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN };
    // 
    private static PointStyle[] STYLES = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, PointStyle.POINT,
            PointStyle.SQUARE, PointStyle.TRIANGLE };
    // 
    private XYMultipleSeriesDataset mSeriesDataset = new XYMultipleSeriesDataset();// 
    private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();// 
    private ImageView login_close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        // 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.lay_statistics_user_regist);
        last = (Button) findViewById(R.id.id_statistics_user_regist_last);
        next = (Button) findViewById(R.id.id_statistics_user_regist_next);
        tv_year = (TextView) findViewById(R.id.id_statistics_user_regist_year);

        layout = (LinearLayout) findViewById(R.id.lay_statistics_chart_user_regist);
        //layoutlayout
        setListener();

        /**
         * 1maxyearminyear 212 3series 4
         */

        ThreadPoolUtils.execute(new HttpGetThread(handler1, "statistics.php?action=rangeuserregist"));
        //
        login_close = (ImageView) findViewById(R.id.loginClose);
        login_close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    /**
     * 
     */
    private void initChart() {
        // 
        titles = new String[] { year + "" };
        // 
        mSeriesDataset = buildDataset(x, y);
        //
        setChartStyle();
        //
        mChartView = ChartFactory.getLineChartView(this, mSeriesDataset, mRenderer);
        mRenderer.setClickEnabled(true);
        //
        layout.addView(mChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    }

    //
    private void clearData() {
        if (mChartView != null) {
            x.clear();
            y.clear();
            x = new ArrayList<double[]>();
            y = new ArrayList<double[]>();
            mSeriesDataset.clear();
            mSeriesDataset = new XYMultipleSeriesDataset();
            mRenderer = new XYMultipleSeriesRenderer();
        }
    }

    /**
     * 
     */
    private void setListener() {
        // +
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (year == max_year) {
                    System.out.println("" + max_year + "");
                    Toast.makeText(getApplicationContext(), "" + max_year + "", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    System.out.println("" + year + "     + ");
                    clearData();
                    year++;
                    tv_year.setText(year + "");
                    ThreadPoolUtils.execute(
                            new HttpGetThread(handler2, "statistics.php?action=userregistcount&year=" + year));
                }
            }
        });
        // -
        last.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (year == min_year) {
                    System.out.println("" + min_year + " ");
                    Toast.makeText(getApplicationContext(), "" + min_year + " ", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    System.out.println("" + year + "     - ");
                    clearData();

                    year--;
                    tv_year.setText(year + "");
                    ThreadPoolUtils.execute(
                            new HttpGetThread(handler2, "statistics.php?action=userregistcount&year=" + year));
                }
            }
        });
    }

    // xy 
    protected XYMultipleSeriesDataset buildDataset(List xValues, List yValues) {

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int length = titles.length; // 
        for (int i = 0; i < length; i++) {// i
            XYSeries series = new XYSeries(titles[i]); // 
            double[] xV = (double[]) xValues.get(i); // i
            double[] yV = (double[]) yValues.get(i);
            int seriesLength = xV.length; // 
            for (int k = 0; k < seriesLength; k++) // 
            {
                series.add(xV[k], yV[k]);
            }
            // 
            dataset.addSeries(series);
        }
        return dataset;
    }

    /**
     * 
     * 
     * @param colors
     * @param styles
     * @param fill
     * @return
     */
    protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles, boolean fill) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        int length = colors.length;
        for (int i = 0; i < length; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            r.setLineWidth(2);
            r.setPointStyle(styles[i]);
            r.setFillPoints(fill);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }

    /**
     * 
     */
    protected void setChartStyle() {
        // 
        int[] colors = new int[years];
        PointStyle[] styles = new PointStyle[years];
        for (int i = 0; i < years; i++) {
            colors[i] = COLORS[i % COLORS.length];
            styles[i] = STYLES[i % STYLES.length];
        }
        // 
        mRenderer = buildRenderer(colors, styles, false);
        setChartSettings(mRenderer, "", "X Month", "Y Count", 1, 12, 0, max_value, Color.LTGRAY,
                Color.LTGRAY);

        // 
        int length = mRenderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) mRenderer.getSeriesRendererAt(i)).setFillPoints(true);
        }
    }

    /**
     * 
     * 
     * @param renderer
     * @param title
     * @param xTitle
     * @param yTitle
     * @param xMin
     * @param xMax
     * @param yMin
     * @param yMax
     * @param axesColor
     * @param labelsColor
     */
    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.setAxisTitleTextSize(25);
        renderer.setChartTitleTextSize(25);
        renderer.setChartValuesTextSize(20);
        renderer.setLabelsTextSize(30);
        renderer.setLegendTextSize(20);
        renderer.setPointSize(10);
        renderer.setZoomEnabled(true);
        renderer.setZoomButtonsVisible(true);

        renderer.setXLabels(12);
        renderer.setYLabels(10);
        renderer.setShowGrid(true);
        renderer.setXLabelsAlign(Align.RIGHT);
        renderer.setYLabelsAlign(Align.RIGHT);
        renderer.setZoomButtonsVisible(true);
        renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
        renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
        renderer.setPanEnabled(false);// 
        renderer.setMargins(new int[] { 20, 30, 100, 0 });

        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }

    // handler
    Handler handler1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            if (msg.what == 404) {
                Toast.makeText(getApplicationContext(), "", 1).show();
            } else if (msg.what == 100) {
                Toast.makeText(getApplicationContext(), "", 1).show();
            } else if (msg.what == 200) {
                String result = (String) msg.obj;
                if (result != null) {
                    System.out.println("     " + result);
                    String[] arr = result.split(",");
                    min_year = Integer.parseInt(arr[0]);
                    max_year = Integer.parseInt(arr[1]);
                    year = max_year;
                    tv_year.setText(year + "");
                    // 
                    System.out.println("year=" + year);
                    System.out.println("min_year=" + min_year + "max_year=" + max_year);

                    ThreadPoolUtils.execute(
                            new HttpGetThread(handler2, "statistics.php?action=userregistcount&year=" + year));

                }
            }
        };
    };

    // handler
    Handler handler2 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            if (msg.what == 404) {
                Toast.makeText(getApplicationContext(), "", 1).show();
            } else if (msg.what == 100) {
                Toast.makeText(getApplicationContext(), "", 1).show();
            } else if (msg.what == 200) {
                String result = (String) msg.obj;
                if (result != null) {
                    System.out.println(
                            "" + year + "     " + result);
                    try {
                        JSONArray jsoncounts = new JSONArray(result);
                        JSONObject jsontemp = jsoncounts.getJSONObject(0);
                        int[] Mon = new int[12];
                        Mon[0] = jsontemp.getInt("January");
                        Mon[1] = jsontemp.getInt("February");
                        Mon[2] = jsontemp.getInt("March");
                        Mon[3] = jsontemp.getInt("April");
                        Mon[4] = jsontemp.getInt("May");
                        Mon[5] = jsontemp.getInt("June");
                        Mon[6] = jsontemp.getInt("July");
                        Mon[7] = jsontemp.getInt("August");
                        Mon[8] = jsontemp.getInt("September");
                        Mon[9] = jsontemp.getInt("October");
                        Mon[10] = jsontemp.getInt("November");
                        Mon[11] = jsontemp.getInt("December");

                        System.out.println(year + " ");
                        max_value = 0;
                        for (int m : Mon) {
                            if (m > max_value)
                                max_value = m;
                            System.out.println(m);
                        }
                        System.out.println("" + max_value);
                        max_value *= 1.1;
                        System.out.println("*1.1=" + max_value);

                        x = new ArrayList<double[]>();
                        y = new ArrayList<double[]>();
                        x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
                        y.add(new double[] { Mon[0], Mon[1], Mon[2], Mon[3], Mon[4], Mon[5], Mon[6], Mon[7], Mon[8],
                                Mon[9], Mon[10], Mon[11] });
                        Toast.makeText(getApplicationContext(), year + "", Toast.LENGTH_SHORT).show();

                        layout.removeAllViews();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
            initChart();
        };
    };
}