我的应用上有一个显示以下图表的屏幕:
有没有办法改变我的传奇,以便它可以说出每种颜色代表什么?目前我可以显示蓝色方块,但它不能代表任何数字。以下是我在创建图表时使用的代码:
GraphView graph = (GraphView) findViewById(R.id.graph);
BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[]{
new DataPoint(1, personal),
new DataPoint(2, fun),
new DataPoint(3, work),
new DataPoint(4, food),
new DataPoint(5, commute),
new DataPoint(6,bills)
});
graph.setTitle("Expenses");
graph.addSeries(series);
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
series.setValueDependentColor(new ValueDependentColor<DataPoint>() {
@Override
public int get(DataPoint data) {
return Color.rgb((int) data.getX() * 255 / 4, (int) Math.abs(data.getY() * 255 / 6), 100);
}
});
series.setSpacing(50);
series.setDrawValuesOnTop(true);
series.setValuesOnTopColor(Color.RED);
backToMainMenu();
}
答案 0 :(得分:2)
我认为您不需要自定义图例。 LegendRenderer类使用每个数据系列的标题来显示图表上的条形图。
series.setTitle("This will display in the legend.");
但是,在此示例中,您只有一系列数据。如果每个栏的图例中都必须有标签,我建议在图表中添加多个具有不同标题的系列。每个系列都有自己的标题和颜色。
// Each series represents one bar.
BarGraphSeries<DataPoint> series1 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(1, personal)});
BarGraphSeries<DataPoint> series2 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(2, fun)});
BarGraphSeries<DataPoint> series3 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(3, work)});
BarGraphSeries<DataPoint> series4 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(4, food)});
BarGraphSeries<DataPoint> series5 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(5, commute)});
BarGraphSeries<DataPoint> series6 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(6, bills)});
// Add titles to be displayed in the legend.
series1.setTitle("personal");
series2.setTitle("fun");
series3.setTitle("work");
series4.setTitle("food");
series5.setTitle("commute");
series6.setTitle("bills");
// Add color to your bars.
series1.setColor(Color.rbg(1,2,3));
series2.setColor(Color.rbg(4,5,6));
series3.setColor(Color.rbg(7,8,9));
series4.setColor(Color.rbg(0,1,2));
series5.setColor(Color.rbg(3,4,5));
series6.setColor(Color.rbg(6,7,8));
// Add each series to your graph.
graph.addSeries(series1);
graph.addSeries(series2);
graph.addSeries(series3);
graph.addSeries(series4);
graph.addSeries(series5);
graph.addSeries(series6);
// Display the legend.
graph.getLegendRenderer().setVisible(true);
答案 1 :(得分:0)
您可以使用自定义图例渲染器并扩展默认实现。 https://github.com/jjoe64/GraphView/blob/master/src/main/java/com/jjoe64/graphview/LegendRenderer.java
或者您在graphview中禁用图例,使用FrameLayout并使用您自己的图例覆盖图表