private XYMultipleSeriesRenderer getCostsRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    int renderCount = 0; // which color to use for the current render
    for (Calculator curCalculator : calculatorList) {
      XYSeriesRenderer existingCosts = new XYSeriesRenderer();
      existingCosts.setColor(COLOR_LIST[renderCount]);
      existingCosts.setPointStyle(PointStyle.POINT);
      existingCosts.setLineWidth(3.0f);
      renderer.addSeriesRenderer(existingCosts);

      renderCount = incrementRenderCount(renderCount);

      XYSeriesRenderer solarCosts = new XYSeriesRenderer();
      solarCosts.setColor(COLOR_LIST[renderCount]);
      solarCosts.setPointStyle(PointStyle.POINT);
      solarCosts.setLineWidth(3.0f);
      renderer.addSeriesRenderer(solarCosts);

      renderCount = incrementRenderCount(renderCount);
    }

    renderer.setAxesColor(Color.BLACK);
    renderer.setLabelsColor(Color.BLACK);
    renderer.setXLabelsColor(Color.BLACK);
    renderer.setYLabelsColor(0, Color.BLACK);
    renderer.setMarginsColor(Color.WHITE);
    renderer.setYLabels(
        16); // Needs to be about double what we want - not sure why...it works as expected on the
             // other graph - TP
    renderer.setXTitle("Year");
    renderer.setYTitle("Annual Electricity Cost ($)");
    renderer.setPanEnabled(false); // don't think this is working as expected
    renderer.setZoomEnabled(false); // don't think this is working as expected
    return renderer;
  }
Beispiel #2
0
  protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
    renderer.setAxisTitleTextSize(16);
    renderer.setChartTitleTextSize(20);
    renderer.setLabelsTextSize(15);
    renderer.setLegendTextSize(15);
    renderer.setPointSize(5f);
    renderer.setYLabels(5);
    renderer.setMargins(new int[] {30, 15, 30, 15});
    int length = colors.length;
    for (int i = 0; i < length; i++) {
      XYSeriesRenderer r = new XYSeriesRenderer();
      r.setColor(colors[i]);
      r.setPointStyle(styles[i]);
      renderer.addSeriesRenderer(r);
    }
    renderer.setClickEnabled(true);
    renderer.setSelectableBuffer(100);
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.argb(100, 50, 50, 50));

    renderer.setAxesColor(Color.LTGRAY);
    renderer.setZoomButtonsVisible(true);
    renderer.setPointSize(10);

    renderer.setChartTitle("Fuel Efficiency and Cost");
    // mRenderer.setXTitle("Date");

    renderer.setShowGrid(true);
    renderer.setXLabelsAlign(Align.RIGHT);
    renderer.setYLabelsAlign(Align.LEFT);

    renderer.setYTitle(cunits, 0);

    renderer.setLabelsColor(Color.WHITE);
    renderer.setXLabelsColor(Color.GREEN);
    renderer.setYLabelsColor(0, colors[0]);
    renderer.setYLabelsColor(1, colors[1]);

    renderer.setYTitle(dunits, 1);
    renderer.setYAxisAlign(Align.RIGHT, 1);
    renderer.setYLabelsAlign(Align.RIGHT, 1);
  }
  @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);
  }
