在achartengine库中设置图例标题

时间:2014-03-19 08:25:57

标签: android achartengine

我知道使用以下代码在AchartEngine

中显示图例框
renderer.setShowLegend(true);
renderer.setFitLegend(true);

但我不知道如何设置Legend Title

人们知道,

请告诉我,

感谢。

已更新:我找到了答案,我可以通过Legend Title设置CategorySeries

叫它

mChartView = getBarChart();
mLl.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));

从以下代码:

public GraphicalView getBarChart() {
    XYMultipleSeriesRenderer barChartRenderer = getBarChartRenderer();
    setBarChartSettings(barChartRenderer);
    GraphicalView mGp = ChartFactory.getBarChartView(getActivity(),
            getBarDemoDataset(), barChartRenderer, Type.DEFAULT);
    return mGp;
}

private XYMultipleSeriesDataset getBarDemoDataset() {
    XYMultipleSeriesDataset barChartDataset = new XYMultipleSeriesDataset();

    int[] DEEP_SLEEP = new int[DashBoard.mLlDeepSleep.size()];
    for (int i = 0; i < DashBoard.mLlDeepSleep.size(); i++) {
        DEEP_SLEEP[i] = 1;
    }
    int[] LIGHT_SLEEP = new int[DashBoard.mLlLightSleep.size()];
    for (int i = 0; i < DashBoard.mLlLightSleep.size(); i++) {
        LIGHT_SLEEP[i] = 1;
    }

    CategorySeries firstSeries = new CategorySeries("4 hrs 53 mins");
    for (int i = 0; i < DEEP_SLEEP.length; i++)
        firstSeries.add(DEEP_SLEEP[i]);
    barChartDataset.addSeries(firstSeries.toXYSeries());

    CategorySeries secondSeries = new CategorySeries("2 hrs 32 mins");
    for (int j = 0; j < LIGHT_SLEEP.length; j++)
        secondSeries.add(LIGHT_SLEEP[j]);
    barChartDataset.addSeries(secondSeries.toXYSeries());

    return barChartDataset;
}

public XYMultipleSeriesRenderer getBarChartRenderer() {
    int[] colors = new int[] {
            getResources().getColor(R.color.green_deep_sleep),
            getResources().getColor(R.color.green_light_sleep) };

    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

    renderer.setAntialiasing(true);
    renderer.setAxesColor(Color.BLACK);
    renderer.setBarSpacing(0f);
    renderer.setGridColor(Color.BLACK);
    renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
    renderer.setShowGridX(true);
    renderer.setPanEnabled(false);
    renderer.setShowAxes(false);
    renderer.setXLabels(0);
    renderer.setYLabels(5);
    renderer.setYLabelsColor(0, Color.TRANSPARENT);
    renderer.setZoomEnabled(false, false);

    SimpleSeriesRenderer r = new SimpleSeriesRenderer();
    r.setColor(colors[0]);
    renderer.addSeriesRenderer(r);

    r = new SimpleSeriesRenderer();
    r.setColor(colors[1]);
    r.setChartValuesTextAlign(Align.RIGHT);
    renderer.addSeriesRenderer(r);

    return renderer;
}

private void setBarChartSettings(XYMultipleSeriesRenderer renderer) {
    // TODO Set records in here
    renderer.setXAxisMin(0);
    renderer.setXAxisMax(600);
    renderer.setYAxisMin(0);
    renderer.setYAxisMax(1);
}

1 个答案:

答案 0 :(得分:2)

默认情况下,图例标题设置为您在渲染器对象中添加的系列的标题。 因此,设置您希望看作图例标题的系列标题。