Skip to content

Instantly share code, notes, and snippets.

@yamato8
Created February 19, 2014 01:54
Show Gist options
  • Save yamato8/9084671 to your computer and use it in GitHub Desktop.
Save yamato8/9084671 to your computer and use it in GitHub Desktop.
積み上げグラフに挑戦:Androidplot
package com.example.androidplottest;
import java.text.DecimalFormat;
import java.util.Arrays;
import com.androidplot.ui.SizeLayoutType;
import com.androidplot.ui.SizeMetrics;
import com.androidplot.xy.BarFormatter;
import com.androidplot.xy.BarRenderer;
import com.androidplot.xy.BarRenderer.BarWidthStyle;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private XYPlot plot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plot = (XYPlot) findViewById(R.id.aprLevelsPlot);
Number[] series1Numbers10 = {2, null, 5, 2, 7, 4, 3, 7, 4, 5};
Number[] series2Numbers10 = {4, 6, 3, null, 2, 0, 7, 4, 5, 4};
Number[] series1Numbers = series1Numbers10;
Number[] series2Numbers = series2Numbers10;
XYSeries series1 = new SimpleXYSeries(Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Us");
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Them");
plot.addSeries(series1, new BarFormatter(Color.BLUE, Color.BLACK));
plot.addSeries(series2, new BarFormatter(Color.RED, Color.BLACK));
plot.setDomainStepValue(10);
plot.setDomainLabel("X軸ラベル");
plot.setRangeBoundaries(0, 10, BoundaryMode.FIXED);//Y軸 0-10
plot.setRangeValueFormat(new DecimalFormat("0"));//整数
plot.setDomainValueFormat(new DecimalFormat("0"));//X軸目盛整数
//グリッド
plot.setGridPadding(20, 10, 20, 0);//left, top, right, bottom グリッドのパディング
plot.setPlotPaddingBottom(10);//ビューのパディング
plot.getGraphWidget().setMarginBottom(10);//グラフと凡例の隙間
//凡例の調整
plot.getLegendWidget().setSize(new SizeMetrics (20, SizeLayoutType.ABSOLUTE, 100, SizeLayoutType.ABSOLUTE ));
//BarRenderer の取得 変更
BarRenderer<?> barRenderer = (BarRenderer<?>) plot.getRenderer(BarRenderer.class);
if(barRenderer != null) {
Log.v("----","OK");
barRenderer.setBarWidth(10);
//barRenderer.get
barRenderer.setBarWidthStyle(BarWidthStyle.FIXED_WIDTH);
//barRenderer.setBarGap(-30);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment