gpt4 book ai didi

java - JFreeChart:如何为蜘蛛图中的系列设置渐变绘画

转载 作者:行者123 更新时间:2023-11-30 06:20:18 26 4
gpt4 key购买 nike

我有一张包含此演示文稿的图表:

Current chart

但我需要这样做:

Expected chart

如何正确设置系列的渐变绘画?。这是我所拥有的:

public class SpiderWebChartDemo1 extends ApplicationFrame {

public SpiderWebChartDemo1(String s) {
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}

private static CategoryDataset createDataset() {
String s = "First";
String s3 = "Self leadership";
String s4 = "Organization leadership";
String s5 = "Team leadership";

DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, s, s3);
defaultcategorydataset.addValue(4D, s, s4);
defaultcategorydataset.addValue(3D, s, s5);
return defaultcategorydataset;
}

private static JFreeChart createChart(CategoryDataset categorydataset) {
Color bckColor1 = Color.decode("#4282CE"); //Light blue
Color bckColor2 = Color.decode("#9BC1FF"); //Dark blue
Color axisColor = Color.decode("#DD0010"); //Red

SpiderWebPlot plot = new SpiderWebPlot(categorydataset);
Paint p = new GradientPaint(0,0,bckColor1,0,0,bckColor2);

plot.setSeriesPaint(p);
plot.setAxisLinePaint(axisColor);

JFreeChart chart = new JFreeChart("Spider Web Chart Demo 1"
, TextTitle.DEFAULT_FONT, plot, false);

LegendTitle legendtitle = new LegendTitle(plot);
legendtitle.setPosition(RectangleEdge.BOTTOM);
chart.addSubtitle(legendtitle);
return chart;
}

public static JPanel createDemoPanel() {
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}

public static void main(String args[]) {
SpiderWebChartDemo1 spiderwebchartdemo1 = new SpiderWebChartDemo1("SpiderWebChartDemo1");
spiderwebchartdemo1.pack();
RefineryUtilities.centerFrameOnScreen(spiderwebchartdemo1);
spiderwebchartdemo1.setVisible(true);
}
}

我在条形图中看到过渐变绘制,但在蜘蛛图中却没有看到。我得到的只是透明系列。

谢谢。

最佳答案

您正在正确设置油漆,但是您应该意识到两件事。

  • java 中的渐变绘制声明了起点和终点。第一种颜色将从点 1 开始,并在点 2 转换为颜色 2。如果您使用它来绘制多边形,则这些点与多边形尺寸无关。这是要显示的图片,图片中的 pt1 和 pt2 是定义渐变起点和终点的位置。

enter image description here

  • 在理想情况下,库中的每个设置都是可编辑的,但很多时候情况并非如此。我们可以通过重写子类中的方法来克服这个问题。您将需要覆盖 SpiderWebPlot 类并实现一些绘画方法。这是我编写的一个快速类(class),它就是这样做的。

看看它实际绘制多边形的最后一端。我直接从 SpiderWebPlot 源代码中获取并修改了最后。要在您的程序中使用它,请像这样调用它GradientSpiderWebPlot plot = new GradientSpiderWebPlot(categorydataset, Color.decode("#4282CE"), Color.decode("#9BC1FF"), .8f);

这是结果

enter image description here

public class GradientSpiderWebPlot extends SpiderWebPlot {

private Color startColor, endColor;
private float alpha;

public GradientSpiderWebPlot(CategoryDataset data, Color startColor, Color endColor, float alpha) {
// TODO Auto-generated constructor stub
super(data);
this.startColor = startColor;
this.endColor = endColor;
this.alpha = alpha;
}

@Override
protected void drawRadarPoly(Graphics2D g2,
Rectangle2D plotArea,
Point2D centre,
PlotRenderingInfo info,
int series, int catCount,
double headH, double headW) {

Polygon polygon = new Polygon();

EntityCollection entities = null;
if (info != null) {
entities = info.getOwner().getEntityCollection();
}

// plot the data...
for (int cat = 0; cat < catCount; cat++) {

Number dataValue = getPlotValue(series, cat);

if (dataValue != null) {
double value = dataValue.doubleValue();

if (value >= 0) { // draw the polygon series...

// Finds our starting angle from the centre for this axis

double angle = getStartAngle()
+ (getDirection().getFactor() * cat * 360 / catCount);

// The following angle calc will ensure there isn't a top
// vertical axis - this may be useful if you don't want any
// given criteria to 'appear' move important than the
// others..
// + (getDirection().getFactor()
// * (cat + 0.5) * 360 / catCount);

// find the point at the appropriate distance end point
// along the axis/angle identified above and add it to the
// polygon

Point2D point = getWebPoint(plotArea, angle,
value / this.getMaxValue());
polygon.addPoint((int) point.getX(), (int) point.getY());

// put an elipse at the point being plotted..

Paint paint = getSeriesPaint(series);
Paint outlinePaint = getSeriesOutlinePaint(series);
Stroke outlineStroke = getSeriesOutlineStroke(series);

Ellipse2D head = new Ellipse2D.Double(point.getX()
- headW / 2, point.getY() - headH / 2, headW,
headH);
g2.setPaint(paint);
g2.fill(head);
g2.setStroke(outlineStroke);
g2.setPaint(outlinePaint);
g2.draw(head);

if (entities != null) {
int row = 0; int col = 0;
if (this.getDataExtractOrder() == TableOrder.BY_ROW) {
row = series;
col = cat;
}
else {
row = cat;
col = series;
}
String tip = null;
if (this.getToolTipGenerator() != null) {
tip = this.getToolTipGenerator().generateToolTip(
this.getDataset(), row, col);
}

String url = null;
if (this.getURLGenerator() != null) {
url = this.getURLGenerator().generateURL(this.getDataset(),
row, col);
}

Shape area = new Rectangle(
(int) (point.getX() - headW),
(int) (point.getY() - headH),
(int) (headW * 2), (int) (headH * 2));
CategoryItemEntity entity = new CategoryItemEntity(
area, tip, url, this.getDataset(),
this.getDataset().getRowKey(row),
this.getDataset().getColumnKey(col));
entities.add(entity);
}

}
}
}
// Plot the polygon

// Lastly, fill the web polygon if this is required

Rectangle2D rec = polygon.getBounds2D();

//Paint paint = getSeriesPaint(series);
// create linear vertical gradient based upon the bounds of the polygon.
Paint paint = new GradientPaint(new Point2D.Double(rec.getCenterX(),rec.getMinY()), startColor,
new Point2D.Double(rec.getCenterX(),rec.getMaxY()), endColor);

g2.setPaint(paint);
g2.setStroke(getSeriesOutlineStroke(series));
g2.draw(polygon);


if (this.isWebFilled()) {
// made this the variable alpha instead of the fixed .1f
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
alpha));
g2.fill(polygon);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
getForegroundAlpha()));
}
}
}

关于java - JFreeChart:如何为蜘蛛图中的系列设置渐变绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com