码迷,mamicode.com
首页 > 移动开发 > 详细

hellocharts-android开源图表库(一)线状图

时间:2016-05-26 18:45:23      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

1.效果图 技术分享 技术分享 2.源代码 MainActivity.java public class MainActivity extends AppCompatActivity { public enum ChartType { LINE_CHART, COLUMN_CHART, PIE_CHART, BUBBLE_CHART, PREVIEW_LINE_CHART, PREVIEW_COLUMN_CHART, OTHER } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); } } public static class PlaceholderFragment extends Fragment implements AdapterView.OnItemClickListener { private ListView listView; private ChartSamplesAdapter adapter; public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); listView = (ListView) rootView.findViewById(android.R.id.list); adapter = new ChartSamplesAdapter(getActivity(), 0, generateSamplesDescriptions()); listView.setAdapter(adapter); listView.setOnItemClickListener(this); return rootView; } @Override public void onItemClick(AdapterView adapter, View view, int position, long id) { Intent intent; switch (position) { case 0: // Line Chart; intent = new Intent(getActivity(), LineChartActivity.class); startActivity(intent); break; case 1: // Column Chart; intent = new Intent(getActivity(), ColumnChartActivity.class); startActivity(intent); break; case 2: // Pie Chart; intent = new Intent(getActivity(), PieChartActivity.class); startActivity(intent); break; default: break; } } private List generateSamplesDescriptions() { List list = new ArrayList(); list.add(new ChartSampleDescription("Line Chart", "This is Line Chart", ChartType.LINE_CHART)); list.add(new ChartSampleDescription("Column Chart", "This is Column chart", ChartType.COLUMN_CHART)); list.add(new ChartSampleDescription("Pie Chart", "This is Pie Chart", ChartType.PIE_CHART)); return list; } } public static class ChartSamplesAdapter extends ArrayAdapter { public ChartSamplesAdapter(Context context, int resource, List objects) { super(context, resource, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(getContext(), R.layout.list_item_sample, null); holder = new ViewHolder(); holder.text1 = (TextView) convertView.findViewById(R.id.text1); holder.text2 = (TextView) convertView.findViewById(R.id.text2); holder.chartLayout = (FrameLayout) convertView.findViewById(R.id.chart_layout); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } ChartSampleDescription item = getItem(position); holder.chartLayout.setVisibility(View.VISIBLE); holder.chartLayout.removeAllViews(); AbstractChartView chart; switch (item.chartType) { case LINE_CHART: chart = new LineChartView(getContext()); holder.chartLayout.addView(chart); break; case COLUMN_CHART: chart = new ColumnChartView(getContext()); holder.chartLayout.addView(chart); break; case PIE_CHART: chart = new PieChartView(getContext()); holder.chartLayout.addView(chart); break; case BUBBLE_CHART: chart = new BubbleChartView(getContext()); holder.chartLayout.addView(chart); break; default: chart = null; holder.chartLayout.setVisibility(View.GONE); break; } if (null != chart) { chart.setInteractive(false);// Disable touch handling for chart on the ListView. } holder.text1.setText(item.text1); holder.text2.setText(item.text2); return convertView; } private class ViewHolder { TextView text1; TextView text2; FrameLayout chartLayout; } } public static class ChartSampleDescription { String text1; String text2; ChartType chartType; public ChartSampleDescription(String text1, String text2, ChartType chartType) { this.text1 = text1; this.text2 = text2; this.chartType = chartType; } } } LineChartActivity.java public class LineChartActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_line_chart); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit(); } } /** * A fragment containing a line chart. */ public static class PlaceholderFragment extends Fragment { private LineChartView chart; private LineChartData data; private int numberOfLines = 1; private int maxNumberOfLines = 4; private int numberOfPoints = 12; float[][] randomNumbersTab = new float[maxNumberOfLines][numberOfPoints]; private boolean hasAxes = true; private boolean hasAxesNames = true; private boolean hasLines = true; private boolean hasPoints = true; private ValueShape shape = ValueShape.CIRCLE; private boolean isFilled = false; private boolean hasLabels = false; private boolean isCubic = false; private boolean hasLabelForSelected = false; private boolean pointsHaveDifferentColor; public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { setHasOptionsMenu(true); View rootView = inflater.inflate(R.layout.fragment_line_chart, container, false); chart = (LineChartView) rootView.findViewById(R.id.chart); chart.setOnValueTouchListener(new ValueTouchListener()); // 产生一些随机值。 generateValues(); generateData(); // 禁用视图重新计算,更多信息参见togglecubic()方法。 chart.setViewportCalculationEnabled(false); resetViewport(); return rootView; } //每一个列的值 private void generateValues() { for (int i = 0; i < maxNumberOfLines; ++i) { for (int j = 0; j < numberOfPoints; ++j) { // randomNumbersTab[i][j] = (float) Math.random() * 100f; randomNumbersTab[i][j] =5; } } } private void resetViewport() { // Reset viewport height range to (0,100) final Viewport v = new Viewport(chart.getMaximumViewport()); v.bottom = 0; v.top = 100; v.left = 0; v.right = numberOfPoints - 1; chart.setMaximumViewport(v); chart.setCurrentViewport(v); } //generate 生成 private void generateData() { List lines = new ArrayList(); for (int i = 0; i < numberOfLines; ++i) { List values = new ArrayList(); for (int j = 0; j < numberOfPoints; ++j) { values.add(new PointValue(j, randomNumbersTab[i][j])); } Line line = new Line(values); line.setColor(ChartUtils.COLORS[i]); line.setShape(shape); line.setCubic(isCubic); line.setFilled(isFilled); line.setHasLabels(hasLabels); line.setHasLabelsOnlyForSelected(hasLabelForSelected); line.setHasLines(hasLines); line.setHasPoints(hasPoints); if (pointsHaveDifferentColor){ line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]); } lines.add(line); } data = new LineChartData(lines); if (hasAxes) { Axis axisX = new Axis(); Axis axisY = new Axis().setHasLines(true); if (hasAxesNames) { axisX.setName("Axis X"); axisY.setName("Axis Y"); } data.setAxisXBottom(axisX); data.setAxisYLeft(axisY); } else { data.setAxisXBottom(null); data.setAxisYLeft(null); } data.setBaseValue(Float.NEGATIVE_INFINITY); chart.setLineChartData(data); } private class ValueTouchListener implements LineChartOnValueSelectListener { @Override public void onValueSelected(int lineIndex, int pointIndex, PointValue value) { Toast.makeText(getActivity(), "Selected: " + value, Toast.LENGTH_SHORT).show(); } @Override public void onValueDeselected() { // TODO Auto-generated method stub } } } } 3.xml activity_main.xml fragment_main.xml activity_line_chart.xml fragment_line_chart.xml

hellocharts-android开源图表库(一)线状图

标签:

原文地址:http://www.cnblogs.com/520-1314/p/5532154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!