数字轴setTickUnit包含指定数字 [英] Number axis setTickUnit to contain a specified number
问题描述
我正在使用JFreeChart显示正态分布,并将刻度号更改为标准偏差;但我也希望在中间出现刻度线时始终具有平均"价值.交叉发布在此处.
I am displaying normal distribution using JFreeChart, and I change the tick number to standard deviation; but I would also want there to always be 'mean' value in the middle from which ticks emerge. Cross-posted here.
所以标准偏差= 2;平均值= 1
So standard deviation = 2 ; mean = 1
-3 -1 1 3 5
标准偏差= 5;平均值= 15
Standard deviation = 5 ; mean = 15
0 5 10 15 20 25 30
JFreeChart.java
JFreeChart.java
public class JFreeChartPanel extends JPanel {
private final XYPlot plot;
double mean = 0.0, sd = 1.0;
XYDataset dataset = initDataset();
NumberAxis domain = new NumberAxis("Y") {
@Override
protected double calculateLowestVisibleTickValue() {
double lowTickValue = super.calculateLowestVisibleTickValue();
if (mean % 2 == 1) {
return lowTickValue + 1;
} else {
return lowTickValue;
}
}
};
public JFreeChartPanel(){
JFreeChart chart = ChartFactory.createXYLineChart(
"Normal Distribution",
"X",
"PDF",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false
);
plot=chart.getXYPlot();
domain.setAutoRangeStickyZero(false);
domain.setTickUnit(new NumberTickUnit(sd));
plot.setDomainAxis(domain);
final ChartPanel chartPanel = new ChartPanel(chart);
setLayout(new BorderLayout());
add(chartPanel);
}
private XYDataset initDataset() {
double minX=mean-(4*sd),maxX=mean+(4*sd);
Function2D normal = new NormalDistributionFunction2D(mean, sd);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, minX, maxX, 100, "Normal");
return dataset;
}
public double getMean() {
return mean;
}
public void setMean(double mean) {
this.mean = mean;
plot.setDataset(initDataset());
}
public double getSd() {
return sd;
}
public void setSd(double sd) {
this.sd = sd;
domain.setTickUnit(new NumberTickUnit(sd));
plot.setDataset(initDataset());
}
}
UI.java
public class UI extends javax.swing.JFrame {
public UI() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
Auto-generated Netbeans GUI COde
}
public void updateMean()
{
try{
double m = Double.parseDouble(mean.getText());
jFreeChartPanel.setMean(m);
}catch(Exception e){
}
}
public void updateSd()
{
try{
double sd = Double.parseDouble(standardDeviation.getText());
jFreeChartPanel.setSd(sd);
}catch(Exception e){
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UI().setVisible(true);
}
});
}
private javax.swing.JPanel inputPanel;
private main.JFreeChartPanel jFreeChartPanel;
private javax.swing.JPanel jPanel1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JTextField mean;
private javax.swing.JLabel meanLabel;
private javax.swing.JTextField standardDeviation;
private javax.swing.JLabel standardDeviationLabel;
}
推荐答案
从这个示例开始,我做了以下工作根据您的变化进行更改,以得到以下所示的µ = 15和σ= 5的结果:
Starting from this example, I made the following changes, adapted from yours, to get the result illustrated below for µ=15 and σ=5:
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
…
private double mean = 15.0, sigma = 5.0;
…
private XYDataset initDataset() {
double minX = mean - (3 * sigma), maxX = mean + (3 * sigma);
Function2D normal = new NormalDistributionFunction2D(mean, sigma);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, minX, maxX, 100, "Normal");
return dataset;
}
…
public JFreeChartPanel() {
…
plot = chart.getXYPlot();
NumberAxis domain = (NumberAxis) plot.getDomainAxis();
domain.setTickUnit(new NumberTickUnit(sigma));
…
}
在另一种情况下,µ = 1和σ= 2,需要重写NumberAxis
方法calculateLowestVisibleTickValue()
,以使刻度线落在奇数值上.
Your other case, µ=1 and σ=2, requires overriding the NumberAxis
method calculateLowestVisibleTickValue()
to make ticks fall on odd values.
NumberAxis domain = new NumberAxis("Y") {
@Override
protected double calculateLowestVisibleTickValue() {
double lowTickValue = super.calculateLowestVisibleTickValue();
if (mean % 2 == 1) {
return lowTickValue + 1;
} else {
return lowTickValue;
}
}
};
domain.setTickUnit(new NumberTickUnit(sigma));
plot = chart.getXYPlot();
plot.setDomainAxis(domain);
为什么图形的平均值3和sd 1也略微偏斜,平均值6 sd 2,平均值9 sd 3依此类推;以此类推;这是显示此内容的图片.
如此处所述,自动确定范围时,会影响到添加到轴范围的边距大小的标记."
As discussed here, there's "A flag that affects the size of the margins added to the axis range when the range is determined automatically."
domain.setAutoRangeStickyZero(false);
我添加了...代码.
I've added…code.
将修订版6 JFreeChartPanel
的实例添加到具有== 1和σ= 2的JFrame
产生以下结果:
Adding and instance of your revision 6 JFreeChartPanel
to a JFrame
with µ=1 and σ=2 produces the following result:
这种情况的发生率更高.
This happens with higher mean.
您可以从自动量程中排除零.
You can exclude zero from the auto-range.
domain.setAutoRangeIncludesZero(false);
这篇关于数字轴setTickUnit包含指定数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!