achartengine/MPAndroidChart——图表实现之Java

  关于android的图表,这里就换作chart吧,如果要自己实现的话,那工作量可是很大的,好在有好几个开源的框架可以拿来使用,首先是achartengine了:achartengine github源码链接。其次是MPAndroidChart:MPAndroidChart github源码链接。关于更详细的介绍可以参考上面的链接,这里主要是简单讲下使用。因为没找到android studio的dependencies,所以就网上下载了相应的jar包了,具体已经在百度云上了,可以参考下面的链接。

  链接: http://pan.baidu.com/s/1i4N2glB 密码: 2fe2

  运行效果如下

  

achartengine/MPAndroidChart——图表实现之Java_第1张图片

     这里依次是atchartengine的折线图,MPAndroidChart的折线图和饼图。

  achartengine

    至于怎么包含jar包,怎么建工程这就不多讲了,既然都要学习第三方的框架了,这些基础肯定有的了。首先是怎么把chart安在界面上,achartengine可以直接使用LinearLayout,然后把需要的chart绘画在这个LinearLayout上。具体xml如下所示:


<code class="language-xml hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">LinearLayout
</span>    <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@+id/chart"</span>
    <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span>
    <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"150dp"</span>
    <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:orientation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"vertical"</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">LinearLayout</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

  

  具体代码实现如下,基本上都加了注释了,理解起来还是很方便的了,具体看ChartActivity代码中:

    当然atchartengine还有其他更加强大的功能,这里只是简单用了下折线图。

  MPAndroidChart

  折线图配置

    MPAndroidChart的实现需要用到自定义的空间com.github.mikephil.charting.charts.LineChart来实现折线图:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charts</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LineChart</span>
    android:id=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"@+id/spread_line_chart"</span>
    android:layout_width=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"match_parent"</span>
    android:layout_height=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"150dp"</span> /></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

饼图配置

  MPAndroidChart的实现需要用到自定义的空间com.github.mikephil.charting.charts.PieChart来实现折线图:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charts</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.PieChart</span>
    android:id=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"@+id/spread_pie_chart"</span>
    android:layout_width=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"match_parent"</span>
    android:layout_height=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"200dp"</span>/></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

act_chart xml实现

<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-pi" style="color: rgb(0, 102, 102); box-sizing: border-box;"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">LinearLayout</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">xmlns:android</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span>
    <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span>
    <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:orientation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"vertical"</span>></span>

    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">LinearLayout
</span>        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@+id/chart"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"150dp"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:orientation</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"vertical"</span>></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">LinearLayout</span>></span>

    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">com.github.mikephil.charting.charts.LineChart
</span>        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@+id/spread_line_chart"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"150dp"</span> /></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">com.github.mikephil.charting.charts.PieChart
</span>        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@+id/spread_pie_chart"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"200dp"</span>/></span>
    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">Button
</span>        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"@+id/getData"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"wrap_content"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"match_parent"</span>
        <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:text</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"获取当访问量"</span> /></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">LinearLayout</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>

ChartActivity java代码实现:

  代码的主要介绍在注释里面:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">package <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.jared</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.emdatabindingstudy</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ui</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.graphics</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Color</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.graphics</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Paint</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.graphics</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Typeface</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.os</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Bundle</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.support</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.annotation</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Nullable</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.util</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.DisplayMetrics</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.view</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.View</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.widget</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Button</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.widget</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LinearLayout</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charts</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LineChart</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charts</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.PieChart</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.components</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Legend</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.components</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.XAxis</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.components</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.YAxis</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.data</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Entry</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.data</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LineData</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.data</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LineDataSet</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.data</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.PieData</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.github</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mikephil</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.charting</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.data</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.PieDataSet</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.jared</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.emdatabindingstudy</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.R</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

import org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.achartengine</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ChartFactory</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.achartengine</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.GraphicalView</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.achartengine</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.chart</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.PointStyle</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.achartengine</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.model</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.CategorySeries</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.achartengine</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.model</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.XYMultipleSeriesDataset</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.achartengine</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.renderer</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.XYMultipleSeriesRenderer</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import org<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.achartengine</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.renderer</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.XYSeriesRenderer</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

