java - 如何在 XYLineChart 中设置域和范围轴的对数刻度

标签 java jasper-reports jfreechart dynamic-reports

有没有办法设置x轴和y轴的对数刻度。

我在下面添加了用于创建图表的引用代码。

XyLineChartBuilder lineBuilder = DynamicReports.cht.xyLineChart()
    .setTitle(reportSection.getGraphName())
    .setTitleFont(boldFont)
    .setXValue(xColumn)
    .series(yAxisLineSeries)
    .setXAxisFormat(DynamicReports.cht.axisFormat()
    .setLabel(reportSection.getxAxisCaption()))
    .setYAxisFormat(DynamicReports.cht.axisFormat()
    .setLabel(reportSection.getyAxisCaption()))
    .setDataSource(createDataSource(reportSection, noOfYaxis));

最佳答案

I tried with base 10 and base 2, but both didn't plot negative values.

LogarithmicAxis提供一个 setAllowNegativesFlag() 方法,可以设置“true 以允许数据中出现负值”,或设置“false”以便能够绘制任意接近于零的正值。”我已经改编了这个example来说明效果。

log axis

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @See https://stackoverflow.com/a/53912014/230513
 * @see https://stackoverflow.com/a/22450677/230513
 * @see https://stackoverflow.com/a/10353270/230513
 */
public class Test {

    private static final int N = 10;

    private void display() {
        XYSeries series = new XYSeries("Series");
        for (int i = -N; i <= N; i++) {
            series.add(i, i);
        }
        LogarithmicAxis xAxis = new LogarithmicAxis("X");
        xAxis.setAllowNegativesFlag(true);
        LogarithmicAxis yAxis = new LogarithmicAxis("Y");
        yAxis.setAllowNegativesFlag(true);
        XYPlot plot = new XYPlot(new XYSeriesCollection(series),
            xAxis, yAxis, new XYLineAndShapeRenderer(true, false));
        JFreeChart chart = new JFreeChart(
            "Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(700, 400);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}

关于java - 如何在 XYLineChart 中设置域和范围轴的对数刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53895733/

相关文章:

java - 无法实例化集合持久化器

mysql - 在 Jasper 报告变量中使用 SUM

java - 如何使用 JFreeChart 库创建空心饼图

java - JFreeChart 折线图,每个点都有文本

java - 如何将 MySQL Timestamp 转换为 JFreeChart TimeSeries Graph X 轴值?

java - 不使用 split() 分割单词

java - 在 Java 中执行设置文件的最简单方法是什么?

java - 如何获取jar文件的包信息

jasper-reports - Jasper Reports - 如何让列页脚直接出现在详细信息区域下方

arraylist - 在JasperSoft Studio报告中显示特定格式的ArrayList参数