private void init(Context context, AttributeSet attrs, int defStyle) { PixelUtils.init(getContext()); layoutManager = new LayoutManager(); titleWidget = new TextLabelWidget( layoutManager, new Size(25, SizeLayoutType.ABSOLUTE, 100, SizeLayoutType.ABSOLUTE), TextOrientationType.HORIZONTAL); titleWidget.position( 0, XLayoutStyle.RELATIVE_TO_CENTER, 0, YLayoutStyle.ABSOLUTE_FROM_TOP, AnchorPosition.TOP_MIDDLE); // initialize attr defaults: titleWidget.getLabelPaint().setTextSize(PixelUtils.spToPix(DEFAULT_TITLE_WIDGET_TEXT_SIZE_SP)); onPreInit(); // make sure the title widget is always the topmost widget: layoutManager.moveToTop(titleWidget); if (context != null && attrs != null) { loadAttrs(attrs, defStyle); } layoutManager.onPostInit(); if (renderMode == RenderMode.USE_BACKGROUND_THREAD) { renderThread = new Thread( new Runnable() { @Override public void run() { keepRunning = true; while (keepRunning) { isIdle = false; synchronized (pingPong) { Canvas c = pingPong.getCanvas(); renderOnCanvas(c); pingPong.swap(); } synchronized (renderSynch) { postInvalidate(); // prevent this thread from becoming an orphan // after the view is destroyed if (keepRunning) { try { renderSynch.wait(); } catch (InterruptedException e) { keepRunning = false; } } } } } }); } }
private void onPlotClicked(PointF point) { // make sure the point lies within the graph area. we use gridrect // because it accounts for margins and padding as well. if (plot.containsPoint(point.x, point.y)) { Number x = plot.getXVal(point); Number y = plot.getYVal(point); selection = null; double xDistance = 0; double yDistance = 0; // find the closest value to the selection: for (SeriesBundle<XYSeries, ? extends XYSeriesFormatter> sfPair : plot.getRegistry().getSeriesAndFormatterList()) { XYSeries series = sfPair.getSeries(); for (int i = 0; i < series.size(); i++) { Number thisX = series.getX(i); Number thisY = series.getY(i); if (thisX != null && thisY != null) { double thisXDistance = Region.measure(x, thisX).doubleValue(); double thisYDistance = Region.measure(y, thisY).doubleValue(); if (selection == null) { selection = new Pair<>(i, series); xDistance = thisXDistance; yDistance = thisYDistance; } else if (thisXDistance < xDistance) { selection = new Pair<>(i, series); xDistance = thisXDistance; yDistance = thisYDistance; } else if (thisXDistance == xDistance && thisYDistance < yDistance && thisY.doubleValue() >= y.doubleValue()) { selection = new Pair<>(i, series); xDistance = thisXDistance; yDistance = thisYDistance; } } } } } else { // if the press was outside the graph area, deselect: selection = null; } if (selection == null) { selectionWidget.setText(NO_SELECTION_TXT); } else { selectionWidget.setText( "Selected: " + selection.second.getTitle() + " Value: " + selection.second.getY(selection.first)); } plot.redraw(); }
/** @param title The title to display on this Plot. */ public void setTitle(String title) { titleWidget.setText(title); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bar_plot_example); // initialize our XYPlot reference: plot = (XYPlot) findViewById(R.id.plot); formatter1 = new MyBarFormatter(Color.rgb(100, 150, 100), Color.LTGRAY); formatter1.setMarginLeft(PixelUtils.dpToPix(1)); formatter1.setMarginRight(PixelUtils.dpToPix(1)); formatter2 = new MyBarFormatter(Color.rgb(100, 100, 150), Color.LTGRAY); formatter2.setMarginLeft(PixelUtils.dpToPix(1)); formatter2.setMarginRight(PixelUtils.dpToPix(1)); selectionFormatter = new MyBarFormatter(Color.YELLOW, Color.WHITE); selectionWidget = new TextLabelWidget( plot.getLayoutManager(), NO_SELECTION_TXT, new Size( PixelUtils.dpToPix(100), SizeMode.ABSOLUTE, PixelUtils.dpToPix(100), SizeMode.ABSOLUTE), TextOrientation.HORIZONTAL); selectionWidget.getLabelPaint().setTextSize(PixelUtils.dpToPix(16)); // add a dark, semi-transparent background to the selection label widget: Paint p = new Paint(); p.setARGB(100, 0, 0, 0); selectionWidget.setBackgroundPaint(p); selectionWidget.position( 0, HorizontalPositioning.RELATIVE_TO_CENTER, PixelUtils.dpToPix(45), VerticalPositioning.ABSOLUTE_FROM_TOP, Anchor.TOP_MIDDLE); selectionWidget.pack(); // reduce the number of range labels plot.setLinesPerRangeLabel(3); plot.setRangeLowerBoundary(0, BoundaryMode.FIXED); plot.setLinesPerDomainLabel(2); // setup checkbox listers: series1CheckBox = (CheckBox) findViewById(R.id.s1CheckBox); series1CheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { onS1CheckBoxClicked(b); } }); series2CheckBox = (CheckBox) findViewById(R.id.s2CheckBox); series2CheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { onS2CheckBoxClicked(b); } }); plot.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { onPlotClicked(new PointF(motionEvent.getX(), motionEvent.getY())); } return true; } }); spRenderStyle = (Spinner) findViewById(R.id.spRenderStyle); ArrayAdapter<BarRenderer.BarOrientation> adapter = new ArrayAdapter<BarRenderer.BarOrientation>( this, android.R.layout.simple_spinner_item, BarRenderer.BarOrientation.values()); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spRenderStyle.setAdapter(adapter); spRenderStyle.setSelection(BarRenderer.BarOrientation.OVERLAID.ordinal()); spRenderStyle.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { updatePlot(); } @Override public void onNothingSelected(AdapterView<?> arg0) {} }); spWidthStyle = (Spinner) findViewById(R.id.spWidthStyle); ArrayAdapter<BarRenderer.BarGroupWidthMode> adapter1 = new ArrayAdapter<BarRenderer.BarGroupWidthMode>( this, android.R.layout.simple_spinner_item, BarRenderer.BarGroupWidthMode.values()); adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spWidthStyle.setAdapter(adapter1); spWidthStyle.setSelection(BarRenderer.BarGroupWidthMode.FIXED_WIDTH.ordinal()); spWidthStyle.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if (BarRenderer.BarGroupWidthMode.FIXED_WIDTH.equals(spWidthStyle.getSelectedItem())) { sbFixedWidth.setVisibility(View.VISIBLE); sbVariableWidth.setVisibility(View.INVISIBLE); } else { sbFixedWidth.setVisibility(View.INVISIBLE); sbVariableWidth.setVisibility(View.VISIBLE); } updatePlot(); } @Override public void onNothingSelected(AdapterView<?> arg0) {} }); spSeriesSize = (Spinner) findViewById(R.id.spSeriesSize); ArrayAdapter<SeriesSize> adapter11 = new ArrayAdapter<SeriesSize>( this, android.R.layout.simple_spinner_item, SeriesSize.values()); adapter11.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spSeriesSize.setAdapter(adapter11); spSeriesSize.setSelection(SeriesSize.TEN.ordinal()); spSeriesSize.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { final SeriesSize selectedSize = (SeriesSize) arg0.getSelectedItem(); switch (selectedSize) { case TEN: series1Numbers = series1Numbers10; series2Numbers = series2Numbers10; break; case TWENTY: series1Numbers = series1Numbers20; series2Numbers = series2Numbers20; break; case SIXTY: series1Numbers = series1Numbers60; series2Numbers = series2Numbers60; break; default: break; } updatePlot(selectedSize); } @Override public void onNothingSelected(AdapterView<?> arg0) {} }); sbFixedWidth = (SeekBar) findViewById(R.id.sbFixed); sbFixedWidth.setProgress(50); sbFixedWidth.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { updatePlot(); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); sbVariableWidth = (SeekBar) findViewById(R.id.sbVariable); sbVariableWidth.setProgress(1); sbVariableWidth.setVisibility(View.INVISIBLE); sbVariableWidth.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { updatePlot(); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); plot.getGraph() .getLineLabelStyle(XYGraphWidget.Edge.BOTTOM) .setFormat( new NumberFormat() { @Override public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) { int year = (int) (value + 0.5d) / 12; int month = (int) ((value + 0.5d) % 12); return new StringBuffer( DateFormatSymbols.getInstance().getShortMonths()[month] + " '0" + year); } @Override public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) { throw new UnsupportedOperationException("Not yet implemented."); } @Override public Number parse(String string, ParsePosition position) { throw new UnsupportedOperationException("Not yet implemented."); } }); updatePlot(); }