2

I generate a stackedAreaChart with value & date axis and it is generating fine, but I don't want to show each & every date because it is not looking fine or it is not possible to show each & every date for long duration. Here is my code:

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);

And this is the chart generation method:

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;

}

Any lead Would be appreciated. Thanks & Regards, Javed Ansari

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34

0 Answers0