同一数据上有多个轴 [英] Multiple axes on the same data
问题描述
我正在尝试在同一数据上有两个轴。
I'm trying to have two axes on the same data.
数据是几个 DefaultTableXYDatasets
。该图是 XYPlot
,我有两个 XYLineAndShapeRenderers
和一个 StackedXYAreaRenderer2
。
The data is a couple of DefaultTableXYDatasets
. The plot is a XYPlot
, and I have two XYLineAndShapeRenderers
and one StackedXYAreaRenderer2
.
y值的所有数据都以米为单位,我想让一个轴以米为单位显示,一个轴以英尺显示。现在这感觉很常见,但我无法决定最明显的方法。一种方法是复制数据并使y值以英尺为单位,然后添加另一个 NumberAxis
并完成它。
All data is in meters for the y-values, and I want to have one axis displaying it in meters and one axis displaying it in feet. Now this feels like a common thing to do, but I can't decide on the most obvious way to do it. One way that works would be to duplicate the data and have the y-values in feet, then add another NumberAxis
and be done with it.
但我认为继承 NumberAxis
更为明智,或者将一些功能注入 NumberAxis
来缩放值。或者我应该采用第一种方法?
But I thought it would be wiser to subclass NumberAxis
, or inject some functionality into NumberAxis
to scale the values. Or should I go with the first approach?
您怎么看?
推荐答案
最终我决定使用这个解决方案,它可能不是最优雅但它有效。我有第二个轴 feetAxis
,并在名为 meterAxis <的第一个轴上添加了
AxisChangeListener
/ code>。当 meterAxis
更改时,设置 feetAxis
的范围。
Eventually i settled on this solution, it might not be the most elegant but it worked. I have a second axis feetAxis
, and added a AxisChangeListener
on the first axis called meterAxis
. When the meterAxis
changes set the range on feetAxis
.
我使用 SwingUtilities.invokeLater
,否则缩小图表时范围会不正确,然后 feetAxis
只会从0变为1.虽然没有检查原因。
I used SwingUtilities.invokeLater
, otherwise the range would be incorrect when zooming out of the chart, then the feetAxis
would only go from 0 to 1. Didn't check why though.
feetAxis = new NumberAxis("Height [ft]");
metersAxis = new NumberAxis("Height [m]");
pathPlot.setRangeAxis(0, metersAxis);
pathPlot.setRangeAxis(1, feetAxis);
metersAxis.addChangeListener(new AxisChangeListener() {
@Override
public void axisChanged(AxisChangeEvent event) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
feetAxis.setRange(metersAxis.getLowerBound() * MetersToFeet, metersAxis.getUpperBound() * MetersToFeet);
}
});
}
});
这篇关于同一数据上有多个轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!