如何在JFreechart中的CategoryPlot中使用DateAxis作为x轴(使用createStackedAreaChart()方法)

时间:2016-03-30 11:10:28

标签: date format jfreechart stacked-area-chart

我生成stackedAreaChart值{&};日期轴,它生成很好,但我不想显示每个&每个日期,因为它看起来不好或不可能显示每个日期和每个日期持续很长时间。 这是我的代码:

ArrayList al_Main = (ArrayList)xyChartDetails.get("totalAlist");
ArrayList relativePriceMvmtOn = (ArrayList)xyChartDetails.get("recordName");
String strLegendMsg=(String)xyChartDetails.get("LegendMsg");
String strFileName=(String)xyChartDetails.get("FileName");
String series = null;
String temp =null;
final CategoryDataset dataset =null;
double data[][]=null;
double date2[][]=null;
int height = 300;
int width = 750;
String xaxisLabel=(String)xyChartDetails.get("X-Axis");
String yaxisLabel=(String)xyChartDetails.get("Y-Axis");
SimpleDateFormat stformat = new SimpleDateFormat("dd-MMM-yyyy");
DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset();
JFreeChart chart=null;
try{
    for(int i=0;i<al_Main.size();i++)
    {           
        series = (String)relativePriceMvmtOn.get(i);
        ArrayList al_under = (ArrayList) al_Main.get(i);
        String strName=null;
        double strVal=0.0;
        for(int j=0;j<al_under.size();j++)
        {
        temp=(String)al_under.get(j);
        if(temp != null) {
                strName=temp.substring(temp.indexOf(",")+1);

                strVal=Double.parseDouble(strName.substring(0,strName.indexOf("~")));

                String date =strName.substring(strName.indexOf("~")+1);

                    line_chart_dataset.addValue( strVal , series , df.parse(date) );

        }
        }
    }

    chart = createChart(line_chart_dataset,strLegendMsg,xaxisLabel,yaxisLabel);
    final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
    final File file1 = new File(strFileName);       
    ChartUtilities.saveChartAsPNG(file1, chart, width , height, info);

这是图表生成方法:

public JFreeChart createChart(CategoryDataset dataset,String legendMsg,String xAxis,String yAxis) {

    final JFreeChart chart = ChartFactory.createStackedAreaChart(
            legendMsg,      // chart title
            xAxis,                // domain axis label
            yAxis,           // range axis label
        dataset,                   // data
        PlotOrientation.VERTICAL,  // orientation
        true,                      // include legend
        true,
        false
    );
    chart.setBackgroundPaint(Color.white);

    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setForegroundAlpha(0.5f);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    final CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    //domainAxis.setTickLabelPaint(Color.white);


    // change the auto tick unit selection to integer units only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    final CategoryItemRenderer renderer = plot.getRenderer();
    renderer.setItemLabelsVisible(true);
    //final DateAxis axis = (DateAxis) plot.getDomainAxis();
    //axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); 
    return chart;

}

任何领导都将不胜感激。 谢谢&amp;问候, Javed Ansari

0 个答案:

没有答案