使用JFreeChart的XYLineAndShapeRenderer绘制自定义线 [英] Draw Custom Lines using JFreeChart's XYLineAndShapeRenderer
问题描述
我有一些逻辑可以用来构建一系列集群.到目前为止,为表示图上每个点所属的群集,我使用了一系列颜色,其中属于同一群集的点具有相同的颜色.
I have some logic which I am using to construct a series of clusters. So far, to denote the cluster to which each point on the graph belongs to, I am using a series of colours, where points belonging to the same cluster are of the same colour.
除此之外,我还想显示每个集群的中心,因为这将帮助我了解集群构建算法的性能.目前,我要通过使用XPointerAnnotation
类在图形上编写一些文本.问题在于,我认为将文本放在点上会导致混乱的情节(考虑到很有可能会有数百个点).
Besides that, I would also like to display the centre of each cluster since this will help me see how my cluster building algorithm performs. To do this at the moment, I am writing some text on the graph through the use of the XPointerAnnotation
class. The problem with this is that I think that having text on top of points can lead to a messy plot (considering that it is highly likely that there will be hundreds of points).
我想到了从中心点到群集的每个成员向外绘制的线.我面临的问题是,我似乎找不到合适的方法来做到这一点.
I thought of drawing lines going outwards, from the centre point to each of the members of its cluster. The problem I am facing is that I can't quite seem to find the correct method or methods which does that.
我设法找到了源 > XYLineAndShapeRenderer
,并尝试将其用作指导,但是在绘图上我仍然没有绘制自定义线.我试图覆盖drawPrimaryLine
,drawPrimaryLineAsPath
和drawSecondaryPass
方法,但无济于事.
I have managed to find the source of XYLineAndShapeRenderer
and have tried to use it as a guide, but I still get no custom lines drawn on the plot. I have tried to override the drawPrimaryLine
, drawPrimaryLineAsPath
and drawSecondaryPass
methods, but to no avail.
我用来渲染线条的代码如下:
The code I am using to render the lines is as follows:
int x1 = (int) dataset.getXValue(series, 0);
int y1 = (int) dataset.getYValue(series, 0);
int x2 = (int) dataset.getXValue(series, item);
int y2 = (int) dataset.getYValue(series, item);
g2.drawLine(x1, y1, x2, y2);
System.out.println(String.format("Drawing %d %d %d %d %s", x1, y1, x2, y2, g2.getColor()));
State s = (State) state;
if (item == s.getLastItemIndex()) {
// draw path
drawFirstPassShape(g2, pass, series, item, s.seriesPath);
}
print语句打印正确的坐标和正确的颜色,因此似乎我正在添加的图形没有被渲染.我尝试在执行代码之前和之后都调用super
,但也无济于事.
The print statement prints the right coordinates and the right colours, so it just seems that the graphics that I am adding is not being rendered. I have tried calling super
, both before and after my code is executed but to no avail either.
任何方向将不胜感激. 谢谢.
Any directions would be appreciated. Thanks.
推荐答案
更仔细地查看所发布的代码,从dataset
获得的 xy 值表示 data 坐标.在渲染这样的点之前,必须将其转换为相对于dataArea
的 graphics 坐标.例如,
Looking more closely at the code posted, the xy value obtained from the dataset
represents a point in data coordinates. Before such a point can be rendered, it must be transformed into graphics coordinates, relative to the dataArea
. As an example, drawPrimaryLineAsPath()
uses the corresponding axis method, valueToJava2D()
, to convert a data value to a graphics coordinate.
double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation);
double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation);
附录:drawPrimaryLineAsPath()
方法/XYLineAndShapeRenderer.html#line.887"rel =" nofollow> drawItem()
仅当drawSeriesLineAsPath
为true
时,例如setDrawSeriesLineAsPath(true)
.
Addendum: The drawPrimaryLineAsPath()
method is invoked from drawItem()
only when drawSeriesLineAsPath
is true
, e.g. setDrawSeriesLineAsPath(true)
.
这篇关于使用JFreeChart的XYLineAndShapeRenderer绘制自定义线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!