java JFreeChart 问题,X轴显示的是时间,但是数据太多,挤成一团,如何让X轴只显示10个时间

1、x轴的数据太多了 重叠了 看不清楚,我想让X 轴的数据竖着斜着显示~

xAxis: {categories: [],labels: {rotation: -10,align: 'right',style: { font: 'normal 13px Verdana, sans-serif'}}},yAxis: {min: 0,title: {text: '媒体舆情总量'}},legend: {enabled: false},tooltip: {formatter: function() {return ''+ this.x +''+'媒体舆情总量为: '+ Highcharts.numberFormat(this.y, 0) + ' 条';}},
这个属性里面-10的那个参数就是设置的 自己研究吧

// 设置x轴的显示刻度
domainAxis.setAutoTickUnitSelection(false);
DateTickUnit tickUnit = new DateTickUnit(DateTickUnitType.YEAR, 2, new SimpleDateFormat("yyyy")); // 第二个参数是时间轴间距
domainAxis.setTickUnit(tickUnit);
domainAxis.setMinimumDate(sdf.parse(data[0][0]-1+""));
domainAxis.setMaximumDate(sdf.parse(data[data.length-1][0]+2+""));
这个好像只针对DateAxis,不知道有用吗,折线图还没分析过,大概看了下类包,没找到这个类。DateTickUnit这个类可以调整x轴显示的个数。

调整前(默认1年单位)

调整后(5年单位)

取得XYPlot,然后在DomainAxis里可以设置刻度。

(找了1小时。。。求采纳,没有功劳也有苦劳啊)

下面的代码是5年单位显示。

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class TimeSeriesChartSample {

    public static void main(String[] args) throws IOException {
        // Create Data
        TimeSeries s1 = new TimeSeries("XXXX");
        for (int i = 0; i < 10; i++) {
            int year = 2000 + i;
            s1.add(new Month(1, year), 101.8);
            s1.add(new Month(2, year), 104.8);
            s1.add(new Month(3, year), 103.3);
            s1.add(new Month(4, year), 105.8);
            s1.add(new Month(5, year), 110.6);
            s1.add(new Month(6, year), 120.8);
            s1.add(new Month(7, year), 115.3);
            s1.add(new Month(8, year), 130.9);
            s1.add(new Month(9, year), 131.7);
            s1.add(new Month(10, year), 140.2);
            s1.add(new Month(11, year), 141.8);
            s1.add(new Month(12, year), 160.6);
        }

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);

        // Create JFreeChart
        JFreeChart chart = ChartFactory.createTimeSeriesChart("TITLE", "TIME AXIS LABEL",
                "VALUE AXIS LABEL", dataset, true, true, false);
        
        // 这里是关键
        XYPlot xyplot = (XYPlot) chart.getPlot();
        DateAxis domainAxis = (DateAxis) xyplot.getDomainAxis(); //x轴设置
        domainAxis.setTickUnit(new DateTickUnit(DateTickUnit.YEAR, 5, new SimpleDateFormat("yyyy")));

        // Output
        File outputFile = new File("SampleTimeSeriesChart.png");
        ChartUtilities.saveChartAsPNG(outputFile, chart, 500, 500);
    }
}

推荐这里,Jfreechart大全:

http://my.oschina.net/abian/blog/278465



数据抽取一些出来,即是进步大一些

相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网