如何在 Android Studio 中使用 Graphview 设置图例
How to set up a legend with Graphview in Android Studio
我的应用程序上有一个显示下图的屏幕:
有没有办法改变我的图例,让它可以说出每种颜色代表什么?目前我可以让蓝色方块出现,但它不代表任何数字。这是我在创建图表时使用的代码:
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();
}
您可以使用自定义图例渲染器并扩展默认实现。
https://github.com/jjoe64/GraphView/blob/master/src/main/java/com/jjoe64/graphview/LegendRenderer.java
或者您在 graphview 中禁用图例,使用 FrameLayout 并用您自己的图例视图覆盖图表
我认为您不需要自定义图例。 LegendRenderer class 使用每个数据系列的标题来显示图表上的条形代表什么。
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);
我的应用程序上有一个显示下图的屏幕:
有没有办法改变我的图例,让它可以说出每种颜色代表什么?目前我可以让蓝色方块出现,但它不代表任何数字。这是我在创建图表时使用的代码:
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();
}
您可以使用自定义图例渲染器并扩展默认实现。 https://github.com/jjoe64/GraphView/blob/master/src/main/java/com/jjoe64/graphview/LegendRenderer.java
或者您在 graphview 中禁用图例,使用 FrameLayout 并用您自己的图例视图覆盖图表
我认为您不需要自定义图例。 LegendRenderer class 使用每个数据系列的标题来显示图表上的条形代表什么。
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);