2016-12-05 258 views
1

如何以此顯示條形圖?水平顯示條形圖

enter image description here

目前默認條形圖是上下顛倒。有很多酒吧,所以我需要它從上到下堆疊。

public void showBarDiagram() { 
    String[] titles = new String[]{""}; 
    List<double[]> values = new ArrayList<double[]>(); 
    values.add(new double[]{14230, 12300, 14240, 300, 15900, 19200, 22030, 21200, 19500, 15500, 
     12600, 14000}); 
    int[] colors = new int[]{ColorUtil.BLUE}; 
    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); 
    setChartSettings(renderer, "Enquiry", "Month", "", 0.5, 
      12.5, 0, 24000, ColorUtil.GRAY, ColorUtil.LTGRAY); 
    ((XYSeriesRenderer) renderer.getSeriesRendererAt(0)).setDisplayChartValues(true); 
    renderer.setXLabels(12); 
    renderer.setYLabels(10); 
    renderer.setXLabelsAlign(Component.LEFT); 
    renderer.setYLabelsAlign(Component.LEFT); 
    renderer.setZoomEnabled(false); 
    renderer.setZoomRate(1.1f); 
    renderer.setBarSpacing(0.5f); 
    initRendererer(renderer); 
    BarChart chart = new BarChart(buildBarDataset(titles, values), renderer, 
      BarChart.Type.STACKED); 
    ChartComponent c = new ChartComponent(chart); 
    c.setFocusable(true); 
    c.setZoomEnabled(true); 
    c.setPanEnabled(true); 
    barContainer.add(c); 
} 

protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) { 
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); 
    renderer.setAxisTitleTextSize(smallFont.getHeight()/2); 
    renderer.setChartTitleTextFont(smallFont); 
    renderer.setLabelsTextSize(smallFont.getHeight()/2); 
    renderer.setLegendTextSize(smallFont.getHeight()/2); 
    int length = colors.length; 
    for (int i = 0; i < length; i++) { 
     XYSeriesRenderer r = new XYSeriesRenderer(); 
     r.setColor(colors[i]); 
     renderer.addSeriesRenderer(r); 
    } 
    return renderer; 
} 

protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, 
     String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, 
     int labelsColor) { 
    renderer.setChartTitle(title); 
    renderer.setXTitle(xTitle); 
    renderer.setYTitle(yTitle); 
    renderer.setXAxisMin(xMin); 
    renderer.setXAxisMax(xMax); 
    renderer.setYAxisMin(yMin); 
    renderer.setYAxisMax(yMax); 
    renderer.setAxesColor(axesColor); 
    renderer.setLabelsColor(labelsColor); 
} 

protected void initRendererer(DefaultRenderer renderer) { 
    renderer.setBackgroundColor(0xffffffff); 
    renderer.setApplyBackgroundColor(true); 
    renderer.setLabelsColor(0xff000000); 
    renderer.setAxesColor(0xff000000); 
    if(Font.isNativeFontSchemeSupported()) { 
     Font fnt = Font.createTrueTypeFont("native:MainLight", "native:MainLight"). 
       derive(Display.getInstance().convertToPixels(2.5f), Font.STYLE_PLAIN); 
     renderer.setTextTypeface(fnt); 
     renderer.setChartTitleTextFont(fnt); 
     renderer.setLabelsTextFont(fnt); 
     renderer.setLegendTextFont(fnt); 

     if(renderer instanceof XYMultipleSeriesRenderer) { 
      ((XYMultipleSeriesRenderer)renderer).setAxisTitleTextFont(fnt); 
     } 
     if(renderer instanceof XYMultipleSeriesRenderer) { 
      XYMultipleSeriesRenderer x = (XYMultipleSeriesRenderer)renderer; 
      x.setMarginsColor(0xfff7f7f7); 
      x.setXLabelsColor(0xff000000); 
      x.setYLabelsColor(0, 0xff000000); 
     } 
    } 
} 

protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) { 
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 
    int length = titles.length; 
    for (int i = 0; i < length; i++) { 
     CategorySeries series = new CategorySeries(titles[i]); 
     double[] v = values.get(i); 
     int seriesLength = v.length; 
     for (int k = 0; k < seriesLength; k++) { 
      series.add(v[k]); 
     } 
     dataset.addSeries(series.toXYSeries()); 
    } 
    return dataset; 
} 

protected ChartComponent newChart(AbstractChart chart) { 

    ChartComponent c = new ChartComponent(chart); 
    c.setFocusable(true); 
    c.setZoomEnabled(true); 
    c.setPanEnabled(true); 
    return c; 
} 

更新1:

setChartSettings(渲染器, 「」, 「」, 「否」,0.5,17.5,0,400,ColorUtil.GRAY,ColorUtil.LTGRAY);

enter image description here

我用定向chartSetting垂直的,但得到了與X,Y軸的價值交換的麻煩。

XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); 
renderer.setOrientation(XYMultipleSeriesRenderer.Orientation.VERTICAL); 
setChartSettings(renderer, "No", "", "", 0, 400,0.5, 17.5, ColorUtil.GRAY, ColorUtil.LTGRAY); 

enter image description here

+0

我不認爲當前支持條形圖包含該選項,但我已經請求Steve驗證此問題,並且可能以您可以自己實現的方式指向 –

回答

1

XYMultipleSeriesRenderer incldudes一個setOrientation方法。

XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); 
renderer.setOrientation(Orientation.HORIZONTAL); 

這應該達到預期的效果。

+0

renderer.setOrientation(XYMultipleSeriesRenderer.Orientation.VERTICAL)招。謝謝 – beck

+0

另外我還需要交換x和y軸的值。它運行不正常。 Plz看看問題中上面的「更新1」。 – beck