@Override
  public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    mDataCache = DataCache.getInstance();

    mDataset = new XYMultipleSeriesDataset();
    mRenderer = new XYMultipleSeriesRenderer();
    // Renderer
    // Basic
    mRenderer.setMargins(new int[] {100, 150, 30, 100});
    mRenderer.setChartTitleTextSize(20);
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(0xffE8E8E7);
    mRenderer.setMarginsColor(0xffE8E8E7);
    mRenderer.setLegendTextSize(15);
    mRenderer.setPointSize(5);
    mRenderer.setShowLegend(true);
    mRenderer.setClickEnabled(false);
    // Axes
    mRenderer.setAxisTitleTextSize(50);
    mRenderer.setBarSpacing(50);
    mRenderer.setYTitle("Light");
    // mRenderer.setAxesColor(Color.BLACK);
    // Labels
    // mRenderer.setLabelsColor(Color.WHITE);
    mRenderer.setLabelsTextSize(35);
    mRenderer.setXLabels(0);
    mRenderer.setYLabels(10);
    mRenderer.setYAxisMin(0);
    mRenderer.setYAxisMax(1000);
    mRenderer.setXLabelsColor(Color.BLACK);
    mRenderer.setYLabelsColor(0, Color.BLACK);
    mRenderer.setXLabelsAngle(-45f);
    mRenderer.setXLabelsAlign(Align.CENTER);
    mRenderer.setYLabelsAlign(Align.RIGHT);

    mRenderer.setPanEnabled(false, false);
    mRenderer.setZoomEnabled(false, false);
  }
  private void setupTopChart() {

    // Creating an  XYSeries for Sound
    visitsSeries = new XYSeries("");
    secondSeries = new XYSeries("");

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

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

    // Adding datasets together
    multiDateSet = new XYMultipleSeriesDataset();
    multiDateSet.addSeries(visitsSeries);
    multiDateSet.addSeries(secondSeries);

    // Creating XYSeriesRenderer to customize visitsSeries
    visitsRenderer = new XYSeriesRenderer();
    visitsRenderer.setColor(getResources().getColor(R.color.colorAccent));
    visitsRenderer.setPointStyle(PointStyle.CIRCLE);
    visitsRenderer.setFillPoints(true);
    visitsRenderer.setDisplayChartValues(false);
    visitsRenderer.setLineWidth(10);

    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    multiRenderer = new XYMultipleSeriesRenderer();

    multiRenderer.setMargins(new int[] {0, 0, 0, 0});
    multiRenderer.setPanEnabled(false);
    multiRenderer.setShowLegend(false);
    multiRenderer.setChartTitle("");
    // multiRenderer.setFitLegend(true);

    // Eliminate the S***e
    multiRenderer.setApplyBackgroundColor(true);
    multiRenderer.setXTitle("");
    multiRenderer.setShowLabels(false);
    multiRenderer.setShowAxes(false);

    multiRenderer.setMarginsColor(Color.TRANSPARENT);
    multiRenderer.setBackgroundColor(Color.TRANSPARENT);
    multiRenderer.setXLabelsColor(Color.TRANSPARENT);
    multiRenderer.setYLabelsColor(0, Color.TRANSPARENT);
    multiRenderer.setGridColor(Color.TRANSPARENT);
    multiRenderer.setXLabelsPadding(0);
    multiRenderer.setYLabelsPadding(0);
    multiRenderer.setBarWidth(100);

    // Establish Beginning Dimensions
    multiRenderer.setXAxisMin(0);
    multiRenderer.setXAxisMax(10);
    multiRenderer.setYAxisMin(0);
    multiRenderer.setYAxisMax(0);

    // Make the bar graphs touching
    multiRenderer.setBarSpacing(0);

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

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

    mChart =
        (GraphicalView)
            ChartFactory.getBarChartView(
                getBaseContext(),
                multiDateSet,
                multiRenderer,
                org.achartengine.chart.BarChart.Type.DEFAULT);

    // Adding the Line Chart to the LinearLayout
    chartContainer.addView(mChart);
  }
  @Override
  protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    try {
      elec_thread.join();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    int k = CH.getNum_of_msg(); // 보여줄 데이터 개수

    Log.d("get", "onStart시작");

    int seriesLength = 0;
    double max = 0;

    recv_packet = new Packet[k];

    for (int i = 0; i < k; i++) {
      recv_packet[i] = CH.get_Packet(i);
    }
    Log.d("get", "recv_packet배열 저장");

    List<Float[]> values = new ArrayList<Float[]>();
    Float[][] a = new Float[4][k];
    int[][] c = new int[4][k];

    for (int j = 0; j < k; j++) {
      a[0][j] = Float.parseFloat(recv_packet[j].getData(3));
      a[1][j] = Float.parseFloat(recv_packet[j].getData(4));
      a[2][j] = Float.parseFloat(recv_packet[j].getData(5));
      a[3][j] = Float.parseFloat(recv_packet[j].getData(7));
      c[0][j] = (int) (a[0][j] * 10);
      c[1][j] = (int) (a[1][j] * 10);
      c[2][j] = (int) (a[2][j] * 10);
      c[3][j] = (int) (a[3][j] * 10);
      a[0][j] = ((float) c[0][j] / (float) 10);
      a[1][j] = ((float) c[1][j] / (float) 10);
      a[2][j] = ((float) c[2][j] / (float) 10);
      a[3][j] = ((float) c[3][j] / (float) 10);

      // a[3][j] = (float) Math.round(Float.parseFloat(recv_packet[j].getData(7))*10000)/10000;
      // values.add(a);// 이번달
    }
    for (int w = 0; w < 4; w++) {
      //        a[w] = Math.round(a[w]*10) / 10.0;
      values.add(a[w]);
    }

    Log.d("get", "values배열 저장");
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

    renderer.setChartTitle("과거 사용량과의 비교");
    renderer.setChartTitleTextSize(50);

    String[] titles = new String[] {"3달 전", "2달 전", "1달 전", "이번 달"};

    int[] colors =
        new int[] {
          Color.rgb(255, 90, 217),
          Color.rgb(54, 255, 255),
          Color.rgb(255, 255, 72),
          Color.rgb(83, 255, 76)
        };
    // //////////////////////////////////////////////////////
    //   renderer.setLegendTextSize(35);

    int length = values.size(); // 항목갯수

    Log.d("get", "legnth : " + length);
    for (int i = 0; i < length; i++) {
      SimpleSeriesRenderer r = new SimpleSeriesRenderer();
      r.setColor(colors[i]);
      renderer.addSeriesRenderer(r);
    }

    renderer.setXTitle("제품별");
    renderer.setYTitle("사용량");
    renderer.setAxisTitleTextSize(28);
    renderer.setMarginsColor(Color.WHITE);

    renderer.setLabelsTextSize(20);
    renderer.setLegendHeight(200);
    renderer.setXAxisMin(0.5);
    // renderer.setXAxisMax(length + 0.5); // 장비 갯수 +0.5
    renderer.setYAxisMin(0);

    renderer.setLabelsColor(Color.BLACK);
    renderer.setBackgroundColor(Color.WHITE);

    // 그래프 위에 값 표시하기
    for (int i = 0; i < length; i++) {
      renderer.getSeriesRendererAt(i).setDisplayChartValues(true);
      renderer.getSeriesRendererAt(i).setChartValuesTextAlign(Align.RIGHT);
      renderer.getSeriesRendererAt(i).setChartValuesTextSize(20);
    }
    Log.d("get", "그래프위값 표시하기");

    renderer.setYLabels(5);

    // x,y축 정렬 방향
    renderer.setXLabelsAlign(Align.CENTER);
    renderer.setYLabelsAlign(Align.CENTER);
    renderer.setPanEnabled(true, true); // x,y축 스크롤 여부 on/off
    renderer.setZoomEnabled(true); // zoom기능 on/off
    renderer.setZoomRate(1.0f);
    renderer.setBarSpacing(0.5f);

    Log.d("get", "xy축 정렬 방향");

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    for (int i = 0; i < titles.length; i++) {
      CategorySeries series = new CategorySeries(titles[i]);
      Float[] v = values.get(i);

      Log.d("get", "int i = 0; i < titles.length; i++");

      seriesLength = v.length;
      renderer.setXAxisMax(seriesLength + 0.5);

      for (int q = 0; q < seriesLength; q++) {
        series.add(v[q]);
        if (max < v[q]) {
          max = v[q];
        }
        // Log.d("Test", "series.add : " + v[q]);
        renderer.setXLabels(seriesLength + 1);
      }
      dataset.addSeries(series.toXYSeries());
    }
    Log.d("get", "111111111");
    String[] x_name = new String[seriesLength];
    for (int b = 0; b < k; b++) {
      x_name[b] = recv_packet[b].getData(2);
    }
    Log.d("get", "222222222");
    renderer.setXLabels(0);
    for (int i = 0; i < seriesLength; i++) {
      renderer.addXTextLabel(i + 1, x_name[i]);
    }
    renderer.setYAxisMax(max + (max * 0.1)); // max 찾아서 넣기
    GraphicalView gv = ChartFactory.getBarChartView(this, dataset, renderer, Type.DEFAULT);

    setContentView(gv);
  }
  /**
   * Executes the chart demo.
   *
   * @param context the context
   * @return the built intent
   */
  public Intent execute(Context context) {
    String[] titles = new String[] {"Crete Air Temperature", "Skiathos Air Temperature"};
    List<double[]> x = new ArrayList<double[]>();
    for (int i = 0; i < titles.length; i++) {
      x.add(new double[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
    }
    List<double[]> values = new ArrayList<double[]>();
    values.add(
        new double[] {12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 13.9});
    values.add(new double[] {9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10});
    int[] colors = new int[] {Color.GREEN, Color.rgb(200, 150, 0)};
    PointStyle[] styles = new PointStyle[] {PointStyle.CIRCLE, PointStyle.DIAMOND};
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
    renderer.setPointSize(5.5f);
    int length = renderer.getSeriesRendererCount();

    for (int i = 0; i < length; i++) {
      XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
      r.setLineWidth(5);
      r.setFillPoints(true);
    }
    setChartSettings(
        renderer,
        "Weather data",
        "Month",
        "Temperature",
        0.5,
        12.5,
        0,
        40,
        Color.LTGRAY,
        Color.LTGRAY);

    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});

    XYValueSeries sunSeries = new XYValueSeries("Sunshine hours");
    sunSeries.add(1, 35, 4.3);
    sunSeries.add(2, 35, 4.9);
    sunSeries.add(3, 35, 5.9);
    sunSeries.add(4, 35, 8.8);
    sunSeries.add(5, 35, 10.8);
    sunSeries.add(6, 35, 11.9);
    sunSeries.add(7, 35, 13.6);
    sunSeries.add(8, 35, 12.8);
    sunSeries.add(9, 35, 11.4);
    sunSeries.add(10, 35, 9.5);
    sunSeries.add(11, 35, 7.5);
    sunSeries.add(12, 35, 5.5);
    XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
    lightRenderer.setColor(Color.YELLOW);

    XYSeries waterSeries = new XYSeries("Water Temperature");
    waterSeries.add(1, 16);
    waterSeries.add(2, 15);
    waterSeries.add(3, 16);
    waterSeries.add(4, 17);
    waterSeries.add(5, 20);
    waterSeries.add(6, 23);
    waterSeries.add(7, 25);
    waterSeries.add(8, 25.5);
    waterSeries.add(9, 26.5);
    waterSeries.add(10, 24);
    waterSeries.add(11, 22);
    waterSeries.add(12, 18);
    renderer.setBarSpacing(0.5);
    XYSeriesRenderer waterRenderer = new XYSeriesRenderer();
    waterRenderer.setColor(Color.argb(250, 0, 210, 250));

    XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
    dataset.addSeries(0, sunSeries);
    dataset.addSeries(0, waterSeries);
    renderer.addSeriesRenderer(0, lightRenderer);
    renderer.addSeriesRenderer(0, waterRenderer);
    waterRenderer.setDisplayChartValues(true);
    waterRenderer.setChartValuesTextSize(10);

    String[] types =
        new String[] {BarChart.TYPE, BubbleChart.TYPE, LineChart.TYPE, CubicLineChart.TYPE};
    Intent intent =
        ChartFactory.getCombinedXYChartIntent(
            context, dataset, renderer, types, "Weather parameters");
    return intent;
  }
