Android - 为BarSeries中的每个数据点设置其他颜色(shinobicharts)

时间:2014-04-19 11:24:05

标签: android shinobi

我尝试使用shinobicharts来设计如下图所示:

http://www.upsieutoc.com/images/2014/04/19/barseries.png

检测BarSeries

我使用过BarSeries,我遇到了一些问题:

  • 我找不到在每个DataPoint(20,25,30)上设置数据的功能。

  • 我想为每个数据点设置其他颜色,如上图所示。 (红色,绿色,紫色)

  • 右对齐标签。 (法国,马来西亚,西班牙)。

这段代码:

 ChartFragment chartFragment = (ChartFragment) getFragmentManager().findFragmentById(R.id.chart);
  ShinobiChart shinobiChart = chartFragment.getShinobiChart();

   NumberAxis xAxis = new NumberAxis();
    xAxis.setPosition(Position.REVERSE);
    shinobiChart.addXAxis(xAxis);
    CategoryAxis yAxis = new CategoryAxis();
    shinobiChart.addYAxis(yAxis);
    BarSeries series = new BarSeries();
    series.setTitle("Country");
    DataAdapter<Integer, String> adapter = new SimpleDataAdapter<Integer, String>(); 

     adapter.add(new DataPoint<Integer, String>(20, "Spain"));
     adapter.add(new DataPoint<Integer, String>(25, "Malaysia"));
     adapter.add(new DataPoint<Integer, String>(30, "France"));

    series.setDataAdapter(adapter);
    shinobiChart.addSeries(series);

请帮助我!

2 个答案:

答案 0 :(得分:0)

显然是you can't

解决方案是使3个系列分别具有一个值,而不是一个具有3个值的系列。 E.g:

ChartFragment chartFragment = (ChartFragment) getFragmentManager().findFragmentById(R.id.chart);
  ShinobiChart shinobiChart = chartFragment.getShinobiChart();
   NumberAxis xAxis = new NumberAxis();
    xAxis.setPosition(Position.REVERSE);
    shinobiChart.addXAxis(xAxis);
    CategoryAxis yAxis = new CategoryAxis();
    shinobiChart.addYAxis(yAxis);

    BarSeries series1 = new BarSeries();
    series1.setTitle("Country");
    BarSeriesStyle bss1 = series1.getStyle();
    bss1.setAreaColor(Color.RED);
    DataAdapter<Integer, String> adapter = new SimpleDataAdapter<Integer, String>(); 
    adapter.add(new DataPoint<Integer, String>(20, "Spain"));
    series1.setDataAdapter(adapter);
    shinobiChart.addSeries(series1);

    BarSeries series2 = new BarSeries();
    series2.setTitle("Country");
    BarSeriesStyle bss2 = series2.getStyle();
    bss2.setAreaColor(Color.GREEN);
    DataAdapter<Integer, String> adapter = new SimpleDataAdapter<Integer, String>(); 
    adapter.add(new DataPoint<Integer, String>(25, "Malaysia"));
    series2.setDataAdapter(adapter);
    shinobiChart.addSeries(series2);

    BarSeries series3 = new BarSeries();
    series3.setTitle("Country");
    BarSeriesStyle bs3 = series3.getStyle();
    bss3.setAreaColor(Color.BLUE);
    DataAdapter<Integer, String> adapter = new SimpleDataAdapter<Integer, String>(); 
    adapter.add(new DataPoint<Integer, String>(30, "France"));
    series3.setDataAdapter(adapter);
    shinobiChart.addSeries(series3);

答案 1 :(得分:0)

我或许可以为你问题的第2点提供部分答案:&#34;我想为每个数据点设置其他颜色,如上图所示。 (红色,绿色,紫色)&#34;

您是否尝试过继承SChartBarSeries?如果没有,试试这个:

#pragma mark - Custom Styling of Bars

@interface customBarSeries:SChartBarSeries

@end

@implementation customBarSeries

- (SChartBarSeriesStyle *)styleForPoint:(id<SChartData>)point {

    SChartBarSeriesStyle *newStyle = [super styleForPoint:point];

    int country = [[point sChartXValue] intValue];  

    // datapoint specific customization:
    switch (country) {
        case COUNTRY_FRANCE:
            newStyle.areaColor = [UIColor redColor];
            break;
        case COUNTRY_MALAYSIA:
            newStyle.areaColor = [UIColor greenColor];
            break;
        case COUNTRY_SPAIN:
            newStyle.areaColor = [UIColor purpleColor];
            break;
    }
    return newStyle;
}

@end



// And in you chart's "sChart: seriesAtIndex:" method, use your custom bars instead of the shinobi default:

-(SChartSeries *)sChart:(ShinobiChart *)chart seriesAtIndex:(NSInteger)index {
    switch (index) {
        case COUNTRY_BARS_SERIE_INDEX:
        {
// Instead of this:            SChartBarSeries *barSeries = [SChartBarSeries new];
            customBarSeries *barSeries = [customBarSeries new];   // <-- Here you use your customized sub-class
            barSeries.title = @"Countries";

            // Style things that are common to all data points in the serie
            barSeries.style.dataPointLabelStyle.displayValues = SChartDataPointLabelDisplayValuesX;
            barSeries.style.dataPointLabelStyle.showLabels = YES;  // <-- You may need this as well

            return barSeries;
            break;
        }           
   }
}

不知道这个标签部分是否有效或标签放在何处。在编写此示例时,我偶然发现了dataPointStyle的label属性,因此您必须使用它来查看会发生什么。

至于Y轴上的国家/地区名称,您必须为此自定义Y轴,它不会附带该系列。

希望有所帮助!