如何在条形图android上绘制轴之间的虚线

时间:2015-12-21 11:28:40

标签: android android-graphview

我正在使用achartengine jar进行条形图视图。我对条形图设计示例图的要求如下所示。

enter image description here

  • 顶部图片是我的要求条形图
  • 底部图像是我的输出图像。

我的代码是:

public class TestClass  extends AppCompatActivity {
    private GraphicalView BarChartView;

    @SuppressLint("ParserError")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graph);
        if (BarChartView==null )
        {

            XYMultipleSeriesRenderer renderer = buildBarRenderer();
            setChartSettings(renderer);
            BarChartView  = ChartFactory.getBarChartView(this, getBarDataset(), renderer, Type.DEFAULT);
            LinearLayout playout = (LinearLayout) findViewById(R.id.graph2);
            playout.addView(BarChartView);

        } else
        {
            BarChartView.repaint();
            BarChartView.setVisibility(View.GONE);
        }
    }

    private void setChartSettings(XYMultipleSeriesRenderer renderer)
    {
        renderer.setYTitle("Hits");
        renderer.setXAxisMin(0);
        renderer.setXAxisMax(7);
        renderer.setYAxisMin(0);
        renderer.setYAxisMax(100);

    }
    private XYMultipleSeriesDataset getBarDataset()
    {
        XYSeries series1 = new XYSeries("A");
        XYSeries series2 = new XYSeries("B");
        XYSeries series3 = new XYSeries("C");
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        series1.add(3, 50);
        dataset.addSeries(series1);

        series2.add(4, 25);
        dataset.addSeries(series2);

        series3.add(5, 100);
        dataset.addSeries(series3);

        return dataset;

    }
    protected XYMultipleSeriesRenderer buildBarRenderer() {
        XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();

        //double[] range = {5,5,0};

        mrenderer.setAxisTitleTextSize(16);
        mrenderer.setChartTitleTextSize(20);
        mrenderer.setLabelsTextSize(15);
        mrenderer.setLegendTextSize(15);
        mrenderer.setBarSpacing(0.5);
        mrenderer.setXLabels(0);
        mrenderer.addXTextLabel(0, "SAVINGS TOTAL");
        mrenderer.addXTextLabel(1.5, "SPEND BREAKDOWN");
        mrenderer.addXTextLabel(3, "DEPT");
        mrenderer.setMargins(new int[]{20, 30, 15, 0});
        mrenderer.setAxesColor(Color.WHITE);
        mrenderer.setChartTitle("");
        mrenderer.setXTitle("");
       // mrenderer.setInitialRange(range, 1);
        mrenderer.setBarWidth(100);

        XYSeriesRenderer renderer1 = new XYSeriesRenderer();
        renderer1.setColor(Color.GREEN);
        XYSeriesRenderer renderer2 = new XYSeriesRenderer();
        renderer2.setColor(Color.RED);
        XYSeriesRenderer renderer3 = new XYSeriesRenderer();
        renderer3.setColor(Color.BLUE);
        mrenderer.addSeriesRenderer(renderer1);
        mrenderer.addSeriesRenderer(renderer2);
        mrenderer.addSeriesRenderer(renderer3);

        return mrenderer;

    }


}

请帮我解决x和Y轴之间的条形Grapg虚线。 如果有人有想法请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用

renderer.setStroke(BasicStroke.DASHED);

renderer.setStroke(BasicStroke.DOTTED);