Chart:MPAndroidChart (Open source) 畫圖前要先洗手,免得弄髒紙:)
Chart的作者及教學 ,2016年11月原作者還有在更新。
Example 1
private void initPieChart(final PieChart pieChart) { initBaseChart(pieChart); pieChart.setDrawSliceText(false); pieChart.setUsePercentValues(true); pieChart.setHoleRadius(45); pieChart.setTransparentCircleRadius(50); pieChart.setHoleColorTransparent(true); pieChart.setRotationEnabled(false); pieChart.setRotationAngle(180); pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { @Override public void onValueSelected(Entry entry, int i, Highlight h) { if (entry instanceof EctsEntry) { float ects = ((EctsEntry) entry).getEcts(); if (ects > 0) { pieChart.setDrawCenterText(true); pieChart.setCenterText(String.format("%.2f\nECTS", ects)); } } } @Override public void onNothingSelected() { pieChart.setDrawCenterText(false); } }); if (pieChart.getRenderer() != null) { ((PieChartRenderer) pieChart.getRenderer()).getPaintCenterText().setColor(mTextColorPrimary); } }Example 2
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Init data storage: batteryData = new BatteryData(); // Load prefs: appPrefes = new AppPrefes(getActivity(), "ovms"); mShowVolt = appPrefes.getData("battery_show_volt").equals("on"); mShowTemp = appPrefes.getData("battery_show_temp").equals("on"); if (!mShowVolt && !mShowTemp) mShowVolt = true; // Setup UI: ProgressOverlay progressOverlay = createProgressOverlay(inflater, container, false); progressOverlay.setOnCancelListener(this); View rootView = inflater.inflate(R.layout.fragment_battery, null); // // Setup Cell status chart: // XAxis xAxis; YAxis yAxis; cellChart = (CandleStickChart) rootView.findViewById(R.id.chart_cells); cellChart.setDescription(getString(R.string.battery_cell_description)); cellChart.getPaint(LineChart.PAINT_DESCRIPTION).setColor(Color.LTGRAY); cellChart.setDrawGridBackground(false); cellChart.setDrawBorders(true); xAxis = cellChart.getXAxis(); xAxis.setTextColor(Color.WHITE); yAxis = cellChart.getAxisLeft(); yAxis.setTextColor(COLOR_VOLT); yAxis.setGridColor(COLOR_VOLT_GRID); yAxis.setStartAtZero(false); yAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float v) { return String.format("%.2f", v); } }); yAxis = cellChart.getAxisRight(); yAxis.setTextColor(COLOR_TEMP); yAxis.setGridColor(COLOR_TEMP_GRID); yAxis.setStartAtZero(false); yAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float v) { return String.format("%.0f", v); } }); // // Setup Pack history chart: // packChart = (LineChart) rootView.findViewById(R.id.chart_pack); packChart.setDescription(getString(R.string.battery_pack_description)); packChart.getPaint(LineChart.PAINT_DESCRIPTION).setColor(Color.LTGRAY); packChart.setDrawGridBackground(false); packChart.setDrawBorders(true); packChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { @Override public void onValueSelected(Entry entry, int dataSet, Highlight highlight) { // remember user data set selection: highlightSetNr = dataSet; highlightSetLabel = packChart.getData().getDataSetByIndex(dataSet).getLabel(); // update seek bar: seekPack.setProgress(entry.getXIndex()); // fires listener event (fromUser=false) // update cell chart: showCellStatus(entry.getXIndex()); } @Override public void onNothingSelected() { // nop } }); xAxis = packChart.getXAxis(); xAxis.setTextColor(Color.WHITE); yAxis = packChart.getAxisLeft(); yAxis.setStartAtZero(false); yAxis.setSpaceTop(5f); yAxis.setSpaceBottom(5f); yAxis.setTextColor(COLOR_SOC_TEXT); yAxis.setGridColor(COLOR_SOC_GRID); yAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float v) { return String.format("%.0f%%", v); } }); yAxis = packChart.getAxisRight(); yAxis.setStartAtZero(false); yAxis.setSpaceTop(15f); yAxis.setSpaceBottom(15f); yAxis.setTextColor(COLOR_VOLT); yAxis.setGridColor(COLOR_VOLT_GRID); yAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float v) { return String.format("%.0f", v); } }); // // Setup Pack history seek bar: // seekPack = (SeekBar) rootView.findViewById(R.id.seek_pack); seekPack.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int val, boolean fromUser) { if (fromUser) { // highlight entry: highlightPackEntry(val); // update cell chart: showCellStatus(val); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { // nop } @Override public void onStopTrackingTouch(SeekBar seekBar) { // nop } }); // default data set to highlight: highlightSetLabel = getString(R.string.battery_data_soc); // attach menu: setHasOptionsMenu(true); return rootView; }Example 3
void initializeChart() { Resources r = getResources(); int CgpaColor ; if(Cgpa>8.0f) CgpaColor = r.getColor(R.color.highAttend); else if(Cgpa>6.0f&&Cgpa<8 .0f="" arraylist="" cgpacolor="r.getColor(R.color.lowAttend);" else="" ntry=""> data = new ArrayList<>(); ArrayListCGPAentryList = new ArrayList<>(); float maxGpa = 0.0f, minGpa = 10.0f; ArrayList 8>xVals = new ArrayList<>(); for (int i = 0; i < semesterWiseGrades.size(); i++) { float gpa = (float) semesterWiseGrades.get(i).getGpa(); xVals.add(getString(R.string.label_semester_no, i + 1)); data.add(new Entry(gpa, i)); if (gpa > maxGpa) maxGpa = gpa; if (gpa < minGpa) minGpa = gpa; } CGPAentryList.add(new Entry(Cgpa,0)); CGPAentryList.add(new Entry(Cgpa,data.size()-1)); LineDataSet dset = new LineDataSet(data, getString(R.string.label_grade_gpa)); LineDataSet CGPAdset = new LineDataSet(CGPAentryList,"CGPA " + String.valueOf(Cgpa)); CGPAdset.setDrawCircleHole(false); CGPAdset.setHighlightEnabled(false); CGPAdset.setDrawFilled(true); CGPAdset.setFillAlpha(80); CGPAdset.enableDashedLine(1.0f,0.5f,0.5f); CGPAdset.setDrawCircles(false); CGPAdset.setDrawValues(false); dset.setLineWidth(2.0f);CGPAdset.setLineWidth(1.0f); dset.setValueTextSize(10.0f);CGPAdset.setValueTextSize(10.0f); dset.setColor(r.getColor(R.color.colorPrimary));CGPAdset.setColor(CgpaColor); CGPAdset.setFillColor(CgpaColor); dset.setHighLightColor(r.getColor(R.color.colorPrimary)); dset.setCircleColor(r.getColor(R.color.colorAccent)); dset.setDrawCircleHole(false); LineData chartData = new LineData(xVals, Arrays.asList(dset,CGPAdset)); YAxis leftaxis = chart.getAxisLeft(); leftaxis.setStartAtZero(false); leftaxis.setAxisMinValue(minGpa - 0.1f); leftaxis.setAxisMaxValue(maxGpa + 0.1f); chart.setDescription(""); YAxis rightAxis = chart.getAxisRight(); leftaxis.setEnabled(false); rightAxis.setEnabled(false); chart.getXAxis().setEnabled(false); chart.setData(chartData); chart.setPinchZoom(false); chart.setDoubleTapToZoomEnabled(false); chart.highlightValue(0, 0); chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { @Override public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { pager.setCurrentItem(e.getXIndex()); } @Override public void onNothingSelected() { pager.setCurrentItem(0); } }); }
沒有留言:
張貼留言