Beispiel #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;
  }
  public void AChart() {
    //		mDataset = new XYMultipleSeriesDataset();
    //		mRenderer = new XYMultipleSeriesRenderer();
    //		mCurrentRenderer = new XYSeriesRenderer();

    mRenderer.setApplyBackgroundColor(true); // 设置是否显示背景色
    mRenderer.setBackgroundColor(Color.argb(50, 214, 241, 255)); // 设置背景色

    mRenderer.setAxisTitleTextSize(32); // 设置轴标题文字的大小
    mRenderer.setAxesColor(Color.BLACK); // 坐标轴颜色

    mRenderer.setChartTitleTextSize(32); // ?设置整个图表标题文字大小
    mRenderer.setChartTitle(appState.userID);

    mRenderer.setXLabelsColor(Color.BLACK); // 设置X轴刻度颜色
    mRenderer.setYLabelsColor(0, Color.BLACK); // 设置Y轴刻度颜色	
    mRenderer.setLabelsTextSize(32); // 设置刻度显示文字的大小(XY轴都会被设置)

    mRenderer.setLegendTextSize(32); // 图例文字大小

    mRenderer.setMargins(new int[] {30, 30, 50, 10}); // 设置图表的外边框(上/左/下/右)
    mRenderer.setMarginsColor(Color.argb(50, 214, 241, 255)); // 边框颜色

    mRenderer.setZoomButtonsVisible(false); // 是否显示放大缩小按钮

    mRenderer.setShowGrid(true); // 显示网格

    mRenderer.setPointSize(10); // 设置点的大小(图上显示的点的大小和图例中点的大小都会被设置)

    String seriesTitle = "I-Sleep";

    if (mDataset.getSeriesCount() > 0) {
      mDataset.removeSeries(0);
      mRenderer.removeAllRenderers();
    }

    XYSeries series = new XYSeries(seriesTitle); // 定义XYSeries
    mDataset.addSeries(series); // 在XYMultipleSeriesDataset中添加XYSeries
    mCurrentSeries = series; // 设置当前需要操作的XYSeries  \	

    addPoint();

    XYSeriesRenderer renderer = new XYSeriesRenderer(); // 定义XYSeriesRenderer
    mRenderer.addSeriesRenderer(renderer); // 将单个XYSeriesRenderer增加到XYMultipleSeriesRenderer
    renderer.setPointStyle(PointStyle.CIRCLE); // 点的类型是圆形
    renderer.setFillPoints(true); // 设置点是否实心
    renderer.setColor(Color.BLUE); // 折线颜色

    mCurrentRenderer = renderer;

    //	    if (mChartView == null) {
    if (layout != null) {
      layout.removeAllViews();
    }
    layout = (LinearLayout) getActivity().findViewById(R.id.chart);
    mChartView =
        ChartFactory.getLineChartView(getActivity().getApplicationContext(), mDataset, mRenderer);
    mRenderer.setClickEnabled(true); // 设置图表是否允许点击
    mRenderer.setSelectableBuffer(100); // 设置点的缓冲半径值(在某点附件点击时,多大范围内都算点击这个点)
    mChartView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            // 这段代码处理点击一个点后,获得所点击的点在哪个序列中以及点的坐标.
            // -->start
            SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
            double[] xy = mChartView.toRealPoint(0);
            if (seriesSelection == null) {
              //	              Toast.makeText(getActivity().getApplicationContext(), "No chart
              // element was clicked", Toast.LENGTH_SHORT).show();
            } else {
              //	              Toast.makeText(getActivity().getApplicationContext(),
              //	                  "Chart element in series index " +
              // seriesSelection.getSeriesIndex()
              //	                      + " data point index " + seriesSelection.getPointIndex() + "
              // was clicked"
              //	                      + " closest point value X=" + seriesSelection.getXValue() +
              // ", Y=" + seriesSelection.getValue()
              //	                      + " clicked point value X=" + (float) xy[0] + ", Y=" +
              // (float) xy[1], Toast.LENGTH_SHORT).show();

              //	              tv_graphdata.setText("X = " +
              // String.valueOf(seriesSelection.getXValue()) +
              //	            		  " Y = " + String.valueOf(seriesSelection.getValue()));
              tv_graphdata.setText(
                  appState.userID
                      + "  "
                      + appState.userName
                      + "  "
                      + "Duration:"
                      + duration[(int) seriesSelection.getXValue() - 1]
                      + "hours\n"
                      + "Starttime:"
                      + starttime[(int) seriesSelection.getXValue() - 1]
                      + "\n"
                      + "Endtime:"
                      + endtime[(int) seriesSelection.getXValue() - 1]);
              tv_graphdata.setTextSize(16);

              // 播放音频
              if (!"".equals(recfile[(int) seriesSelection.getXValue() - 1])) {
                playRecWithSysPlayer(recfile[(int) seriesSelection.getXValue() - 1]);
              } else {
                Toast.makeText(getActivity(), "There's no record.", Toast.LENGTH_LONG).show();
              }
            }
            // -->end
          }
        });
    mChartView.setOnLongClickListener(
        new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
            SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
            if (seriesSelection == null) {
              //	              Toast.makeText(XYChartBuilder.this, "No chart element was long
              // pressed",Toast.LENGTH_SHORT);
              return false; // no chart element was long pressed, so let something
              // else handle the event
            } else {
              //	              Toast.makeText(XYChartBuilder.this, "Chart element in series index "
              //	                  + seriesSelection.getSeriesIndex() + " data point index "
              //	                  + seriesSelection.getPointIndex() + " was long pressed",
              // Toast.LENGTH_SHORT);
              return true; // the element was long pressed - the event has been
              // handled
            }
          }
        });
    // 这段代码处理放大缩小
    // -->start
    mChartView.addZoomListener(
        new ZoomListener() {
          public void zoomApplied(ZoomEvent e) {
            String type = "out";
            if (e.isZoomIn()) {
              type = "in";
            }
            System.out.println("Zoom " + type + " rate " + e.getZoomRate());
          }

          public void zoomReset() {
            System.out.println("Reset");
          }
        },
        true,
        true);
    // -->end
    // 设置拖动图表时后台打印出图表坐标的最大最小值.
    mChartView.addPanListener(
        new PanListener() {
          public void panApplied() {
            System.out.println(
                "New X range=["
                    + mRenderer.getXAxisMin()
                    + ", "
                    + mRenderer.getXAxisMax()
                    + "], Y range=["
                    + mRenderer.getYAxisMax()
                    + ", "
                    + mRenderer.getYAxisMax()
                    + "]");
          }
        });
    layout.addView(
        mChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    boolean enabled = mDataset.getSeriesCount() > 0;
    //	      } else {
    //	        mChartView.repaint();
    //	      }
  }