0

我正在尝试绘制具有以下属性的阶梯图: x 轴:时间(毫秒)[实际数据包含此作为双精度值] y 轴:存储为整数的另一个值。

我正在填充数据集,如下所示:

private XYSeries populateStepChartDataSet(HashMap<Double, Integer> dataGrid){
    XYSeries xySeries = new XYSeries("Step Plot", true, true);

    if(dataGrid != null){
        for (Double timeStamp : dataGrid.keySet()) {
            xySeries.add(timeStamp, dataGrid.get(timeStamp));
        }
    }

    return xySeries;
}

我创建情节的部分如下:

        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(populateStepChartDataSet(dspDataGrid));

        final JFreeChart chart = ChartFactory.createXYStepChart(
            title,
            xAxisLabel, yAxisLabel,
            dataset,
            PlotOrientation.VERTICAL,
            true,   // legend
            true,   // tooltips
            false   // urls
        );

我期望的是该图在 x 轴上以毫秒为单位显示时间,但该值正在转换为一些奇怪的时间。这是情节的样子在此处输入图像描述

有人可以帮我取回 x 轴的 ms 格式的时间戳吗?

4

2 回答 2

2

看起来 x 轴正在格式化为日期,解决此问题的一种方法是提供NumberFormatOverride

创建后添加此代码chart

XYPlot plot = (XYPlot)chart.getPlot();
plot.setDomainAxis(0, new NumberAxis()); 
NumberAxis axis = (NumberAxis) plot.getDomainAxis();
axis.setNumberFormatOverride( new NumberFormat(){

    @Override
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {

    return new StringBuffer(String.format("%f", number));
    }

    @Override
    public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
    return new StringBuffer(String.format("%9.0f", number));
    }

    @Override
    public Number parse(String source, ParsePosition parsePosition) {
    return null;
    }
    } );
    axis.setAutoRange(true);
    axis.setAutoRangeIncludesZero(false);

然后你会得到这个图表:

图表

于 2012-06-13T16:37:54.643 回答
1

仅供参考,每当您遍历 Map 时使用entrySet()而不是遍历 keySet() 然后获取每个键的值。

于 2012-08-07T15:51:11.183 回答