import java<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.util</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ArrayList</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import java<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.util</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.List</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
 * Created by jared on 16/4/1.
 */</span>
public class ChartActivity extends BaseActivity {

    private final static String TAG = ChartActivity<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getSimpleName</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

    private LinearLayout chartLyt<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    private LineChart mLineChart<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    private PieChart mPieChart<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    Typeface mTf<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; // 自定义显示字体</span>

    private Button getDataBtn<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

    private List<Integer> lists = new ArrayList<Integer>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

    private void setLists() {
        lists<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.clear</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        for (int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; i < 20; i++) {</span>
            int value = ((int) (Math<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.random</span>() * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
            lists<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(value)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.onCreate</span>(savedInstanceState)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        setContentView(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.act</span>_chart)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        getDataBtn = (Button) findViewById(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getData</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        getDataBtn<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setOnClickListener</span>(this)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        chartLyt = (LinearLayout) findViewById(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.chart</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        mTf = Typeface<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.createFromAsset</span>(getAssets(), <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"OpenSans-Bold.ttf"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        drawTheChart()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        drawTheChartByMPAndroid()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        drawPieChart()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    private void drawPieChart() {
        mPieChart = (PieChart) findViewById(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.spread</span>_pie_chart)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        PieData mPieData = getPieData(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        showPieChart(mPieChart, mPieData)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    private void showPieChart(PieChart pieChart, PieData pieData) {
        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setHoleColorTransparent</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setHoleRadius</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //半径</span>
        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTransparentCircleRadius</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //半透明圈</span>

        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDescription</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawHoleEnabled</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setRotationAngle</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //初始角度</span>
        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setRotationEnabled</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //可以手动旋转</span>
        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setUsePercentValues</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //显示百分比</span>

        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawCenterText</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //饼状图中间可以添加文字</span>
        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setCenterText</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"人员分布"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setCenterTextColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.GRAY</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setCenterTextTypeface</span>(mTf)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setData</span>(pieData)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        Legend mLegend = pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLegend</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //设置比例图</span>
        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPosition</span>(Legend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LegendPosition</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.RIGHT</span>_OF_CHART)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //坐右边显示</span>
        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setXEntrySpace</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYEntrySpace</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTypeface</span>(mTf)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.GRAY</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        pieChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.animateXY</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    private PieData getPieData(int count, float range) {
        ArrayList<String> xValues = new ArrayList<String>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //用来表示每个饼块上的内容</span>
        String[]  content = new String[] {<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<10"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"10~20"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"21~40"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">">40"</span>}<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        for (int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; i < count; i++) {</span>
            xValues<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"年龄("</span>+content[i]+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">")"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        }

        ArrayList<Entry> yValue = new ArrayList<Entry>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //用来表示封装每个饼块的实际数据</span>

        List<Float> qs = new ArrayList<Float>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        qs<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; qs.add(14f);qs.add(34f);qs.add(38f);</span>
        for (int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; i < qs.size(); i++) {</span>
            yValue<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(new Entry(qs<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(i), i))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        }

        PieDataSet pieDataSet = new PieDataSet(yValue, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2015浏览量统计"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        pieDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setSliceSpace</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        ArrayList<Integer> colors = new ArrayList<Integer>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //饼图颜色
        colors<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rgb</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">205</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">205</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">205</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        colors<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rgb</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">114</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">188</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">223</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        colors<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rgb</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">123</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">124</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        colors<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rgb</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">57</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">135</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        pieDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setColors</span>(colors)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //设置颜色</span>
        pieDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setValueTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        pieDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setValueTextColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        pieDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setValueTypeface</span>(mTf)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //设置字体样式</span>
        DisplayMetrics metrics = getResources()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getDisplayMetrics</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        float px = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> * (metrics<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.densityDpi</span> / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">160</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        pieDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setSelectionShift</span>(px)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //选中态多出的长度</span>
        PieData pieData = new PieData(xValues, pieDataSet)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        return pieData<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    private void drawTheChartByMPAndroid() {
        mLineChart = (LineChart) findViewById(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.spread</span>_line_chart)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        LineData lineData = getLineData(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">36</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        showChart(mLineChart, lineData, Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.rgb</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">137</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">230</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">81</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    private void showChart(LineChart lineChart, LineData lineData, int color) {
        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawBorders</span>(false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //在折线图上添加边框</span>
        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDescription</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //数据描述</span>
        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setNoDataTextDescription</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"You need to provide data for the chart."</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawGridBackground</span>(false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //表格颜色</span>
        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setGridBackgroundColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span> & <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x70FFFFFF</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //表格的颜色,设置一个透明度</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTouchEnabled</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //可点击</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDragEnabled</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  //可拖拽</span>
        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setScaleEnabled</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  //可缩放</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPinchZoom</span>(false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setBackgroundColor</span>(color)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //设置背景颜色</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setData</span>(lineData)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  //填充数据</span>

        Legend mLegend = lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLegend</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //设置标示,就是那个一组y的value的</span>

        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setForm</span>(Legend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LegendForm</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.CIRCLE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //样式</span>
        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setFormSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //字体</span>
        mLegend<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //颜色</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setVisibleXRange</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;   //x轴可显示的坐标范围</span>
        XAxis xAxis = lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getXAxis</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  //x轴的标示</span>
        xAxis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPosition</span>(XAxis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.XAxisPosition</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.BOTTOM</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //x轴位置</span>
        xAxis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;    //字体的颜色</span>
        xAxis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //字体大小</span>
        xAxis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setGridColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//网格线颜色</span>
        xAxis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawGridLines</span>(false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //不显示网格线</span>
        xAxis<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTypeface</span>(mTf)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        YAxis axisLeft = lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getAxisLeft</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //y轴左边标示</span>
        YAxis axisRight = lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getAxisRight</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //y轴右边标示</span>
        axisLeft<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //字体颜色</span>
        axisLeft<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //字体大小</span>
        axisLeft<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setAxisMaxValue</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //最大值</span>
        axisLeft<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setLabelCount</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //显示格数</span>
        axisLeft<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setGridColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //网格线颜色</span>
        axisLeft<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTypeface</span>(mTf)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        axisRight<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawAxisLine</span>(false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        axisRight<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawGridLines</span>(false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        axisRight<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDrawLabels</span>(false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        lineChart<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.animateX</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2500</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;  //立即执行动画</span>
    }

    private LineData getLineData(int count, float range) {
        ArrayList<String> xValues = new ArrayList<String>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        for (int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; i < count; i++) {</span>
            // <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>轴显示的数据,这里默认使用数字下标显示
            xValues<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span> + (i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        }

        // <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">y</span>轴的数据
        ArrayList<Entry> yValues = new ArrayList<Entry>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        for (int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; i < count; i++) {</span>
            float value = (int) (Math<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.random</span>() * range)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
            yValues<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(new Entry(value, i))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        }
        // create a dataset <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> give it a type
        // <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">y</span>轴的数据集合
        LineDataSet lineDataSet = new LineDataSet(yValues, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"访问量统计"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        // mLineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setFillAlpha</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">110</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        // mLineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setFillColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.RED</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        //用<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">y</span>轴的集合来设置参数
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setLineWidth</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.75</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; // 线宽</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setCircleSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;// 显示的圆形大小</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;// 显示颜色</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setCircleColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;// 圆形的颜色</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setHighLightColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; // 高亮的线的颜色</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setHighlightEnabled</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setValueTextColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; //数值显示的颜色</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setValueTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;     //数值显示的大小</span>
        lineDataSet<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setValueTypeface</span>(mTf)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        lineDataSets<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(lineDataSet)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; // 添加数据集合</span>

        //创建lineData
        LineData lineData = new LineData(xValues, lineDataSets)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        return lineData<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    public void drawTheChart() {
        XYMultipleSeriesRenderer mRenderer = getXYMulSeriesRenderer()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        XYSeriesRenderer renderer = getXYSeriesRenderer()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        mRenderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.addSeriesRenderer</span>(renderer)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        setLists()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        XYMultipleSeriesDataset dataset = getDataSet()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        GraphicalView chartView = ChartFactory<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLineChartView</span>(this, dataset, mRenderer)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        chartLyt<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.addView</span>(chartView, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //chartLyt<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.invalidate</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    public XYSeriesRenderer getXYSeriesRenderer() {
        XYSeriesRenderer renderer = new XYSeriesRenderer()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //设置折线宽度
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setLineWidth</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //设置折线颜色
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.GRAY</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDisplayBoundingPoints</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //点的样式
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPointStyle</span>(PointStyle<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.CIRCLE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //设置点的大小
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPointStrokeWidth</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //设置数值显示的字体大小
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setChartValuesTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //显示数值
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDisplayChartValues</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        return renderer<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    public XYMultipleSeriesDataset getDataSet() {
        XYMultipleSeriesDataset barDataset = new XYMultipleSeriesDataset()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        CategorySeries barSeries = new CategorySeries(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2016年"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        for (int i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; i < lists.size(); i++) {</span>
            barSeries<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(lists<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.get</span>(i))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        }

        barDataset<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.addSeries</span>(barSeries<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.toXYSeries</span>())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        return barDataset<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    public XYMultipleSeriesRenderer getXYMulSeriesRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setMarginsColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.argb</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xF3</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xF3</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0xF3</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        // 设置背景颜色
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setApplyBackgroundColor</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setBackgroundColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        //设置Title的内容和大小
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setChartTitle</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"访问量统计"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setChartTitleTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        //图表与四周的边距
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setMargins</span>(new int[]{<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>})<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        //设置<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">X</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Y</span>轴title的内容和大小
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setXTitle</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"日期"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYTitle</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"访问数"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setAxisTitleTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        //renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setAxesColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WHITE</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setLabelsColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.BLACK</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        //图例文字的大小
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setLegendTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        // <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>、<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">y</span>轴上刻度颜色和大小
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setXLabelsColor</span>(Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.BLACK</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYLabelsColor</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, Color<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.BLACK</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setLabelsTextSize</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYLabelsPadding</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        // 设置<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">X</span>轴的最小数字和最大数字,由于我们的数据是从<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>开始,所以设置为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.5</span>就可以在<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>之前让出一部分
        // 有兴趣的童鞋可以删除下面两行代码看一下效果
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPanEnabled</span>(false, false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        //显示网格
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setShowGrid</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        //<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">X</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Y</span>轴上的数字数量
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setXLabels</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYLabels</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        // 设置<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">X</span>轴的最小数字和最大数字
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setXAxisMin</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setXAxisMax</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        // 设置<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Y</span>轴的最小数字和最大数字
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYAxisMin</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYAxisMax</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        // 设置渲染器显示缩放按钮
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setZoomButtonsVisible</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        // 设置渲染器允许放大缩小
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setZoomEnabled</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        // 消除锯齿
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setAntialiasing</span>(true)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        // 刻度线与<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">X</span>轴坐标文字左侧对齐
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setXLabelsAlign</span>(Paint<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Align</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LEFT</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        // <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Y</span>轴与<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Y</span>轴坐标文字左对齐
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setYLabelsAlign</span>(Paint<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Align</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.LEFT</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        // 允许左右拖动,但不允许上下拖动.
        renderer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setPanEnabled</span>(true, false)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

        return renderer<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
    }

    @Override
    public void onClick(View view) {
        super<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.onClick</span>(view)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        switch (view<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getId</span>()) {
            case R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getData</span>:
                drawTheChart()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
                drawTheChartByMPAndroid()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
                drawPieChart()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
            default:
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
        }
    }
}</code>

你可能感兴趣的:(achartengine/MPAndroidChart——图表实现之Java)