Android画折线图、柱状图、饼图(使用achartengine.jar)

转载 2016年08月31日 11:19:16

目录(?)[-]

  1. 所需要的jar包achartenginejar
  2. 折线代码布局文件就不上传了很简单
    1. 另一种线的渲染器
  3. 扇形图代码
  4. 柱状图代码
  5. 属性总结部分代码
  6. 新测试代码下载地址

自从用了画折线的jar包之后,就不想再用canvas画布去画了,编程就是要站在巨人的肩膀上。


所需要的jar包:achartengine.jar

下载地址:http://download.csdn.net/detail/zhengyikuangge/9460642


折线代码(布局文件就不上传了,很简单):

<code class="hljs java 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.example.chartest02;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.text.SimpleDateFormat;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.util.Date;

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

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.app.Activity;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.graphics.Color;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.os.Bundle;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.widget.LinearLayout;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Activity</span> {</span>
    XYMultipleSeriesDataset mDataset;
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 多个系列的数据集合,即多条线的数据集合</span>
    XYSeries series;
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 一个系列的数据,即一条线的数据集合</span>

    XYMultipleSeriesRenderer mRenderer;
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 多个系列的环境渲染,即整个画折线的区域</span>
    XYSeriesRenderer r;
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 一个系列的环境渲染,即一条线的环境渲染</span>
    GraphicalView view;
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 整个view</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>;
    LinearLayout ll;

    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll = (LinearLayout) findViewById(R.id.ll);
        lineView();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Thread(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Runnable() {

            <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">run</span>() {
                <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// TODO Auto-generated method stub</span>
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>) {
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> d = Math.random() * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>;
                    series.add(i, d);
                    mRenderer.addXTextLabel(i, nowtime());
                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//动态添加数据和标题</span>
                    view.repaint();
                    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//重绘折线</span>
                    i++;
                    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> {
                        Thread.sleep(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>);
                    } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (InterruptedException e) {
                        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// TODO Auto-generated catch block</span>
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">lineView</span>() {
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 同样是需要数据dataset和视图渲染器renderer</span>
        mDataset = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYMultipleSeriesDataset();
        series = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYSeries(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Frist"</span>);
        series.add(<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;">6</span>);
        series.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>);
        series.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>);
        series.add(<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;">4</span>);
        mDataset.addSeries(series);

        mRenderer = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYMultipleSeriesRenderer();

        mRenderer
                .setOrientation(XYMultipleSeriesRenderer.Orientation.HORIZONTAL);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置图表的X轴的当前方向</span>
        mRenderer.setXTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"X轴"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置为X轴的标题</span>
        mRenderer.setYTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Y轴"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置y轴的标题</span>
        mRenderer.setAxisTitleTextSize(<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>
        mRenderer.setChartTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ChartTest"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置图表标题</span>
        mRenderer.setChartTitleTextSize(<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>
        mRenderer.setLabelsTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置标签的文字大小</span>
        mRenderer.setLegendTextSize(<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>
        mRenderer.setPointSize(<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>
        mRenderer.setYAxisMin(<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;">// 设置y轴最小值是0</span>
        mRenderer.setYAxisMax(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>);
        mRenderer.setYLabels(<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;">// 设置Y轴刻度个数(貌似不太准确)</span>
        mRenderer.setXAxisMax(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置X轴坐标个数</span>


        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将x标签栏目显示如:1,2,3,4替换为显示1月,2月,3月,4月</span>
        mRenderer.addXTextLabel(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1月"</span>);
        mRenderer.addXTextLabel(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2月"</span>);
        mRenderer.addXTextLabel(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"3月"</span>);
        mRenderer.addXTextLabel(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"4月"</span>);
        mRenderer.setXLabels(<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;">// 设置只显示如1月,2月等替换后的东西,不显示1,2,3等</span>
        mRenderer.setMargins(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] { <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</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>
        mRenderer.setPanEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第一个参数设置X轴是否可滑动,第二个参数设置Y轴是够可滑动</span>
        r = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYSeriesRenderer();
        r.setColor(Color.BLUE);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置颜色</span>
        r.setPointStyle(PointStyle.CIRCLE);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置点的样式</span>
        r.setFillPoints(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 填充点(显示的点是空心还是实心)</span>
        r.setDisplayChartValues(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将点的值显示出来</span>
        r.setChartValuesSpacing(<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>
        r.setChartValuesTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</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;">// r.setFillBelowLine(true);//是否填充折线图的下方</span>
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// r.setFillBelowLineColor(Color.GREEN);//填充的颜色,如果不设置就默认与线的</span>
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//颜色一致</span>
        r.setLineWidth(<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>
        mRenderer.addSeriesRenderer(r);

        view = ChartFactory.getLineChartView(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>, mDataset, mRenderer);
        view.setBackgroundColor(Color.BLACK);
        ll.addView(view);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将画好折线的view添加到xml中的一个布局里</span>
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String <span class="hljs-title" style="box-sizing: border-box;">nowtime</span>() {
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//求当前系统的时分秒</span>
        SimpleDateFormat format = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SimpleDateFormat(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"HH:mm:ss"</span>);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> format.format(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Date());

    }
}
</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><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li><li style="box-sizing: border-box; padding: 0px 5px;">92</li><li style="box-sizing: border-box; padding: 0px 5px;">93</li><li style="box-sizing: border-box; padding: 0px 5px;">94</li><li style="box-sizing: border-box; padding: 0px 5px;">95</li><li style="box-sizing: border-box; padding: 0px 5px;">96</li><li style="box-sizing: border-box; padding: 0px 5px;">97</li><li style="box-sizing: border-box; padding: 0px 5px;">98</li><li style="box-sizing: border-box; padding: 0px 5px;">99</li><li style="box-sizing: border-box; padding: 0px 5px;">100</li><li style="box-sizing: border-box; padding: 0px 5px;">101</li><li style="box-sizing: border-box; padding: 0px 5px;">102</li><li style="box-sizing: border-box; padding: 0px 5px;">103</li><li style="box-sizing: border-box; padding: 0px 5px;">104</li><li style="box-sizing: border-box; padding: 0px 5px;">105</li><li style="box-sizing: border-box; padding: 0px 5px;">106</li><li style="box-sizing: border-box; padding: 0px 5px;">107</li><li style="box-sizing: border-box; padding: 0px 5px;">108</li><li style="box-sizing: border-box; padding: 0px 5px;">109</li><li style="box-sizing: border-box; padding: 0px 5px;">110</li><li style="box-sizing: border-box; padding: 0px 5px;">111</li><li style="box-sizing: border-box; padding: 0px 5px;">112</li><li style="box-sizing: border-box; padding: 0px 5px;">113</li><li style="box-sizing: border-box; padding: 0px 5px;">114</li><li style="box-sizing: border-box; padding: 0px 5px;">115</li><li style="box-sizing: border-box; padding: 0px 5px;">116</li><li style="box-sizing: border-box; padding: 0px 5px;">117</li><li style="box-sizing: border-box; padding: 0px 5px;">118</li><li style="box-sizing: border-box; padding: 0px 5px;">119</li><li style="box-sizing: border-box; padding: 0px 5px;">120</li><li style="box-sizing: border-box; padding: 0px 5px;">121</li><li style="box-sizing: border-box; padding: 0px 5px;">122</li><li style="box-sizing: border-box; padding: 0px 5px;">123</li><li style="box-sizing: border-box; padding: 0px 5px;">124</li><li style="box-sizing: border-box; padding: 0px 5px;">125</li><li style="box-sizing: border-box; padding: 0px 5px;">126</li><li style="box-sizing: border-box; padding: 0px 5px;">127</li><li style="box-sizing: border-box; padding: 0px 5px;">128</li><li style="box-sizing: border-box; padding: 0px 5px;">129</li></ul>

我现在一直想实现的是:x轴可以跟着移动起来,但是一直找不到合适的办法,网上说改x轴的最大值,但效果并不理想,待定。。。


另一种线的渲染器:

<code class="hljs cpp 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">ColoursXYSeriesRenderer rendererMajor = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> 
ColoursXYSeriesRenderer();
rendererMajor.setPointStyle(PointStyle.CIRCLE);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置为曲线图</span>
rendererMajor.setFillPoints(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 数据点被填充</span>
rendererMajor.setDisplayChartValues(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在图表中显示点的值</span>
rendererMajor.setDisplayChartValuesDistance(<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;">// 在X轴上,设置两点之间的距离</span>
rendererMajor.setColor(Color.WHITE);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置线的颜色</span>
rendererMajor.setChartValuesTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置点值文本的尺寸大小</span>
rendererMajor.setChartValuesSpacing(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15f</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 实时数据文本到中心点的间距</span>
rendererMajor.setUseColor(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置当前为双色曲线图</span>
rendererMajor.setPointColor(Color.GREEN);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置点的颜色</span>
rendererMajor.setChartValueTextColor(Color.WHITE);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置数值文本的颜色</span>
rendererMajor.setWarningMinValue(<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>
rendererMajor.setWarningMaxValue(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置告警最大值,新属性</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></ul>

扇形图代码:

<code class="hljs java 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">PieChartBuilder</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Activity</span> {</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> GraphicalView mChartView;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//显示PieChart</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> LinearLayout mLinear;<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;">// public void back(View v) {</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Log.i("qiuzhping", "back onClick");</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Intent intent = new Intent();</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// intent.setClass(PieChartBuilder.this, MainActivity.class);</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// startActivity(intent);</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// PieChartBuilder.this.finish();</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// }</span>

    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.xy_chart);

        mLinear = (LinearLayout) findViewById(R.id.chart);
        mLinear.setBackgroundColor(Color.BLACK);

        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (mChartView == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 为空需要从ChartFactory获取PieChartView</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] COLORS = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] { Color.RED, Color.GREEN, Color.BLUE,
                    Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.DKGRAY };
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> data[] = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>[] { <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</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;">60</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</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;">90</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> };

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> CategorySeries mSeries = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> CategorySeries(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>);

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> DefaultRenderer mRenderer = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> DefaultRenderer();

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> VALUE = <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>
            mRenderer.setZoomButtonsVisible(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 显示放大缩小功能按钮</span>
            mRenderer.setStartAngle(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">180</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置为水平开始</span>
            mRenderer.setDisplayValues(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 显示数据</span>
            mRenderer.setFitLegend(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置是否显示图例</span>
            mRenderer.setLegendTextSize(<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>
            mRenderer.setLegendHeight(<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>
            mRenderer.setChartTitle(<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>

            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < data.length; i++)
                VALUE += data[i];
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < data.length; i++) {
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*这里是重点,只要把数值/总值赋给mSeries,它就可以自动生成每一部分*/</span>
                mSeries.add(<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>), data[i] / VALUE);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置种类名称和对应的数值,前面是(key,value)键值对</span>
                SimpleSeriesRenderer renderer = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SimpleSeriesRenderer();
                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (i < COLORS.length) {
                    renderer.setColor(COLORS[i]);<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;">else</span> {
                    renderer.setColor(getRandomColor());<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置描绘器的颜色</span>
                }
                renderer.setChartValuesFormat(NumberFormat.getPercentInstance());<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置百分比</span>
                mRenderer.setChartTitleTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置饼图标题大小</span>
                mRenderer.addSeriesRenderer(renderer);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将最新的描绘器添加到DefaultRenderer中</span>
            }

            mChartView = ChartFactory.getPieChartView(getApplicationContext(),
                    mSeries, mRenderer);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 构建mChartView</span>
            mRenderer.setClickEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 允许点击事件</span>
            mChartView.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> View.OnClickListener() {<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 具体内容</span>
                        <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onClick</span>(View v) {
                            SeriesSelection seriesSelection = mChartView
                                    .getCurrentSeriesAndPoint();<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;">if</span> (seriesSelection == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) {
                                Toast.makeText(getApplicationContext(),
                                        <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"您未选择数据"</span>, Toast.LENGTH_SHORT).show();
                            } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {
                                <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < mSeries.getItemCount(); i++) {
                                    mRenderer.getSeriesRendererAt(i)
                                            .setHighlighted(
                                                    i == seriesSelection
                                                            .getPointIndex());
                                }
                                mChartView.repaint();
                                Toast.makeText(
                                        getApplicationContext(),
                                        <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"您选择的是第"</span>
                                                + (seriesSelection
                                                        .getPointIndex() + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)
                                                + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" 项 "</span>
                                                + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" 百分比为  "</span>
                                                + NumberFormat
                                                        .getPercentInstance()
                                                        .format(seriesSelection
                                                                .getValue()),
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
            mLinear.addView(mChartView, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {
            mChartView.repaint();
        }
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-title" style="box-sizing: border-box;">getRandomColor</span>() {<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 分别产生RBG数值</span>
        Random random = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Random();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> R = random.nextInt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> G = random.nextInt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> B = random.nextInt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> Color.rgb(R, G, B);
    }
}</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><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li><li style="box-sizing: border-box; padding: 0px 5px;">92</li><li style="box-sizing: border-box; padding: 0px 5px;">93</li><li style="box-sizing: border-box; padding: 0px 5px;">94</li><li style="box-sizing: border-box; padding: 0px 5px;">95</li><li style="box-sizing: border-box; padding: 0px 5px;">96</li><li style="box-sizing: border-box; padding: 0px 5px;">97</li><li style="box-sizing: border-box; padding: 0px 5px;">98</li><li style="box-sizing: border-box; padding: 0px 5px;">99</li><li style="box-sizing: border-box; padding: 0px 5px;">100</li><li style="box-sizing: border-box; padding: 0px 5px;">101</li><li style="box-sizing: border-box; padding: 0px 5px;">102</li><li style="box-sizing: border-box; padding: 0px 5px;">103</li><li style="box-sizing: border-box; padding: 0px 5px;">104</li><li style="box-sizing: border-box; padding: 0px 5px;">105</li></ul>

柱状图代码

<code class="hljs java 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">BarChartBuilder</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Activity</span> {</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> GraphicalView mChartView;

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// public void back(View v) {</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Log.i("qiuzhping", "back onClick");</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Intent intent = new Intent();</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// intent.setClass(BarChartBuilder.this, MainActivity.class);</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// startActivity(intent);</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// BarChartBuilder.this.finish();</span>
    <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;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">setChartSettings</span>(XYMultipleSeriesRenderer renderer,
            String title, String xTitle, String yTitle, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> xMin,
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> xMax, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> yMin, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> yMax, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> axesColor,
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> labelsColor) {
        renderer.setChartTitle(title);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> XYMultipleSeriesRenderer <span class="hljs-title" style="box-sizing: border-box;">buildBarRenderer</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] colors) {
        XYMultipleSeriesRenderer renderer = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>);
        renderer.setChartTitleTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>);
        renderer.setLabelsTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>);
        renderer.setLegendTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>);
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> length = colors.length;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < length; i++) {
            SimpleSeriesRenderer r = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SimpleSeriesRenderer();
            r.setColor(colors[i]);
            renderer.addSeriesRenderer(r);
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> renderer;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> XYMultipleSeriesDataset <span class="hljs-title" style="box-sizing: border-box;">buildBarDataset</span>(String[] titles,
            List<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>[]> values) {<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//柱形图的数据源和饼图差不多,也是由一些键值对组成</span>
        XYMultipleSeriesDataset dataset = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYMultipleSeriesDataset();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> length = titles.length;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < length; i++) {
            CategorySeries series = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> CategorySeries(titles[i]);
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>[] v = values.get(i);
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> seriesLength = v.length;
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> k = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; k < seriesLength; k++) {
                series.add(v[k]);
            }
            dataset.addSeries(series.toXYSeries());
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> dataset;
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.xy_chart);
        LinearLayout mLinear = (LinearLayout) findViewById(R.id.chart);
        mLinear.setBackgroundColor(Color.BLACK);

        String[] titles = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> String[] { <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2012"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2013"</span> };
        List<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>[]> values = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ArrayList<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>[]>();
        values.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>[] { <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1423</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1230</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1424</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1524</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1590</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1920</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2203</span>,
                <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2120</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1950</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1550</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1260</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1400</span> });
        values.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span>[] { <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">523</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">730</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">924</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1054</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">790</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">920</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1100</span>,
                <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">950</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1500</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1500</span> });
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] colors = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>[] { Color.RED, Color.BLUE };
        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 柱形图颜色设置</span>
        setChartSettings(renderer, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"柱形图示例"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"月份"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"数量"</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;">12.5</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2400</span>,
                Color.GRAY, Color.LTGRAY);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置柱形图标题,横轴(X轴)、纵轴(Y轴)、最小的伸所刻度、最大的伸所刻度</span>
        renderer.getSeriesRendererAt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>).setDisplayChartValues(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在第0条柱形图上显示数据</span>
        renderer.getSeriesRendererAt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>).setDisplayChartValues(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在第1条柱形图上显示数据</span>
        renderer.setXLabels(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>);
        renderer.setYLabels(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
        renderer.setXLabelsAlign(Align.LEFT);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 数据从左到右显示</span>
        renderer.setYLabelsAlign(Align.LEFT);
        renderer.setPanEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);
        renderer.setZoomEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);
        renderer.setZoomButtonsVisible(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 显示放大缩小功能按钮</span>
        renderer.setZoomRate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.1</span>f);
        renderer.setBarSpacing(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.5</span>f);<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;">if</span> (mChartView == <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) {<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 构建柱形图</span>
            mChartView = ChartFactory.getBarChartView(getApplicationContext(),
                    buildBarDataset(titles, values), renderer, Type.DEFAULT);
            renderer.setClickEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//renderer.set</span>
            mLinear.addView(mChartView, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
            mChartView.repaint();
    }
}</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><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li><li style="box-sizing: border-box; padding: 0px 5px;">92</li><li style="box-sizing: border-box; padding: 0px 5px;">93</li><li style="box-sizing: border-box; padding: 0px 5px;">94</li><li style="box-sizing: border-box; padding: 0px 5px;">95</li><li style="box-sizing: border-box; padding: 0px 5px;">96</li><li style="box-sizing: border-box; padding: 0px 5px;">97</li><li style="box-sizing: border-box; padding: 0px 5px;">98</li></ul>

属性总结(部分代码):

<code class="hljs cpp 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-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">mRenderer = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYMultipleSeriesRenderer();
        renderer = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> ColoursXYSeriesRenderer();
        series = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYSeries(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"FF"</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//线的名称</span>
        dataset = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> XYMultipleSeriesDataset();
        series.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
        series.add(<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;">10</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// series 添加数据点,第一个参数为x轴坐标,0代表原点;第二个参数为Y轴坐标</span>
        dataset.addSeries(series);
        renderer.setDisplayChartValues(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 是否显示数据</span>
        renderer.setPointStyle(PointStyle.CIRCLE);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 点的样式</span>
        renderer.setWarningMaxValue(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 最大警告值</span>
        renderer.setFillPoints(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 是否填充点</span>
        renderer.setColor(Color.BLACK);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置线的颜色</span>
        renderer.setUseColor(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 是否用双色</span>
        renderer.setLineWidth(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8f</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置线的宽度</span>
        renderer.setChartValuesTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25f</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 线值字的大小</span>
        renderer.setChartValueTextColor(Color.GREEN); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 折线值的颜色</span>
        renderer.setChartValuesSpacing(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20f</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 值到线的举例</span>

        mRenderer.setOrientation(Orientation.HORIZONTAL);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置总方向</span>
        mRenderer.setXAxisMax(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// X轴最大坐标数</span>
        mRenderer.setXLabels(<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;">// X轴不显示默认自带的坐标</span>
        mRenderer.setYAxisMax(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">500</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Y轴最大数值为500</span>
        mRenderer.setYAxisMin(<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;">// Y轴最小数值为0</span>
        mRenderer.setYLabels(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Y轴坐标为5个</span>
        mRenderer.setXLabelsColor(Color.BLACK);
        mRenderer.setYLabelsColor(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, Color.BLACK);
        mRenderer.setLabelsTextSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25f</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 标签的颜色</span>
        mRenderer.setChartTitle(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Test"</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置图标的标题</span>
        mRenderer.setDisplayValues(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 是否显示数据</span>
        mRenderer.setPanEnabled(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第一个参数为X轴不可滑动,第二个参数为X轴不可滑动</span>
        mRenderer.setPointSize(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15f</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 点的大小</span>
        mRenderer.addSeriesRenderer(renderer);

        view_test = ChartFactory.getLineChartView(context, dataset, mRenderer);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置背景颜色一定要在生成View之后再设置</span>
        mRenderer.setApplyBackgroundColor(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">true</span>);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 是否应用背景颜色</span>
        mRenderer.setBackgroundColor(Color.TRANSPARENT);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置背景颜色为透明</span>
        mRenderer.setMarginsColor(Color.TRANSPARENT);
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置图表之外的背景颜色为透明</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><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li></ul>

新测试代码下载地址:

http://download.csdn.net/detail/zhengyikuangge/9517499

出处:http://blog.csdn.net/zhengyikuangge/article/details/50884749

相关文章推荐

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Android画折线图、柱状图、饼图(使用achartengine.jar)

自从用了画折线的jar包之后,就不想再用canvas画布去画了,编程就是要站在巨人的肩膀上。所需要的jar包:achartengine.jar下载地址:http://download.csdn.net...

AChartEngine简介

AChartEngine简介 1.     AChartEngine概况 AChartEngine是为Android应用而设计的绘图工具库。可用于绘制多种图表,目前该绘图工具库的最新最新版本的是ach...
  • qiu_11
  • qiu_11
  • 2014年01月15日 13:40
  • 3042

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

android开发引入第三方Jar包---achartengine为例 【转】

android开发引入第三方Jar包---achartengine为例   将需要引入的包放在新建的工程的根目录(不要放在lib里面,下面还有导入一步,不然会报错)。 ...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Android画折线图、柱状图、饼图(使用achartengine.jar)

From:http://bubufx.com/detail-1629050.html 原文:http://blog.csdn.net/zhengyikuangge/article/detai...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android画折线图、柱状图、饼图(使用achartengine.jar)
举报原因:
原因补充:

(最多只允许输入30个字)