Exemple #5
0
  public static View genBarChart(final Activity activity, final BarChartData data) {
    if (data == null || data.getData() == null) return null;
    Resources resources = activity.getResources();
    final String[] axis_x_string = resources.getStringArray(R.array.week);
    final XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset();
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.WHITE);
    renderer.setMarginsColor(Color.WHITE);
    renderer.setTextTypeface(null, Typeface.NORMAL);

    renderer.setShowGrid(true);
    renderer.setGridColor(resources.getColor(R.color.gray3));

    renderer.setLabelsColor(Color.BLACK);
    renderer.setAxesColor(resources.getColor(R.color.gray1));
    renderer.setBarSpacing(0.5);

    renderer.setXTitle("");
    renderer.setYTitle("");
    renderer.setXLabelsColor(Color.BLACK);
    renderer.setLabelsTextSize(
        activity.getResources().getDimensionPixelSize(R.dimen.ov_statistics_axis_text_size));
    renderer.setYLabelsColor(0, Color.BLACK);
    renderer.setXLabelsPadding(resources.getDimension(R.dimen.ov_statistics_x_axis_padding));
    renderer.setYLabelsPadding(resources.getDimension(R.dimen.ov_statistics_x_axis_padding));
    renderer.setXLabelsAlign(Paint.Align.CENTER);
    renderer.setYLabelsAlign(Paint.Align.CENTER);
    renderer.setXLabelsAngle(0);
    renderer.setShowTickMarks(false);

    renderer.setXLabels(0);
    renderer.setYAxisMin(0);

    for (int i = 0; i < data.getData().length; i++) {
      final XYSeries series = new XYSeries("");
      dataSet.addSeries(series);
      XYSeriesRenderer yRenderer = new XYSeriesRenderer();
      renderer.addSeriesRenderer(yRenderer);
      yRenderer.setColor(resources.getColor(data.sColorId[i]));
      series.add(0, 0);
      renderer.addXTextLabel(0, "");
      for (int j = 0; j < data.getData()[i].length; j++) {
        renderer.addXTextLabel(j + 1, axis_x_string[j]);
        series.add(j + 1, ((float) data.getData()[i][j]) / 3600000);
      }
      series.add(data.getData()[i].length + 1, 0);
      renderer.addXTextLabel(data.getData()[i].length + 1, "");
    }

    renderer.setZoomEnabled(false);
    renderer.setZoomEnabled(false, false);
    renderer.setClickEnabled(true);
    renderer.setPanEnabled(false);
    renderer.setShowLegend(false);
    try {
      renderer.setDrawGridAfterBar(true);
      renderer.setHighLightRectEnabled(true);
      renderer.setHighLightRectStrokeWidth(
          activity
              .getResources()
              .getDimensionPixelOffset(R.dimen.ov_statistics_highlight_stroke_width));
      renderer.setHighLightRectStrokeColor(activity.getResources().getColor(R.color.gray3));
    } catch (NoSuchMethodError e) {
      e.printStackTrace();
    }
    View view = ChartFactory.getBarChartView(activity, dataSet, renderer, BarChart.Type.DEFAULT);
    view.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (!(v instanceof GraphicalView)) return;
            final GraphicalView view = (GraphicalView) v;
            SeriesSelection selection = view.getCurrentSeriesAndPoint();
            try {
              view.onClick(selection);
            } catch (NoSuchMethodError e) {
              e.printStackTrace();
            }
            if (selection == null) return;
            int idx = selection.getPointIndex() - 1;
            View popupView = activity.getLayoutInflater().inflate(R.layout.bar_chart_popup, null);
            if (data.from.equals(CaseOverviewFragment.class.getSimpleName())
                || data.from.equals(EquipmentOverviewFragment.class.getSimpleName())) {
              ((TextView) popupView.findViewById(R.id.date)).setText(data.getDate()[idx]);
              if (data.getData()[0][idx] >= 0) {
                int hour = (int) data.getData()[0][idx] / 3600000;
                int min = (int) ((data.getData()[0][idx] % 3600000)) / 60000;
                ((TextView) popupView.findViewById(R.id.working_time))
                    .setText(
                        activity.getResources().getString(R.string.statistics_popup_time_finish)
                            + " "
                            + (hour < 10 ? ("0" + hour) : hour)
                            + " : "
                            + (min < 10 ? ("0" + min) : min));
              } else {
                popupView.findViewById(R.id.vg_working_time).setVisibility(View.GONE);
              }
              popupView.findViewById(R.id.vg_overtime).setVisibility(View.GONE);
            } else if (data.from.equals(WorkerOverviewFragment.class.getSimpleName())) {
              ((TextView) popupView.findViewById(R.id.date)).setText(data.getDate()[idx]);
              if (data.getData()[0][idx] >= 0) {
                int hour = (int) data.getData()[0][idx] / 3600000;
                int min = (int) ((data.getData()[0][idx] % 3600000)) / 60000;
                ((TextView) popupView.findViewById(R.id.working_time))
                    .setText(
                        activity.getResources().getString(R.string.statistics_popup_time_work)
                            + " "
                            + (hour < 10 ? ("0" + hour) : hour)
                            + " : "
                            + (min < 10 ? ("0" + min) : min));
              } else {
                popupView.findViewById(R.id.vg_working_time).setVisibility(View.GONE);
              }
              if (data.getData()[1][idx] >= 0) {
                int hour = (int) data.getData()[1][idx] / 3600000;
                int min = (int) ((data.getData()[1][idx] % 3600000)) / 60000;
                ((TextView) popupView.findViewById(R.id.overtime))
                    .setText(
                        activity.getResources().getString(R.string.statistics_popup_time_overtime)
                            + " "
                            + (hour < 10 ? ("0" + hour) : hour)
                            + " : "
                            + (min < 10 ? ("0" + min) : min));
              } else {
                popupView.findViewById(R.id.vg_overtime).setVisibility(View.GONE);
              }
            }
            final PopupWindow popup =
                new PopupWindow(
                    popupView,
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            popup.setBackgroundDrawable(new BitmapDrawable());
            popup.setOutsideTouchable(true);
            popup.setOnDismissListener(
                new PopupWindow.OnDismissListener() {
                  @Override
                  public void onDismiss() {
                    try {
                      view.onClick(null);
                    } catch (NoSuchMethodError e) {
                      e.printStackTrace();
                    }
                  }
                });
            /* calculate popup position */
            popupView.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            int[] dateChooserLocations = new int[2];
            (activity.findViewById(R.id.ov_statistics_week_chooser))
                .getLocationOnScreen(dateChooserLocations);
            int[] statisticsViewLocations = new int[2];
            v.getLocationOnScreen(statisticsViewLocations);
            int[] barChartLocations = new int[2];
            v.getLocationOnScreen(barChartLocations);
            int x_pos =
                barChartLocations[0]
                    + (int) ((XYChart) view.getChart()).getXAxisPos((double) (idx + 1))
                    - popupView.getMeasuredWidth() / 2;
            int y_base =
                dateChooserLocations[1]
                    + (activity.findViewById(R.id.ov_statistics_week_chooser)).getMeasuredHeight();
            int y_pos =
                (statisticsViewLocations[1] - y_base) / 2
                    + y_base
                    - popupView.getMeasuredHeight() / 2;
            popup.showAtLocation(v, Gravity.NO_GRAVITY, x_pos, y_pos);
          }
        });
    return view;
  }