简体   繁体   中英

Inappropriate view of x-axis labels in jfreecharts.

I have an issue with my jfreechart graph. Actually at x-axis the value are in big numbers (ie 1500-2000) and the amounts of these values are also too large (ie in significant notation).So the chart is just showing a dashed line instead of values at x-axis.

public static byte[] createImageByXyChart(String str_chartLabel,
        String str_xAxisLabel, String str_yAxisLabel,
        String str_fileFormat, Integer i_height, Integer i_width,
        List<double[]> co_ordinates) throws IOException {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (int i = 0; i < co_ordinates.size(); i++) {
        double[] xy_coOrdinates = co_ordinates.get(i);
        double xCoordiate = xy_coOrdinates[0];
        double yCoordiate = xy_coOrdinates[1];
        dataset.setValue(yCoordiate, str_chartLabel, "" + xCoordiate);

}

      JFreeChart chart =
     ChartFactory.createLineChart(str_chartLabel,str_xAxisLabel,
     str_yAxisLabel, dataset);
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setRangePannable(true);
    plot.setRangeGridlinesVisible(true);
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlineStroke(new BasicStroke(1.0f));
    plot.setRangeGridlineStroke(new BasicStroke(1.0f));
    plot.setDomainGridlinePaint(Color.GRAY.brighter());
    plot.setRangeGridlinePaint(Color.GRAY.brighter());

    ChartPanel panel = new ChartPanel(chart);

    JFrame frame = new JFrame("Show chart");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);

    frame.setSize(600, 600);
    frame.setVisible(true);

    return null;
}
public static void main(String[] arg) throws Exception {

    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@  ");
    File fileData = new File("curve.txt");

    BufferedReader br = new BufferedReader(new FileReader(fileData));
    List<double[]> co_ordinates = new ArrayList<double[]>();
    String line = null;

    while ((line = br.readLine()) != null) {

        String[] abcd = line.split(",");
        double[] xy_cor = new double[2];
        NumberFormat df = new DecimalFormat("######.####");
        xy_cor[0] = Double.parseDouble(df.format(Double
                .parseDouble(abcd[0])));
        System.out.println(xy_cor[0]);
        xy_cor[1] = Double.parseDouble(abcd[1]);

        System.out.println(Arrays.toString(xy_cor));
        co_ordinates.add(xy_cor);

    }

    br.close();



    String str_chartLaber = "Curve";
    String str_xAxisLabel = "Time(s)";
    String str_yAxisLaber = "Accelaration (mm_per__s_2)";
    String str_fileFormate = ImageFormats.JPEG;
    Integer heigth = 1800;
    Integer width = 1080;

    byte[] image_bytes = ImageUtils.createImageByXyChart(str_chartLaber,
            str_xAxisLabel, str_yAxisLaber, str_fileFormate, heigth, width,
            co_ordinates);

    }

它给了我这个没有x轴值的图表 the input data is that

Heading

i use

XYDataset dataset ;

instead of

DefaultCategoryDataset dataset = new DefaultCategoryDataset();

The XYDataset enable auto interval where DefaultCategoryDataset do not.I don't know the reason but it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM