我使用Jfreechart API 1.0.8来生成TimeSeriesChart(折线图)。
当我生成图表时,我面临着重叠的问题。在这里,我试图通过结合使用XYLineAndShapeRenderer和StandardXYItemLabelGenerator来显示渲染点(图形渲染点)。
当显示点时,数据点与生成的折线图(图)重叠。我将X轴作为时间,Y轴作为组织的收入,我在这里使用折线图。
我将按照下面的讨论显示这些点。
通过使用"renderer.setBasePositiveItemLabelPosition“方法,我在考虑呈现的"ItemLabelAnchor”的同时,全局地设置了图形点(数据点)在xyplot呈现的图表中的位置。
我在这里发送我的示例代码:
chart = ChartFactory.createTimeSeriesChart("", "", "", newxyseries, false, true, false);
renderer = new XYLineAndShapeRenderer();
renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer();
renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator("{2}", monthDate, formatSymbol));
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.TOP_RIGHT));
chart.getXYPlot().setRenderer(renderer);但在使用Excel的Ms-office工具生成图表时,没有出现标签重叠的问题,点的显示效果很好,没有任何重叠。
发布于 2014-07-04 15:28:38
JFreeChart不会对项目标签进行任何重叠检测。这将是一个伟大的功能,但没有人写的代码来做到这一点。
发布于 2021-01-05 03:51:24

您必须通过定义自己的算法或逻辑来避免标签重叠,因为随着图的不断增长,标签将更快地开始重叠。关键是使一些或替代标签变得透明,这样就不会发生重叠。例如,在下面的P.O.C中,将只绘制35个标签,但图形的刻度将保持不变,而标签将减少
CategoryAxis domainAxis = plot.getDomainAxis();
CategoryLabelPositions pos = domainAxis.getCategoryLabelPositions();
double size = plot.getCategories().size();
double occurence = Math.ceil(plot.getCategories().size() / 20);
double interval = Math.ceil(plot.getCategories().size() / 20);
for (int i = 1; i <= plot.getCategories().size(); i++) {
if (plot.getCategories().size() > 35) {
if(plot.getCategories().size()>35 && plot.getCategories().size()<250) {
if(i%8==0){
String cat_Name = (String) plot.getCategories().get(i-1);
} else{
String cat_Names = (String) plot.getCategories().get(i-1);
domainAxis.setTickLabelPaint(cat_Names, new Color(0,0,0,0));
}
}else if(plot.getCategories().size()>250){
[![enter image description here][1]][1]
if (i == occurence) {
String cat_Name = (String) plot.getCategories().get(i - 1);
if (occurence + interval >= size) {
// occurence=size;
} else {
occurence = occurence + interval;
}
} else {
String cat_Names = (String) plot.getCategories().get(i - 1);
domainAxis.setTickLabelPaint(cat_Names, new Color(0, 0, 0, 0));
}
}
}下面的线条将使标签透明
domainAxis.setTickLabelPaint(cat_Names, new Color(0,0,0,0));https://stackoverflow.com/questions/24568190
复制相似问题