示例:
一、概述
本章将重点介绍各个图表类型特定的设置。
默认情况下,所有的图表类型都支持 Legend 且在设置图表数据后会自动生成 Legend 。
Legend 通常由一个标签的 形式/形状 来表示多个条目( entries ) 的每一个。
entries 数量自动生成的 legend 取决于DataSet 的标签 不同颜色的数量(在所有 DataSet 的对象)。 Legend 的标签取决于图表中所使用的 DataSet 对象。 如果没有为 DataSet 对象指定标签,图表将自动生成它们。 如果多个颜色用于一个 DataSet ,这些颜色分类 ,只通过一个标签说明。
对于定制的 Legend ,可以通过图表对象的 getLegend() 方法先获取 Legen 在进行调用对应方法:
Legend legend = chart.getLegend();
二、是否绘制 Legend
setEnabled(boolean enabled) : 设置Legend启用或禁用。 如果禁用, Legend 将不会被绘制。
private BarChart chart; private static XAxis xLabels; private static YAxis leftAxis; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_barchart); setTitle("AnotherBarActivity11"); chart = (BarChart)findViewById(R.id.chart1); chart.getDescription().setEnabled(false); // if more than 60 entries are displayed in the chart, no values will be // drawn chart.setMaxVisibleValueCount(60); // scaling can now only be done on x- and y-axis separately chart.setPinchZoom(false); chart.setDrawBarShadow(false); chart.setDrawGridBackground(false); xLabels = chart.getXAxis(); xLabels.setPosition(XAxisPosition.BOTTOM); xLabels.setDrawGridLines(false); leftAxis = chart.getAxisLeft(); leftAxis.setDrawGridLines(false); leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); //y轴的数值显示在外侧 leftAxis.setAxisMinimum(0f); ////为这个轴设置一个自定义的最小值。如果设置,这个值不会自动根据所提供的数据计算 chart.getAxisRight().setEnabled(false); chart.getAxisLeft().setDrawGridLines(false); // add a nice and smooth animation //chart.animateY(1500); initData(); Legend l = chart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.VERTICAL); //l.setLayout(true); l.setDrawInside(false); l.setFormSize(8f); l.setFormToTextSpace(4f); l.setXEntrySpace(6f); l.setYEntrySpace(0.1F); l.setYOffset(5); //chart.getLegend().setEnabled(false); chart.setScaleYEnabled(false); chart.setScaleXEnabled(false); } private void initData() { ArrayList<BarEntry> values = new ArrayList<>(); final ArrayList<String> xLabelValue = new ArrayList<>(); for(int i=0;i<7;i++) { xLabelValue.add((i+1)+"月份"); } for(int i=0;i<7;i++) { float[] val = new float[2]; if(i == 0) { val[1] = 2000; val[0] = 1000; }else if(i == 1) { val[1] = 1500; val[0] = 500; }else if(i == 2) { val[1] = 3000; val[0] = 2000; }else if(i == 3) { val[1] = 700; val[0] = 300; }else if(i == 4) { val[1] = 5100; val[0] = 900; }else if(i == 5) { val[1] = 1300; val[0] = 200; } values.add(new BarEntry(i, val)); } BarDataSet set1; if (chart.getData() != null && chart.getData().getDataSetCount() > 0) { set1 = (BarDataSet) chart.getData().getDataSetByIndex(0); set1.setValues(values); chart.getData().notifyDataChanged(); chart.notifyDataSetChanged(); } else { set1 = new BarDataSet(values, ""); set1.setColors(getColors()); set1.setDrawValues(false); set1.setStackLabels(new String[]{"钢销售额", "总销售额"}); ArrayList<IBarDataSet> dataSets = new ArrayList<>(); dataSets.add(set1); xLabels.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float v, AxisBase axisBase) { return xLabelValue.get((int) v); } }); //右侧Y轴自定义值 leftAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float v, AxisBase axisBase) { return (int)v+"元"; } }); BarData data = new BarData(dataSets); data.setValueFormatter(new StackedValueFormatter(true, "", 0) { }); chart.setData(data); chart.setFitBars(true); } chart.getData().setHighlightEnabled(true); chart.setFitBars(true); chart.invalidate(); } private static int[] getColors() { // have as many colors as stack-values per entry int[] colors = new int[2]; System.arraycopy(NewColorTemplate.TITILE_COLORS, 0, colors, 0, 2); return colors; }
重点的几句:
getColors圆柱体两部分的颜色分类
set1.setStackLabels(new String[]{"钢销售额", "总销售额"});
两部分的labels:
Legend的设置:
Legend l = chart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.VERTICAL); //l.setLayout(true); l.setDrawInside(false); l.setFormSize(8f); l.setFormToTextSpace(4f); l.setXEntrySpace(6f); l.setYEntrySpace(0.1F); l.setYOffset(5);
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