java - 如何在 XYLineChart 中为域和范围轴设置对数刻度
问题描述
有没有办法为 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));
解决方案
我尝试使用基数 10 和基数 2,但两者都没有绘制负值。
LogarithmicAxis
提供了一种setAllowNegativesFlag()
方法,可以设置为“true
允许数据中的负值”或“false
能够绘制任意接近零的正值”。我已经修改了这个例子来说明效果。
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();
}
});
}
}
推荐阅读
- c++ - 新操作员的异常安全
- spring - spring 集成 jsonignore 属性
- javascript - 是否有一种有效的方法可以从 NgRx 状态中选择多个位置的相同数据并处理该选择的许多订阅?
- python - 安装的 Python 包 spaCy 抛出以下错误:
- python - 如何在 gRPC Python 中发送服务器流的初始元数据
- node.js - 想要显示未运行 mongodb 的失败消息
- python - Python使用最后n行创建数据框
- google-sheets - 将 FILTER 的 SUM 应用于工作表中的所有行(如使用 ARRAYFORMULA)
- .net - IIS 日志未显示 .NET Core 应用程序的“cs-username”字段
- android - Android 检查最新的依赖版本