版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
Android繪圖機(jī)制(四)——使用HelloCharts開源框架搭建一系列炫酷圖表,柱形圖,折線圖,餅狀圖和動畫特效,抽絲剝繭帶你認(rèn)識圖表之美一.官方截圖二.項(xiàng)目架構(gòu)這里我們首先來看一下我排版的思維導(dǎo)圖然后跳轉(zhuǎn)一個(gè)Activity@OverridepublicvoidonItemClick(AdapterView<?>adapter,Viewview,intposition,longid){Intentintent;switch(position){case0://LineChart;intent=newIntent(getActivity(),LineChartActivity.class);startActivity(intent);break;case1://ColumnChart;intent=newIntent(getActivity(),ColumnChartActivity.class);startActivity(intent);break;case2://PieChart;intent=newIntent(getActivity(),PieChartActivity.class);startActivity(intent);break;case3://BubbleChart;intent=newIntent(getActivity(),BubbleChartActivity.class);startActivity(intent);break;case4://PreviewLineChart;intent=newIntent(getActivity(),PreviewLineChartActivity.class);startActivity(intent);break;case5://PreviewColumnChart;intent=newIntent(getActivity(),PreviewColumnChartActivity.class);startActivity(intent);break;case6://ComboChart;intent=newIntent(getActivity(),ComboLineColumnChartActivity.class);startActivity(intent);break;case7://LineColumnDependency;intent=newIntent(getActivity(),LineColumnDependencyActivity.class);startActivity(intent);break;case8://Tempolinechart;intent=newIntent(getActivity(),TempoChartActivity.class);startActivity(intent);break;case9://Speedlinechart;intent=newIntent(getActivity(),SpeedChartActivity.class);startActivity(intent);break;case10://GoodBadfilledlinechart;intent=newIntent(getActivity(),GoodBadChartActivity.class);startActivity(intent);break;case11://GoodBadfilledlinechart;intent=newIntent(getActivity(),ViewPagerChartsActivity.class);startActivity(intent);break;default:break;}}Activity繼承的是FragmentActivity,我們在FragmentActivity內(nèi)部編寫一個(gè)Fragment這樣就可以綁定主Activity而不用繼承自View去多寫一個(gè)類了publicstaticclassPlaceholderFragmentextendsFragment所以你看到的項(xiàng)目也是非常簡潔的項(xiàng)目只有一個(gè)主Activity——MainActivity和一個(gè)關(guān)于軟件的AboutActivity,然后就是十二個(gè)對應(yīng)的類了好了,我們可以編寫了三,實(shí)現(xiàn)圖標(biāo)1.折線圖從這里我們就可以看出,其實(shí)圖表操作主要還是看menu菜單,所以,我們先實(shí)現(xiàn)我們fragment要綁定的布局fragment_line_chart<lecho.lib.hellocharts.view.LineChartViewandroid:id="@+id/chart"android:layout_width="match_parent"android:layout_height="match_parent"></lecho.lib.hellocharts.view.LineChartView>LineChartActivitypackagelecho.lib.hellocharts.samples;importjava.util.ArrayList;importjava.util.List;importlecho.lib.hellocharts.animation.ChartAnimationListener;importlecho.lib.hellocharts.gesture.ZoomType;importlecho.lib.hellocharts.listener.LineChartOnValueSelectListener;importlecho.lib.hellocharts.model.Axis;importlecho.lib.hellocharts.model.Line;importlecho.lib.hellocharts.model.LineChartData;importlecho.lib.hellocharts.model.PointValue;importlecho.lib.hellocharts.model.ValueShape;importlecho.lib.hellocharts.model.Viewport;importlecho.lib.hellocharts.util.ChartUtils;importlecho.lib.hellocharts.view.Chart;importlecho.lib.hellocharts.view.LineChartView;importandroid.os.Bundle;importandroid.support.v4.app.Fragment;importandroid.support.v4.app.FragmentActivity;importandroid.view.LayoutInflater;importandroid.view.Menu;importandroid.view.MenuInflater;importandroid.view.MenuItem;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.Toast;publicclassLineChartActivityextendsFragmentActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_line_chart);if(savedInstanceState==null){getSupportFragmentManager().beginTransaction().add(R.id.container,newPlaceholderFragment()).commit();}}/***Afragmentcontainingalinechart.*/publicstaticclassPlaceholderFragmentextendsFragment{privateLineChartViewchart;privateLineChartDatadata;privateintnumberOfLines=1;privateintmaxNumberOfLines=4;privateintnumberOfPoints=12;float[][]randomNumbersTab=newfloat[maxNumberOfLines][numberOfPoints];privatebooleanhasAxes=true;privatebooleanhasAxesNames=true;privatebooleanhasLines=true;privatebooleanhasPoints=true;privateValueShapeshape=ValueShape.CIRCLE;privatebooleanisFilled=false;privatebooleanhasLabels=false;privatebooleanisCubic=false;privatebooleanhasLabelForSelected=false;privatebooleanpointsHaveDifferentColor;publicPlaceholderFragment(){}@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){setHasOptionsMenu(true);ViewrootView=inflater.inflate(R.layout.fragment_line_chart,container,false);chart=(LineChartView)rootView.findViewById(R.id.chart);chart.setOnValueTouchListener(newValueTouchListener());//Generatesomerandomevalues.generateValues();generateData();//Disableviewpirtrecalculations,seetoggleCubic()methodfor//moreinfo.chart.setViewportCalculationEnabled(false);resetViewport();returnrootView;}//MENU@OverridepublicvoidonCreateOptionsMenu(Menumenu,MenuInflaterinflater){inflater.inflate(R.menu.line_chart,menu);}//menu的操作@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){intid=item.getItemId();if(id==R.id.action_reset){reset();generateData();returntrue;}if(id==R.id.action_add_line){addLineToData();returntrue;}if(id==R.id.action_toggle_lines){toggleLines();returntrue;}if(id==R.id.action_toggle_points){togglePoints();returntrue;}if(id==R.id.action_toggle_cubic){toggleCubic();returntrue;}if(id==R.id.action_toggle_area){toggleFilled();returntrue;}if(id==R.id.action_point_color){togglePointColor();returntrue;}if(id==R.id.action_shape_circles){setCircles();returntrue;}if(id==R.id.action_shape_square){setSquares();returntrue;}if(id==R.id.action_shape_diamond){setDiamonds();returntrue;}if(id==R.id.action_toggle_labels){toggleLabels();returntrue;}if(id==R.id.action_toggle_axes){toggleAxes();returntrue;}if(id==R.id.action_toggle_axes_names){toggleAxesNames();returntrue;}if(id==R.id.action_animate){prepareDataAnimation();chart.startDataAnimation();returntrue;}if(id==R.id.action_toggle_selection_mode){toggleLabelForSelected();Toast.makeText(getActivity(),"Selectionmodesetto"+chart.isValueSelectionEnabled()+"selectanypoint.",Toast.LENGTH_SHORT).show();returntrue;}if(id==R.id.action_toggle_touch_zoom){chart.setZoomEnabled(!chart.isZoomEnabled());Toast.makeText(getActivity(),"IsZoomEnabled"+chart.isZoomEnabled(),Toast.LENGTH_SHORT).show();returntrue;}if(id==R.id.action_zoom_both){chart.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);returntrue;}if(id==R.id.action_zoom_horizontal){chart.setZoomType(ZoomType.HORIZONTAL);returntrue;}if(id==R.id.action_zoom_vertical){chart.setZoomType(ZoomType.VERTICAL);returntrue;}returnsuper.onOptionsItemSelected(item);}privatevoidgenerateValues(){for(inti=0;i<maxNumberOfLines;++i){for(intj=0;j<numberOfPoints;++j){randomNumbersTab[i][j]=(float)Math.random()*100f;}}}privatevoidreset(){numberOfLines=1;hasAxes=true;hasAxesNames=true;hasLines=true;hasPoints=true;shape=ValueShape.CIRCLE;isFilled=false;hasLabels=false;isCubic=false;hasLabelForSelected=false;pointsHaveDifferentColor=false;chart.setValueSelectionEnabled(hasLabelForSelected);resetViewport();}privatevoidresetViewport(){//Resetviewportheightrangeto(0,100)finalViewportv=newViewport(chart.getMaximumViewport());v.bottom=0;v.top=100;v.left=0;v.right=numberOfPoints-1;chart.setMaximumViewport(v);chart.setCurrentViewport(v);}privatevoidgenerateData(){List<Line>lines=newArrayList<Line>();for(inti=0;i<numberOfLines;++i){List<PointValue>values=newArrayList<PointValue>();for(intj=0;j<numberOfPoints;++j){values.add(newPointValue(j,randomNumbersTab[i][j]));}Lineline=newLine(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=newLineChartData(lines);if(hasAxes){AxisaxisX=newAxis();AxisaxisY=newAxis().setHasLines(true);if(hasAxesNames){axisX.setName("AxisX");axisY.setName("AxisY");}data.setAxisXBottom(axisX);data.setAxisYLeft(axisY);}else{data.setAxisXBottom(null);data.setAxisYLeft(null);}data.setBaseValue(Float.NEGATIVE_INFINITY);chart.setLineChartData(data);}/***Addslinestodata,afterthatdatashouldbesetagainwith*{@linkLineChartView#setLineChartData(LineChartData)}.Last4thline*hasnon-monotonicallyxvalues.*/privatevoidaddLineToData(){if(data.getLines().size()>=maxNumberOfLines){Toast.makeText(getActivity(),"Samplesappusesmax4lines!",Toast.LENGTH_SHORT).show();return;}else{++numberOfLines;}generateData();}privatevoidtoggleLines(){hasLines=!hasLines;generateData();}privatevoidtogglePoints(){hasPoints=!hasPoints;generateData();}privatevoidtoggleCubic(){isCubic=!isCubic;generateData();if(isCubic){/***手動設(shè)置高一點(diǎn)最大立方行,因?yàn)橛袝r(shí)超過或低于最大值/最小值。為此使用Viewport.inest()*方法并傳遞負(fù)值作為dy參數(shù)或手動設(shè)置頂部和底部的值*。在這個(gè)例子中我知道Y值(0100)范圍內(nèi)我手動設(shè)置視口高度范圍(105)*。讓這個(gè)作品在動畫應(yīng)該使用Chart.setViewportCalculationEnabled*(false)之前修改窗口。記住你叫setLineChartData后設(shè)置窗口()。*/finalViewportv=newViewport(chart.getMaximumViewport());v.bottom=-5;v.top=105;//Youhavetosetmaxandcurrentviewportsseparately.chart.setMaximumVwport(v);//Ichangingcurrentviewportwithanimationinthiscase.chart.setCurrentViewportWithAnimation(v);}else{//Ifnotcubicrestoreviewportto(0,100)range.finalViewportv=newViewport(chart.getMaximumViewport());v.bottom=0;v.top=100;/***你必須單獨(dú)設(shè)置最大和當(dāng)前視窗。在這種情況下,如果我想要?jiǎng)赢嬑冶仨毾仍O(shè)置currentviewport和使用動畫偵聽器。*最大視窗將onAnimationFinished方法。*/chart.setViewportAnimationListener(newChartAnimationListener(){@OverridepublicvoidonAnimationStarted(){//TODOAuto-generatedmethodstub}@OverridepublicvoidonAnimationFinished(){//設(shè)置最大viewpirt和刪除偵聽器。chart.setMaximumViewport(v);chart.setViewportAnimationListener(null);}});//設(shè)置當(dāng)前viewpirt動畫;chart.setCurrentViewportWithAnimation(v);}}privatevoidtoggleFilled(){isFilled=!isFilled;generateData();}privatevoidtogglePointColor(){pointsHaveDifferentColor=!pointsHaveDifferentColor;generateData();}privatevoidsetCircles(){shape=ValueShape.CIRCLE;generateData();}privatevoidsetSquares(){shape=ValueShape.SQUARE;generateData();}privatevoidsetDiamonds(){shape=ValueShape.DIAMOND;generateData();}privatevoidtoggleLabels(){hasLabels=!hasLabels;if(hasLabels){hasLabelForSelected=false;chart.setValueSelectionEnabled(hasLabelForSelected);}generateData();}privatevoidtoggleLabelForSelected(){hasLabelForSelected=!hasLabelForSelected;chart.setValueS
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度綠色交通合伙清算合作協(xié)議3篇
- 二零二五年度全款購房合同:房地產(chǎn)項(xiàng)目投資并購及整合協(xié)議3篇
- 2025年度農(nóng)業(yè)現(xiàn)代化貸款擔(dān)保協(xié)議3篇
- 2025年度全新官方版二零二五年度離婚協(xié)議書與子女監(jiān)護(hù)權(quán)協(xié)議3篇
- 二零二五年度知識產(chǎn)權(quán)侵權(quán)律師費(fèi)協(xié)議3篇
- 二零二五年度農(nóng)村土地占用與農(nóng)村文化傳承合同協(xié)議
- 2025年度航空航天公司干股分紅與飛行器研發(fā)合作協(xié)議3篇
- 二零二五年度衛(wèi)浴安裝與智能家居系統(tǒng)集成與優(yōu)化服務(wù)協(xié)議3篇
- 二零二五年度太陽能電池板加工服務(wù)合同3篇
- 二零二五年度物聯(lián)網(wǎng)解決方案公司轉(zhuǎn)讓合同3篇
- 社會學(xué)概論期末復(fù)習(xí)題及答案
- 五輸穴與臨床應(yīng)用課件
- 物料吊籠安全技術(shù)標(biāo)準(zhǔn)
- 工程項(xiàng)目施工方案比選
- 盾構(gòu)始發(fā)施工技術(shù)要點(diǎn)PPT(44頁)
- 甲烷(沼氣)的理化性質(zhì)及危險(xiǎn)特性表
- 某鋼鐵有限責(zé)任公司管理專案報(bào)告書---提升配電系統(tǒng)管理水平降低變配電裝置事故率
- 促銷費(fèi)用管理辦法15
- 《三國演義》整本書閱讀任務(wù)單
- GB 13296-2013 鍋爐、熱交換器用不銹鋼無縫鋼管(高清版)
- 中醫(yī)院中藥的飲片處方用名與調(diào)劑給付規(guī)定
評論
0/150
提交評論