最近項目涉及到這個統計圖形,經過實現,記錄下,防止忘記了。
1、Github地址:MPAndroidChart
官方使用RadarChart demo:RadarChartActivitry
2、使用MPAndroidChart,還需要添加 nineoldandroids-2.4.0.jar
官方地址:http://nineoldandroids.com/
使用的是MPAndroidChart v2.2.2 的,還沒有jar包。
在app目錄下build.gradle的文件添加:
1 repositories { 2 flatDir { 3 dirs 'libs' 4 } 5 maven { url "https://jitpack.io" } 6 maven { // this is for realm-db 7 url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' 8 } 9 } 10 11 dependencies { 12 compile fileTree(dir: 'libs', include: ['*.jar']) 13 compile project(':MPChartLib') 14 }
在setting.gradle的文件添加:
1 include ':MPChartLib'
對於實心填充
設置圖例:
1 Legend l = mChart.getLegend(); 2 // 圖例位置 3 l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); 4 // 圖例字體樣式 5 // l.setTypeface(tf); 6 // 圖例X間距 7 l.setXEntrySpace(2f); 8 // 圖例Y間距 9 l.setYEntrySpace(1f); 10 l.setTextColor(textColor); //字體顏色
設置x、y:
1 XAxis xAxis = mChart.getXAxis(); 2 // X坐標值字體樣式 3 // xAxis.setTypeface(tf); 4 // X坐標值字體大小 5 xAxis.setTextSize(12f); 6 xAxis.setTextColor(textColor); 7 8 YAxis yAxis = mChart.getYAxis(); 9 // Y坐標值字體樣式 10 // yAxis.setTypeface(tf); 11 // Y坐標值標簽個數 12 yAxis.setLabelCount(6, false); 13 // Y坐標值字體大小 14 yAxis.setTextSize(15f); 15 // Y坐標值是否從0開始 16 yAxis.setStartAtZero(true); 17 // 是否顯示y值在圖表上 18 yAxis.setDrawLabels(false);
設置顯示內容塊:
RadarDataSet set1 = new RadarDataSet(yVals, "蝦米手機"); // 數據顏色設置 set1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]); // 實心填充區域顏色 set1.setFillColor(ColorTemplate.VORDIPLOM_COLORS[0]); // 是否實心填充區域 set1.setDrawFilled(true); // 數據線條寬度 set1.setLineWidth(2f);
來一發效果圖:

如果想要自定義網狀:
1、com.github.mikephil.charting.charts.RadarChart
2、在上面類中的 init 方法中,有如下三行:
1 // 對網進行繪畫 2 mRenderer = new RadarChartRenderer(this, mAnimator, mViewPortHandler); 3 // 對y軸的繪畫 4 mYAxisRenderer = new YAxisRendererRadarChart(mViewPortHandler, mYAxis, this); 5 // 對x軸的繪畫 6 mXAxisRenderer = new XAxisRendererRadarChart(mViewPortHandler, mXAxis, this);
3、這下找到 RadarChartRenderer 下的 drawWeb(Canvas c):
(1)首先會看到第一個循環體,這個循環體是進行從中心點發出到六邊角的直線:
1 // 圖表中心點到各六邊角的的直線繪畫,所以可在此之后增加直線后繪制的內容,比如增加圓點等。 2 for (int i = 0; i < mChart.getData().getXValCount(); i += xIncrements) { 3 4 PointF p = Utils.getPosition( 5 center, 6 mChart.getYRange() * factor, 7 sliceangle * i + rotationangle); 8 9 c.drawLine(center.x, center.y, p.x, p.y, mWebPaint); 10 11 p = Utils.getPosition( 12 center, 13 mChart.getYRange() * factor + 15, 14 sliceangle * i + rotationangle); 15 16 // 畫圓點 17 c.drawCircle(p.x, p.y, 5, mWebPointPaint); 18 }
(2)往下有第二個循環體,開始繪制點到點的直線,繪制成網,這個時候就可以針對一層層網的背景,進行自定義繪制:
1 // y軸標簽數,就是多少層網。 2 for (int j = 0; j < labelCount; j++) { 3 4 if(j < 5) { 5 mWebPaint.setAlpha(0); 6 } 7 8 if(j >= 5) { 9 mWebPaint.setAlpha(10); 10 mWebPaint.setStrokeWidth(0.5f + width); 11 width += 0.5f; 12 } 13 14 if(j == labelCount - 1){ 15 mWebPaint.setAlpha(20); 16 mWebPaint.setStrokeWidth(0.5f + width); 17 } 18 19 Path path3 = new Path(); 20 // 繪制循環到的網層 21 for (int i = 0; i < mChart.getData().getXValCount(); i++) { 22 23 float r = (mChart.getYAxis().mEntries[j] - mChart.getYChartMin()) * factor; 24 25 PointF p1 = Utils.getPosition(center, r, sliceangle * i + rotationangle); 26 PointF p2 = Utils.getPosition(center, r, sliceangle * (i + 1) + rotationangle); 27 28 c.drawLine(p1.x, p1.y, p2.x, p2.y, mWebPaint); 29 30 // 從50% 開始 31 if(j >= 5) { 32 if (i == 0) { 33 path3.moveTo(p1.x, p1.y); 34 path3.lineTo(p2.x, p2.y); 35 } 36 path3.lineTo(p1.x, p1.y); 37 } 38 } 39 40 // 從50% 開始 41 if(j >= 5){ 42 mWebContentPaint.setAlpha(5); 43 path3.close(); 44 c.drawPath(path3, mWebContentPaint); 45 } 46 }
查找到的資源:
1、SpiderWebChart:http://code1.okbase.net/codefile/SpiderWebChart.java_2014121928718_71.htm
2、csdn 上分享寫的 MPAndroidChart 教程
4、Radar (Spider Charts) {html用的}http://www.fusioncharts.com/charts/radar-spider-charts/
