我是JFreeChart
的新手,并且不知道如何解决这个问题;我不知道是否有可能做我假装的事情。问题在于,我试图在Java Swing应用程序中显示图形,我传递日期和要显示的值。日期是由用户选择的,也许不是连续的;我的意思是,如果用户选择从星期四到星期一,星期六和星期日都可以避免,并在星期四,星期五和星期一转到图表。问题是图表从星期四到星期一(星期六和星期日包括)并且在X轴上显示它们不应该。基于此example,我的课程如下:
ArrayList<StringBuffer> dates={20-02-2015,23-02-2015,24-02-2015,25-02-2015,26-02-2015};
args="tittle";
frame=(the frame where I do the call,although it's not needed in myChartPanelDemo2,just added for setting errorDialogs purpose);
ArrayList<StringBuffer> amount = {"serie1","serie2"};
myChartPanelDemo2.init(amount,args,dates,frame);
public class myChartPanelDemo2 {
static int datasetIndex;
private static ChartPanel chartPanel;
public static JPanel init(ArrayList<StringBuffer> amount, String args,
ArrayList<StringBuffer> dates, JFrame frame) {
//*****just getting the values***********//
datasetIndex = 0;
int cantity = amount.size();
int cantity2 = fechas.size();
float[][] resultArray = new float[cantity][cantity];
float[] result = new float[cantity2];
for (int x = 0; x < cantity; x++) {
for (int y = 0; y < cantity2; y++) {
result[y] = callingDataBase.main(amount.get(x), args, dates.get(y));
}
for (int v = 0; v < result.length; v++) {
resultArray[x][v] = result[v];
}
//resultArray will be the array from where chartPanel will take the values
}
XYDataset roiData = createDataset(args, dates, resultArray, amount);
JFreeChart chart = ChartFactory.createTimeSeriesChart(args, "Days",
"Values", roiData, true, true, false);
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
chartPanel = new ChartPanel(chart);
JPanel f = new JPanel(new BorderLayout());
f.add(chartPanel, BorderLayout.CENTER);
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
JPanel panel = new JPanel(new BorderLayout());
panel.add(createTrace(), BorderLayout.SOUTH);
f.add(panel, BorderLayout.SOUTH);
f.setVisible(true);
chartPanel.setSize(new Dimension(frame.getWidth(), frame.getHeight()));
chartPanel.setMaximumSize(new Dimension(frame.getWidth(), frame.getHeight()));
chartPanel.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight()));
return f;
}
/** @see https://stackoverflow.com/a/5522583/230513 */
private static JComboBox<String> createTrace() {
final JComboBox<String> trace = new JComboBox<String>();
final String[] traceCmds = {"Enable Trace", "Disable Trace"};
trace.setModel(new DefaultComboBoxModel<String>(traceCmds));
trace.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (traceCmds[0].equals(trace.getSelectedItem())) {
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
chartPanel.repaint();
} else {
chartPanel.setHorizontalAxisTrace(false);
chartPanel.setVerticalAxisTrace(false);
chartPanel.repaint();
}
}
});
return trace;
}
private static XYDataset createDataset(String name,
ArrayList<StringBuffer> dates, float[][] resultArray,
ArrayList<StringBuffer> amount) {
TimeSeriesCollection tsc = new TimeSeriesCollection();
for (int x = 0; x < GraficoEmpresas.size(); x++) {
tsc.addSeries(createSeries(GraficoEmpresas.get(x).toString(), fechas,
resultArray, x));
}
return tsc;
private static TimeSeries createSeries(String name,
ArrayList<StringBuffer> dates, float[][] resultArray, int x) {
final TimeSeries series = new TimeSeries(name);
int cantity2 = dates.size();
RegularTimePeriod t = new Day();
for (int y = 0; y < cantity2; y++) {
Date fecha;
SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
day = new Date();
try {
day = formatoDelTexto.parse(fechas.get(y).toString());
} catch (ParseException e) {
e.printStackTrace();
}
RegularTimePeriod p = new Day(day);
series.addOrUpdate(p, resultArray[x][y]);
}
return series;
}
}
我无法上传图片,信誉不足,但结果是x轴值的图表:{20-02-2015 - 21-02-2015 - 22-02-2015 - -23-02-2015 - 24-02-2015 - 25-02-2015 - 26-02-2015}在21和22之外的每个点都有数据集值,但在轴标签中添加了21和22。
我同时使用JFreechart和JCommon 1.0.16。 我想这是一个自动完成问题,但这只是一个猜测,我无法检查它是否真实。 有谁知道如何避免这种情况或者这是不可能的? 谢谢!!
答案 0 :(得分:1)
如果没有完整的示例,则很难确定,但您可以将SegmentedTimeline
应用于所选DataAxis
创建的ChartFactory
。使用静态工厂方法newMondayThroughFridayTimeline()
创建星期一到星期五SegmentedTimeline
,如图here所示。您可以从图表的图中获得DataAxis
的引用;或者,跳过ChartFactory
并创建图表 de novo ,如图here所示。
答案 1 :(得分:1)
非常感谢你!只需添加这两行解决了问题:
来源是:this example
ValueAxis domainAxis = plot.getDomainAxis();
((DateAxis) domainAxis).setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() );
整个班级将是:
public class myChartPanelDemo2 {
static int datasetIndex;
private static ChartPanel chartPanel;
public static JPanel init(ArrayList<StringBuffer> amount, String args,
ArrayList<StringBuffer> dates, JFrame frame) {
// *****只获取值*********** //
datasetIndex = 0;
int cantity = amount.size();
int cantity2 = fechas.size();
float[][] resultArray = new float[cantity][cantity];
float[] result = new float[cantity2];
for (int x = 0; x < cantity; x++) {
for (int y = 0; y < cantity2; y++) {
result[y] = callingDataBase.main(amount.get(x), args, dates.get(y));
}
for (int v = 0; v < result.length; v++) {
resultArray[x][v] = result[v];
}
// resultArray将是chartPanel将取值
的数组 }
XYDataset roiData = createDataset(args, dates, resultArray, amount);
JFreeChart chart = ChartFactory.createTimeSeriesChart(args, "Days",
"Values", roiData, true, true, false);
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
ValueAxis domainAxis = plot.getDomainAxis();
((DateAxis) domainAxis).setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() );
chartPanel = new ChartPanel(chart);
JPanel f = new JPanel(new BorderLayout());
f.add(chartPanel, BorderLayout.CENTER);
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
JPanel panel = new JPanel(new BorderLayout());
panel.add(createTrace(), BorderLayout.SOUTH);
f.add(panel, BorderLayout.SOUTH);
f.setVisible(true);
chartPanel.setSize(new Dimension(frame.getWidth(), frame.getHeight()));
chartPanel.setMaximumSize(new Dimension(frame.getWidth(), frame.getHeight()));
chartPanel.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight()));
return f;
}
/** @see https://stackoverflow.com/a/5522583/230513 */
private static JComboBox<String> createTrace() {
final JComboBox<String> trace = new JComboBox<String>();
final String[] traceCmds = {"Enable Trace", "Disable Trace"};
trace.setModel(new DefaultComboBoxModel<String>(traceCmds));
trace.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (traceCmds[0].equals(trace.getSelectedItem())) {
chartPanel.setHorizontalAxisTrace(true);
chartPanel.setVerticalAxisTrace(true);
chartPanel.repaint();
} else {
chartPanel.setHorizontalAxisTrace(false);
chartPanel.setVerticalAxisTrace(false);
chartPanel.repaint();
}
}
});
return trace;
}
private static XYDataset createDataset(String name,
ArrayList<StringBuffer> dates, float[][] resultArray,
ArrayList<StringBuffer> amount) {
TimeSeriesCollection tsc = new TimeSeriesCollection();
for (int x = 0; x < GraficoEmpresas.size(); x++) {
tsc.addSeries(createSeries(GraficoEmpresas.get(x).toString(), fechas,
resultArray, x));
}
return tsc;
private static TimeSeries createSeries(String name,
ArrayList<StringBuffer> dates, float[][] resultArray, int x) {
final TimeSeries series = new TimeSeries(name);
int cantity2 = dates.size();
RegularTimePeriod t = new Day();
for (int y = 0; y < cantity2; y++) {
Date fecha;
SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
day = new Date();
try {
day = formatoDelTexto.parse(fechas.get(y).toString());
} catch (ParseException e) {
e.printStackTrace();
}
RegularTimePeriod p = new Day(day);
series.addOrUpdate(p, resultArray[x][y]);
}
return series;
}
}